Skip to content

Commit 5c9db74

Browse files
Merge pull request #45 from ryotsun/patch-35
Don't attempt to rename ZIPs coming from a GitHub archive release/tag
2 parents cfd2b7f + 6fd40e4 commit 5c9db74

2 files changed

Lines changed: 41 additions & 9 deletions

File tree

features/plugin-install.feature

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,28 @@ Feature: Install WordPress plugins
145145
"""
146146
Warning: Destination folder already exists. "{WORKING_DIR}/wp-content/plugins/akismet/"
147147
"""
148+
149+
Scenario: Don't attempt to rename ZIPs coming from a GitHub archive release/tag
150+
Given a WP install
151+
152+
When I run `wp plugin install https://github.com/wp-cli-test/generic-example-plugin/archive/v0.1.0.zip`
153+
Then STDOUT should contain:
154+
"""
155+
Plugin installed successfully.
156+
"""
157+
And STDOUT should contain:
158+
"""
159+
package from https://github.com/wp-cli-test/generic-example-plugin/archive/v0.1.0.zip
160+
"""
161+
And STDOUT should contain:
162+
"""
163+
Renamed Github-based project from 'generic-example-plugin-0.1.0' to 'generic-example-plugin'.
164+
"""
165+
And STDOUT should contain:
166+
"""
167+
Plugin installed successfully.
168+
"""
169+
And STDERR should be empty
170+
And the wp-content/plugins/generic-example-plugin directory should exist
171+
And the wp-content/plugins/generic-example-plugi directory should not exist
172+
And the wp-content/plugins/generic-example-plugin-0.1.0 directory should not exist

src/WP_CLI/CommandWithUpgrade.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,22 @@ public function install( $args, $assoc_args ) {
152152
if ( preg_match( '#github\.com/([^/]+)/([^/]+)/releases/download/#', $local_or_remote_zip_file ) || preg_match( '#github\.com/([^/]+)/([^/]+)/raw/#', $local_or_remote_zip_file ) ) {
153153
return $source;
154154
}
155-
$branch_length = strlen( pathinfo( $local_or_remote_zip_file, PATHINFO_FILENAME ) );
156-
if ( $branch_length ) {
157-
$new_path = substr( rtrim( $source, '/' ), 0, - ( $branch_length + 1 ) ) . '/';
158-
if ( $GLOBALS['wp_filesystem']->move( $source, $new_path ) ) {
159-
WP_CLI::log( "Renamed Github-based project from '" . Utils\basename( $source ) . "' to '" . Utils\basename( $new_path ) . "'." );
160-
return $new_path;
161-
} else {
162-
return new \WP_Error( 'wpcli_install_gitub', "Couldn't move Github-based project to appropriate directory." );
163-
}
155+
156+
$branch_name = pathinfo( $local_or_remote_zip_file, PATHINFO_FILENAME );
157+
158+
// Always be attached "archive" into release tag and tag on github.
159+
if ( preg_match( '#github\.com/([^/]+)/([^/]+)/archive/#', $local_or_remote_zip_file ) ) {
160+
$path_array = explode( "/", pathinfo( $local_or_remote_zip_file, PATHINFO_DIRNAME) );
161+
array_pop( $path_array );
162+
$branch_name = end( $path_array );
163+
}
164+
165+
$new_path = str_replace( pathinfo( $source, PATHINFO_BASENAME ), $branch_name, $source );
166+
if ( $GLOBALS['wp_filesystem']->move( $source, $new_path ) ) {
167+
WP_CLI::log( "Renamed Github-based project from '" . Utils\basename( $source ) . "' to '" . Utils\basename( $new_path ) . "'." );
168+
return $new_path;
169+
} else {
170+
return new \WP_Error( 'wpcli_install_github', "Couldn't move Github-based project to appropriate directory." );
164171
}
165172
return $source;
166173
};

0 commit comments

Comments
 (0)