-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
128 lines (108 loc) · 3.79 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
include_once(__DIR__."/vendor/autoload.php");
use Cocur\Slugify\Slugify;
class CreatePlugin
{
public function __construct()
{
$this->templateDirectory = __DIR__."/template-files";
$slugify = new Slugify();
$this->input = $input = $this->promptForDetails();
$this->projectNiceName = $input['projectName'];
$this->projectName = $slugify->slugify($input['projectName']);
$this->description = $input['description'];
$this->line("Project name slugified to {$this->projectName}");
$this->userName = $input['userName'];
$this->camelCaseName = $this->dashesToCamelCase($this->projectName);
$this->createRepo();
$this->createFolder();
$this->getTemplateFiles();
$this->commitFiles();
}
public function line($string)
{
echo $string.PHP_EOL;
}
public function promptForDetails()
{
return [
'projectName' => readline("Project name: "),
'description' => readline("Description: "),
'userName' => readline("Gihub username: ")
];
}
public function createRepo()
{
$config = [
'name' => $this->projectName,
'description' => $this->description
];
$configJSON = json_encode($config);
$createRepo = "curl -u '".$this->userName."' https://api.github.com/orgs/shortlist-digital/repos -d '$configJSON'";
echo shell_exec($createRepo);
}
public function fillTemplate($fields, $string)
{
$string = preg_replace_callback('/{{(\w+)}}/', function ($match) use ($fields) {
return $fields[$match[1]];
}, $string);
return $string;
}
public function createFolder()
{
$createFolder = "mkdir {$this->projectName};";
echo shell_exec($createFolder);
chdir($this->projectName);
}
public function getTemplateFiles()
{
$this->getFile('.gitignore');
$this->getFile('.editorconfig');
$this->getFile('LICENSE');
$this->getComposer();
$this->getReadme();
$this->getPluginFile();
}
public function getFile($fileName)
{
$getEditorConfig = "cp {$this->templateDirectory}/$fileName .";
echo shell_exec($getEditorConfig);
}
public function getComposer()
{
$composerString = file_get_contents($this->templateDirectory."/composer.json");
$fileString = $this->fillTemplate([
'className' => $this->camelCaseName,
'description' => $this->description,
'projectName'=> $this->projectName
], $composerString);
file_put_contents('composer.json', $fileString);
}
public function getReadme()
{
$readmeString = file_get_contents($this->templateDirectory."/readme.md");
$fileString = $this->fillTemplate($this->input, $readmeString);
file_put_contents('readme.md', $fileString);
}
public function getPluginFile()
{
$composerString = file_get_contents($this->templateDirectory."/plugin-file.php");
$fileString = $this->fillTemplate([
'className' => $this->camelCaseName,
'description' => $this->description,
'projectName'=> $this->projectName,
'projectNiceName'=> $this->projectNiceName
], $composerString);
file_put_contents("{$this->projectName}.php", $fileString);
}
public function dashesToCamelCase($string)
{
return str_replace(' ', '', ucwords(str_replace('-', ' ', $string)));
}
public function commitFiles()
{
$commitFiles = "git init; git add .; git commit -m 'init'; git remote add origin [email protected]:shortlist-digital/{$this->projectName}.git; git push origin master";
echo shell_exec($commitFiles);
}
}
new CreatePlugin();