Skip to content

Commit 49f394e

Browse files
authored
Merge pull request #31 from Sidsector9/issue/GH#4232
GH#4232 Added check for package name
2 parents a8f5b9c + b43d412 commit 49f394e

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

‎features/package-install.feature‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,15 @@ Feature: Install WP-CLI packages
122122

123123
@github-api
124124
Scenario: Install a package from a Git URL
125+
When I run `wp package install git@github.com:wp-cli-test/repository-name.git`
126+
And STDOUT should contain:
127+
"""
128+
Package name mismatch...Updating the name with correct value.
129+
"""
130+
And the {PACKAGE_PATH}composer.json file should contain:
131+
"""
132+
"wp-cli-test/package-name": "dev-master"
133+
"""
125134
Given an empty directory
126135

127136
When I try `wp package install git@github.com:wp-cli.git`

‎src/Package_Command.php‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,21 @@ public function install( $args, $assoc_args ) {
210210
preg_match( '#([^:\/]+\/[^\/]+)\.git#', $package_name, $matches );
211211
if ( ! empty( $matches[1] ) ) {
212212
$package_name = $matches[1];
213+
214+
// Generate raw git URL of composer.json file.
215+
$raw_content_url = 'https://raw.githubusercontent.com/' . $package_name . '/master/composer.json';
216+
217+
// Convert composer.json JSON to Array.
218+
$composer_content_as_array = json_decode( WP_CLI\Utils\http_request( 'GET', $raw_content_url )->body, true );
219+
220+
// Package name in composer.json that is hosted on GitHub.
221+
$package_name_on_repo = $composer_content_as_array['name'];
222+
223+
// If package name and repository name are not identical, then fix it.
224+
if ( $package_name !== $package_name_on_repo ) {
225+
$package_name = $package_name_on_repo;
226+
WP_CLI::warning( 'Package name mismatch...Updating the name with correct value.' );
227+
}
213228
} else {
214229
WP_CLI::error( "Couldn't parse package name from expected path '<name>/<package>'." );
215230
}
@@ -282,7 +297,6 @@ public function install( $args, $assoc_args ) {
282297
WP_CLI::log( sprintf( 'Registering %s as a path repository...', $dir_package ) );
283298
$json_manipulator->addRepository( $package_name, array( 'type' => 'path', 'url' => $dir_package ) );
284299
}
285-
286300
$composer_backup_decoded = json_decode( $composer_backup, true );
287301
// If the composer file does not contain the current package index repository, refresh the repository definition.
288302
if ( empty( $composer_backup_decoded['repositories']['wp-cli']['url'] ) || self::PACKAGE_INDEX_URL != $composer_backup_decoded['repositories']['wp-cli']['url'] ) {
@@ -300,7 +314,6 @@ public function install( $args, $assoc_args ) {
300314
// Set up the EventSubscriber
301315
$event_subscriber = new \WP_CLI\PackageManagerEventSubscriber;
302316
$composer->getEventDispatcher()->addSubscriber( $event_subscriber );
303-
304317
// Set up the installer
305318
$install = Installer::create( new ComposerIO, $composer );
306319
$install->setUpdate( true ); // Installer class will only override composer.lock with this flag

0 commit comments

Comments
 (0)