-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from wp-cli/as-21-short-package-identifiers
Add support for short package identifiers.
- Loading branch information
Showing
2 changed files
with
75 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -170,6 +170,50 @@ Feature: Install WP-CLI packages | |
wp-cli/google-sitemap-generator-cli | ||
""" | ||
|
||
@github-api @shortened | ||
Scenario: Install a package from Git using a shortened package identifier | ||
Given an empty directory | ||
|
||
When I run `wp package install wp-cli/google-sitemap-generator-cli` | ||
Then STDOUT should contain: | ||
""" | ||
Installing package wp-cli/google-sitemap-generator-cli (dev-master) | ||
Updating {PACKAGE_PATH}composer.json to require the package... | ||
Registering [email protected]:wp-cli/google-sitemap-generator-cli.git as a VCS repository... | ||
Using Composer to install the package... | ||
""" | ||
And STDOUT should contain: | ||
""" | ||
Success: Package installed. | ||
""" | ||
|
||
When I run `wp package list --fields=name` | ||
Then STDOUT should be a table containing rows: | ||
| name | | ||
| wp-cli/google-sitemap-generator-cli | | ||
|
||
When I run `wp google-sitemap` | ||
Then STDOUT should contain: | ||
""" | ||
usage: wp google-sitemap rebuild | ||
""" | ||
|
||
When I run `wp package uninstall wp-cli/google-sitemap-generator-cli` | ||
Then STDOUT should contain: | ||
""" | ||
Removing require statement from {PACKAGE_PATH}composer.json | ||
""" | ||
And STDOUT should contain: | ||
""" | ||
Success: Uninstalled package. | ||
""" | ||
|
||
When I run `wp package list --fields=name` | ||
Then STDOUT should not contain: | ||
""" | ||
wp-cli/google-sitemap-generator-cli | ||
""" | ||
|
||
Scenario: Install a package in a local zip | ||
Given an empty directory | ||
And I run `wget -O google-sitemap-generator-cli.zip https://github.com/wp-cli/google-sitemap-generator-cli/archive/master.zip` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -197,7 +197,7 @@ public function install( $args, $assoc_args ) { | |
|
||
$git_package = $dir_package = false; | ||
$version = 'dev-master'; | ||
if ( '.git' === strtolower( substr( $package_name, -4, 4 ) ) ) { | ||
if ( $this->is_git_repository( $package_name ) ) { | ||
$git_package = $package_name; | ||
preg_match( '#([^:\/]+\/[^\/]+)\.git#', $package_name, $matches ); | ||
if ( ! empty( $matches[1] ) ) { | ||
|
@@ -245,7 +245,10 @@ public function install( $args, $assoc_args ) { | |
if ( false !== strpos( $package_name, ':' ) ) { | ||
list( $package_name, $version ) = explode( ':', $package_name ); | ||
} | ||
$package = $this->get_community_package_by_name( $package_name ); | ||
$package = $this->get_package_by_shortened_identifier( $package_name ); | ||
if ( $this->is_git_repository( $package ) ) { | ||
$git_package = $package; | ||
} | ||
if ( ! $package ) { | ||
WP_CLI::error( "Invalid package." ); | ||
} | ||
|
@@ -656,14 +659,27 @@ private function show_packages( $context, $packages, $assoc_args ) { | |
} | ||
|
||
/** | ||
* Get a community package by its name. | ||
* Get a package by its shortened identifier. | ||
* | ||
* A shortened identifier has the form `<vendor>/<package>`. | ||
* | ||
* This method first checks the deprecated package index, for BC reasons, | ||
* and then falls back to the corresponding GitHub URL. | ||
*/ | ||
private function get_community_package_by_name( $package_name ) { | ||
private function get_package_by_shortened_identifier( $package_name ) { | ||
// Check the package index first, so we don't break existing behavior. | ||
foreach( $this->get_community_packages() as $package ) { | ||
if ( $package_name == $package->getName() ) { | ||
return $package; | ||
} | ||
} | ||
|
||
// Fall back to GitHub URL if we had no match in the package index. | ||
$response = Utils\http_request( 'GET', "https://github.com/{$package_name}.git" ); | ||
if ( 20 === (int) substr( $response->status_code, 0, 2 ) ) { | ||
return "[email protected]:{$package_name}.git"; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
|
@@ -877,4 +893,15 @@ private function get_pool( Composer $composer ) { | |
} | ||
return $this->pool; | ||
} | ||
|
||
/** | ||
* Check whether a given package is a git repository. | ||
* | ||
* @param string $package Package name to check. | ||
* | ||
* @return bool Whether the package is a git repository. | ||
*/ | ||
private function is_git_repository( $package ) { | ||
return '.git' === strtolower( substr( $package, -4, 4 ) ); | ||
} | ||
} |