Skip to content

Commit ba0b043

Browse files
Archive to .tar.gz with --format=targz
1 parent d074963 commit ba0b043

File tree

3 files changed

+62
-13
lines changed

3 files changed

+62
-13
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,5 @@ Once you've decided to commit the time to seeing your pull request through, plea
8585
3. Include functional tests with your changes. [Read the WP-CLI documentation](https://wp-cli.org/docs/pull-requests/#functional-tests) for an introduction.
8686
4. Follow the [WordPress Coding Standards](http://make.wordpress.org/core/handbook/coding-standards/).
8787

88+
8889
*This README.md is generated dynamically from the project's codebase using `wp scaffold package-readme` ([doc](https://github.com/wp-cli/scaffold-package-command#wp-scaffold-package-readme)). To suggest changes, please submit a pull request against the corresponding part of the codebase.*

command.php

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,25 +96,42 @@
9696
}
9797
}
9898

99-
if ( 'zip' === $assoc_args['format'] ) {
100-
if ( is_null( $archive_file ) ) {
101-
$archive_file = dirname( $path ) . '/' . $archive_base . $version . '.zip';
99+
if ( is_null( $archive_file ) ) {
100+
$archive_file = dirname( $path ) . '/' . $archive_base . $version;
101+
if ( 'zip' === $assoc_args['format'] ) {
102+
$archive_file .= '.zip';
103+
} else if ( 'targz' === $assoc_args['format'] ) {
104+
$archive_file .= '.tar.gz';
102105
}
106+
}
107+
108+
chdir( dirname( $path ) );
109+
110+
if ( 'zip' === $assoc_args['format'] ) {
103111
$excludes = implode( ' --exclude ', $ignored_files );
104112
if ( ! empty( $excludes ) ) {
105113
$excludes = ' --exclude ' . $excludes;
106114
}
107-
chdir( dirname( $path ) );
108115
$cmd = "zip -r {$archive_file} {$archive_base} {$excludes}";
109-
WP_CLI::debug( "Running: {$cmd}", 'dist-archive' );
110-
$ret = WP_CLI::launch( escapeshellcmd( $cmd ), false, true );
111-
if ( 0 === $ret->return_code ) {
112-
$filename = pathinfo( $archive_file, PATHINFO_BASENAME );
113-
WP_CLI::success( "Created {$filename}" );
114-
} else {
115-
$error = $ret->stderr ? $ret->stderr : $ret->stdout;
116-
WP_CLI::error( $error );
117-
}
116+
} else if ( 'targz' === $assoc_args['format'] ) {
117+
$excludes = array_map( function( $ignored_file ){
118+
if ( '/*' === substr( $ignored_file, -2 ) ) {
119+
$ignored_file = substr( $ignored_file, 0, ( strlen( $ignored_file ) - 2 ) );
120+
}
121+
return "--exclude='{$ignored_file}'";
122+
}, $ignored_files );
123+
$excludes = implode( ' ', $excludes );
124+
$cmd = "tar {$excludes} -zcvf {$archive_file} {$archive_base}";
125+
}
126+
127+
WP_CLI::debug( "Running: {$cmd}", 'dist-archive' );
128+
$ret = WP_CLI::launch( escapeshellcmd( $cmd ), false, true );
129+
if ( 0 === $ret->return_code ) {
130+
$filename = pathinfo( $archive_file, PATHINFO_BASENAME );
131+
WP_CLI::success( "Created {$filename}" );
132+
} else {
133+
$error = $ret->stderr ? $ret->stderr : $ret->stdout;
134+
WP_CLI::error( $error );
118135
}
119136

120137
};

features/dist-archive.feature

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ Feature: Generate a distribution archive of a project
1010
And the wp-content/plugins/hello-world/bin directory should exist
1111

1212
When I run `wp dist-archive wp-content/plugins/hello-world`
13+
Then STDOUT should be:
14+
"""
15+
Success: Created hello-world.0.1.0.zip
16+
"""
17+
And STDERR should be empty
1318
And the wp-content/plugins/hello-world.0.1.0.zip file should exist
1419

1520
When I run `wp plugin delete hello-world`
@@ -21,6 +26,32 @@ Feature: Generate a distribution archive of a project
2126
And the wp-content/plugins/hello-world/.travis.yml file should not exist
2227
And the wp-content/plugins/hello-world/bin directory should not exist
2328

29+
Scenario: Generates a tarball archive with a flag
30+
Given a WP install
31+
32+
When I run `wp scaffold plugin hello-world`
33+
Then the wp-content/plugins/hello-world directory should exist
34+
And the wp-content/plugins/hello-world/hello-world.php file should exist
35+
And the wp-content/plugins/hello-world/.travis.yml file should exist
36+
And the wp-content/plugins/hello-world/bin directory should exist
37+
38+
When I run `wp dist-archive wp-content/plugins/hello-world --format=targz`
39+
Then STDOUT should be:
40+
"""
41+
Success: Created hello-world.0.1.0.tar.gz
42+
"""
43+
And STDERR should be empty
44+
And the wp-content/plugins/hello-world.0.1.0.tar.gz file should exist
45+
46+
When I run `wp plugin delete hello-world`
47+
Then the wp-content/plugins/hello-world directory should not exist
48+
49+
When I run `cd wp-content/plugins/ && tar -zxvf hello-world.0.1.0.tar.gz`
50+
Then the wp-content/plugins/hello-world directory should exist
51+
And the wp-content/plugins/hello-world/hello-world.php file should exist
52+
And the wp-content/plugins/hello-world/.travis.yml file should not exist
53+
And the wp-content/plugins/hello-world/bin directory should not exist
54+
2455
Scenario: Generate a ZIP archive to a custom path
2556
Given a WP install
2657

0 commit comments

Comments
 (0)