├── LICENSE ├── artisan.plugin.zsh └── README.md /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Jess Archer 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /artisan.plugin.zsh: -------------------------------------------------------------------------------- 1 | #-------------------------------------------------------------------------- 2 | # Laravel artisan plugin for zsh 3 | #-------------------------------------------------------------------------- 4 | # 5 | # This plugin adds an `artisan` shell command that will find and execute 6 | # Laravel's artisan command from anywhere within the project. It also 7 | # adds shell completions that work anywhere artisan can be located. 8 | 9 | function artisan() { 10 | local artisan_path=`_artisan_find` 11 | 12 | if [ "$artisan_path" = "" ]; then 13 | >&2 echo "zsh-artisan: artisan not found. Are you in a Laravel directory?" 14 | return 1 15 | fi 16 | 17 | local laravel_path=`dirname $artisan_path` 18 | local docker_compose_config_path=`find $laravel_path -maxdepth 1 \( -name "compose.yaml" -o -name "compose.yml" -o -name "docker-compose.yaml" -o -name "docker-compose.yml" \) | head -n1` 19 | local artisan_cmd 20 | 21 | if [ "$docker_compose_config_path" = '' ]; then 22 | artisan_cmd="$artisan_path" 23 | else 24 | if [ "`grep "LARAVEL_SAIL: 1" $docker_compose_config_path | head -n1`" != '' ]; then 25 | artisan_cmd="$laravel_path/vendor/bin/sail artisan" 26 | else 27 | local docker_compose_cmd=`_docker_compose_cmd` 28 | local docker_compose_service_name=`$=docker_compose_cmd ps --services 2>/dev/null | grep 'app\|php\|api\|workspace\|laravel\.test\|webhost' | head -n1` 29 | if [ -t 1 ]; then 30 | artisan_cmd="$docker_compose_cmd exec $docker_compose_service_name php artisan" 31 | else 32 | # The command is not being run in a TTY (e.g. it's being called by the completion handler below) 33 | artisan_cmd="$docker_compose_cmd exec -T $docker_compose_service_name php artisan" 34 | fi 35 | fi 36 | fi 37 | 38 | local artisan_start_time=`date +%s` 39 | 40 | eval $artisan_cmd $* 41 | 42 | local artisan_exit_status=$? # Store the exit status so we can return it later 43 | 44 | if [[ $1 = "make:"* && $ARTISAN_OPEN_ON_MAKE_EDITOR != "" ]]; then 45 | # Find and open files created by artisan 46 | find \ 47 | "$laravel_path/app" \ 48 | "$laravel_path/tests" \ 49 | "$laravel_path/database" \ 50 | -type f \ 51 | -newermt "-$((`date +%s` - $artisan_start_time + 1)) seconds" \ 52 | -exec $ARTISAN_OPEN_ON_MAKE_EDITOR {} \; 2>/dev/null 53 | fi 54 | 55 | return $artisan_exit_status 56 | } 57 | 58 | compdef _artisan_add_completion artisan 59 | 60 | function _artisan_find() { 61 | # Look for artisan up the file tree until the root directory 62 | local dir=. 63 | until [ $dir -ef / ]; do 64 | if [ -f "$dir/artisan" ]; then 65 | echo "$dir/artisan" 66 | return 0 67 | fi 68 | 69 | dir+=/.. 70 | done 71 | 72 | return 1 73 | } 74 | 75 | function _artisan_add_completion() { 76 | if [ "`_artisan_find`" != "" ]; then 77 | compadd `_artisan_get_command_list` 78 | fi 79 | } 80 | 81 | function _artisan_get_command_list() { 82 | artisan --raw --no-ansi list | sed "s/[[:space:]].*//g" 83 | } 84 | 85 | function _docker_compose_cmd() { 86 | docker compose &> /dev/null 87 | if [ $? = 0 ]; then 88 | echo "docker compose" 89 | else 90 | echo "docker-compose" 91 | fi 92 | } 93 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | zsh-artisan - Enhanced Laravel integration for zsh 3 |

