My new favorite way to completely underuse a Makefile? Creating personalized, per-project repository workflow command aliases that you can check in.
我最喜歡的完全沒用Makefile的方法? 創建個性化的按項目存儲庫工作流命令別名,您可以檢入。
Can a Makefile improve your DevOps and keep developers happy? How awesome would it be if a new developer working on your project didn’t start out by copying and pasting commands from your README? What if instead of:
Makefile可以改善您的DevOps并使開發人員滿意嗎? 如果一個新的開發人員沒有通過復制和粘貼自述文件中的命令來開始工作,那將有多棒? 如果不是:
pip3 install pipenv
pipenv shell --python 3.8
pipenv install --dev
npm install
pre-commit install --install-hooks
# look up how to install Framework X...
# copy and paste from README...
npm run serve
… you could just type:
…您可以輸入:
make start
make start
…and then start working?
…然后開始工作?
有所作為 (Making a difference)
I use make
every day to take the tedium out of common development activities like updating programs, installing dependencies, and testing.
我每天都會使用make
來消除繁瑣的開發活動,例如更新程序,安裝依賴項和測試。
To do all this with a Makefile (GNU make), we use Makefile rules and recipes. Similar parallels exist for POSIX flavor make, like Target Rules. Here’s a great article on POSIX-compatible Makefiles.
為了使用Makefile(GNU make)來完成所有這些工作,我們使用Makefile規則和配方 。 POSIX風味制造也存在類似的相似之處,例如目標規則 。 這是一篇有關POSIX兼容Makefile的好文章 。
Here’s some examples of things we can make
easier (sorry):
這里的有些東西在例子中,我們可以make
更容易(不好意思):
update: ## Do apt upgrade and autoremovesudo apt update && sudo apt upgrade -ysudo apt autoremove -yenv:pip3 install pipenvpipenv shell --python 3.8install: ## Install or update dependenciespipenv install --devnpm installpre-commit install --install-hooksserve: ## Run the local development serverhugo serve --enableGitInfo --disableFastRender --environment developmentinitial: update env install serve ## Install tools and start development server
Now we have some command-line aliases that you can check in. Great idea! If you’re wondering what’s up with that weird ##
comment syntax, it gets better.
現在我們有了一些命令行別名,您可以簽入。好主意! 如果您想知道奇怪的##
注釋語法有什么用,它會變得更好。
一個自記錄的Makefile (A self-documenting Makefile)
Aliases are great, if you remember what they all are and what they do without constantly typing cat Makefile
. Naturally, you need a help
command:
別名很棒,如果您記住它們的全部含義而無需不斷輸入cat Makefile
就能做什么。 當然,您需要一個help
命令:
.PHONY: help
help: ## Show this help@egrep -h '\s##\s' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'
With a little command-line magic, this egrep
command takes the output of MAKEFILE_LIST
, sorts it, and uses awk
to find strings that follow the ##
pattern. It then prints a helpful formatted version of the comments.
使用一點命令行魔術,此egrep
命令獲取MAKEFILE_LIST
的輸出,對其進行排序,然后使用awk
查找遵循##
模式的字符串。 然后,它會打印出有用的格式化的注釋版本。
We’ll put it at the top of the file so it’s the default target. Now to see all our handy shortcuts and what they do, we just run make
, or make help
:
我們將其放在文件的頂部,因此它是默認目標。 現在,要查看我們所有方便的快捷方式及其作用,我們只需運行make
或make help
:
help Show this help
initial Install tools and start development server
install Install or update dependencies
serve Run the local development server
update Do apt upgrade and autoremove
Now we have our very own personalized and project-specific CLI tool!
現在,我們有了自己的個性化和特定于項目的CLI工具!
The possibilities for improving your DevOps flow with a self-documenting Makefile are almost endless. You can use one to simplify any workflow and produce some very happy developers.
使用自記錄的Makefile改善DevOps流程的可能性幾乎是無限的。 您可以使用它來簡化任何工作流程并產生一些非常滿意的開發人員。
翻譯自: https://www.freecodecamp.org/news/self-documenting-makefile/