编程

使用别名增强 Laravel 开发工作流程

478 2024-01-01 15:03:00

命令行别名是缩短现有命令或将一组命令组合为一个有意义的单词或缩写的便捷方法。

今天,我将与大家分享一些我最常用的别名,帮助我每天节省时间。随着时间的推移,这可能会导致节省数小时甚至数天的时间,甚至更少的错误。让我们深入研究。

如何添加别名

打开 ~/.bashrc (或者,如果使用的是 ZSH, .zshrc)。别名的结构非常简单,由一个简单的键/值对组成:

alias gs="git status"

首先是 alias 关键字,然后是您将在命令行中键入的别名别名(gs),以及您的终端识别和运行的实际命令(如上面示例中的 git status)。保存了这个别名后,每次在命令行中键入 gs 时,它实际上都会运行 git status。这是最简单的例子,但它可以远远超出这个范围,例如按顺序执行多个命令。你已经可以看到这将如何节省你的时间。

请记住,保存 ~/.bashrc 文件后,您的别名不会立即在同一命令行会话中工作。要使新的更改生效,请重新启动命令行,或者通过在命令行中键入 source ~/.bashrc 来重新加载配置。

Git 别名

这些别名很可能是您日常工作流程中使用最多的别名。你有多少次在写 git addgit commit 甚至 git status 时出现拼写错误?我知道我在输入 git branch 时遇到了很多麻烦,尤其是 git-push/git-pull 命令。因此,让我们从一些小而简单的改进开始。

# Display the status of the current branch
alias gs="git status"

# Display a list of branches on your local machine
alias gb="git branch"

# Add all files to the staging area
alias ga="git add ."

# Commit staged files with a message
alias gc="git commit -m"

# Add all files to the staging area and commit them with a message
alias gac="git add . && git commit -m"

# Checkout to a new branch
alias gnew="git checkout -b"

### functions can also be called from the command line,
### which is kind of like an alias, but with advanced capabilities

# Pull the latest changes from a remote branch
# (current by default, or you can speficy which specific branch you'd like to pull from)
function gpull () {
  if [ -z "$1" ]
    then
      git pull origin `git rev-parse --abbrev-ref HEAD`
    else
      git pull origin $1
  fi
}

# Push the local changes to the remote branch
# (current branch by default, or you can specify a different one)
function gpush () {
  if [ -z "$1" ]
    then
      git push origin `git rev-parse --abbrev-ref HEAD`
    else
      git push origin $1
  fi
}

# Checkout to a different branch and pull the latest changes from it automatically
function ch() { git checkout $1; gpull; }

以下是我使用以下命令的常用工作流程:

  1. 我开始开发一个新功能,所以让我们签出一个新分支:
    输入 gnew feature/uploading-profile-photos
    现在,我们在新特性分支里了。
  2. 我做了一些工作,我已经准备好提交我的代码了。
    输入 gac "Users can now upload their own profile images"
    这些文件现在已经在一行中添加并提交。
  3. 我们来推送代码到远端!
    输入 gpush
    好了,代码现在同步到远程分支了 :)
     

现在假设其他人已经选择了您的功能分支并进行了一些更改。通常,当你想获取最新的更改时,你必须键入分支机构的完整名称,比如 git pull origin feature/uploading-profile-photos。但是,你有多少次发现自己打错了字,然后不得不重新打一遍?所有这些浪费的时间和理智可以用一个简单的别名 gpull 来保存!它将为您下拉当前分支,而无需键入全名。同样的事情也适用于 gpush 别名,它使用当前分支名称将代码推送到远程。

一周过去了,您的团队对主分支进行了一些重要的更改,您希望使功能分支保持最新。通常(在功能分支上),您会键入git pull origin master。同样的事情现在可以通过别名 gpull master 来实现。

一旦你完成了功能分支,并准备好开始一些功能,只需执行 ch master(“checkout master”),它将签出到主分支并从中提取最新的更改,然后执行 gnew <branch name>以开始一个新分支:)

Laravel 及 Node 别名

现在,我个人大量使用 Laravel 和 VueJS,我发现一些别名在节省时间和避免长期头痛方面非常有帮助。

团队经常会处理新的功能集,其中包括新的或更新的依赖项。您可以询问您的团队是否有更改,查看相关功能分支上的更改,或者尝试运行代码,看看是否有任何更改。最好的情况是,您会立即遇到错误,并有望意识到更新您的依赖关系。最糟糕的情况是,代码实际上会在不正确和过时的依赖项上运行,这最终会给您带来不正确的结果。这就是为什么,无论何时签出到不同的分支,您通常都希望始终运行 composer install npm install,以便始终了解该分支中使用的正确依赖关系。一次键入一个命令,等待每个命令完成运行后再键入下一个命令可能会很乏味。

输入别名。

# Install Composer and Node packages. Replace "npm" with "yarn" if you're using Yarn
alias install="composer install && npm install"

# Just an even shorter alias to "install" above
alias i="install"

# I call it "Build Resources" - br. Install packages, run the migrations and build the frontend resources
alias br="install && php artisan migrate && npm run dev"

# "Build Resources & Watch" - brw. Same as above, but also watch for file changes
alias brw="install && php artisan migrate && npm run watch"

# PHP Artisan commands
alias art="php artisan"

# Even shorter alias for Artisan commands, if you perfer
alias a="php artisan"

# Launch a Tinker session
alias tinker="php artisan tinker"

# Run the Laravel Queue worker. Add your additional queues there if you have any, separated by commas
alias work="php artisan queue:work --queue=default --tries=3 --timeout=90"

以下是我的工作流:

  1. 每当我签出到一个新分支时,我只需键入 br 即可安装所有依赖项,运行迁移(如果有的话),还可以构建前端资产,这样我就可以立即预览项目,因为我知道它处于正确的状态,并且具有正确的依赖项。
  2. 如果我要处理分支,我将运行 brw,它会做完全相同的事情,只是它会监视文件更改并自动重新构建前端资产,这通常是我在处理功能时想要的。
  3. 如果我的开发环境中有任何排队的 Laravel 作业准备处理,我可以用一个简单的 work 别名启动队列 Worker。

就是这样!再加上顶部描述的几个 git 别名,您将在不必要的键入或偶尔的键入错误中节省大量时间。