2
2
3
3
namespace Somnambulist \ProjectManager \Commands ;
4
4
5
+ use IlluminateAgnostic \Str \Support \Str ;
5
6
use RuntimeException ;
6
7
use Somnambulist \ProjectManager \Commands \Behaviours \GetCurrentActiveProject ;
7
8
use Somnambulist \ProjectManager \Commands \Behaviours \ProjectConfigAwareCommand ;
8
9
use Somnambulist \ProjectManager \Contracts \ProjectConfigAwareInterface ;
9
10
use Somnambulist \ProjectManager \Models \Project ;
11
+ use Symfony \Component \Console \Input \InputArgument ;
10
12
use Symfony \Component \Console \Input \InputInterface ;
11
13
use Symfony \Component \Console \Input \InputOption ;
12
14
use Symfony \Component \Console \Output \OutputInterface ;
16
18
use function sprintf ;
17
19
18
20
/**
19
- * Class PrepareMacCommand
21
+ * Class PrepareDevEnvironmentCommand
20
22
*
21
23
* @package Somnambulist\ProjectManager\Commands
22
- * @subpackage Somnambulist\ProjectManager\Commands\PrepareMacCommand
24
+ * @subpackage Somnambulist\ProjectManager\Commands\PrepareDevEnvironmentCommand
23
25
*/
24
- class PrepareMacCommand extends AbstractCommand implements ProjectConfigAwareInterface
26
+ class PrepareDevEnvironmentCommand extends AbstractCommand implements ProjectConfigAwareInterface
25
27
{
26
28
27
29
use GetCurrentActiveProject;
@@ -30,27 +32,55 @@ class PrepareMacCommand extends AbstractCommand implements ProjectConfigAwareInt
30
32
protected function configure ()
31
33
{
32
34
$ 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 ' )
35
39
->addOption ('step ' , 's ' , InputOption::VALUE_OPTIONAL , 'Skip to this step in the chain ' , 0 )
36
40
->addOption ('test ' , 't ' , InputOption::VALUE_NONE , 'Run a test outputting the commands that will be run ' )
37
41
->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 )
38
58
;
39
59
}
40
60
41
61
protected function execute (InputInterface $ input , OutputInterface $ output )
42
62
{
43
63
$ this ->setupConsoleHelper ($ input , $ output );
44
64
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
+
45
75
$ project = $ this ->getActiveProject ();
46
- $ steps = $ this ->loadInitialisationSteps ($ project );
76
+ $ steps = $ this ->loadInitialisationSteps ($ project, $ arch );
47
77
$ bashMode = $ input ->getOption ('bash ' );
48
78
49
79
if ($ bashMode ) {
50
80
$ this ->tools ()->disableOutput ();
51
81
}
52
82
53
- $ this ->tools ()->warning ('Starting macOS preparation ' );
83
+ $ this ->tools ()->warning ('Starting <info>%s</info> preparation ' , $ arch );
54
84
$ this ->tools ()->warning ('Please note: setup may require sudo for some steps ' );
55
85
56
86
if (!$ bashMode ) {
@@ -81,8 +111,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
81
111
82
112
if ('exit ' === $ toRun ) {
83
113
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 );
86
116
$ this ->tools ()->newline ();
87
117
88
118
return 0 ;
@@ -98,14 +128,16 @@ protected function execute(InputInterface $input, OutputInterface $output)
98
128
if ($ input ->getOption ('test ' ) || $ bashMode ) {
99
129
$ this ->tools ()->step ('RUN ' , $ toRun );
100
130
!$ bashMode ?: $ output ->writeln ($ toRun );
131
+
132
+ continue ;
133
+ }
134
+
135
+ if ($ this ->tools ()->execute ($ command )) {
136
+ $ this ->tools ()->success (' ...command completed successfully ' );
101
137
} 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! ' );
106
139
107
- return 1 ;
108
- }
140
+ return 1 ;
109
141
}
110
142
}
111
143
} else {
@@ -114,18 +146,20 @@ protected function execute(InputInterface $input, OutputInterface $output)
114
146
}
115
147
116
148
$ 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
+ }
118
152
$ this ->tools ()->newline ();
119
153
120
154
return 0 ;
121
155
}
122
156
123
- private function loadInitialisationSteps (Project $ project ): array
157
+ private function loadInitialisationSteps (Project $ project, string $ arch ): array
124
158
{
125
159
$ this ->tools ()->info ('Reading config data from <info>%s</info> project ' , $ project ->name ());
126
160
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 ));
129
163
}
130
164
131
165
$ config = strtr (file_get_contents ($ file ), [
0 commit comments