diff --git a/plugins/laravel/README.md b/plugins/laravel/README.md index b790d2c81..f3205c798 100644 --- a/plugins/laravel/README.md +++ b/plugins/laravel/README.md @@ -2,10 +2,44 @@ This plugin adds aliases and autocompletion for Laravel [Artisan](https://laravel.com/docs/artisan) and [Bob](http://daylerees.github.io/laravel-bob/) command-line interfaces. +It replaces the `laravel4` and `laravel5` plugins. Enable only `laravel` to use +the shared aliases and completion with Laravel projects of different versions. + ``` plugins=(... laravel) ``` +## Completion + +For Laravel 4 and later, completion reads the available commands from +`artisan list --raw --no-ansi` in the current project, including custom commands. +The legacy static command-name completion is retained for Laravel 3, which does +not support that command. Artisan runs only when completing a command name; +command arguments use file completion without booting the application. +No Laravel command runs when the plugin is loaded. + +## Migrating from laravel4 or laravel5 + +Replace `laravel4` or `laravel5` in your `plugins` array with `laravel`. +The old plugin names still load `laravel`, with a deprecation notice, and retain +their own version-specific aliases listed below. These aliases are available +only while the corresponding legacy plugin is enabled. When switching to +`laravel` alone, use `artisan` or the shared aliases instead; add any shortcuts +you still need to your own configuration. + +| Alias | Command | +|-------|---------| +| `la4`, `la5` | `php artisan` | +| `la4dump` | `php artisan dump-autoload` | +| `la4cache`, `la5cache` | `php artisan cache:clear` | +| `la4routes` | `php artisan routes` (Laravel 4) | +| `la5routes` | `php artisan route:list` (Laravel 5 and later) | +| `la5vendor` | `php artisan vendor:publish` | + +Only commands supported by your project's Laravel version can be run. The +version-specific aliases keep their original meaning; they do not translate +commands between versions. + ## Settings By default the aliases run `php artisan`. To use a different PHP command, such as @@ -19,6 +53,16 @@ zstyle ':omz:plugins:laravel' php 'herd php' `php` in the aliases below is then replaced with that command. +For a wrapper such as `herd php`, Zsh normally attempts completion for the +wrapper after expanding the alias. To complete `artisan`, `la4`, and `la5` +directly with this plugin instead, enable Zsh's `COMPLETE_ALIASES` option: + +```zsh +setopt COMPLETE_ALIASES +``` + +This option applies to all aliases in your shell. + ## Aliases | Alias | Description | diff --git a/plugins/laravel/_artisan b/plugins/laravel/_artisan index 8637514cf..ca57f0993 100644 --- a/plugins/laravel/_artisan +++ b/plugins/laravel/_artisan @@ -8,6 +8,9 @@ # - Adds aliases and autocompletion for bob local curcontext="$curcontext" state line _opts _bundles ret=1 +local php_command command_list +local -a artisan_commands + _arguments -C \ '1: :->cmds' \ '*:: :->args' && ret=0 @@ -15,6 +18,21 @@ _arguments -C \ case $state in cmds) + # Boot the application only to discover command names, not file arguments. + # Laravel 3 falls back to the static command names below. + zstyle -s ':omz:plugins:laravel' php php_command || php_command=php + if [[ -f artisan ]] && command_list=$( + "${(@Q)${(z)php_command}}" artisan list --raw --no-ansi 2>/dev/null + ); then + artisan_commands=("${(@f)command_list}") + artisan_commands=(${artisan_commands[@]%%[[:space:]]*}) + fi + + if (( ${#artisan_commands} )); then + compadd -- "${artisan_commands[@]}" + return $? + fi + _values "Artisan command" \ 'session\:install[Create a session table]' \ 'migrate[Manage Migrations]' \ @@ -24,16 +42,8 @@ case $state in ret=0 ;; args) - case $line[1] in - migrate) - _values \ - 'install[Create the Laravel migration table' \ - 'make[Create a migration]' \ - 'rollback[Roll back to the last migration operation]' \ - 'reset[Roll back all migrations that have ever run]' - ret=0 - ;; - esac + _files + return $? ;; esac diff --git a/plugins/laravel/laravel.plugin.zsh b/plugins/laravel/laravel.plugin.zsh index 798d81d02..b40e3a6f0 100644 --- a/plugins/laravel/laravel.plugin.zsh +++ b/plugins/laravel/laravel.plugin.zsh @@ -63,3 +63,10 @@ alias pamomr="$_omz_laravel_php artisan module:migrate-rollback" alias pamos="$_omz_laravel_php artisan module:seed" unset _omz_laravel_php + +# Also load completion when reached through a legacy plugin name. +if (( ! ${fpath[(Ie)${0:A:h}]} )); then + fpath+=("${0:A:h}") +fi +autoload -Uz _artisan +compdef _artisan artisan diff --git a/plugins/laravel/tests/laravel.test.zsh b/plugins/laravel/tests/laravel.test.zsh new file mode 100644 index 000000000..a50a2f6a6 --- /dev/null +++ b/plugins/laravel/tests/laravel.test.zsh @@ -0,0 +1,171 @@ +#!/usr/bin/env zsh + +# Run with: zsh -df plugins/laravel/tests/laravel.test.zsh +emulate -LR zsh + +plugin_dir=${0:A:h:h} +test_dir=$(mktemp -d) || exit 1 +trap 'command rm -rf -- "$test_dir"' EXIT +typeset -i tests=0 failures=0 fixture_status=0 +typeset completion_state=cmds fixture_output output + +autoload -Uz compinit +compinit -D -u + +# Record the interpreter's arguments even when invoked in a subshell. +php() { + print -rl -- "$@" >| "$test_dir/call" + print -r -- "$fixture_output" + return $fixture_status +} + +check() { + local description=$1 + shift + (( tests++ )) + if "$@"; then + print -r -- "PASS: $description" + else + print -u2 -r -- "FAIL: $description" + (( failures++ )) + fi +} + +source "$plugin_dir/laravel.plugin.zsh" +check 'plugin loading does not execute PHP' test ! -e "$test_dir/call" +check 'completion is registered for artisan' \ + test "${_comps[artisan]}" = _artisan +for name in la4 la4dump la4cache la4routes la5 la5cache la5routes la5vendor; do + check "laravel alone does not add $name" test "${+aliases[$name]}" = 0 +done +for name in la4 la5; do + check "laravel alone does not register $name completion" \ + test "${+_comps[$name]}" = 0 +done + +for entry in \ + 'pas:serve' 'pam:migrate' 'pamm:make:model' 'bob:bob::build'; do + name=${entry%%:*} + expected_command=${entry#*:} + expected_call=$'artisan\n'"$expected_command"$'\nargument with spaces' + eval "$name 'argument with spaces'" > /dev/null + check "$name preserves its command and arguments" \ + test "$(command cat -- "$test_dir/call")" = "$expected_call" +done + +# Exercise completion dispatch without requiring an interactive ZLE session. +_arguments() { state=$completion_state; line=(migrate); return 1; } +compadd() { [[ $1 == -- ]] && shift; print -rl -- "$@"; } +_values() { print -rl -- "$@"; } +_files() { print -r -- file-completion; } + +mkdir "$test_dir/project with spaces" +builtin cd "$test_dir/project with spaces" +touch artisan +fixture_output='cache:clear Clear the application cache +migrate Run migrations +project:custom Run a custom command' +output=$(_artisan) +check 'project commands, namespaces and custom commands are completed' \ + test "$output" = $'cache:clear\nmigrate\nproject:custom' +check 'completion uses the portable raw list interface' \ + test "$(command cat -- "$test_dir/call")" = $'artisan\nlist\n--raw\n--no-ansi' + +fixture_output='routes List Laravel 4 routes +dump-autoload Regenerate autoload files' +output=$(_artisan) +check 'Laravel 4 commands are discovered rather than hardcoded' \ + test "$output" = $'routes\ndump-autoload' + +completion_state=args +for fixture_status in 0 1; do + command rm -f -- "$test_dir/call" + output=$(_artisan) + check "command arguments use file completion (PHP status $fixture_status)" \ + test "$output" = file-completion + check "argument completion never boots Artisan (PHP status $fixture_status)" \ + test ! -e "$test_dir/call" +done +completion_state=cmds + +fixture_status=1 +fixture_output='Unsupported option: --raw' +output=$(_artisan) +check 'legacy Artisan retains its static completion when raw listing fails' \ + test "${output[(f)-1]}" = 'key\:\:generate[Generate a key]' +fixture_status=0 + +fixture_output='' +output=$(_artisan) +check 'an empty command list falls back to legacy completion' \ + test "${output[(f)-1]}" = 'key\:\:generate[Generate a key]' + +command rm "$test_dir/call" +builtin cd "$test_dir" +output=$(_artisan) +check 'completion outside a project does not execute PHP' \ + test ! -e "$test_dir/call" + +# A configured command may contain multiple words and quoted arguments. +wrapper() { + print -rl -- "$@" >| "$test_dir/wrapper-call" + print -r -- "$fixture_output" +} +zstyle ':omz:plugins:laravel' php 'wrapper --site "site with spaces" php' +source "$plugin_dir/../laravel5/laravel5.plugin.zsh" 2> "$test_dir/warning" +eval 'la5routes "argument with spaces"' > /dev/null +expected_call=$'--site\nsite with spaces\nphp\nartisan\nroute:list' +expected_call+=$'\nargument with spaces' +check 'legacy aliases honor a configured PHP wrapper' \ + test "$(command cat -- "$test_dir/wrapper-call")" = "$expected_call" +builtin cd "$test_dir/project with spaces" +fixture_output=$'project:custom Run a custom command' +output=$(_artisan) +expected_call=$'--site\nsite with spaces\nphp\nartisan\nlist\n--raw\n--no-ansi' +check 'completion honors a configured PHP wrapper' \ + test "$(command cat -- "$test_dir/wrapper-call")" = "$expected_call" +check 'wrapper output is completed' test "$output" = project:custom +zstyle -d ':omz:plugins:laravel' php + +for plugin in laravel4 laravel5; do + # Start a separate shell so no previously loaded completion can hide a + # missing autoload path in the compatibility plugin. + output=$(zsh -df -c ' + autoload -Uz compinit + compinit -D -u + source "$1" 2> "$2" + prefix=la${3#laravel} + for name in artisan $prefix; do + [[ ${_comps[$name]} == _artisan ]] || exit 1 + done + if [[ $prefix == la4 ]]; then + [[ ${+aliases[la5]} == 0 ]] || exit 1 + expected_aliases=( + la4 "php artisan" la4dump "php artisan dump-autoload" + la4cache "php artisan cache:clear" la4routes "php artisan routes" + ) + else + [[ ${+aliases[la4]} == 0 ]] || exit 1 + expected_aliases=( + la5 "php artisan" la5cache "php artisan cache:clear" + la5routes "php artisan route:list" + la5vendor "php artisan vendor:publish" + ) + fi + for name expected in "${expected_aliases[@]}"; do + [[ ${aliases[$name]} == "$expected" ]] || exit 1 + done + autoload +X _artisan || exit 1 + [[ ${functions_source[_artisan]} == */laravel/_artisan ]] || exit 1 + print -r -- loaded + ' zsh "$plugin_dir/../$plugin/$plugin.plugin.zsh" \ + "$test_dir/warning" "$plugin") + result=$? + check "$plugin loads the unified completion and original aliases" \ + test "$result:$output" = '0:loaded' + check "$plugin explains how to migrate" \ + test "$(command cat -- "$test_dir/warning")" != '' +done + +print -r -- "$tests tests, $failures failures" +(( failures == 0 )) diff --git a/plugins/laravel4/README.md b/plugins/laravel4/README.md index 97a6a7899..5cf50dfd6 100644 --- a/plugins/laravel4/README.md +++ b/plugins/laravel4/README.md @@ -1,18 +1,13 @@ # Laravel 4 plugin -This plugin adds some aliases for common [Laravel 4](https://laravel.com/docs/4.2) commands. - -To use it, add `laravel4` to the plugins array in your zshrc file: +This plugin is deprecated. Use the [laravel plugin](../laravel) instead: ```zsh -plugins=(... laravel4) +plugins=(... laravel) ``` -## Aliases - -| Alias | Command | Description | -|-----------|-----------------------------|-------------------------------------| -| la4 | `php artisan` | Main Artisan command | -| la4dump | `php artisan dump-autoload` | Regenerate framework autoload files | -| la4cache | `php artisan cache:clear` | Flush the application cache | -| la4routes | `php artisan routes` | List all registered routes | +This compatibility loader sources `laravel` for shared aliases and completion, +then adds the original `la4`, `la4dump`, `la4cache`, and `la4routes` aliases. +These version-specific aliases are only available while `laravel4` is enabled. +When switching to `laravel` alone, use `artisan` or the shared aliases, or define +any shortcuts you still need in your own configuration. diff --git a/plugins/laravel4/laravel4.plugin.zsh b/plugins/laravel4/laravel4.plugin.zsh index 0edc84970..5daf8ec76 100644 --- a/plugins/laravel4/laravel4.plugin.zsh +++ b/plugins/laravel4/laravel4.plugin.zsh @@ -1,20 +1,15 @@ -# Laravel4 basic command completion -_laravel4_get_command_list () { - php artisan --no-ansi | sed "1,/Available commands/d" | awk '/^ +[a-z]+/ { print $1 }' -} +print -Pru2 -- \ + '%F{yellow}[oh-my-zsh] The %Blaravel4%b plugin is deprecated.' \ + 'Use %Blaravel%b instead.%f' -_laravel4 () { - if [ -f artisan ]; then - compadd `_laravel4_get_command_list` - fi -} +source "${0:A:h:h}/laravel/laravel.plugin.zsh" -compdef _laravel4 artisan -compdef _laravel4 la4 +# Keep compatibility aliases scoped to users of this legacy plugin. +zstyle -s ':omz:plugins:laravel' php _omz_laravel_php || _omz_laravel_php=php +alias la4="$_omz_laravel_php artisan" +alias la4dump="$_omz_laravel_php artisan dump-autoload" +alias la4cache="$_omz_laravel_php artisan cache:clear" +alias la4routes="$_omz_laravel_php artisan routes" +unset _omz_laravel_php -#Alias -alias la4='php artisan' - -alias la4dump='php artisan dump-autoload' -alias la4cache='php artisan cache:clear' -alias la4routes='php artisan routes' +compdef _artisan la4 diff --git a/plugins/laravel5/README.md b/plugins/laravel5/README.md index 933342a9e..cccfa6156 100644 --- a/plugins/laravel5/README.md +++ b/plugins/laravel5/README.md @@ -1,18 +1,13 @@ # Laravel 5 plugin -This plugin adds some aliases for common [Laravel 5](https://laravel.com/docs) commands. - -To use it, add `laravel5` to the plugins array in your zshrc file: +This plugin is deprecated. Use the [laravel plugin](../laravel) instead: ```zsh -plugins=(... laravel5) +plugins=(... laravel) ``` -## Aliases - -| Alias | Command | Description | -|-----------|------------------------------|----------------------------------------------------| -| la5 | `php artisan` | Main Artisan command | -| la5cache | `php artisan cache:clear` | Flush the application cache | -| la5routes | `php artisan route:list` | List all registered routes | -| la5vendor | `php artisan vendor:publish` | Publish any publishable assets from vendor package | +This compatibility loader sources `laravel` for shared aliases and completion, +then adds the original `la5`, `la5cache`, `la5routes`, and `la5vendor` aliases. +These version-specific aliases are only available while `laravel5` is enabled. +When switching to `laravel` alone, use `artisan` or the shared aliases, or define +any shortcuts you still need in your own configuration. diff --git a/plugins/laravel5/laravel5.plugin.zsh b/plugins/laravel5/laravel5.plugin.zsh index 70dc4ed02..aa28938ce 100644 --- a/plugins/laravel5/laravel5.plugin.zsh +++ b/plugins/laravel5/laravel5.plugin.zsh @@ -1,19 +1,15 @@ -# Alias -alias la5='php artisan' -alias la5cache='php artisan cache:clear' -alias la5routes='php artisan route:list' -alias la5vendor='php artisan vendor:publish' +print -Pru2 -- \ + '%F{yellow}[oh-my-zsh] The %Blaravel5%b plugin is deprecated.' \ + 'Use %Blaravel%b instead.%f' -# Laravel5 basic command completion -_laravel5_get_command_list () { - php artisan --raw --no-ansi list | sed "s/[[:space:]].*//g" -} +source "${0:A:h:h}/laravel/laravel.plugin.zsh" -_laravel5 () { - if [[ -f artisan ]]; then - compadd $(_laravel5_get_command_list) - fi -} +# Keep compatibility aliases scoped to users of this legacy plugin. +zstyle -s ':omz:plugins:laravel' php _omz_laravel_php || _omz_laravel_php=php +alias la5="$_omz_laravel_php artisan" +alias la5cache="$_omz_laravel_php artisan cache:clear" +alias la5routes="$_omz_laravel_php artisan route:list" +alias la5vendor="$_omz_laravel_php artisan vendor:publish" +unset _omz_laravel_php -compdef _laravel5 artisan -compdef _laravel5 la5 +compdef _artisan la5