Skip to content

Commit 6f5e13c

Browse files
committed
Refactor init:mac to setup:env to allow other envs
Add config update to init command
1 parent 664391e commit 6f5e13c

File tree

3 files changed

+62
-21
lines changed

3 files changed

+62
-21
lines changed

readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,10 +449,10 @@ file yourself.
449449

450450
### Customising the development environment
451451

452-
spm includes an `init:mac` command that will read a YAML file with various setup instructions
452+
spm includes a `setup:env` command that will read a YAML file with various setup instructions
453453
intended to get a new user up and running very quickly with the project. This file can include
454454
any number of shell commands to set various aspects on the machine. The file must be named:
455-
`init_mac.yaml`.
455+
`init_[mac|linux|windows].yaml`. This command is aliased to: `init:mac`, `init:linux` and `init:windows`.
456456

457457
For example: you can create steps that install brew, and additional packages; or that creates
458458
overrides / default config files in various places.

src/Commands/InitCommand.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Somnambulist\ProjectManager\Commands\Behaviours\UseEnvironmentTemplate;
66
use Symfony\Component\Console\Input\InputInterface;
7+
use Symfony\Component\Console\Input\InputOption;
78
use Symfony\Component\Console\Output\OutputInterface;
89
use function file_exists;
910
use function file_get_contents;
@@ -28,6 +29,7 @@ protected function configure()
2829
$this
2930
->setName('init')
3031
->setDescription('Initialise the Project Manager config folder')
32+
->addOption('update', 'u', InputOption::VALUE_NONE, 'Update the main spm config file with the latest defaults')
3133
;
3234
}
3335

@@ -54,6 +56,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
5456
$this->tools()->success('Configuration re-created at <info>%s</info>', $dir);
5557
}
5658

59+
if ($input->getOption('update')) {
60+
$this->tools()->warning('Updating configuration file latest <info>spm</info> default');
61+
file_put_contents($dir . DIRECTORY_SEPARATOR . 'project_manager.yaml', $this->config());
62+
}
63+
5764
$this->tools()->newline();
5865

5966
return 0;

src/Commands/PrepareMacCommand.php renamed to src/Commands/PrepareDevEnvironmentCommand.php

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
namespace Somnambulist\ProjectManager\Commands;
44

5+
use IlluminateAgnostic\Str\Support\Str;
56
use RuntimeException;
67
use Somnambulist\ProjectManager\Commands\Behaviours\GetCurrentActiveProject;
78
use Somnambulist\ProjectManager\Commands\Behaviours\ProjectConfigAwareCommand;
89
use Somnambulist\ProjectManager\Contracts\ProjectConfigAwareInterface;
910
use Somnambulist\ProjectManager\Models\Project;
11+
use Symfony\Component\Console\Input\InputArgument;
1012
use Symfony\Component\Console\Input\InputInterface;
1113
use Symfony\Component\Console\Input\InputOption;
1214
use Symfony\Component\Console\Output\OutputInterface;
@@ -16,12 +18,12 @@
1618
use function sprintf;
1719

