Repository navigation
Expand file tree
/
Copy pathdeploy.php
More file actions
443 lines (331 loc) · 9.79 KB
/
Copy pathdeploy.php
File metadata and controls
443 lines (331 loc) · 9.79 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
<?php
/**
* Autoload.
*/
require __DIR__ . '/vendor/autoload.php';
use SolveBeam\GitHubActionsToolkit\Core;
use SolveBeam\GitHubActionsToolkit\Shell;
use SolveBeam\GitHubActionsToolkit\Style;
use SolveBeam\GitHubActionsToolkit\Summary;
/**
* Default environment variables.
*
* @link https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/variables#default-environment-variables
*/
$repository = getenv( 'GITHUB_REPOSITORY' );
/**
* Setup.
*/
$core = new Core();
$style = new Style();
$shell = new Shell();
$username = $core->get_required_input( 'username' );
$password = $core->get_required_input( 'password' );
$slug = $core->get_required_input( 'slug' );
$tag = $core->get_required_input( 'tag' );
$version = ltrim( $tag, 'v' );
$dry_run = $core->get_boolean_input( 'dry-run' );
$svn_url = "https://plugins.svn.wordpress.org/$slug";
$svn_url_tag = "$svn_url/tags/$version";
$public_url = "https://wordpress.org/plugins/$slug/";
$trac_url = "https://plugins.trac.wordpress.org/browser/$slug/";
$release_url = "https://github.com/$repository/releases/tag/$tag";
/**
* Filename.
*
* Notation: “{name}.{version}”.
*
* @link https://developer.wordpress.org/cli/commands/dist-archive/
*/
$filename = "$slug.$version.zip";
/**
* Files.
*/
$work_dir = tempnam( sys_get_temp_dir(), '' );
unlink( $work_dir );
mkdir( $work_dir );
$archives_dir = $work_dir . '/archives';
$plugins_dir = $work_dir . '/plugins';
$svn_dir = $work_dir . '/svn';
mkdir( $archives_dir );
mkdir( $plugins_dir );
mkdir( $svn_dir );
$plugin_dir = $plugins_dir . '/' . $slug;
$readme_file = $plugin_dir . '/readme.txt';
/**
* Start.
*/
$core->start_group( 'ℹ️ Release to WordPress.org' );
echo '• ', $style->bold( 'Subversion URL:' ), ' ', $svn_url, PHP_EOL;
echo '• ', $style->bold( 'Subversion username:' ), ' ', $username, PHP_EOL;
echo '• ', $style->bold( 'Dry run:' ), ' ', $dry_run ? 'yes' : 'no', PHP_EOL;
$core->end_group();
$changes = [];
$commands = [];
$error = null;
try {
/**
* GitHub release.
*/
$core->start_group( '🐙 Check GitHub release' );
$command = sprintf(
'gh release view %s --repo %s',
escapeshellarg( $tag ),
escapeshellarg( $repository )
);
$commands[] = $command;
$shell->must_run( $command );
$core->end_group();
/**
* Download release.
*
* @link https://cli.github.com/manual/gh_release_download
*/
$core->start_group( '📥 Download plugin' );
$command = sprintf(
'gh release download %s --pattern %s --dir %s --repo %s',
escapeshellarg( $tag ),
escapeshellarg( $filename ),
escapeshellarg( $archives_dir ),
escapeshellarg( $repository )
);
$commands[] = $command;
$shell->must_run( $command );
$core->end_group();
/**
* Unzip.
*/
$core->start_group( '📦 Unzip plugin' );
$command = sprintf(
'unzip %s -d %s',
escapeshellarg( $archives_dir . '/' . $filename ),
escapeshellarg( $plugins_dir )
);
$commands[] = $command;
$shell->must_run( $command );
$core->end_group();
/**
* Parse stable tag.
*/
if ( ! is_file( $readme_file ) ) {
throw new RuntimeException( "The archive $filename does not contain the file $slug/readme.txt." );
}
$readme_content = file_get_contents( $readme_file );
$pattern = "/Stable tag: (.*)/";
$stable_tag = '';
if ( 1 === preg_match( $pattern, $readme_content, $matches ) ) {
$stable_tag = trim( $matches[1] );
}
if ( $stable_tag !== $version ) {
throw new RuntimeException( "Stable tag in readme.txt ($stable_tag) is not equal to release version ($version)." );
}
/**
* Check tag existence.
*/
$core->start_group( '🔎 Check tag existence' );
$command = sprintf(
'svn info %s',
escapeshellarg( $svn_url_tag )
);
$commands[] = $command;
$result_code = $shell->run( $command );
if ( 0 === $result_code ) {
throw new RuntimeException( "There is already a tag for version $version: $svn_url_tag" );
}
$core->end_group();
/**
* Subversion.
*
* @link https://stackoverflow.com/a/122291
*/
$core->start_group( '⬇ Subversion checkout WordPress.org' );
$command = sprintf(
'svn checkout %s %s --depth immediates',
escapeshellarg( $svn_url ),
escapeshellarg( $svn_dir )
);
$commands[] = $command;
$shell->must_run( $command );
chdir( $svn_dir );
$command = 'svn update trunk --set-depth infinity';
$commands[] = $command;
$shell->must_run( $command );
$core->end_group();
/**
* Synchronize.
*
* @link http://stackoverflow.com/a/14789400
* @link http://askubuntu.com/a/476048
*/
$core->start_group( '🔄 Synchronize plugin' );
$command = sprintf(
'rsync --archive --delete --verbose %s %s',
escapeshellarg( $plugin_dir . '/' ),
escapeshellarg( $svn_dir . '/trunk/' )
);
$commands[] = $command;
$shell->must_run( $command );
$core->end_group();
/**
* Subversion modifications.
*/
$core->start_group( '💾 Subversion modifications' );
$command = 'svn status';
$commands[] = $command;
$shell->must_run( $command );
$output = shell_exec( 'svn status --xml' );
$xml = simplexml_load_string( $output );
if ( false === $xml ) {
throw new RuntimeException( 'A problem occurred while reading the `svn status --xml`.' );
}
foreach ( $xml->target->entry as $entry ) {
$path = (string) $entry['path'];
$wc_status = (string) $entry->{'wc-status'}['item'];
/**
* Peg revision.
*
* A path can point to different files over time, because files can be
* renamed, deleted or replaced. Subversion therefore allows a revision
* to be appended to a path, as in `path@REV`, to identify which file is
* meant: the one that had this path in revision `REV`. This is called
* a peg revision.
*
* Subversion applies this notation to every path argument and treats
* everything after the last at sign as the peg revision. A file like
* `icon@2x.png` is therefore read as the path `icon` with peg revision
* `2x.png`, and `svn add` and `svn rm` fail with “a peg revision is not
* allowed here”.
*
* Because only the last at sign counts, we append one to every path:
* `icon@2x.png@` is read as the path `icon@2x.png` with an empty peg
* revision. This is harmless for paths without an at sign.
*
* @link https://svnbook.red-bean.com/en/1.7/svn.advanced.pegrevs.html
*/
switch ( $wc_status ) {
case 'missing':
$command = sprintf(
'svn rm %s',
escapeshellarg( $path . '@' )
);
$commands[] = $command;
$shell->must_run( $command );
$changes[] = [ '➖ Removed', $path ];
break;
case 'modified':
// Modified entry will be commited.
$changes[] = [ '✏️ Modified', $path ];
break;
case 'unversioned':
$command = sprintf(
'svn add %s',
escapeshellarg( $path . '@' )
);
$commands[] = $command;
$shell->must_run( $command );
$changes[] = [ '➕ Added', $path ];
break;
default:
throw new RuntimeException( "Unsupported working copy status: $wc_status - $path." );
}
}
$core->end_group();
/**
* Commit.
*/
$core->start_group( '⬆ Subversion commit WordPress.org' );
$command = sprintf(
'svn commit --message %s --non-interactive --username %s --password %s',
escapeshellarg( 'Update' ),
escapeshellarg( $username ),
escapeshellarg( $password )
);
if ( $dry_run ) {
$shell->skip( $command );
$commands[] = "# Skipped (dry run): $command";
} else {
$commands[] = $command;
$shell->must_run( $command );
}
$core->end_group();
/**
* Tag.
*/
$core->start_group( '🏷️ Subversion tag WordPress.org' );
$command = sprintf(
'svn cp %s %s --message %s --non-interactive --username %s --password %s',
escapeshellarg( "$svn_url/trunk" ),
escapeshellarg( $svn_url_tag ),
escapeshellarg( "Tagging version $version" ),
escapeshellarg( $username ),
escapeshellarg( $password )
);
if ( $dry_run ) {
$shell->skip( $command );
$commands[] = "# Skipped (dry run): $command";
} else {
$commands[] = $command;
$shell->must_run( $command );
}
$core->end_group();
/**
* Clean up.
*/
$core->start_group( '🗑️ Clean up' );
$command = sprintf(
'rm -f -R %s',
escapeshellarg( $work_dir )
);
$commands[] = $command;
$shell->must_run( $command );
$core->end_group();
} catch ( RuntimeException $e ) {
$error = $e->getMessage();
}
/**
* Job summary.
*
* Written before `set_failed()`, because that exits.
*/
$status = match ( true ) {
null !== $error => "❌ Failed: $error",
$dry_run => '🧪 Dry run, nothing committed or tagged.',
default => '✅ Deployed.',
};
$summary = new Summary();
$summary
->add_heading( "Deploy $slug $version to WordPress.org" . ( $dry_run ? ' (dry run)' : '' ), 2 )
->add_eol()
->add_raw( $status, true )
->add_eol()
->add_raw( "- **GitHub release:** <$release_url>", true )
->add_raw( "- **Plugin:** <$public_url>", true )
->add_raw( "- **Subversion:** <$svn_url>", true )
->add_raw( "- **Subversion tag:** <$svn_url_tag>", true )
->add_raw( "- **Trac:** <$trac_url>", true )
->add_eol()
->add_heading( 'Subversion changes', 3 )
->add_eol();
if ( [] === $changes ) {
$summary->add_raw( 'No changes.', true );
} else {
$summary
->add_raw( '| Status | Path |', true )
->add_raw( '| --- | --- |', true );
foreach ( $changes as list( $status, $path ) ) {
$summary->add_raw( "| $status | `$path` |", true );
}
}
$summary
->add_eol()
->add_heading( 'Commands', 3 )
->add_eol()
->add_code_block( implode( PHP_EOL, $commands ), 'shell' )
->add_eol()
->add_raw( '---', true )
->add_eol()
->add_raw( '<sub>Deployed with [solvebeam/action-wordpress-org-plugin-deploy](https://github.com/solvebeam/action-wordpress-org-plugin-deploy), built on [solvebeam/github-actions-toolkit-php](https://github.com/solvebeam/github-actions-toolkit-php). Made with ❤️ by [SolveBeam](https://www.solvebeam.com/).</sub>', true )
->write();
if ( null !== $error ) {
$core->set_failed( $error );
}