Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions features/aliases.feature
Original file line number Diff line number Diff line change
Expand Up @@ -637,3 +637,30 @@ Feature: Create shortcuts to specific WordPress installs

When I try `wp cli alias is-group @foo`
Then the return code should be 1

Scenario: Automatically add "@" prefix to an alias
Given a WP install
And a wp-cli.yml file:
"""
@foo:
path: foo
"""

When I run `wp cli alias add hello --set-path=/path/to/wordpress`
Then STDOUT should be:
"""
Success: Added '@hello' alias.
"""

When I run `wp eval --skip-wordpress 'echo realpath( getenv( "RUN_DIR" ) );'`
Then save STDOUT as {TEST_DIR}

When I run `wp cli alias list`
Then STDOUT should be YAML containing:
"""
@all: Run command against every registered alias.
@hello:
path: /path/to/wordpress
@foo:
path: {TEST_DIR}/foo
"""
2 changes: 1 addition & 1 deletion php/class-wp-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,7 @@ public static function error_to_string( $errors ) {

// PHP 7+: internal and user exceptions must implement Throwable interface.
// PHP 5: internal and user exceptions must extend Exception class.
if ( interface_exists( 'Throwable' ) && ( $errors instanceof Throwable ) || ( $errors instanceof Exception ) ) {
if ( ( interface_exists( 'Throwable' ) && ( $errors instanceof Throwable ) ) || ( $errors instanceof Exception ) ) {
return get_class( $errors ) . ': ' . $errors->getMessage();
}

Expand Down
20 changes: 20 additions & 0 deletions php/commands/src/CLI_Alias_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ private function validate_config_file( $config_path ) {
* @return mixed
*/
private function build_aliases( $aliases, $alias, $assoc_args, $is_grouping, $grouping = '', $is_update = false ) {
$alias = $this->normalize_alias( $alias );

if ( $is_grouping ) {
$valid_assoc_args = [ 'config', 'grouping' ];
Expand Down Expand Up @@ -507,6 +508,8 @@ private function validate_alias_type( $aliases, $alias, $assoc_args, $grouping )
* @param string $operation Current operation string fro message.
*/
private function process_aliases( $aliases, $alias, $config_path, $operation = '' ) {
$alias = $this->normalize_alias( $alias );

// Convert data to YAML string.
$yaml_data = Spyc::YAMLDump( $aliases );

Expand All @@ -515,4 +518,21 @@ private function process_aliases( $aliases, $alias, $config_path, $operation = '
WP_CLI::success( "$operation '{$alias}' alias." );
}
}

/**
* Normalize the alias to an expected format.
*
* - Add @ if not present.
*
* @param string $alias Name of alias.
*/
private function normalize_alias( $alias ) {
// Check if the alias starts with the @.
// See: https://github.com/wp-cli/wp-cli/issues/5391
if ( strpos( $alias, '@' ) !== 0 ) {
$alias = '@' . ltrim( $alias, '@' );
}

return $alias;
}
}