如何创建一个自记录的Makefile

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 autoremove
    sudo apt update && sudo apt upgrade -y
    sudo apt autoremove -y

env:
    pip3 install pipenv
    pipenv shell --python 3.8

install: ## Install or update dependencies
    pipenv install --dev
    npm install
    pre-commit install --install-hooks

serve: ## Run the local development server
    hugo serve --enableGitInfo --disableFastRender --environment development

initial: 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:

我们将其放在文件的顶部,因此它是默认目标。 现在,要查看我们所有方便的快捷方式及其作用,我们只需运行makemake 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/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值