-
Notifications
You must be signed in to change notification settings - Fork 69
/
splitter.php
158 lines (141 loc) · 4.39 KB
/
splitter.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
/**
* Split each core plugin and theme into separate repos and push the to GitHub. For
* example, the "CMS" plugin is mapped to "[email protected]:quickapps-plugins/cms.git"
*
* ### Usage:
*
* ```
* php splitter.php --main-branch="2.0" --plugins="Block,Bootstrap"
* ```
*
* - If no main branch name is provided "2.0" will be used by default.
* - If no plugin names are given (or wildcard * is provided), all of
* them will be splitted.
*/
if (!function_exists('readline')) {
function readline($prompt = null){
if ($prompt){
echo $prompt;
}
$fp = fopen('php://stdin', 'r');
$line = rtrim(fgets($fp, 1024));
return $line;
}
}
/**
* Main branch name.
*
* @var string
*/
$options = getopt('', ['main-branch::', 'plugins::']);
if (empty($options['main-branch'])) {
die("No main branch name given, you must provide one using the '--main-branch' option. For example: --main-branch=\"2.0\".\n");
}
$mainBranch = $options['main-branch'];
/**
* List of core plugins located in the "plugins" directory.
*
* @var array
*/
$validPlugins = [
'Block',
'Bootstrap',
'Captcha',
'CMS',
'Comment',
'Content',
'Eav',
'Field',
'Installer',
'Jquery',
'Locale',
'MediaManager',
'Menu',
'Search',
'System',
'Taxonomy',
'User',
'Wysiwyg',
'BackendTheme',
'FrontendTheme',
];
/**
* List of selected plugins to be splitted.
*
* @var array
*/
$selectedPlugins = [];
/**
* Plugin picker interface.
*/
if (empty($options['plugins'])) {
$line = 'dummy';
while ($line != '') {
echo " [*] All plugins\n";
foreach ($validPlugins as $i => $p) {
$index = in_array($p, $selectedPlugins) ? 'x' : $i + 1;
echo sprintf(" [%s] %s\n", $index, $p);
}
echo "\n";
$line = readline("Which plugins would you like to split?\n(press intro to stop selecting, or select again to uncheck a plugin): ");
if ($line == '*') {
$selectedPlugins = $validPlugins;
} elseif (isset($validPlugins[$line - 1])) {
if (($key = array_search($validPlugins[$line - 1], $selectedPlugins)) !== false) {
unset($selectedPlugins[$key]);
echo sprintf('Plugin "%s" has been UNSELECTED', $validPlugins[$line - 1]) . "\n";
} else {
$selectedPlugins[] = $validPlugins[$line - 1];
echo sprintf('Plugin "%s" has been selected', $validPlugins[$line - 1]) . "\n";
}
} else {
echo "Invalid option, try again\n";
}
echo "\n";
}
} else {
$selectedPlugins = $options['plugins'] === '*' ? $validPlugins : array_intersect($validPlugins, explode(',', $options['plugins']));
}
if (empty($selectedPlugins)) {
die("ERROR: You must indicate one valid plugin at least.\n");
}
/**
* Null device, based on OS.
*
* @var string
*/
$null = DIRECTORY_SEPARATOR === '/' ? '/dev/null' : 'NUL';
/**
* Creates a new branch for every plugin and theme, and push to corresponding GitHub
* repository. Such branches are removed after pushed.
*/
foreach ($selectedPlugins as $index => $plugin) {
$title = sprintf('Processing: %s [%d/%d]', $plugin, ($index + 1), count($selectedPlugins));
$pluginDirectory = dirname(__FILE__) . "/plugins/{$plugin}";
$jsonPath = "{$pluginDirectory}/composer.json";
echo "{$title}\n";
echo str_repeat('-', strlen($title)) . "\n\n";
if (!is_readable($jsonPath)) {
echo sprintf('ERROR: Missing file: "%s"', $jsonPath);
echo "\n\n";
continue;
}
$composer = json_decode(file_get_contents($jsonPath), true);
if (!isset($composer['name']) || strpos($composer['name'], '/') === false) {
echo 'ERROR: Invalid "composer.json" file, missing or invalid "name" key.';
echo "\n\n";
continue;
}
list($org, $plg) = explode('/', $composer['name']);
exec("git checkout {$mainBranch} > {$null}");
exec("git remote add {$plg} [email protected]:{$org}/{$plg}.git -f 2> {$null}");
exec("git branch -D {$plg} 2> {$null}");
exec("git checkout -b {$plg}");
exec("git filter-branch --prune-empty --subdirectory-filter plugins/{$plugin} -f {$plg}");
exec("git push {$plg} {$plg}:master --force");
exec("git checkout {$mainBranch} > {$null}");
exec("git branch -D {$plg}");
exec("git remote rm {$plg}");
echo "\n\n";
}