|
21 | 21 | * - targz |
22 | 22 | * --- |
23 | 23 | * |
24 | | - * @when before_wp_Load |
| 24 | + * @when before_wp_load |
25 | 25 | */ |
26 | 26 | $dist_archive_command = function( $args, $assoc_args ) { |
27 | 27 |
|
28 | 28 | 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 | + } |
30 | 33 |
|
31 | | - $dist_ignore_path = $path . '.distignore'; |
| 34 | + $dist_ignore_path = $path . '/.distignore'; |
32 | 35 | if ( ! file_exists( $dist_ignore_path ) ) { |
33 | 36 | WP_CLI::error( 'No .distignore file found.' ); |
34 | 37 | } |
35 | 38 |
|
| 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 | + |
36 | 70 | }; |
37 | 71 | WP_CLI::add_command( 'dist-archive', $dist_archive_command ); |
0 commit comments