4 | 5 | This plugin adds an `artisan` shell command with the following features: 6 | 7 | * It will find and execute `artisan` from anywhere within the project file tree 8 | (and you don't need to prefix it with `php` or `./`) 9 | * It provides auto-completion for `artisan` commands (that also work anywhere 10 | within the project). 11 | * You can specify an editor to automatically open new files created by `artisan 12 | make:*` commands 13 | * It automatically runs artisan commands using `sail` when appropriate. 14 | * It will run commands using `docker compose` (or `docker-compose`) if a known container name is found. 15 | 16 |

17 | 18 |

19 | 20 | ## Requirements 21 | 22 | * [Zsh](https://www.zsh.org/) 23 | * A Zsh package manager (e.g. [oh-my-zsh](https://github.com/robbyrussell/oh-my-zsh), [Antigen](https://github.com/zsh-users/antigen), or [zplug](https://github.com/zplug/zplug)) 24 | * A [Laravel](https://laravel.com/) project 25 | 26 | ## Installation 27 | 28 | ### [Antigen](https://github.com/zsh-users/antigen) 29 | 30 | Add the following bundle to your `.zshrc`: 31 | 32 | ```zsh 33 | antigen bundle jessarcher/zsh-artisan 34 | ``` 35 | 36 | ### [Fig](https://fig.io) 37 | 38 | Fig adds apps, shortcuts, and autocomplete to your existing terminal. 39 | 40 | Install `artisan` in just one click. 41 | 42 | 43 | 44 | ### Oh-my-zsh 45 | 46 | First download the plugin to your oh-my-zsh custom plugin location: 47 | 48 | ```zsh 49 | git clone https://github.com/jessarcher/zsh-artisan.git "${ZSH_CUSTOM:-~/.oh-my-zsh/custom}"/plugins/artisan 50 | ``` 51 | 52 | > Note that the repository name is prefixed with `zsh-`, however the plugin 53 | > directory name should just be "artisan". 54 | 55 | Then enable the plugin in your `.zshrc` file. For example: 56 | 57 | ```zsh 58 | plugins=( 59 | artisan 60 | composer 61 | git 62 | ) 63 | ``` 64 | 65 | ### [zplug](https://github.com/zplug/zplug) 66 | 67 | Add the following to your `.zshrc`: 68 | 69 | ```zsh 70 | zplug "jessarcher/zsh-artisan" 71 | ``` 72 | 73 | ## Configuration 74 | 75 | If you wish to automatically open new files created by `artisan make:*` commands 76 | then you will need to configure the `ARTISAN_OPEN_ON_MAKE_EDITOR` environment 77 | variable. The best place for this is probably your `.zshrc` file. For example: 78 | 79 | ```zsh 80 | ARTISAN_OPEN_ON_MAKE_EDITOR=vim 81 | #ARTISAN_OPEN_ON_MAKE_EDITOR=subl # Sublime Text 82 | #ARTISAN_OPEN_ON_MAKE_EDITOR=pstorm # PHPStorm 83 | #ARTISAN_OPEN_ON_MAKE_EDITOR=atom # Atom (May require shell commands to be enabled) 84 | #ARTISAN_OPEN_ON_MAKE_EDITOR=code # VSCode (May require shell commands to be enabled) 85 | ``` 86 | 87 | > The author uses [mhinz/neovim-remote](https://github.com/mhinz/neovim-remote), 88 | combined with a wrapper script, to automatically open files in an existing neovim 89 | session within the same tmux session, and automatically switch to the correct 90 | tmux window (tab). 91 | 92 | Note that you will need to re-source your `.zshrc` or restart `zsh` to pick up 93 | the changes. 94 | 95 | ## Usage 96 | 97 | Simply use the command `artisan` from anywhere within the directory structure of 98 | a Laravel project and it will search up the tree for the `artisan` command and 99 | execute it. E.g: 100 | 101 | ```zshrc 102 | $ pwd 103 | ~/MyProject/tests/Feature 104 | 105 | $ artisan make:model MyAwesomeModel 106 | Model created successfully. 107 | ``` 108 | 109 | Tab-completion will work anywhere that `artisan` can be found, and the available 110 | commands are retrieved on-demand. This means that you will see any Artisan 111 | commands that are available to you, including any custom commands that have 112 | been defined. 113 | 114 | If you configured the `ARTISAN_OPEN_ON_MAKE_EDITOR` environment variable, any 115 | files created by `artisan make:*` commands should automatically be opened, 116 | including when multiple files are created (E.g. by `artisan make:model -m -c -r`) 117 | 118 | The plugin does not create any aliases for you, but the author would like to 119 | offer some suggestions: 120 | 121 | ```zsh 122 | alias a="artisan" 123 | alias tinker="artisan tinker" 124 | alias serve="artisan serve" 125 | ``` 126 | 127 | Many more can be found at https://laravel-news.com/bash-aliases 128 | 129 | ## Homestead Setup 130 | 131 | The Zsh Artisan plugin can be installed automatically with any new or provisioned Laravel Homestead instance. 132 | In the root of your Homestead project, add the following to your `after.sh` file. 133 | ```bash 134 | ARTISAN=/home/vagrant/.oh-my-zsh/custom/plugins/artisan 135 | if [ -d "$ARTISAN" ]; then 136 | echo "$ARTISAN exist" 137 | else 138 | git clone https://github.com/jessarcher/zsh-artisan.git $ARTISAN 139 | sed -i 's/plugins=(git)/plugins=(git composer artisan)/g' /home/vagrant/.zshrc 140 | source /home/vagrant/.zshrc 141 | fi 142 | ``` 143 | *Note:* If you are re-provisioning your Homestead box, and already have other Zsh plugins defined in your Zsh config files, you wil need to adjust the `sed` command to includes those in the list. 144 | 145 | ## License 146 | 147 | This project is open-sourced software licensed under the MIT License - see the 148 | [LICENSE](LICENSE) file for details 149 | 150 | ## Acknowledgements 151 | 152 | * [antonioribeiro/artisan-anywhere](https://github.com/antonioribeiro/artisan-anywhere) 153 | for some of the initial artisan location logic 154 | * The `laravel5` plugin that comes with oh-my-zsh for the initial completion 155 | logic 156 | * [ahuggins/open-on-make](https://github.com/ahuggins/open-on-make) for the 157 | "open on make" functionality idea. Unfortunately, adding a dev dependency like 158 | this isn't an option on some of the projects I work on. 159 | --------------------------------------------------------------------------------