-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathDist_Archive_Command.php
508 lines (454 loc) · 17.1 KB
/
Dist_Archive_Command.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
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
<?php
use Inmarelibero\GitIgnoreChecker\GitIgnoreChecker;
use WP_CLI\Utils;
/**
* Create a distribution archive based on a project's .distignore file.
*/
class Dist_Archive_Command {
/**
* @var GitIgnoreChecker
*/
private $checker;
/**
* Create a distribution archive based on a project's .distignore file.
*
* For a plugin in a directory 'wp-content/plugins/hello-world', this command
* creates a distribution archive 'wp-content/plugins/hello-world.zip'.
*
* You can specify files or directories you'd like to exclude from the archive
* with a .distignore file in your project repository:
*
* ```
* .distignore
* .editorconfig
* .git
* .gitignore
* .travis.yml
* circle.yml
* ```
*
* Use one distribution archive command for many projects, instead of a bash
* script in each project.
*
* ## OPTIONS
*
* <path>
* : Path to the project that includes a .distignore file.
*
* [<target>]
* : Path and optional file name for the distribution archive.
* If only a path is provided, the file name defaults to the project directory name plus the version, if discoverable.
* Also, if only a path is given, the directory that it points to has to already exist for the command to function correctly.
*
* [--create-target-dir]
* : Automatically create the target directory as needed.
*
* [--plugin-dirname=<plugin-slug>]
* : Set the archive extract directory name. Defaults to project directory name.
*
* [--format=<format>]
* : Choose the format for the archive.
* ---
* default: zip
* options:
* - zip
* - targz
* ---
*
* [--filename-format=<filename-format>]
* : Use a custom format for archive filename. Available substitutions: {name}, {version}.
* This is ignored if the <target> parameter is provided or the version cannot be determined.
* ---
* default: "{name}.{version}"
* ---
*
* @when before_wp_load
*/
public function __invoke( $args, $assoc_args ) {
list( $source_dir_path, $destination_dir_path, $archive_file_name, $archive_output_dir_name ) = $this->get_file_paths_and_names( $args, $assoc_args );
$this->checker = new GitIgnoreChecker( $source_dir_path, '.distignore' );
$dist_ignore_filepath = $source_dir_path . '/.distignore';
if ( file_exists( $dist_ignore_filepath ) ) {
$file_ignore_rules = explode( PHP_EOL, file_get_contents( $dist_ignore_filepath ) );
} else {
WP_CLI::warning( 'No .distignore file found. All files in directory included in archive.' );
$file_ignore_rules = [];
}
if ( basename( $source_dir_path ) !== $archive_output_dir_name || $this->is_path_contains_symlink( $source_dir_path ) ) {
$tmp_dir = sys_get_temp_dir() . '/' . uniqid( $archive_file_name );
$new_path = "{$tmp_dir}/{$archive_output_dir_name}";
mkdir( $new_path, 0777, true );
foreach ( $this->get_file_list( $source_dir_path ) as $relative_filepath ) {
$source_item = $source_dir_path . $relative_filepath;
if ( is_dir( $source_item ) ) {
mkdir( "{$new_path}/{$relative_filepath}", 0777, true );
} else {
copy( $source_item, $new_path . $relative_filepath );
}
}
$source_path = $new_path;
} else {
$source_path = $source_dir_path;
}
$archive_absolute_filepath = "{$destination_dir_path}/{$archive_file_name}";
if ( file_exists( $archive_absolute_filepath ) ) {
WP_CLI::warning( 'Archive file already exists' );
WP_CLI::log( $archive_absolute_filepath );
$answer = \cli\prompt(
'Do you want to skip or replace it with a new archive?',
$default = false,
$marker = ' [s/r]: '
);
$should_overwrite = 'r' === strtolower( $answer );
if ( ! $should_overwrite ) {
WP_CLI::log( 'Skipping' . PHP_EOL );
WP_CLI::log( 'Archive generation skipped.' );
exit( 0 );
}
WP_CLI::log( 'Replacing' . PHP_EOL );
}
chdir( dirname( $source_path ) );
// If the files are being zipped in place, we need the exclusion rules.
// whereas if they were copied for any reasons above, the rules have already been applied.
if ( $source_path !== $source_dir_path || empty( $file_ignore_rules ) ) {
if ( 'zip' === $assoc_args['format'] ) {
$cmd = "zip -r '{$archive_absolute_filepath}' {$archive_output_dir_name}";
} elseif ( 'targz' === $assoc_args['format'] ) {
$cmd = "tar -zcvf {$archive_absolute_filepath} {$archive_output_dir_name}";
}
} else {
$tmp_dir = sys_get_temp_dir() . '/' . uniqid( $archive_file_name );
mkdir( $tmp_dir, 0777, true );
if ( 'zip' === $assoc_args['format'] ) {
$include_list_filepath = $tmp_dir . '/include-file-list.txt';
file_put_contents(
$include_list_filepath,
trim(
implode(
"\n",
array_map(
function ( $relative_path ) use ( $source_path ) {
return basename( $source_path ) . $relative_path;
},
$this->get_file_list( $source_path )
)
)
)
);
$cmd = "zip -r '{$archive_absolute_filepath}' {$archive_output_dir_name} -i@{$include_list_filepath}";
} elseif ( 'targz' === $assoc_args['format'] ) {
$exclude_list_filepath = "{$tmp_dir}/exclude-file-list.txt";
$excludes = array_filter(
array_map(
function ( $ignored_file ) use ( $source_path ) {
$regex = preg_quote( basename( $source_path ) . $ignored_file, '\\' );
return ( php_uname( 's' ) === 'Linux' ) ? $regex : "^{$regex}$";
},
$this->get_file_list( $source_path, true )
)
);
file_put_contents(
$exclude_list_filepath,
trim( implode( "\n", $excludes ) )
);
$anchored_flag = ( php_uname( 's' ) === 'Linux' ) ? '--anchored ' : '';
$cmd = "tar {$anchored_flag} --exclude-from={$exclude_list_filepath} -zcvf {$archive_absolute_filepath} {$archive_output_dir_name}";
}
}
$escape_whitelist = 'targz' === $assoc_args['format'] ? array( '^', '*' ) : array();
WP_CLI::debug( "Running: {$cmd}", 'dist-archive' );
$escaped_shell_command = $this->escapeshellcmd( $cmd, $escape_whitelist );
$ret = WP_CLI::launch( $escaped_shell_command, false, true );
if ( 0 === $ret->return_code ) {
$filename = pathinfo( $archive_absolute_filepath, PATHINFO_BASENAME );
WP_CLI::success( "Created {$filename}" );
} else {
$error = $ret->stderr ?: $ret->stdout;
WP_CLI::error( $error );
}
}
/**
* Determine the full paths and names to use from the CLI input.
*
* I.e. the source directory, the output directory, the output filename, and the directory name the archive will
* extract to.
*
* @param non-empty-array<string> $args Source path (required), target (path or name, optional).
* @param array{format:string,filename-format:string,plugin-dirname?:string,create-target-dir?:bool} $assoc_args
*
* @return string[] $source_dir_path, $destination_dir_path, $destination_archive_name, $archive_output_dir_name
*/
private function get_file_paths_and_names( $args, $assoc_args ) {
$source_dir_path = realpath( $args[0] );
if ( ! is_dir( $source_dir_path ) ) {
WP_CLI::error( 'Provided input path is not a directory.' );
}
if ( isset( $args[1] ) ) {
$destination_input = $args[1];
// If the end of the string is a filename (file.ext), use it for the output archive filename.
if ( 1 === preg_match( '/(zip$|tar$|tar.gz$)/', $destination_input ) ) {
$archive_file_name = basename( $destination_input );
// If only the filename was supplied, use the plugin's parent directory for output, otherwise use
// the supplied directory.
$destination_dir_path = basename( $destination_input ) === $destination_input
? dirname( $source_dir_path )
: dirname( $destination_input );
} else {
// Only a path was supplied, not a filename.
$destination_dir_path = $destination_input;
$archive_file_name = null;
}
} else {
// Use the plugin's parent directory for output.
$destination_dir_path = dirname( $source_dir_path );
$archive_file_name = null;
}
// Convert relative path to absolute path (check does it begin with e.g. "c:" or "/").
if ( 1 !== preg_match( '/(^[a-zA-Z]+:|^\/)/', $destination_dir_path ) ) {
$destination_dir_path = getcwd() . '/' . $destination_dir_path;
}
if ( Utils\get_flag_value( $assoc_args, 'create-target-dir' ) ) {
$this->maybe_create_directory( $destination_dir_path );
}
$destination_dir_path = realpath( $destination_dir_path );
if ( ! is_dir( $destination_dir_path ) ) {
WP_CLI::error( "Target directory does not exist: {$destination_dir_path}" );
}
// Use the optionally supplied plugin-dirname, or use the name of the directory containing the source files.
$archive_output_dir_name = isset( $assoc_args['plugin-dirname'] )
? rtrim( $assoc_args['plugin-dirname'], '/' )
: basename( $source_dir_path );
if ( is_null( $archive_file_name ) ) {
$version = $this->get_version( $source_dir_path );
// If the version number has been found, substitute it into the filename-format template, or just use the name.
$archive_file_stem = ! empty( $version )
? str_replace( [ '{name}', '{version}' ], [ $archive_output_dir_name, $version ], $assoc_args['filename-format'] )
: $archive_output_dir_name;
$archive_file_name = 'zip' === $assoc_args['format']
? $archive_file_stem . '.zip'
: $archive_file_stem . '.tar.gz';
}
return [ $source_dir_path, $destination_dir_path, $archive_file_name, $archive_output_dir_name ];
}
/**
* Determine the plugin version from style.css, the main plugin .php file, or composer.json.
*
* Append the commit hash to `-alpha` versions.
*
* @param string $source_dir_path
*
* @return string
*/
private function get_version( $source_dir_path ) {
$version = '';
/**
* If the path is a theme (meaning it contains a style.css file)
* parse the theme's version from the headers using a regex pattern.
* The pattern used is extracted from the get_file_data() function in core.
*
* @link https://developer.wordpress.org/reference/functions/get_file_data/
*/
if ( file_exists( $source_dir_path . '/style.css' ) ) {
$contents = file_get_contents( $source_dir_path . '/style.css', false, null, 0, 5000 );
$contents = str_replace( "\r", "\n", $contents );
$pattern = '/^' . preg_quote( 'Version', ',' ) . ':(.*)$/mi';
if ( preg_match( $pattern, $contents, $match ) && $match[1] ) {
$version = trim( preg_replace( '/\s*(?:\*\/|\?>).*/', '', $match[1] ) );
}
}
if ( empty( $version ) ) {
foreach ( glob( $source_dir_path . '/*.php' ) as $php_file ) {
$contents = file_get_contents( $php_file, false, null, 0, 5000 );
$version = $this->get_version_in_code( $contents );
if ( ! empty( $version ) ) {
$version = trim( $version );
break;
}
}
}
if ( empty( $version ) && file_exists( $source_dir_path . '/composer.json' ) ) {
$composer_obj = json_decode( file_get_contents( $source_dir_path . '/composer.json' ) );
if ( ! empty( $composer_obj->version ) ) {
$version = trim( $composer_obj->version );
}
}
if ( ! empty( $version ) && false !== stripos( $version, '-alpha' ) && is_dir( $source_dir_path . '/.git' ) ) {
$response = WP_CLI::launch( "cd {$source_dir_path}; git log --pretty=format:'%h' -n 1", false, true );
$maybe_hash = trim( $response->stdout );
if ( $maybe_hash && 7 === strlen( $maybe_hash ) ) {
$version .= '-' . $maybe_hash;
}
}
return $version;
}
/**
* Create the directory for a target file if it does not exist yet.
*
* @param string $destination_dir_path Directory path for the target file.
* @return void
*/
private function maybe_create_directory( $destination_dir_path ) {
if ( ! is_dir( $destination_dir_path ) ) {
mkdir( $destination_dir_path, $mode = 0777, $recursive = true );
}
}
/**
* Gets the content of a version tag in any doc block in the given source code string.
*
* The version tag might be specified as "@version x.y.z" or "Version: x.y.z" and it can
* be preceded by an asterisk (*).
*
* @param string $code_str The source code string to look into.
* @return null|string The detected version string.
*/
private function get_version_in_code( $code_str ) {
$tokens = array_values(
array_filter(
token_get_all( $code_str ),
function ( $token ) {
return ! is_array( $token ) || T_WHITESPACE !== $token[0];
}
)
);
foreach ( $tokens as $token ) {
if ( T_DOC_COMMENT === $token[0] ) {
$version = $this->get_version_in_docblock( $token[1] );
if ( null !== $version ) {
return $version;
}
}
}
return null;
}
/**
* Gets the content of a version tag in a docblock.
*
* @param string $docblock Docblock to parse.
* @return null|string The content of the version tag.
*/
private function get_version_in_docblock( $docblock ) {
$docblocktags = $this->parse_doc_block( $docblock );
if ( isset( $docblocktags['version'] ) ) {
return $docblocktags['version'];
}
return null;
}
/**
* Parses a docblock and gets an array of tags with their values.
*
* The tags might be specified as "@version x.y.z" or "Version: x.y.z" and they can
* be preceded by an asterisk (*).
*
* This code is based on the 'phpactor' package.
* @see https://github.com/phpactor/docblock/blob/master/lib/Parser.php
*
* @param string $docblock Docblock to parse.
* @return array Associative array of parsed data.
*/
private function parse_doc_block( $docblock ) {
$tag_documentor = '{@([a-zA-Z0-9-_\\\]+)\s*?(.*)?}';
$tag_property = '{\s*\*?\s*(.*?):(.*)}';
$lines = explode( PHP_EOL, $docblock );
$tags = [];
foreach ( $lines as $line ) {
if ( 0 === preg_match( $tag_documentor, $line, $matches ) ) {
if ( 0 === preg_match( $tag_property, $line, $matches ) ) {
continue;
}
}
$tag_name = strtolower( $matches[1] );
$metadata = trim( isset( $matches[2] ) ? $matches[2] : '' );
$tags[ $tag_name ] = $metadata;
}
return $tags;
}
/**
* Run PHP's escapeshellcmd() then undo escaping known intentional characters.
*
* Escaped by default: &#;`|*?~<>^()[]{}$\, \x0A and \xFF. ' and " are escaped when not paired.
*
* @see escapeshellcmd()
*
* @param string $cmd The shell command to escape.
* @param string[] $whitelist Array of exceptions to allow in the escaped command.
*
* @return string
*/
protected function escapeshellcmd( $cmd, $whitelist ) {
$escaped_command = escapeshellcmd( $cmd );
foreach ( $whitelist as $undo_escape ) {
$escaped_command = str_replace( '\\' . $undo_escape, $undo_escape, $escaped_command );
}
return $escaped_command;
}
/**
* Given the path to a directory, check are any of the directories inside it symlinks.
*
* If the plugin contains a symlink, we will first copy it to a temp directory, potentially omitting any
* symlinks that are excluded via the `.distignore` file, avoiding recursive loops as described in #57.
*
* @param string $source_dir_path The path to the directory to check.
*
* @return bool
*/
protected function is_path_contains_symlink( $source_dir_path ) {
if ( ! is_dir( $source_dir_path ) ) {
throw new Exception( 'Path `' . $source_dir_path . '` is not a directory' );
}
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator( $source_dir_path, RecursiveDirectoryIterator::SKIP_DOTS ),
RecursiveIteratorIterator::SELF_FIRST
);
/**
* @var RecursiveIteratorIterator $iterator
* @var SplFileInfo $item
*/
foreach ( $iterator as $item ) {
if ( is_link( $item->getPathname() ) ) {
return true;
}
}
return false;
}
/**
* Filter all files in a path to either: a list of files to include in, or a list of files to exclude from, the archive.
*
* Exclude list should contain directory names when no files in that directory exist in the include list.
*
* @param string $source_dir_path Path to process.
* @param bool $excluded Whether to return the list of files to exclude. Default (false) returns the list of files to include.
* @return string[] Filtered list of files to include or exclude (depending on $excluded flag).
*/
private function get_file_list( $source_dir_path, $excluded = false ) {
$included_files = [];
$excluded_files = [];
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator( $source_dir_path, RecursiveDirectoryIterator::SKIP_DOTS ),
RecursiveIteratorIterator::SELF_FIRST
);
/**
* @var RecursiveIteratorIterator $iterator
* @var SplFileInfo $item
*/
foreach ( $iterator as $item ) {
$relative_filepath = str_replace( $source_dir_path, '', $item->getPathname() );
if ( $this->checker->isPathIgnored( $relative_filepath ) ) {
$excluded_files[] = $relative_filepath;
} else {
$included_files[] = $relative_filepath;
}
}
// Check all excluded directories and remove them from the excluded list if they contain included files.
foreach ( $excluded_files as $excluded_file_index => $excluded_relative_path ) {
if ( ! is_dir( $source_dir_path . $excluded_relative_path ) ) {
continue;
}
foreach ( $included_files as $included_relative_path ) {
if ( 0 === strpos( $included_relative_path, $excluded_relative_path ) ) {
unset( $excluded_files[ $excluded_file_index ] );
}
}
}
return $excluded ? $excluded_files : $included_files;
}
}