|
| 1 | +name: Generate Release PDF |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + paths-ignore: |
| 8 | + - '.gitignore' |
| 9 | + - '**.md' |
| 10 | + |
| 11 | +env: |
| 12 | + TYPST_FILE_NAME: book |
| 13 | + TYPST_FONT_PATH: fonts |
| 14 | + |
| 15 | +jobs: |
| 16 | + build_typst: |
| 17 | + runs-on: ubuntu-latest |
| 18 | + permissions: |
| 19 | + contents: write |
| 20 | + steps: |
| 21 | + - name: Set up Git repository for secondary repo |
| 22 | + uses: actions/checkout@v4 |
| 23 | + |
| 24 | + - name: Cache Fonts |
| 25 | + uses: actions/cache@v4 |
| 26 | + id: cache-fonts |
| 27 | + with: |
| 28 | + path: fonts |
| 29 | + key: ${{ runner.os }}-fonts-noto_sans_sc |
| 30 | + |
| 31 | + - name: Download Fonts |
| 32 | + if: steps.cache-fonts.outputs.cache-hit != 'true' |
| 33 | + run: | |
| 34 | + if [ -f "${{ env.TYPST_FONT_PATH }}/NotoSansSC-Regular.ttf" ]; then |
| 35 | + echo "Font already existed." |
| 36 | + else |
| 37 | + # Download font metadata |
| 38 | + mkdir -p cv |
| 39 | + curl -s "https://fonts.google.com/download/list?family=Noto%20Sans%20SC" > cv/noto_sans_sc_raw.txt |
| 40 | +
|
| 41 | + # Remove ")]}'" from the beginning of the file |
| 42 | + tail -c +5 cv/noto_sans_sc_raw.txt > cv/noto_sans_sc_metadata.json |
| 43 | +
|
| 44 | + # Extract the necessary information from the JSON file |
| 45 | + file_refs=$(jq -rc '.manifest.fileRefs[]' cv/noto_sans_sc_metadata.json) |
| 46 | +
|
| 47 | + # Download, save the font file to /fonts |
| 48 | + mkdir -p ${{ env.TYPST_FONT_PATH }} |
| 49 | + while IFS= read -r file_ref; do |
| 50 | + filename=$(echo "$file_ref" | jq -r '.filename') |
| 51 | + url=$(echo "$file_ref" | jq -r '.url') |
| 52 | + fontname=$(basename "$filename") |
| 53 | + echo $url $fontname |
| 54 | + curl "$url" -o "${{ env.TYPST_FONT_PATH }}/$fontname" |
| 55 | + done <<< "$file_refs" |
| 56 | + ls -l ${{ env.TYPST_FONT_PATH }} |
| 57 | + fi |
| 58 | + for form in Light Regular Bold; do |
| 59 | + curl -sSL "https://github.com/lxgw/LxgwWenKai/releases/download/v1.330/LXGWWenKai-$form.ttf" -o "${{ env.TYPST_FONT_PATH }}/LXGWWenKai-$form.ttf" |
| 60 | + done |
| 61 | +
|
| 62 | + - name: Prepare Typst environment |
| 63 | + uses: typst-community/setup-typst@v3 |
| 64 | + |
| 65 | + - name: Compile Typst document |
| 66 | + run: | |
| 67 | + typst fonts --variants --font-path ${{ env.TYPST_FONT_PATH }} |
| 68 | + typst compile ${{ env.TYPST_FILE_NAME }}.typ ${{ env.TYPST_FILE_NAME }}.pdf --font-path ${{ env.TYPST_FONT_PATH }} |
| 69 | +
|
| 70 | + - name: Delete old Release |
| 71 | + uses: actions/github-script@v5 |
| 72 | + with: |
| 73 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 74 | + script: | |
| 75 | + const { owner, repo } = context.repo |
| 76 | + try { |
| 77 | + const { data: { id } } = await github.rest.repos.getLatestRelease({ owner, repo }) |
| 78 | + await github.rest.repos.deleteRelease({ owner, repo, release_id: id }) |
| 79 | + } catch {} |
| 80 | +
|
| 81 | + - name: Generate release tag |
| 82 | + id: tag |
| 83 | + run: | |
| 84 | + echo "::set-output name=release_tag::latest_$(date +"%Y-%m-%d_%H-%M")" |
| 85 | +
|
| 86 | + - name: Release |
| 87 | + uses: softprops/action-gh-release@v1 |
| 88 | + with: |
| 89 | + name: latest version |
| 90 | + body: Latest version of `${{ env.TYPST_FILE_NAME }}.pdf` |
| 91 | + tag_name: ${{ steps.tag.outputs.release_tag }} |
| 92 | + files: ${{ env.TYPST_FILE_NAME }}.pdf |
0 commit comments