您的位置:首页 > 其它

如何创建一个自记录的Makefile

2020-08-21 14:37 337 查看

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
:

我们将其放在文件的顶部,因此它是默认目标。 现在,要查看我们所有方便的快捷方式及其作用,我们只需运行

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/

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