diff --git a/.github/workflows/check-and-test.yml b/.github/workflows/check-and-test.yml new file mode 100644 index 0000000..f917c1b --- /dev/null +++ b/.github/workflows/check-and-test.yml @@ -0,0 +1,17 @@ +name: Run checks and tests +run-name: Run checks and tests ⚡️ +on: + push: + paths: + - '**.sh' +jobs: + shellcheck: + name: Shellcheck + runs-on: ubuntu-latest + steps: + + - name: Check out repository code + uses: actions/checkout@v4 + + - name: Run ShellCheck + uses: ludeeus/action-shellcheck@master diff --git a/README.md b/README.md new file mode 100644 index 0000000..c670738 --- /dev/null +++ b/README.md @@ -0,0 +1,50 @@ +# update-brew-tap-toc-action + +This GitHub action reads the Homebrew tap folder and procduces a table of content. +The list of formulae is then placed in the specified files. + +## Example + +An example workflow can look like. + +```yaml +name: Update TOC +run-name: Update TOC 🚀 +on: + push: + branches: + - main + paths: + - Formula/*.rb + - README.md +jobs: + update-toc: + runs-on: ubuntu-latest + steps: + - name: Check out repository code + uses: actions/checkout@v4 + + - name: Update TOC + uses: qaware/update-brew-tap-toc-action@main + + - name: Commit & Push changes + # Use some action that fits your needs +``` + +You can pass options. + +```yaml +# ... +steps: + - name: Update TOC + uses: qaware/update-brew-tap-toc-action@main + with: + formula-folder: Formula-cstm + replace-in: README.md,TOC.md,docs/content.adoc + replace-marker-start: '// START TOC' + replace-marker-end: '// END TOC' +``` + +## Maintainers + +* Alexander Eimer ([@aeimer](https://github.com/aeimer)) diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..f7503f4 --- /dev/null +++ b/action.yml @@ -0,0 +1,31 @@ +name: update-brew-tap-toc-action +description: Reads the formulae stored in the tap (repo) and updates the list in the README +author: aeimer +inputs: + formula-folder: + description: The folder path from the repo root where the Formulae are placed + required: true + default: Formula + replace-in: + description: In which files should the TOC be updated. Eg. "README.md,docs/content.adoc" + required: true + default: README.md + replace-marker-start: + description: Where to replace the TOC starts + required: true + default: + replace-marker-end: + description: Where to replace the TOC ends + required: true + default: +runs: + using: composite + steps: + - name: Update TOC + shell: bash + run: ${{ github.action_path }}/update-toc.sh + env: + INPUT_FORMULA_FOLDER: ${{ inputs.formula-folder }} + INPUT_REPLACE_IN: ${{ inputs.replace-in }} + INPUT_REPLACE_MARKER_START: ${{ inputs.replace-marker-start }} + INPUT_REPLACE_MARKER_END: ${{ inputs.replace-marker-end }} diff --git a/update-toc.sh b/update-toc.sh new file mode 100755 index 0000000..ad1af8c --- /dev/null +++ b/update-toc.sh @@ -0,0 +1,69 @@ +#!/usr/bin/env bash +set -euo pipefail + +# Enable debug - print commands +#set -x + +############# +# FUNCTIONS # +############# + +function generateToc() { + programNameList=() + + while read -r file; do + programName=$(cut -d@ -f1 <<<"$file" | sed 's/.rb//') + programNameList+=("$programName") + done < <(find "$INPUT_FORMULA_FOLDER" -maxdepth 1 -type f -name '*.rb' -exec basename {} \; | sort) + + if [[ ${#programNameList[@]} == "0" ]]; then + programNameList=("_n/a (no formula found)_") + fi + + printf "* %s\n" "${programNameList[@]}" | sort -u +} + +############### +# CHECK INPUT # +############### + +echo "Checking input" +if [[ -z "${INPUT_FORMULA_FOLDER:-}" ]]; then + echo "ERROR: No formula-folder defined" + exit 1 +fi +echo " formula-folder: $INPUT_FORMULA_FOLDER" + +if [[ -z "${INPUT_REPLACE_IN:-}" ]]; then + echo "ERROR: No replace-in defined" + exit 1 +fi +echo " replace-in: $INPUT_REPLACE_IN" + +if [[ -z "${INPUT_REPLACE_MARKER_START:-}" ]]; then + echo "ERROR: No replace-marker-start defined" + exit 1 +fi +echo " replace-marker-start: $INPUT_REPLACE_MARKER_START" + +if [[ -z "${INPUT_REPLACE_MARKER_END:-}" ]]; then + echo "ERROR: No replace-marker-end defined" + exit 1 +fi +echo " replace-marker-start: $INPUT_REPLACE_MARKER_END" + +####### +# RUN # +####### + +echo "Generating TOC..." +toc=$(generateToc) +echo "TOC:" +echo "----" +echo "$toc" +echo "----" + +for file in ${INPUT_REPLACE_IN//,/ }; do + # https://stackoverflow.com/questions/2699666/replace-delimited-block-of-text-in-file-with-the-contents-of-another-file + sed -i -ne '/'"$INPUT_REPLACE_MARKER_START"'/ {p; r '<(cat <<<"$toc") -e ':a; n; /'"$INPUT_REPLACE_MARKER_START"'/ {p; b}; ba}; p' "$file" +done