😺

Zenn CLIとMakefileで事始め(2)

2025/03/01に公開

まとめ

https://zenn.dev/yohei_washizaki/articles/20250228115200_9f4f78fc

Makefileとbashスクリプトを拡張して、記事の作成とPR作成、作業環境のクリーンアップ、次の記事のための準備ができるようになった。

記事を作成する

make
# または
make article
  1. %Y%m%d%H%M%S_<a-z,0-1のランダムな8文字> で記事のスラグを生成する。
  2. article/<スラグ> でブランチを作成し、そのブランチに移動する。

実行結果

eagle@G-GEAR:~/zenn-doc$ make
bash  ./scripts/article.sh
Creating a new article with slug: 20250301110822_vbqvcqgu
created: articles/20250301110822_vbqvcqgu.md
Switched to a new branch 'article/20250301110822_vbqvcqgu'
eagle@G-GEAR:~/zenn-doc$ git status
On branch article/20250301110822_vbqvcqgu
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        articles/20250301110822_vbqvcqgu.md

nothing added to commit but untracked files present (use "git add" to track)

https://github.com/yohei-washizaki/zenn-doc/blob/33e959c840adcead4c4a213dffedcaf048f8870c/scripts/article.sh

記事公開用のPRを作成する

make publish

ローカルリポジトリにコミット済みの差分をリモートへプッシュし、 gh pr create --fill を実行する。

https://github.com/yohei-washizaki/zenn-doc/blob/c4f8d5db470cbc04c6c5c8fc2c7d2af8b3371432/scripts/publish.sh

実行結果

eagle@G-GEAR:~/zenn-doc$ make publish
bash  ./scripts/publish.sh
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 16 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 689 bytes | 689.00 KiB/s, done.
Total 4 (delta 3), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (3/3), completed with 3 local objects.
To github.com:yohei-washizaki/zenn-doc.git
   283b50c..cbd7438  HEAD -> article/20250301091854_vw84zxxx

Creating pull request for article/20250301091854_vw84zxxx into main in yohei-washizaki/zenn-doc

https://github.com/yohei-washizaki/zenn-doc/pull/10

次の記事を書くための準備を行う

make ready

ファイル差分をスタッシュし、mainブランチに戻る。 mainブランチで実行しても何も起きない。

current_branch="$(git branch --show-current)"
if [[ "${current_branch}" == "main" ]]; then
    exit
fi

この程度なら破壊的操作ではないし、文言なし&エラーなしで終了させる。

https://github.com/yohei-washizaki/zenn-doc/blob/0141bdd8fac28f01021e65d674e8e2ada631a65b/scripts/ready.sh

実行結果

eagle@G-GEAR:~/zenn-doc$ make ready
bash  ./scripts/ready.sh
No local changes to save
Switched to branch 'main'
Your branch is behind 'origin/main' by 2 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)
Updating 0cb39c6..6095812
Fast-forward
 scripts/ready.sh | 1 +
 1 file changed, 1 insertion(+)

作業環境をクリーンアップする

make clean

マージ済みのブランチの消去とファイル差分を削除する。mainブランチ以外で実行するとエラーになる。

current_branch="$(git branch --show-current)"
if [[ "${current_branch}" != "main" ]]; then
    echo "You must be on a branch other than main to clean up branches."
    exit 1
fi

ブランチ名のチェックは個人的によく使う。

https://github.com/yohei-washizaki/zenn-doc/blob/c4f8d5db470cbc04c6c5c8fc2c7d2af8b3371432/scripts/clean.sh

実行結果

eagle@G-GEAR:~/zenn-doc$ make clean
bash  ./scripts/clean.sh clean
No local changes to save
Deleted branch scripts/clean (was 2e0f2e0).

おわりに

Zennで記事を書くための準備が完了したと思う。この程度の内容であればtechタイプで投稿しても問題なかろうということで、前回とは型を変えている。

Makefileはもっといろいろできるのだが、積極的に学習する意欲はないのでCopilotの生成するままにしている。複雑なことはGithub CLIのようなサブコマンド形式のCLIツールに任せるほうが好みだ。

Discussion