Skip to content

Commit 70cce89

Browse files
first commit
1 parent c989d98 commit 70cce89

File tree

1 file changed

+149
-36
lines changed

1 file changed

+149
-36
lines changed

src/App/Script.php

Lines changed: 149 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace App;
66

7+
use Composer\Composer;
8+
use Composer\IO\IOInterface;
79
use Composer\Script\Event;
810

911
class Script
@@ -36,58 +38,102 @@ class Script
3638
'./src/App/Validation/ExampleUpdateSchema.json',
3739
];
3840

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+
*/
3954
public static function install(Event $event): void
4055
{
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';
4983
} else {
50-
unset($answers['PHPStorm_Config']);
84+
unset($installer->answers['Github_Config']);
5185
}
5286

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>");
5791
}
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);
5994

6095
if (!$start) {
61-
$event->getIO()->write("\n <warning>>>> Canceling...</warning>");
96+
$installer->io->write("\n <warning>>>> Canceling...</warning>");
6297
exit(0);
6398
}
6499

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']);
68103

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();
72107
}
73108

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;
75121
}
76122

77-
private static function renameFiles($resourceName)
123+
private function renameFiles($resourceName)
78124
{
79-
$resourceName = self::toCamelCase($resourceName);
125+
$resourceName = $this->toCamelCase($resourceName);
80126
$resourceName = ucfirst($resourceName);
81127
foreach (self::FILES_TO_RENAME as $file) {
82128
rename($file, str_replace('Example', $resourceName, $file));
83129
}
84130
}
85131

86-
private static function replaceFilesContentResource($resourceName)
132+
private function replaceFilesContentResource($resourceName)
87133
{
88-
$resourceName = self::toCamelCase($resourceName);
134+
$resourceName = $this->toCamelCase($resourceName);
89135
$ucFirstName = ucfirst($resourceName);
90-
$underscoreName = self::toUnderscore($resourceName);
136+
$underscoreName = $this->toUnderscore($resourceName);
91137
foreach (self::FILES_TO_REPLACE_RESOURCE as $file) {
92138
$content = file_get_contents($file);
93139
$content = str_replace('Example', $ucFirstName, $content);
@@ -98,10 +144,10 @@ private static function replaceFilesContentResource($resourceName)
98144
}
99145
}
100146

101-
private static function replaceFilesContentService($serviceName)
147+
private function replaceFilesContentService($serviceName)
102148
{
103-
$serviceName = self::toCamelCase($serviceName);
104-
$serviceName = self::toUnderscore($serviceName);
149+
$serviceName = $this->toCamelCase($serviceName);
150+
$serviceName = $this->toUnderscore($serviceName);
105151
$serviceName = str_replace('_', '-', $serviceName);
106152
foreach (self::FILES_TO_REPLACE_SERVICE as $file) {
107153
$content = file_get_contents($file);
@@ -110,7 +156,7 @@ private static function replaceFilesContentService($serviceName)
110156
}
111157
}
112158

113-
private static function finishScript()
159+
private function finishScript()
114160
{
115161
$search = '
116162
"pre-install-cmd": "App\\\\Script::install",';
@@ -136,27 +182,94 @@ private static function finishScript()
136182
unlink('./src/App/Script.php');
137183
}
138184

139-
private static function toCamelCase($string): string
185+
private function toCamelCase($string): string
140186
{
141187
return lcfirst(str_replace(' ', '', ucwords(preg_replace('/^a-z0-9' . implode('', []) . ']+/', ' ', $string))));
142188
}
143189

144-
private static function toUnderscore($string): string
190+
private function toUnderscore($string): string
145191
{
146192
return strtolower(preg_replace(['/([a-z\d])([A-Z])/', '/([^_])([A-Z][a-z])/'], '$1_$2', $string));
147193
}
148194

149-
private static function addPHPStormConfig($answers): void
195+
private function addPHPStormConfig(): void
150196
{
151197
if (!file_exists('./.idea/runConfigurations')) {
152198
mkdir('./.idea/runConfigurations', 0777, true);
153199
}
154200

155201
$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);
158204
file_put_contents('./src/App/docker_image_config.xml', $content);
159205

160206
copy('./src/App/docker_image_config.xml', './.idea/runConfigurations/docker_image_config.xml');
161207
}
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+
}
162275
}

0 commit comments

Comments
 (0)