LoginSignup
1
0

ドット絵の枚数をREADME.mdに自動反映させたい

Posted at

こんにちは。なっしゅです。
今取り組んでいる作品でドット絵をGitHub上にあげていますが、その枚数を計算してREADME.mdに自動的に表示する機構を作る機会があったので備忘録として残します。

したいこと

  1. ドット絵をdot-worksディレクトリにpushする。
  2. GitHub Actionsのworkflowが実行される。
  3. C++をコンパイル、実行しdot-works内の.pngの枚数を${COUNT}として取得する。
  4. README.md${COUNT}を書き込む。

実装

まず1つ目の目標として「push時にC++をコンパイル、実行する」ところまで実装します。
.github/workflows/.ymlを作成します。

.yml
name: Write the number of dot works down on README.md

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Install g++
      run: sudo apt-get install g++ -y
    - name: Compile C++ program
      run: g++ counter.cpp -o counter
    - name: Run C++ program
      run: ./counter
      

dot-worksディレクトリ内のドット絵の枚数(=.pngの枚数)、を数えるC++のコードは以下の通りです。

counter.cpp
#include <iostream>
#include <filesystem>

int main() {
    const std::string path = "dot-works";
    int count = 0;

    for (const auto& entry : std::filesystem::recursive_directory_iterator(path)) {
        if (entry.path().extension() == ".png") {
            ++count;
        }
    }

    std::cout << count << std::endl;
    return 0;
}

実行したところ、どうやら<filesystem>はC++17の環境で動作するようです。
よって、.ymlを以下のように書き換えました。

.yml
name: Write the number of dot works down on README.md

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Install g++
      run: sudo apt-get install g++ -y
    - name: Compile C++ program
      run: g++ counter.cpp -o counter -std=c++17
    - name: Run C++ program
      run: ./counter
      

pushしたら上手くいきました。
では次にREADME.mdに書き込むように.ymlをいじっていきます。

.yml
name: Write the number of dot works down on README.md

on: [push]

jobs:
  update-readme:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
      with:
        ref: 'main'
    
    - name: Install g++
      run: sudo apt-get install g++ -y
    
    - name: Compile and Run C++ Program
      run: |
        g++ counter.cpp -o counter -std=c++17
        ./counter > output.txt
    
    - name: Update README
      run: |
        COUNT=$(cat output.txt)
        README_CONTENT=$(cat README.md)
        echo -e "${README_CONTENT}\n今までで**${COUNT}**つのドット絵作品が描かれています。" > README.md
    
    - name: Commit and Push if Changes
      run: |
        git config --local user.email "action@github.com"
        git config --local user.name "GitHub Action"
        git add README.md
        git commit -m "Update README with .png file count" -a || echo "No changes to commit"
        git push

これはファイルの末尾に文字列を追加する場合のコードです。
echoでは-eオプションをつけていますが、これは\nを含めたエスケープシーケンスを有効にするためのオプションです。また、echoのあとの文字列をシングルクォーテーションで囲ってしまうと${COUNT}が変数ではなく文字列として認識されてしまうので注意してください。

最後に、READMEの好きな場所に文字列を挿入することを考えましょう。
sedコマンドで文字列を挿入する位置を定めるため、README.mdの文字列を挿入したい場所に次のようにコメントを書きます。

README.md
<!-- PNG_COUNT_START -->
<!-- PNG_COUNT_END -->

このように書くことによって文字列を追加、ではなく更新することができます。
sedコマンドを使って.ymlを次のように書き換えます。

.yml
name: Write the number of dot works down on README.md

on: [push]

jobs:
  update-readme:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
      with:
        ref: 'main'
    
    - name: Install g++
      run: sudo apt-get install g++ -y
    
    - name: Compile and Run C++ Program
      run: |
        g++ counter.cpp -o counter -std=c++17
        ./counter > output.txt
    
    - name: Update README
      run: |
        COUNT=$(cat output.txt)
        sed -i "/<!-- PNG_COUNT_START -->/,/<!-- PNG_COUNT_END -->/c\<!-- PNG_COUNT_START -->\n今までで**${COUNT}**つのドット絵作品が描かれています。\n<!-- PNG_COUNT_END -->" README.md
    
    - name: Commit and Push if Changes
      run: |
        git config --local user.email "action@github.com"
        git config --local user.name "GitHub Action"
        git diff --quiet && git diff --staged --quiet || (git commit -am "Update README with .png file count" && git push)

これで文字列を任意の場所に挿入することができました。
以上です。

ファイル階層

.
├───.github/
│   └───workflows/
│       └───.yml
├───dot-works/
└───counter.cpp
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0