Skip to content

Commit

Permalink
feat: first version
Browse files Browse the repository at this point in the history
  • Loading branch information
aeimer committed Jul 28, 2024
1 parent 9097d2d commit 5a5b8d5
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/check-and-test.yml
Original file line number Diff line number Diff line change
@@ -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
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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))
31 changes: 31 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -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: <!-- BEGIN TOC -->
replace-marker-end:
description: Where to replace the TOC ends
required: true
default: <!-- END TOC -->
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 }}
69 changes: 69 additions & 0 deletions update-toc.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5a5b8d5

Please sign in to comment.