diff --git a/README.md b/README.md index 120790a2..0de674e0 100644 --- a/README.md +++ b/README.md @@ -297,6 +297,30 @@ wp package uninstall [--insecure] Removing package directories and regenerating autoloader... Success: Uninstalled package. + + +### wp package is-installed + +Checks if a given package is installed. + +~~~ +wp package is-installed +~~~ + +Returns exit code 0 when installed, 1 when uninstalled. + +**OPTIONS** + + + The package to check. + +**EXAMPLES** + + # Check whether "foo/bar" is installed; exit status 0 if installed, otherwise 1 + $ wp package is-installed foo/bar + $ echo $? + 1 + ## Installing This package is included with WP-CLI itself, no additional installation necessary. diff --git a/composer.json b/composer.json index 9b53625d..64caff7a 100644 --- a/composer.json +++ b/composer.json @@ -41,7 +41,8 @@ "package install", "package list", "package update", - "package uninstall" + "package uninstall", + "package is-installed" ] }, "autoload": { diff --git a/features/package-install.feature b/features/package-install.feature index fe76b4c5..cafd1e84 100644 --- a/features/package-install.feature +++ b/features/package-install.feature @@ -46,6 +46,11 @@ Feature: Install WP-CLI packages "url": "http://wp-cli.org/package-index/" """ + When I run `WP_CLI_PACKAGES_DIR=. wp package is-installed wp-cli/restful` + Then the return code should be 0 + And STDERR should be empty + And STDOUT should be empty + @require-php-5.6 Scenario: Install a package with 'wp-cli/wp-cli' as a dependency Given a WP install @@ -60,6 +65,11 @@ Feature: Install WP-CLI packages requires wp-cli/wp-cli """ + When I run `wp package is-installed wp-cli-test/test-command` + Then the return code should be 0 + And STDERR should be empty + And STDOUT should be empty + When I run `wp test-command` Then STDOUT should contain: """ @@ -84,6 +94,11 @@ Feature: Install WP-CLI packages faker """ + When I run `wp package is-installed yoast/wp-cli-faker` + Then the return code should be 0 + And STDERR should be empty + And STDOUT should be empty + When I run `wp package list --fields=name` Then STDOUT should be a table containing rows: | name | @@ -111,6 +126,11 @@ Feature: Install WP-CLI packages fzaninotto """ + When I try `wp package is-installed yoast/wp-cli-faker` + Then the return code should be 1 + And STDERR should be empty + And STDOUT should be empty + When I run `wp package list` Then STDOUT should not contain: """ @@ -136,6 +156,11 @@ Feature: Install WP-CLI packages "wp-cli-test/package-name": "dev-master" """ + When I run `wp package is-installed wp-cli-test/package-name` + Then the return code should be 0 + And STDERR should be empty + And STDOUT should be empty + When I try `wp package install git@github.com:wp-cli.git` Then STDERR should contain: """ @@ -162,6 +187,11 @@ Feature: Install WP-CLI packages Success: Package installed. """ + When I run `wp package is-installed wp-cli/google-sitemap-generator-cli` + Then the return code should be 0 + And STDERR should be empty + And STDOUT should be empty + When I run `wp package list --fields=name` Then STDOUT should be a table containing rows: | name | @@ -195,6 +225,11 @@ Feature: Install WP-CLI packages Success: Uninstalled package. """ + When I try `wp package is-installed wp-cli/google-sitemap-generator-cli` + Then the return code should be 1 + And STDERR should be empty + And STDOUT should be empty + When I run `wp package list --fields=name` Then STDOUT should not contain: """ @@ -238,6 +273,11 @@ Feature: Install WP-CLI packages "CapitalWPCLI/examplecommand" """ + When I run `wp package is-installed capitalwpcli/examplecommand` + Then the return code should be 0 + And STDERR should be empty + And STDOUT should be empty + When I run `wp package list --fields=name` Then STDOUT should be a table containing rows: | name | @@ -261,6 +301,11 @@ Feature: Install WP-CLI packages """ And the contents of the {PACKAGE_PATH}composer.json file should match /\"gitlost\/(?:TestMixedCaseCommand|testmixedcasecommand)\"/ + When I run `wp package is-installed gitlost/TestMixedCaseCommand` + Then the return code should be 0 + And STDERR should be empty + And STDOUT should be empty + When I run `wp package list --fields=name` Then STDOUT should be a table containing rows: | name | @@ -296,6 +341,11 @@ Feature: Install WP-CLI packages Success: Package installed. """ + When I run `wp package is-installed wp-cli-test/github-test-command` + Then the return code should be 0 + And STDERR should be empty + And STDOUT should be empty + When I run `wp package list --fields=name,version` Then STDOUT should be a table containing rows: | name | version | @@ -317,6 +367,11 @@ Feature: Install WP-CLI packages Success: Uninstalled package. """ + When I try `wp package is-installed wp-cli-test/github-test-command` + Then the return code should be 1 + And STDERR should be empty + And STDOUT should be empty + When I run `wp package list --fields=name` Then STDOUT should not contain: """ @@ -347,6 +402,11 @@ Feature: Install WP-CLI packages Success: Package installed. """ + When I run `wp package is-installed wp-cli-test/github-test-command` + Then the return code should be 0 + And STDERR should be empty + And STDOUT should be empty + When I run `wp package list --fields=name,version` Then STDOUT should be a table containing rows: | name | version | @@ -368,6 +428,11 @@ Feature: Install WP-CLI packages Success: Uninstalled package. """ + When I try `wp package is-installed wp-cli-test/github-test-command` + Then the return code should be 1 + And STDERR should be empty + And STDOUT should be empty + When I run `wp package list --fields=name` Then STDOUT should not contain: """ @@ -405,6 +470,11 @@ Feature: Install WP-CLI packages Success: Package installed. """ + When I run `wp package is-installed wp-cli-test/github-test-command` + Then the return code should be 0 + And STDERR should be empty + And STDOUT should be empty + When I run `wp package list --fields=name,version` Then STDOUT should be a table containing rows: | name | version | @@ -426,6 +496,11 @@ Feature: Install WP-CLI packages Success: Uninstalled package. """ + When I try `wp package is-installed wp-cli-test/github-test-command` + Then the return code should be 1 + And STDERR should be empty + And STDOUT should be empty + When I run `wp package list --fields=name` Then STDOUT should not contain: """ @@ -456,6 +531,11 @@ Feature: Install WP-CLI packages Success: Package installed. """ + When I run `wp package is-installed wp-cli-test/github-test-command` + Then the return code should be 0 + And STDERR should be empty + And STDOUT should be empty + When I run `wp package list --fields=name,version` Then STDOUT should be a table containing rows: | name | version | @@ -477,6 +557,11 @@ Feature: Install WP-CLI packages Success: Uninstalled package. """ + When I try `wp package is-installed wp-cli-test/github-test-command` + Then the return code should be 1 + And STDERR should be empty + And STDOUT should be empty + When I run `wp package list --fields=name` Then STDOUT should not contain: """ @@ -507,6 +592,11 @@ Feature: Install WP-CLI packages Success: Package installed. """ + When I run `wp package is-installed wp-cli-test/github-test-command` + Then the return code should be 0 + And STDERR should be empty + And STDOUT should be empty + When I run `wp package list --fields=name,version` Then STDOUT should be a table containing rows: | name | version | @@ -528,6 +618,11 @@ Feature: Install WP-CLI packages Success: Uninstalled package. """ + When I try `wp package is-installed wp-cli-test/github-test-command` + Then the return code should be 1 + And STDERR should be empty + And STDOUT should be empty + When I run `wp package list --fields=name` Then STDOUT should not contain: """ @@ -560,6 +655,11 @@ Feature: Install WP-CLI packages """ And the contents of the {PACKAGE_PATH}composer.json file should match /"wp-media\/wp-rocket-cli"/ + When I run `wp package is-installed wp-media/wp-rocket-cli` + Then the return code should be 0 + And STDERR should be empty + And STDOUT should be empty + When I run `wp package list --fields=name` Then STDOUT should be a table containing rows: | name | @@ -589,6 +689,11 @@ Feature: Install WP-CLI packages rocket """ + When I try `wp package is-installed wp-media/wp-rocket-cli` + Then the return code should be 1 + And STDERR should be empty + And STDOUT should be empty + # Install with lowercase name (for BC - no warning) and uninstall with lowercase name (for BC and convenience) When I run `wp package install geekpress/wp-rocket-cli` Then STDERR should be empty @@ -608,6 +713,11 @@ Feature: Install WP-CLI packages """ And the contents of the {PACKAGE_PATH}composer.json file should match /("?:GeekPress|geekpress)\/wp-rocket-cli"/ + When I run `wp package is-installed geekpress/wp-rocket-cli` + Then the return code should be 0 + And STDERR should be empty + And STDOUT should be empty + When I run `wp package list --fields=name` Then STDOUT should be a table containing rows: | name | @@ -633,6 +743,11 @@ Feature: Install WP-CLI packages rocket """ + When I try `wp package is-installed geekpress/wp-rocket-cli` + Then the return code should be 1 + And STDERR should be empty + And STDOUT should be empty + @github-api Scenario: Install a package with a composer.json that differs between versions Given an empty directory @@ -711,6 +826,11 @@ Feature: Install WP-CLI packages Success: Package installed. """ + When I run `wp package is-installed wp-cli/google-sitemap-generator-cli` + Then the return code should be 0 + And STDERR should be empty + And STDOUT should be empty + When I run `wp package list --fields=name` Then STDOUT should be a table containing rows: | name | @@ -732,6 +852,11 @@ Feature: Install WP-CLI packages Success: Uninstalled package. """ + When I try `wp package is-installed wp-cli/google-sitemap-generator-cli` + Then the return code should be 1 + And STDERR should be empty + And STDOUT should be empty + When I run `wp package list --fields=name` Then STDOUT should not contain: """ @@ -775,6 +900,11 @@ Feature: Install WP-CLI packages "CapitalWPCLI/examplecommand" """ + When I run `wp package is-installed capitalwpcli/examplecommand` + Then the return code should be 0 + And STDERR should be empty + And STDOUT should be empty + When I run `wp package list --fields=name` Then STDOUT should be a table containing rows: | name | @@ -830,6 +960,11 @@ Feature: Install WP-CLI packages Success: Package installed. """ + When I run `wp package is-installed wp-cli/google-sitemap-generator-cli` + Then the return code should be 0 + And STDERR should be empty + And STDOUT should be empty + When I run `wp package list --fields=name` Then STDOUT should be a table containing rows: | name | @@ -851,6 +986,11 @@ Feature: Install WP-CLI packages Success: Uninstalled package. """ + When I try `wp package is-installed wp-cli/google-sitemap-generator-cli` + Then the return code should be 1 + And STDERR should be empty + And STDOUT should be empty + When I run `wp package list --fields=name` Then STDOUT should not contain: """ @@ -918,6 +1058,11 @@ Feature: Install WP-CLI packages Success: Package installed. """ + When I run `wp package is-installed wp-cli/community-command` + Then the return code should be 0 + And STDERR should be empty + And STDOUT should be empty + When I run `wp package list --fields=name` Then STDOUT should be a table containing rows: | name | @@ -940,6 +1085,11 @@ Feature: Install WP-CLI packages """ And the path-command directory should exist + When I try `wp package is-installed wp-cli/community-command` + Then the return code should be 1 + And STDERR should be empty + And STDOUT should be empty + When I run `wp package list --fields=name` Then STDOUT should not contain: """ @@ -997,6 +1147,11 @@ Feature: Install WP-CLI packages Success: Package installed. """ + When I run `wp package is-installed wp-cli/community-command` + Then the return code should be 0 + And STDERR should be empty + And STDOUT should be empty + When I run `wp package list --fields=name` Then STDOUT should be a table containing rows: | name | @@ -1019,6 +1174,11 @@ Feature: Install WP-CLI packages """ And the path-command directory should exist + When I try `wp package is-installed wp-cli/community-command` + Then the return code should be 1 + And STDERR should be empty + And STDOUT should be empty + When I run `wp package list --fields=name` Then STDOUT should not contain: """ diff --git a/src/Package_Command.php b/src/Package_Command.php index e6f8273c..7a3fff80 100644 --- a/src/Package_Command.php +++ b/src/Package_Command.php @@ -645,14 +645,27 @@ public function uninstall( $args, $assoc_args ) { } /** - * Checks whether a package is a WP-CLI community package based - * on membership in our package index. + * Checks if a given package is installed. * - * @param object $package A package object - * @return bool + * Returns exit code 0 when installed, 1 when uninstalled. + * + * ## OPTIONS + * + * + * : The package to check. + * + * ## EXAMPLES + * + * # Check whether "foo/bar" is installed; exit status 0 if installed, otherwise 1 + * $ wp package is-installed foo/bar + * $ echo $? + * 1 + * + * @subcommand is-installed */ - private function is_community_package( $package ) { - return $this->package_index()->hasPackage( $package ); + public function is_installed( $args, $assoc_args ) { + list( $package_name ) = $args; + WP_CLI::halt( $this->get_installed_package_by_name( $package_name ) ? 0 : 1 ); } /** @@ -920,17 +933,6 @@ private function get_installed_package_by_name( $package_name ) { return false; } - /** - * Checks if the package name provided is already installed. - */ - private function is_package_installed( $package_name ) { - if ( $this->get_installed_package_by_name( $package_name ) ) { - return true; - } else { - return false; - } - } - /** * Gets the name of the package from the composer.json in a directory path * @@ -1093,6 +1095,9 @@ private function find_latest_package( PackageInterface $package, Composer $compo return $version_selector->findBestCandidate( $name, $target_version, $php_version, $best_stability ); } + /** + * @return VersionSelector + */ private function get_version_selector( Composer $composer ) { if ( ! $this->version_selector ) { if ( $this->is_composer_v2() ) {