1820
/**
19-
* Class PrepareMacCommand
21+
* Class PrepareDevEnvironmentCommand
2022
*
2123
* @package Somnambulist\ProjectManager\Commands
22-
* @subpackage Somnambulist\ProjectManager\Commands\PrepareMacCommand
24+
* @subpackage Somnambulist\ProjectManager\Commands\PrepareDevEnvironmentCommand
2325
*/
24-
class PrepareMacCommand extends AbstractCommand implements ProjectConfigAwareInterface
26+
class PrepareDevEnvironmentCommand extends AbstractCommand implements ProjectConfigAwareInterface
2527
{
2628

2729
use GetCurrentActiveProject;
@@ -30,27 +32,55 @@ class PrepareMacCommand extends AbstractCommand implements ProjectConfigAwareInt
3032
protected function configure()
3133
{
3234
$this
33-
->setName('init:mac')
34-
->setDescription('Sets up preferred libraries and programs for development on macOS')
35+
->setName('setup:env')
36+
->setAliases(['init:mac', 'init:linux', 'init:windows'])
37+
->setDescription('Sets up preferred libraries and programs for development as configured in the current project')
38+
->addArgument('arch', InputArgument::OPTIONAL, 'The specific config type to run: [mac|linux|windows]', 'mac')
3539
->addOption('step', 's', InputOption::VALUE_OPTIONAL, 'Skip to this step in the chain', 0)
3640
->addOption('test', 't', InputOption::VALUE_NONE, 'Run a test outputting the commands that will be run')
3741
->addOption('bash', 'b', InputOption::VALUE_NONE, 'Output commands on each line without comments; runs as test')
42+
->setHelp(<<<HLP
43+
Various commands can be provided in an YAML file for setting up a dev machine
44+
with various applications, configuration defaults for faster dev setup. Typically
45+
these will be for installing company preferred apps, default configuration or
46+
hosts entries, VPN configuration etc etc.
47+
48+
By default, this command will attempt to run the <info>mac</info> config options.
49+
You should specify the alternative setup file using the second argument, or use
50+
one of the aliases to pre-configure the command with that architecture.
51+
52+
<comment>Note:</comment> when developing the setup scripts, you should test in a virtual machine
53+
before running the scripts on a live machine.
54+
55+
<info>No attempt is made to auto-detect the running architecture.</info>
56+
57+
HLP)
3858
;
3959
}
4060

4161
protected function execute(InputInterface $input, OutputInterface $output)
4262
{
4363
$this->setupConsoleHelper($input, $output);
4464

65+
if (Str::startsWith($comm = $input->getArgument('command'), 'init')) {
66+
$input->setArgument('arch', Str::after($comm, 'init:'));
67+
}
68+
69+
if (!in_array($arch = $input->getArgument('arch'), ['mac', 'linux', 'windows'])) {
70+
$this->tools()->error('<error>%s</error> is not a valid architecture option. Must be one of: [mac, windows, linux]', $arch);
71+
72+
return 1;
73+
}
74+
4575
$project = $this->getActiveProject();
46-
$steps = $this->loadInitialisationSteps($project);
76+
$steps = $this->loadInitialisationSteps($project, $arch);
4777
$bashMode = $input->getOption('bash');
4878

4979
if ($bashMode) {
5080
$this->tools()->disableOutput();
5181
}
5282

53-
$this->tools()->warning('Starting macOS preparation');
83+
$this->tools()->warning('Starting <info>%s</info> preparation', $arch);
5484
$this->tools()->warning('Please note: setup may require sudo for some steps');
5585

5686
if (!$bashMode) {
@@ -81,8 +111,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
81111

82112
if ('exit' === $toRun) {
83113
if (!$bashMode) {
84-
$this->tools()->info('init:mac must wait for the previous command');
85-
$this->tools()->info('resume using: <info>init:mac -s %s</info>', $i + 1);
114+
$this->tools()->info('<info>%s</info> must wait for the previous command', $comm);
115+
$this->tools()->info('resume using: <info>%s -s %s</info>', $comm, $i + 1);
86116
$this->tools()->newline();
87117

88118
return 0;
@@ -98,14 +128,16 @@ protected function execute(InputInterface $input, OutputInterface $output)
98128
if ($input->getOption('test') || $bashMode) {
99129
$this->tools()->step('RUN', $toRun);
100130
!$bashMode ?: $output->writeln($toRun);
131+
132+
continue;
133+
}
134+
135+
if ($this->tools()->execute($command)) {
136+
$this->tools()->success(' ...command completed successfully');
101137
} else {
102-
if ($this->tools()->execute($command)) {
103-
$this->tools()->success(' ...command completed successfully');
104-
} else {
105-
$this->tools()->error(' ...command failed to execute!');
138+
$this->tools()->error(' ...command failed to execute!');
106139

107-
return 1;
108-
}
140+
return 1;
109141
}
110142
}
111143
} else {
@@ -114,18 +146,20 @@ protected function execute(InputInterface $input, OutputInterface $output)
114146
}
115147

116148
$this->tools()->success('All steps completed');
117-
$this->tools()->info('If you install Xcode you may wish to run: <info>sudo xcodebuild -license accept</info>');
149+
if ('mac' === $arch) {
150+
$this->tools()->info('If you install Xcode you may wish to run: <info>sudo xcodebuild -license accept</info>');
151+
}
118152
$this->tools()->newline();
119153

120154
return 0;
121155
}
122156

123-
private function loadInitialisationSteps(Project $project): array
157+
private function loadInitialisationSteps(Project $project, string $arch): array
124158
{
125159
$this->tools()->info('Reading config data from <info>%s</info> project', $project->name());
126160

127-
if (!file_exists($file = $project->getFileInProject('init_mac.yaml'))) {
128-
throw new RuntimeException(sprintf('The current project "%s" does not have an "init_mac.yaml" file', $project->name()));
161+
if (!file_exists($file = $project->getFileInProject($f = sprintf('init_%s.yaml', $arch)))) {
162+
throw new RuntimeException(sprintf('The current project "%s" does not have an "%s" file', $project->name(), $f));
129163
}
130164

131165
$config = strtr(file_get_contents($file), [

0 commit comments

Comments
 (0)