Skip to content

Commit 03cba74

Browse files
Hook up functional components of the command
1 parent 3b0fb6e commit 03cba74

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

command.php

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,51 @@
2121
* - targz
2222
* ---
2323
*
24-
* @when before_wp_Load
24+
* @when before_wp_load
2525
*/
2626
$dist_archive_command = function( $args, $assoc_args ) {
2727

2828
list( $path ) = $args;
29-
$path = rtrim( $path, '/' ) . '/';
29+
$path = rtrim( realpath( $path ), '/' );
30+
if ( ! is_dir( $path ) ) {
31+
WP_CLI::error( 'Provided path is not a directory.' );
32+
}
3033

31-
$dist_ignore_path = $path . '.distignore';
34+
$dist_ignore_path = $path . '/.distignore';
3235
if ( ! file_exists( $dist_ignore_path ) ) {
3336
WP_CLI::error( 'No .distignore file found.' );
3437
}
3538

39+
$maybe_ignored_files = explode( PHP_EOL, file_get_contents( $dist_ignore_path ) );
40+
$ignored_files = array();
41+
$archive_base = basename( $path );
42+
foreach( $maybe_ignored_files as $file ) {
43+
$file = trim( $file );
44+
if ( 0 === strpos( $file, '#' ) || empty( $file ) ) {
45+
continue;
46+
}
47+
if ( is_dir( $path . '/' . $file ) ) {
48+
$file .= '/*';
49+
}
50+
$ignored_files[] = $archive_base . '/' . $file;
51+
}
52+
53+
if ( 'zip' === $assoc_args['format'] ) {
54+
$archive_file = $archive_base . '.zip';
55+
$excludes = implode( ' --exclude ', $ignored_files );
56+
if ( ! empty( $excludes ) ) {
57+
$excludes = ' --exclude ' . $excludes;
58+
}
59+
chdir( dirname( $path ) );
60+
$cmd = "zip -r {$archive_file} {$archive_base} {$excludes}";
61+
WP_CLI::debug( "Running: {$cmd}", 'dist-archive' );
62+
$ret = WP_CLI::launch( escapeshellcmd( $cmd ), false, true );
63+
if ( 0 === $ret->return_code ) {
64+
WP_CLI::success( "Created {$archive_file}" );
65+
} else {
66+
WP_CLI::error( $ret->stderr );
67+
}
68+
}
69+
3670
};
3771
WP_CLI::add_command( 'dist-archive', $dist_archive_command );

0 commit comments

Comments
 (0)