-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfreepbx_git.php
executable file
·307 lines (286 loc) · 9.01 KB
/
freepbx_git.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/env php
<?php
/**
* Copyright 2013 by Schmooze Com, Inc.
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* @author mbrevda => gmail ! com
* @author andrew ! nagy => the159 ! com
*
* options:
* run with --help for options
*
*/
require_once('libraries/freepbx.php');
$projects = array(
"FREEPBX" => "FreePBX Open Source",
"FPBXCN" => "FreePBX Contributed Modules",
"FPBXC" => "FreePBX Commercial Modules",
);
$freepbx_conf = freepbx::getFreePBXConfig();
//TODO Maybe move this inside of the function
if (is_array($freepbx_conf) && !empty($freepbx_conf)) {
foreach($freepbx_conf as $key => $value) {
if (isset($value) && $value != '') {
$vars[$key] = $value;
}
}
}
$mode = !empty($vars['mode']) ? $vars['mode'] : 'ssh';
$vars['repo_directory'] = !empty($vars['repo_directory']) ? $vars['repo_directory'] : dirname(dirname(__FILE__));
$help = array(
array('-m', 'Checkout a Single Module. Without the -r option will also search all available Stash Projects for said module'),
array('-s', 'Setup symlinks into Framework for install'),
array('-r', 'Declare Stash Project Key for single module checkout'),
array('-y', 'Answer yes for all questions'),
array('--setup', 'Setup new freepbx dev tools environment (use --force to re-setup environment)'),
array('--clean', 'Prunes all tags and branches that do no exist on the remote, can be used with the -m command for individual'),
array('--refresh', 'Updates all local modules with their remote changes (!!you will lose all untracked branches!!)'),
array('--refreshhard', 'Updates all local modules with their remote changes (!!you will lose all untracked branches and work!!)'),
array('--addmergedriver', 'Updates/Adds Relevant Merge Drivers'),
array('--switch=<branch>', 'Switch all local modules to branch'),
array('--mode=<ssh|http>', 'What Mode to Use GIT in, Default is SSH. Use HTTP if you dont have SSH access or you dont know'),
array('--directory', 'The directory location of the modules, will default to: '.$vars['repo_directory']),
array('--keys', 'Comma separated project keys [freepbx,mykey]')
);
$longopts = array(
"help",
"setup",
"force",
"refresh",
"refreshhard",
"clean",
"addmergedriver",
"directory:",
"switch:",
"mode:",
"keys:"
);
$options = getopt("m:r:sy",$longopts);
if(empty($options) || isset($options['help'])) {
freepbx::showHelp('freepbx_git.php',$help);
exit(0);
}
$mode = !empty($options['mode']) ? $options['mode'] : $mode;
$directory = !empty($options['directory']) ? $options['directory'] : $vars['repo_directory'];
if(!file_exists($directory)) {
$create = isset($options['y']) ? 'y' :
freepbx::getInput("Directory Doesnt Exist, Create? (y/n)",'y');
if($create == 'n' || !mkdir($directory)) {
die($directory . " Does Not Exist \n");
}
}
if(isset($options['clean']) && isset($options['m'])) {
if(file_exists($directory.'/'.$options['m'])) {
freepbx::outn('Cleaning '.$options['m'].'...');
try {
$repo = Git::open($directory.'/'.$options['m']);
$repo->prune('origin');
$repo->delete_all_tags();
} catch (Exception $e) {
freepbx::out($e->getMessage());
exit(1);
}
freepbx::out('Done');
}
exit(0);
} elseif(isset($options['clean'])) {
$modules = glob($directory.'/*', GLOB_ONLYDIR);
foreach($modules as $mod_dir) {
freepbx::outn('Cleaning '.$mod_dir.'...');
try {
$repo = Git::open($mod_dir);
$repo->prune('origin');
$repo->delete_all_tags();
} catch (Exception $e) {
freepbx::out($e->getMessage());
exit(1);
}
freepbx::out('Done');
}
exit(0);
}
if(isset($options['s'])) {
$freepbx = new freepbx('','', false);
$freepbx->setupSymLinks($directory);
exit(0);
}
if(isset($options['m'])) {
if(!file_exists($directory.'/'.$options['m'])) {
if (empty($vars['username'])) {
$username = freepbx::getInput("FreePBX Username");
} else {
$username = $vars['username'];
}
if (empty($vars['password'])) {
$password = freepbx::getPassword("FreePBX Password", true);
} else {
$password = $vars['password'];
}
try{
$stash = new Stash($username,$password);
}catch(Exception $e){
echo $e->getMessage().PHP_EOL;
return false;
}
$repo = false;
if(isset($options['r'])) {
$repo = $stash->getRepo($options['m'], $options['r']);
if ($repo === false) {
freepbx::out("[ERROR] Unable to find ".$options['m']);
exit(0);
}
} else {
foreach($projects as $project => $description) {
$repo = $stash->getRepo($options['m'],$project);
if ($repo === false) {
freepbx::out("[WARN] ".$options['m']." is NOT in the ".$description);
} else {
break;
}
}
if ($repo === false) {
freepbx::out("[ERROR] Unable to find ".$options['m']);
exit(0);
}
}
$uri = ($mode == 'http') ? $repo['cloneUrl'] : $repo['cloneSSH'];
$dir = $directory.'/'.$options['m'];
freepbx::out("Cloning ".$repo['name'] . " into ".$dir);
$repo = Git::create($dir, $uri);
if(($mode == 'http') && version_compare(Git::version(),'1.7.9', '<')) {
freepbx::out("HTTP Mode is only supported with GIT 1.7.9 or Higher");
die();
} elseif($mode == 'http') {
Git::enable_credential_cache();
}
if(isset($options['switch']) && !empty($options['switch'])) {
freepbx::switchBranch($dir,$options['switch']);
}
$repo->add_merge_driver();
freepbx::out("Done");
$freepbx = new freepbx($username,$password);
$freepbx->setupSymLinks($directory);
} else {
freepbx::out("Module Already Exists");
if(!file_exists($directory.'/framework/amp_conf/htdocs/admin/modules/'.$options['m'])) {
if (empty($vars['username'])) {
$username = freepbx::getInput("FreePBX Username");
} else {
$username = $vars['username'];
}
if (empty($vars['password'])) {
$password = freepbx::getPassword("FreePBX Password", true);
} else {
$password = $vars['password'];
}
$freepbx = new freepbx($username,$password);
$freepbx->setupSymLinks($directory);
}
}
if (empty($vars['dev_symlinks'])) {
$ul = isset($options['y']) ? 'y' :
freepbx::getInput("Update Dev SymLinks?",'n');
} else {
$ul = $vars['dev_symlinks'];
}
if($ul == 'yes' || $ul == 'y') {
if (file_exists($directory.'/framework/install')) {
freepbx::outn("Updating links through install...");
$pwd = getcwd();
chdir($directory.'/framework');
passthru('./install --dev-links -n');
chdir($pwd);
freepbx::out("Done");
} else if (file_exists($directory.'/framework/install_amp')) {
freepbx::outn("Updating links through install_amp...");
$pwd = getcwd();
chdir($directory.'/framework');
passthru('./install_amp --update-links');
chdir($pwd);
freepbx::out("Done");
}
}
exit(0);
}
if(!isset($options['setup']) && isset($options['switch']) && !empty($options['switch'])) {
foreach(glob($directory."/*", GLOB_ONLYDIR) as $dir) {
freepbx::switchBranch($dir,$options['switch']);
}
exit(0);
}
if(isset($options['refresh'])) {
foreach(glob($directory."/*", GLOB_ONLYDIR) as $dir) {
freepbx::refreshRepo($dir);
}
exit(0);
}
if(isset($options['refreshhard'])) {
foreach(glob($directory."/*", GLOB_ONLYDIR) as $dir) {
freepbx::refreshRepo($dir,'origin',null,true);
}
exit(0);
}
if(isset($options['addmergedriver'])) {
foreach(glob($directory."/*", GLOB_ONLYDIR) as $dir) {
freepbx::outn("Attempting to open ".$dir."...");
//Attempt to open the module as a git repo, bail if it's not a repo
try {
$repo = Git::open($dir);
freepbx::out("Done");
} catch (Exception $e) {
freepbx::out("Skipping");
continue;
}
$gitatts = $repo->add_merge_driver();
if(!empty($gitatts)) {
file_put_contents($dir.'/.gitattributes', $gitatts);
}
}
exit(0);
}
if(isset($options['setup'])) {
if(is_link($directory)) {
freepbx::out("Confused. $directory is a symbolic link. Please resolve then run this again");
exit(1);
}
if (empty($vars['username'])) {
$username = freepbx::getInput("FreePBX Username");
} else {
$username = $vars['username'];
}
if (empty($vars['password'])) {
$password = freepbx::getPassword("FreePBX Password", true);
} else {
$password = $vars['password'];
}
try {
$freepbx = new freepbx($username,$password);
} catch (Exception $e) {
freepbx::out("Invalid Username/Password Combination");
exit(1);
}
if(isset($options['keys'])) {
$pkeys = explode(",",$options['keys']);
} else {
$pkeys = array("freepbx");
}
$force = isset($options['force']);
$branch = isset($options['switch']) && !empty($options['switch']) ? $options['switch'] : 'develop';
foreach($pkeys as $k) {
$freepbx->setupDevRepos($directory,$force,$mode,$branch,$k);
}
$freepbx->setupSymLinks($directory);
exit(0);
}
freepbx::out("Invalid Command");
freepbx::showHelp('freepbx_git.php',$help);
exit;