-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathajax.php
More file actions
332 lines (255 loc) · 12.4 KB
/
ajax.php
File metadata and controls
332 lines (255 loc) · 12.4 KB
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<?php
require_once dirname(__FILE__) . '/config.php';
$struct = array(
'status' => 1,
'result' => '',
'msg' => '',
);
try {
$cmd = empty($_REQUEST['cmd']) ? 'release_free_plugin' : $_REQUEST['cmd'];
$ver = empty($_REQUEST['new_ver']) ? '' : $_REQUEST['new_ver'];
$plugin_dir = empty($_REQUEST['plugin_dir']) ? '' : $_REQUEST['plugin_dir'];
$plugin_dir = strip_tags($plugin_dir);
$plugin_dir = trim($plugin_dir);
$plugin_dir = str_replace('..', '', $plugin_dir);
if (!is_dir($plugin_dir)) {
throw new Exception("Plugin directory doesn't exist.");
}
switch ($cmd) {
case 'package_pro_plugin':
$main_plugin_file = App_Release_Manager_File::findMainPluginFile($plugin_dir);
if (empty($main_plugin_file)) {
throw new Exception("Cannot find main plugin file.");
}
$wp_res = App_Release_Manager_WP_Lib::parse( $main_plugin_file );
if ( ! is_dir( $wp_res[ 'target_release_dir' ] ) ) {
$mkdir_res = mkdir( $wp_res[ 'target_release_dir' ], 0700, true );
if ( empty( $mkdir_res ) ) {
throw new Exception( "Couldn't create the release dir." );
}
}
if ( empty( $wp_res['plugin_id'] ) ) {
$struct['result'] .= "Something's wrong.";
break;
}
$extra_cool_params = [
'exclude' => [],
];
$buff = '';
$git_ignore = "$plugin_dir/.gitignore";
// We'll exclude some files and directories from the pkg for various reasons
// not necessary or for production.
// @todo skip non-minified versions of the assets.
// check what's in git ignore
if (file_exists($git_ignore)) {
$buff .= file_get_contents($git_ignore);
$buff .= "\n";
}
$rl_ignore = "$plugin_dir/.release_manager_ignore";
if (file_exists($rl_ignore)) {
$buff .= file_get_contents($rl_ignore);
$buff .= "\n";
}
$dist_ignore = "$plugin_dir/.distignore";
if (file_exists($dist_ignore)) {
$buff .= file_get_contents($dist_ignore);
$buff .= "\n";
}
$buff = trim($buff);
if (!empty($buff)) {
$lines = preg_split('#[\r\n]+#si', $buff);
$lines = array_map('trim', $lines);
$lines = array_filter($lines);
$lines = array_unique($lines);
// Let's see what to ignore;
// files: */some-file.txt
// dirs: */mu-plugins/*
// '-x ' . escapeshellarg('*.idea/*'),
foreach ($lines as $item) {
if (empty($item) || preg_match('/^\h*[#;]/si', $item)) { // comments?
continue;
}
$exclude = '';
$item_fmt = $item;
$item_fmt = rtrim($item_fmt, '/');
$period_pos = strpos(basename($item_fmt), '.'); // must be a file
if ($period_pos !== false) {
$exclude = '*/' . $item_fmt;
} else {
$exclude = '*/' . $item_fmt . '/*';
}
$extra_cool_params['exclude'][] = $exclude;
}
}
$target_zip_file = $wp_res['target_release_file'];
$plugin_dir = $wp_res['plugin_dir'];
$zip_res = App_Release_Manager_File::archive( $target_zip_file, $plugin_dir, $extra_cool_params );
if (empty($zip_res)) {
throw new Exception("Couldn't create the zip file.");
}
$update_rec = array(
"author" => "<a href='https://orbisius.com' target='_blank'>Orbisius.com</a>",
"author_profile" => "https://profiles.wordpress.org/lordspace/",
"downloaded" => 'n/a',
"homepage" => "https://orbisius.com/products/wordpress-plugins/{$wp_res['plugin_id']}/",
"requires" => "3.0",
"tested" => $wp_res['tested_with_wp_version'],
"url" => "https://orbisius.com/products/wordpress-plugins/{$wp_res['plugin_id']}/"
);
$cur_dir = getcwd(); // get it so we can go back jic
$exit_code = 0;
App_Release_Manager_Release::initEnv();
$files = []; // to be committed
$upd_file = $wp_res['target_release_dir'] . '/update.json';
$save_res = file_put_contents( $upd_file, json_encode( $update_rec, JSON_PRETTY_PRINT ), LOCK_EX );
if (empty($save_res)) {
throw new Exception("Couldn't save the update.json file in release dir.");
}
if ( ! empty( $wp_res['change_log'] )) { // 1 level up is the change log
$change_log_file = dirname( $wp_res['target_release_dir'] ) . '/changelog.txt';
file_put_contents( $change_log_file, $wp_res['change_log'] );
$files[] = $change_log_file;
}
$struct['result'] .= "<pre>";
$rel_dir_linux = $wp_res['target_release_dir'];
$struct['result'] .= "\nRelease dir (linux): <input type='text' value='$rel_dir_linux' class='full_width' readonly='readonly' onclick='this.select();' />\n";
$rel_dir_win = $wp_res['target_release_dir_windows'];
$struct['result'] .= "\nRelease dir (win): <input type='text' value='$rel_dir_win' class='full_width' readonly='readonly' onclick='this.select();' />\n";
$struct['result'] .= var_export($wp_res, 1);
// $struct['result'] .= var_export($zip_res, 1);
$struct['result'] .= "</pre>";
// Let's add to git the new files to git
$files[] = $upd_file;
$files[] = $target_zip_file;
$git_cli = APP_GIT_BIN;
foreach ($files as $file) {
$file_base = basename($file);
$file_dir = dirname($file);
chdir($file_dir);
$file_esc = escapeshellarg($file_base);
// Let's check if this file was added already
$git_cmd = "$git_cli status -s $file_esc"; // short status and it displays ?? and M
$last_line = exec($git_cmd, $output_arr, $exit_code);
if (!empty($exit_code)) {
$struct['result'] .= "<pre>Error: couldn't git status: [$file_esc]." . htmlentities(join('', $output_arr) . "</pre>");
continue;
}
// if ? or modified then add otherwise skip?
$last_line = trim($last_line);
if (!preg_match('#^\h*(\?+|M)\h+' . preg_quote($file_base, '#') . '#si', $last_line)) {
$struct['result'] .= "File [$file_esc] is not modified or new. Skipping it [$last_line].\n";
continue;
}
// Let's add the file first
$git_cmd = "$git_cli add $file_esc";
$last_line = exec($git_cmd, $output_arr, $exit_code);
if (!empty($exit_code)) {
$struct['result'] .= "<pre>Error: couldn't git add: [$file_esc]." . htmlentities(join('', $output_arr) . "</pre>");
continue;
}
// Let's commit the file. It seems for windows it's better to have the file first
// https://stackoverflow.com/questions/8795097/how-to-git-commit-a-single-file-directory
$git_cmd = "$git_cli commit"
. " -o $file_esc " // -o, --only commit only specified files
. " -m " . escapeshellarg("Committing file [$file] for " . $wp_res['plugin_id']);
$git_cmd .= ' 2>&1';
$last_line = exec($git_cmd, $output_arr, $exit_code);
if (!empty($exit_code)) {
$struct['result'] .= "<pre>Error: couldn't git commit: [$file_esc]." . htmlentities(join('', $output_arr) . "</pre>");
continue;
}
} // loop
// try to push it too
if (strpos($git_cli, '/ogit') !== false) {
// Let's do pull first just in case. ? or fetch?
$git_cmd = "$git_cli pull";
$git_cmd .= ' 2>&1';
$last_line = exec($git_cmd, $output_arr, $exit_code);
if (empty($exit_code)) {
$struct['result'] .= " pulled\n";
} else {
$struct['result'] .= "<pre>Error: couldn't git do git push: [$file_esc]." . htmlentities(join('', $output_arr) . "</pre>");
}
// now try to push
$git_cmd = "$git_cli push origin master";
$git_cmd .= ' 2>&1';
$last_line = exec($git_cmd, $output_arr, $exit_code);
if (empty($exit_code)) {
$struct['result'] .= " pushed\n";
} else {
$struct['result'] .= "<pre>Error: couldn't git do git push: [$file_esc]." . htmlentities(join('', $output_arr) . "</pre>");
}
}
// git push
if (0&& empty($exit_code)) {
$git_cmd = "git push origin master";
$last_line = exec($git_cmd, $output_arr, $exit_code);
if (!empty($exit_code)) {
$struct['result'] .= "Error: couldn't git push {$wp_res['plugin_id']}" . htmlentities(join('', $output_arr));
} else {
$struct['result'] .= ' pushed';
}
}
chdir($cur_dir);
break;
case 'release_free_plugin':
$stored_ver = App_Release_Manager_Release::getRelease($plugin_dir);
// 1.0.1
if (!empty($stored_ver) && version_compare($ver, $stored_ver, '<=')) {
throw new Exception("The plugin has already been tagged with this verison [$ver] or the version is smaller than current one.");
}
if (empty($ver)) {
throw new Exception("Target version not specified.");
}
$cur_dir = getcwd();
chdir($plugin_dir);
$info = `svn info 2>&1`;
$data = App_Release_Manager_File::parsePluginMeta('', $info);
$trunk_url = empty( $data['URL'] ) ? '' : $data['URL'];
if ( empty( $trunk_url ) ) {
throw new Exception("Cannot detect trunk URL for $plugin_dir. [$info]");
}
$trunk_url = rtrim($trunk_url, '/') . '/';
$tags_url = str_replace('trunk', 'tags', $trunk_url);
$new_tag_url = $tags_url . $ver;
$struct['result'] .= $plugin_dir;
$struct['result'] .= "<pre>";
$struct['result'] .= "trunk_url : $trunk_url\n";
$struct['result'] .= "new_tag_url : $new_tag_url\n";
$cmd_tag = "svn cp "
. " --non-interactive "
. " --username=" . escapeshellarg(APP_SVN_USER)
. " --password=" . escapeshellarg(APP_SVN_PASS)
. ' ' . escapeshellarg($trunk_url)
. ' ' . escapeshellarg($new_tag_url)
. ' --message=' . escapeshellarg("Released version $ver") . ' 2>&1';
set_time_limit(6 * 60);
//$run_cmd = $cmd_tag;
$run_cmd = `$cmd_tag`; // svn commit is slow sometimes (or most of the time).
$struct['result'] .= "cmd: [$cmd_tag]";
$struct['result'] .= $run_cmd;
if (!preg_match('#Committed revision\s+\d+#si', $run_cmd)) {
throw new Exception("Commit failed. Cmd output: " . $run_cmd);
} else {
$struct['result'] .= App_Release_Manager_String::msg('Pushed new version successfully.', 1);
}
// $rel_dir_linux = $data['target_release_dir'];
// $struct['result'] .= "Release dir (linux): <input type='text' value='$rel_dir_linux' />";
//
// $rel_dir_win = $data['target_release_dir_windows'];
// $struct['result'] .= "Release dir (win): <input type='text' value='$rel_dir_win' />";
$struct['result'] .= var_export($data, 1);
App_Release_Manager_Release::setRelease($plugin_dir, $ver);
chdir($cur_dir);
$struct['result'] .= "</pre>";
break;
default:
break;
}
} catch (Exception $e) {
$struct['status'] = 0;
$struct['msg'] = $e->getMessage();
$struct['result'] .= App_Release_Manager_String::msg($e->getMessage(), 0);
} finally {
App_Release_Manager_Ajax::sendJSON($struct);
}