4
4
5
5
namespace App ;
6
6
7
+ use Composer \Composer ;
8
+ use Composer \IO \IOInterface ;
7
9
use Composer \Script \Event ;
8
10
9
11
class Script
@@ -36,58 +38,102 @@ class Script
36
38
'./src/App/Validation/ExampleUpdateSchema.json ' ,
37
39
];
38
40
41
+ /** @var IOInterface */
42
+ private $ io ;
43
+
44
+ /** @var Composer */
45
+ private $ composer ;
46
+
47
+ /** @var array */
48
+ private $ answers ;
49
+
50
+ /**
51
+ * @param Event $event
52
+ * @throws \Exception
53
+ */
39
54
public static function install (Event $ event ): void
40
55
{
41
- $ answers ['Service_Name ' ] = $ event ->getIO ()->ask ("\n <question>>>> What is the 'micro-service' name?</question> [default: <comment>my service</comment>] " , 'my service ' );
42
- $ answers ['Resource_Name ' ] = $ event ->getIO ()->ask ("\n <question>>>> What is the 'resource' name?</question> [default: <comment>my resource</comment>] " , 'my resource ' );
43
- $ answers ['PHPStorm_Config ' ] = $ event ->getIO ()->askConfirmation ("\n <question>>>> Are you using PHPStorm? Do you want to auto add Docker image config?</question> (y/n) [default: <comment>yes</comment>] " , true );
44
- if ($ answers ['PHPStorm_Config ' ]) {
45
- $ event ->getIO ()->write ("\n <info>>>> Please provide your AWS credentials</info> " );
46
- $ answers ['Access_Key_ID ' ] = $ event ->getIO ()->ask ("\n <question>>>> Access Key ID:</question> " , 'not-provided ' );
47
- $ answers ['Secret_Access_Key ' ] = $ event ->getIO ()->ask ("\n <question>>>> Secret Access Key:</question> " , 'not-provided ' );
48
- $ answers ['PHPStorm_Config ' ] = 'yes ' ;
56
+ $ installer = new self ($ event ->getIO (), $ event ->getComposer ());
57
+
58
+ $ installer ->answers ['Service_Name ' ] = $ installer ->io ->ask ("\n <question>>>> What is the 'micro-service' name?</question> [default: <comment>my service</comment>] " , 'my service ' );
59
+ // $installer->answers['Resource_Name'] = $installer->io->ask("\n <question>>>> What is the 'resource' name?</question> [default: <comment>my resource</comment>] ", 'my resource');
60
+ // $installer->answers['PHPStorm_Config'] = $installer->io->askConfirmation("\n <question>>>> Are you using PHPStorm? Do you want to auto add Docker image config?</question> (y/n) [default: <comment>yes</comment>] ", true);
61
+ // if ($installer->answers['PHPStorm_Config']) {
62
+ // $installer->io->write("\n <info>>>> Please provide your AWS credentials</info>");
63
+ // $installer->answers['Access_Key_ID'] = $installer->io->ask("\n <question>>>> Access Key ID:</question> ", 'not-provided');
64
+ // $installer->answers['Secret_Access_Key'] = $installer->io->ask("\n <question>>>> Secret Access Key:</question> ", 'not-provided');
65
+ // $installer->answers['PHPStorm_Config'] = 'yes';
66
+ // } else {
67
+ // unset($installer->answers['PHPStorm_Config']);
68
+ // }
69
+
70
+ $ installer ->answers ['Github_Config ' ] = $ installer ->io ->askConfirmation ("\n <question>>>> Do you want to auto add Github repository?</question> (y/n) [default: <comment>yes</comment>] " , true );
71
+ if ($ installer ->answers ['Github_Config ' ]) {
72
+ $ installer ->io ->write ("\n <info>>>> Please provide your Github configurations</info> " );
73
+ $ installer ->answers ['Github_Access_Key_Token ' ] = $ installer ->io ->ask ("\n <question>>>> Access Key Token:</question> " , 'c56c839089358145afde1ab47904ce6f072399b1 ' );
74
+ $ installer ->answers ['Github_Team_ID ' ] = $ installer ->io ->askAndValidate ("\n <question>>>> Team ID (numbers only):</question> " , function ($ value ) {
75
+ if (!intval ($ value )) {
76
+ throw new \Exception ('Team ID should be number only. ' );
77
+ }
78
+
79
+ return $ value ;
80
+ }, 1 , 2833513 );
81
+
82
+ $ installer ->answers ['Github_Config ' ] = 'yes ' ;
49
83
} else {
50
- unset($ answers ['PHPStorm_Config ' ]);
84
+ unset($ installer -> answers ['Github_Config ' ]);
51
85
}
52
86
53
- $ event -> getIO () ->write ("\n ::: Your current configuration: " );
54
- $ event -> getIO () ->write (" ------------------------------- " );
55
- foreach ($ answers as $ key => $ answer ) {
56
- $ event -> getIO () ->write (" ::: {$ key }: <info> {$ answer }</info> " );
87
+ $ installer -> io ->write ("\n ::: Your current configuration: " );
88
+ $ installer -> io ->write (" ------------------------------- " );
89
+ foreach ($ installer -> answers as $ key => $ answer ) {
90
+ $ installer -> io ->write (" ::: {$ key }: <info> {$ answer }</info> " );
57
91
}
58
- $ start = $ event ->getIO ()->askConfirmation ("\n <question>>>> Do you want to proceed?</question> (y/n) [default: <comment>yes</comment>] " , true );
92
+
93
+ $ start = $ installer ->io ->askConfirmation ("\n <question>>>> Do you want to proceed?</question> (y/n) [default: <comment>yes</comment>] " , true );
59
94
60
95
if (!$ start ) {
61
- $ event -> getIO () ->write ("\n <warning>>>> Canceling...</warning> " );
96
+ $ installer -> io ->write ("\n <warning>>>> Canceling...</warning> " );
62
97
exit (0 );
63
98
}
64
99
65
- self :: replaceFilesContentResource ($ answers ['Resource_Name ' ]);
66
- self :: replaceFilesContentService ($ answers ['Service_Name ' ]);
67
- self :: renameFiles ($ answers ['Resource_Name ' ]);
100
+ // $$installer-> replaceFilesContentResource($installer-> answers['Resource_Name']);
101
+ // $$installer-> replaceFilesContentService($installer-> answers['Service_Name']);
102
+ // $$installer-> renameFiles($installer-> answers['Resource_Name']);
68
103
69
- if (isset ($ answers ['PHPStorm_Config ' ])) {
70
- $ event -> getIO () ->write ("\n <info>>>> Creating PHPStorm Configurations...</info> " );
71
- self :: addPHPStormConfig ($ answers );
104
+ if (isset ($ installer -> answers ['PHPStorm_Config ' ])) {
105
+ $ installer -> io ->write ("\n <info>>>> Creating PHPStorm Configurations...</info> " );
106
+ $ $ installer -> addPHPStormConfig ();
72
107
}
73
108
74
- self ::finishScript ();
109
+ if (isset ($ installer ->answers ['Github_Config ' ])) {
110
+ $ installer ->io ->write ("\n <info>>>> Creating Github Configurations...</info> " );
111
+ $ installer ->createGithubConfig ();
112
+ }
113
+
114
+ $ installer ->finishScript ();
115
+ }
116
+
117
+ public function __construct (IOInterface $ io , Composer $ composer , string $ projectRoot = null )
118
+ {
119
+ $ this ->io = $ io ;
120
+ $ this ->composer = $ composer ;
75
121
}
76
122
77
- private static function renameFiles ($ resourceName )
123
+ private function renameFiles ($ resourceName )
78
124
{
79
- $ resourceName = self :: toCamelCase ($ resourceName );
125
+ $ resourceName = $ this -> toCamelCase ($ resourceName );
80
126
$ resourceName = ucfirst ($ resourceName );
81
127
foreach (self ::FILES_TO_RENAME as $ file ) {
82
128
rename ($ file , str_replace ('Example ' , $ resourceName , $ file ));
83
129
}
84
130
}
85
131
86
- private static function replaceFilesContentResource ($ resourceName )
132
+ private function replaceFilesContentResource ($ resourceName )
87
133
{
88
- $ resourceName = self :: toCamelCase ($ resourceName );
134
+ $ resourceName = $ this -> toCamelCase ($ resourceName );
89
135
$ ucFirstName = ucfirst ($ resourceName );
90
- $ underscoreName = self :: toUnderscore ($ resourceName );
136
+ $ underscoreName = $ this -> toUnderscore ($ resourceName );
91
137
foreach (self ::FILES_TO_REPLACE_RESOURCE as $ file ) {
92
138
$ content = file_get_contents ($ file );
93
139
$ content = str_replace ('Example ' , $ ucFirstName , $ content );
@@ -98,10 +144,10 @@ private static function replaceFilesContentResource($resourceName)
98
144
}
99
145
}
100
146
101
- private static function replaceFilesContentService ($ serviceName )
147
+ private function replaceFilesContentService ($ serviceName )
102
148
{
103
- $ serviceName = self :: toCamelCase ($ serviceName );
104
- $ serviceName = self :: toUnderscore ($ serviceName );
149
+ $ serviceName = $ this -> toCamelCase ($ serviceName );
150
+ $ serviceName = $ this -> toUnderscore ($ serviceName );
105
151
$ serviceName = str_replace ('_ ' , '- ' , $ serviceName );
106
152
foreach (self ::FILES_TO_REPLACE_SERVICE as $ file ) {
107
153
$ content = file_get_contents ($ file );
@@ -110,7 +156,7 @@ private static function replaceFilesContentService($serviceName)
110
156
}
111
157
}
112
158
113
- private static function finishScript ()
159
+ private function finishScript ()
114
160
{
115
161
$ search = '
116
162
"pre-install-cmd": "App \\\\Script::install", ' ;
@@ -136,27 +182,94 @@ private static function finishScript()
136
182
unlink ('./src/App/Script.php ' );
137
183
}
138
184
139
- private static function toCamelCase ($ string ): string
185
+ private function toCamelCase ($ string ): string
140
186
{
141
187
return lcfirst (str_replace (' ' , '' , ucwords (preg_replace ('/^a-z0-9 ' . implode ('' , []) . ']+/ ' , ' ' , $ string ))));
142
188
}
143
189
144
- private static function toUnderscore ($ string ): string
190
+ private function toUnderscore ($ string ): string
145
191
{
146
192
return strtolower (preg_replace (['/([a-z\d])([A-Z])/ ' , '/([^_])([A-Z][a-z])/ ' ], '$1_$2 ' , $ string ));
147
193
}
148
194
149
- private static function addPHPStormConfig ($ answers ): void
195
+ private function addPHPStormConfig (): void
150
196
{
151
197
if (!file_exists ('./.idea/runConfigurations ' )) {
152
198
mkdir ('./.idea/runConfigurations ' , 0777 , true );
153
199
}
154
200
155
201
$ content = file_get_contents ('./src/App/docker_image_config.xml ' );
156
- $ content = str_replace ('AWS_ACCESS_KEY_ID ' , $ answers ['Access_Key_ID ' ], $ content );
157
- $ content = str_replace ('AWS_SECRET_ACCESS_KEY ' , $ answers ['Secret_Access_Key ' ], $ content );
202
+ $ content = str_replace ('AWS_ACCESS_KEY_ID ' , $ this -> answers ['Access_Key_ID ' ], $ content );
203
+ $ content = str_replace ('AWS_SECRET_ACCESS_KEY ' , $ this -> answers ['Secret_Access_Key ' ], $ content );
158
204
file_put_contents ('./src/App/docker_image_config.xml ' , $ content );
159
205
160
206
copy ('./src/App/docker_image_config.xml ' , './.idea/runConfigurations/docker_image_config.xml ' );
161
207
}
208
+
209
+ /**
210
+ * @throws \Exception
211
+ */
212
+ private function createGithubConfig ()
213
+ {
214
+ // $this->createRepository();
215
+ $ this ->execGitCommands ();
216
+
217
+ $ this ->io ->write ("\n ::: Your current configuration: " );
218
+ $ this ->io ->write (" ------------------------------- " );
219
+ foreach ($ this ->answers as $ key => $ answer ) {
220
+ $ this ->io ->write (" ::: {$ key }: <info> {$ answer }</info> " );
221
+ }
222
+
223
+ exit ;
224
+ }
225
+
226
+ private function execGitCommands ()
227
+ {
228
+
229
+ exec ('git init ' , $ output , $ return );
230
+ exec ('git config --local user.name "mobingideployer" ' , $ output , $ return );
231
+ exec ('git config --local user.email "dev@mobingi.com" ' , $ output , $ return );
232
+ exec ('git add . ' , $ output , $ return );
233
+ exec ('git commit -m "first commit" ' , $ output , $ return );
234
+ exec ('git remote add origin ' . $ this ->answers ['Github_SSH_Url ' ], $ output , $ return );
235
+ exec ('git push -u origin master ' , $ output , $ return );
236
+ }
237
+
238
+ /**
239
+ * @throws \Exception
240
+ */
241
+ private function createRepository ()
242
+ {
243
+ $ curl = curl_init ();
244
+
245
+ curl_setopt_array ($ curl , array (
246
+ CURLOPT_URL => "https://api.github.com/orgs/mobingilabs/repos?access_token= {$ this ->answers ['Github_Access_Key_Token ' ]}" ,
247
+ CURLOPT_RETURNTRANSFER => true ,
248
+ CURLOPT_ENCODING => "" ,
249
+ CURLOPT_MAXREDIRS => 10 ,
250
+ CURLOPT_TIMEOUT => 30 ,
251
+ CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1 ,
252
+ CURLOPT_USERAGENT => 'php-curl ' ,
253
+ CURLOPT_CUSTOMREQUEST => "POST " ,
254
+ CURLOPT_POSTFIELDS => "{ \n\t\"name \": \"{$ this ->answers ['Service_Name ' ]}\", \n\t\"description \": \"Microservice {$ this ->answers ['Service_Name ' ]}. \", \n\t\"homepage \": null, \n\t\"private \": true, \n\t\"has_issues \": true, \n\t\"has_projects \": true, \n\t\"has_wiki \": true, \n\t\"team_id \": {$ this ->answers ['Github_Team_ID ' ]}, \n\t\"auto_init \": false, \n\t\"allow_squash_merge \": true, \n \"allow_merge_commit \": true, \n \"allow_rebase_merge \": true \n} " ,
255
+ CURLOPT_HTTPHEADER => array (
256
+ "Cache-Control: no-cache " ,
257
+ "Content-Type: application/json "
258
+ ),
259
+ ));
260
+
261
+ $ response = curl_exec ($ curl );
262
+ $ err = curl_error ($ curl );
263
+
264
+ curl_close ($ curl );
265
+
266
+ if ($ err ) {
267
+ throw new \Exception ("cURL Error #: {$ err }" );
268
+ } else {
269
+ echo $ response ;
270
+ $ data = json_decode ($ response );
271
+
272
+ $ this ->answers ['Github_SSH_Url ' ] = $ data ->ssh_url ;
273
+ }
274
+ }
162
275
}
0 commit comments