From d71fc0c822d0251205b9ec91cdb50b643983b98c Mon Sep 17 00:00:00 2001 From: gitlost Date: Mon, 8 Jan 2018 00:40:04 +0000 Subject: [PATCH 1/3] Get package composer.json by version/branch. --- features/package-install.feature | 59 ++++++++++++++++++++++++++++++-- src/Package_Command.php | 36 +++++++++++++++++-- 2 files changed, 90 insertions(+), 5 deletions(-) diff --git a/features/package-install.feature b/features/package-install.feature index 168c53727..cd8e8348c 100644 --- a/features/package-install.feature +++ b/features/package-install.feature @@ -360,10 +360,18 @@ Feature: Install WP-CLI packages Scenario: Install a package from Git using a shortened package identifier with a specific version Given an empty directory - When I run `wp package install schlessera/test-command:0.1.0` + # Need to specify actual tag. + When I try `wp package install schlessera/test-command:0.1.0` + Then STDERR should contain: + """ + Error: Couldn't download composer.json file from 'https://raw.githubusercontent.com/schlessera/test-command/0.1.0/composer.json' (HTTP code 404). + """ + And STDOUT should be empty + + When I run `wp package install schlessera/test-command:v0.1.0` Then STDOUT should contain: """ - Installing package schlessera/test-command (0.1.0) + Installing package schlessera/test-command (v0.1.0) Updating {PACKAGE_PATH}composer.json to require the package... Registering https://github.com/schlessera/test-command.git as a VCS repository... Using Composer to install the package... @@ -588,6 +596,53 @@ Feature: Install WP-CLI packages rocket """ + @github-api + Scenario: Install a package with a composer.json that differs between versions + Given an empty directory + + # Need to give Composer "dev-master#v1.0.0" apparently. + When I run `wp package install gitlost/test-version-composer-json-different:v1.0.0` + Then STDOUT should contain: + """ + Installing package gitlost/test-version-composer-json-different (v1.0.0) + Updating {PACKAGE_PATH}composer.json to require the package... + """ + And STDOUT should contain: + """ + Success: Package installed. + """ + And the {PACKAGE_PATH}/vendor/gitlost/test-version-composer-json-different/composer.json file should exist + And the {PACKAGE_PATH}/vendor/gitlost/test-version-composer-json-different/composer.json file should contain: + """ + 1.0.0 + """ + And the {PACKAGE_PATH}/vendor/gitlost/test-version-composer-json-different/composer.json file should not contain: + """ + 1.0.1 + """ + And the {PACKAGE_PATH}/vendor/wp-cli/profile-command directory should not exist + + When I run `wp package install gitlost/test-version-composer-json-different:v1.0.1` + Then STDOUT should contain: + """ + Installing package gitlost/test-version-composer-json-different (v1.0.1) + Updating {PACKAGE_PATH}composer.json to require the package... + """ + And STDOUT should contain: + """ + Success: Package installed. + """ + And the {PACKAGE_PATH}/vendor/gitlost/test-version-composer-json-different/composer.json file should exist + And the {PACKAGE_PATH}/vendor/gitlost/test-version-composer-json-different/composer.json file should contain: + """ + 1.0.1 + """ + And the {PACKAGE_PATH}/vendor/gitlost/test-version-composer-json-different/composer.json file should not contain: + """ + 1.0.0 + """ + And the {PACKAGE_PATH}/vendor/wp-cli/profile-command directory should exist + Scenario: Install a package in a local zip Given an empty directory And I run `wget -q -O google-sitemap-generator-cli.zip https://github.com/wp-cli/google-sitemap-generator-cli/archive/master.zip` diff --git a/src/Package_Command.php b/src/Package_Command.php index c9fff9175..bc2efee78 100644 --- a/src/Package_Command.php +++ b/src/Package_Command.php @@ -278,7 +278,7 @@ public function install( $args, $assoc_args ) { if ( is_string( $package ) ) { if ( $this->is_git_repository( $package ) ) { $git_package = $package; - $package_name = $this->check_git_package_name( $package_name ); + $package_name = $this->check_git_package_name( $package_name, $version ); } } elseif ( $package_name !== $package->getPrettyName() ) { // BC support for specifying lowercase names for mixed-case package index packages - don't bother warning. @@ -973,10 +973,14 @@ private function is_git_repository( $package ) { /** * Checks that `$package_name` matches the name in the repo composer.json, and return corrected value if not. + * + * @string $package_name Package name to check. + * @string $version Optional. Package version. Default 'master'. + * @string Package name, possibly changed to match that in repo. */ - private function check_git_package_name( $package_name ) { + private function check_git_package_name( $package_name, $version = '' ) { // Generate raw git URL of composer.json file. - $raw_content_url = 'https://raw.githubusercontent.com/' . $package_name . '/master/composer.json'; + $raw_content_url = 'https://raw.githubusercontent.com/' . $package_name . '/' . $this->get_raw_git_version( $version ) . '/composer.json'; $github_token = getenv( 'GITHUB_TOKEN' ); // Use GITHUB_TOKEN if available to avoid authorization failures or rate-limiting. $headers = $github_token ? array( 'Authorization' => 'token ' . $github_token ) : array(); @@ -1005,6 +1009,32 @@ private function check_git_package_name( $package_name ) { return $package_name; } + /** + * Get the version to use for raw github request. + * + * @string $version Package version. + * @string Version to use for github request. + */ + private function get_raw_git_version( $version ) { + if ( '' !== $version ) { + // If Composer hash given then just use whatever's after it. + if ( false !== ( $hash_pos = strpos( $version, '#' ) ) ) { + $version = substr( $version, $hash_pos + 1 ); + } else { + // Strip any Composer 'dev-' prefix. + if ( 0 === strncmp( $version, 'dev-', 4 ) ) { + $version = substr( $version, 4 ); + } + // Ignore/strip any relative suffixes. + if ( false !== ( $rel_pos = strpos( $version, '^' ) ) || false !== ( $rel_pos = strpos( $version, '~' ) ) ) { + $version = substr( $version, 0, $rel_pos ); + } + } + } + // Default to master. + return '' === $version ? 'master' : $version; + } + /** * Sets `COMPOSER_AUTH` environment variable (which Composer merges into the config setup in `Composer\Factory::createConfig()`) depending on available environment variables. * Avoids authorization failures when accessing various sites. From 0f5847846a8f1b3fd8cba06329d233de5f49b30e Mon Sep 17 00:00:00 2001 From: gitlost Date: Mon, 8 Jan 2018 01:36:05 +0000 Subject: [PATCH 2/3] Remove incorrect comment. --- features/package-install.feature | 1 - 1 file changed, 1 deletion(-) diff --git a/features/package-install.feature b/features/package-install.feature index cd8e8348c..6959adc61 100644 --- a/features/package-install.feature +++ b/features/package-install.feature @@ -600,7 +600,6 @@ Feature: Install WP-CLI packages Scenario: Install a package with a composer.json that differs between versions Given an empty directory - # Need to give Composer "dev-master#v1.0.0" apparently. When I run `wp package install gitlost/test-version-composer-json-different:v1.0.0` Then STDOUT should contain: """ From d75a4ae330780e02b5a34d2abcddf33bae34beed Mon Sep 17 00:00:00 2001 From: gitlost Date: Mon, 8 Jan 2018 21:53:28 +0000 Subject: [PATCH 3/3] Use wp-cli-test/version-composer-json-different not gitlost. --- features/package-install.feature | 20 ++++++++++---------- src/Package_Command.php | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/features/package-install.feature b/features/package-install.feature index 6959adc61..bbe7548b0 100644 --- a/features/package-install.feature +++ b/features/package-install.feature @@ -600,43 +600,43 @@ Feature: Install WP-CLI packages Scenario: Install a package with a composer.json that differs between versions Given an empty directory - When I run `wp package install gitlost/test-version-composer-json-different:v1.0.0` + When I run `wp package install wp-cli-test/version-composer-json-different:v1.0.0` Then STDOUT should contain: """ - Installing package gitlost/test-version-composer-json-different (v1.0.0) + Installing package wp-cli-test/version-composer-json-different (v1.0.0) Updating {PACKAGE_PATH}composer.json to require the package... """ And STDOUT should contain: """ Success: Package installed. """ - And the {PACKAGE_PATH}/vendor/gitlost/test-version-composer-json-different/composer.json file should exist - And the {PACKAGE_PATH}/vendor/gitlost/test-version-composer-json-different/composer.json file should contain: + And the {PACKAGE_PATH}/vendor/wp-cli-test/version-composer-json-different/composer.json file should exist + And the {PACKAGE_PATH}/vendor/wp-cli-test/version-composer-json-different/composer.json file should contain: """ 1.0.0 """ - And the {PACKAGE_PATH}/vendor/gitlost/test-version-composer-json-different/composer.json file should not contain: + And the {PACKAGE_PATH}/vendor/wp-cli-test/version-composer-json-different/composer.json file should not contain: """ 1.0.1 """ And the {PACKAGE_PATH}/vendor/wp-cli/profile-command directory should not exist - When I run `wp package install gitlost/test-version-composer-json-different:v1.0.1` + When I run `wp package install wp-cli-test/version-composer-json-different:v1.0.1` Then STDOUT should contain: """ - Installing package gitlost/test-version-composer-json-different (v1.0.1) + Installing package wp-cli-test/version-composer-json-different (v1.0.1) Updating {PACKAGE_PATH}composer.json to require the package... """ And STDOUT should contain: """ Success: Package installed. """ - And the {PACKAGE_PATH}/vendor/gitlost/test-version-composer-json-different/composer.json file should exist - And the {PACKAGE_PATH}/vendor/gitlost/test-version-composer-json-different/composer.json file should contain: + And the {PACKAGE_PATH}/vendor/wp-cli-test/version-composer-json-different/composer.json file should exist + And the {PACKAGE_PATH}/vendor/wp-cli-test/version-composer-json-different/composer.json file should contain: """ 1.0.1 """ - And the {PACKAGE_PATH}/vendor/gitlost/test-version-composer-json-different/composer.json file should not contain: + And the {PACKAGE_PATH}/vendor/wp-cli-test/version-composer-json-different/composer.json file should not contain: """ 1.0.0 """ diff --git a/src/Package_Command.php b/src/Package_Command.php index bc2efee78..03c7c43c2 100644 --- a/src/Package_Command.php +++ b/src/Package_Command.php @@ -1010,7 +1010,7 @@ private function check_git_package_name( $package_name, $version = '' ) { } /** - * Get the version to use for raw github request. + * Get the version to use for raw github request. Very basic. * * @string $version Package version. * @string Version to use for github request.