文章目录
Metaprogramming
Q1
Question
Most makefiles provide a target called clean
. This isn’t intendedto produce a file called clean
, but instead to clean up any files that can be re-built by make. Think of it as a way to “undo” all of the build steps. Implement a clean
target for the paper.pdf
Makefile
above. You will have to make the target phon. You may find the git ls-files
subcommand useful. A number of other very common make targets are listed here.
Solution
Makefile
文件中加入
.PHONY: clean
clean:
git ls-files -o | xargs -d '\n' rm
Q2
Question
Take a look at the various ways to specify version requirements for dependencies in Rust’s build system. Most package repositories support similar syntax. For each one (caret, tilde, wildcard, comparison, and multiple), try to come up with a use-case in which that particular kind of requirement makes sense.
Solution
^:
^1.2.3 := 1.2.3
~:
~1.2.3 := >=1.2.3, <1.3.0
~1.2 := >=1.2.0, <1.3.0
~1 := >=1.0.0, <2.0.0
*:
* := >=0.0.0
1.* := >=1.0.0, <2.0.0
1.2.* := >=1.2.0, <1.3.0
<, <=, >, >=, =:
>= 1.2.0
> 1
< 2
= 1.2.3
multiple requirements
>= 1.2, < 1.5.
Q3
Question
Git can act as a simple CI system(continuous integration system) all by itself. In .git/hooks
inside any git repository, you will find (currently inactive) files that are run as scripts when a particular action happens. Write a pre-commit
hook that runs make paper.pdf
and refuses the commit if the make
command fails. This should prevent any commit from having an unbuildable version of the paper.
Solution
pre-commit是在执行git commit
时被触发的脚本,用于确认提交是否成功,如果失败应该返回非0值则提交无法成功
#!/bin/sh
#
# Pre-commit script to prevent commit if the make fails
# Redirect output to stderr.
exec 1>&2
if make
then
echo "Make successful"
else
cat << EOF
Error: could not make pdf
EOF
exit 1
fi
Q4
Question
Set up a simple auto-published page using GitHubPages.Add a [GitHub Action(https://github.com/features/actions) to therepository to run shellcheck
on any shell files in that repository (here is one way to do it). Check that it works!
Solution
注意yml文件对空格和tab极度敏感
ci.yml
# This is a basic workflow to help you get started with Actions
name: CI
# Controls when the workflow will run
on:
# Triggers the workflow on push or pull request events but only for the main branch
push:
branches: [ main ]
pull_request:
branches: [ main ]
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v3
# Runs a single command using the runners shell
- name: Run a one-line script
run: echo Hello, world!
# Runs a set of commands using the runners shell
- name: Run a multi-line script
run: |
echo Add other actions to build,
echo test, and deploy your project.
- name: ShellCheck
uses: ludeeus/action-shellcheck@master
Q5
Question
Build your own GitHub action to run proselint
or write-good
on all the .md
files in the repository. Enable it in your repository, and check that it works by filing a pull request with a typo in it.
Solution
给的两个proselint和write-good是两个命令行语法检查工具
可能需要现在docker仓库配置好环境 然后再使用fd .md得到的所有结果进行指令的执行