Skip to content

Commit

Permalink
Merge pull request #135 from swissbuechi/generate-help-command-output…
Browse files Browse the repository at this point in the history
…-for-readme

Generate help command output for readme with github action
  • Loading branch information
DomT4 authored Oct 1, 2024
2 parents f1a0ff2 + 8bca942 commit e0cf8d7
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 3 deletions.
57 changes: 57 additions & 0 deletions .github/generate-help-output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import re
import os
import subprocess

def remove_ansi_escape_codes(text):
ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
return ansi_escape.sub('', text)

readme_file = "README.md"

if not os.path.exists(readme_file):
print(f"Error: {readme_file} not found.")
exit()

with open(readme_file, "r") as f:
readme_content = f.read()

start_comment_tag = "<!-- HELP-COMMAND-OUTPUT:START -->"
stop_comment_tag = "<!-- HELP-COMMAND-OUTPUT:END -->"

comment_tag_pattern = rf"{start_comment_tag}(.*?){stop_comment_tag}"
tag_section_content = re.search(comment_tag_pattern, readme_content, re.DOTALL)

if tag_section_content:

try:
help_command_output_new = subprocess.check_output(["brew", "autoupdate", "--help"], text=True)
except subprocess.CalledProcessError as e:
print(f"Error running command: {e}")
exit()

help_command_output_current = tag_section_content.group(1).strip()
help_command_output_new = remove_ansi_escape_codes(help_command_output_new.strip())

start_code_pattern = "```shell"
end_code_pattern = "```"

# Regex to check if the existing help section is in a shell code block
code_block_pattern = re.compile(fr'^{re.escape(start_code_pattern)}.*?\n(.*(?:\n.*)*)\n{re.escape(end_code_pattern)}$', re.DOTALL)
help_command_output_current = re.sub(code_block_pattern, r'\1', help_command_output_current)

if help_command_output_current != help_command_output_new:
print("Content change detected.")
help = f"{start_comment_tag}\n{start_code_pattern}\n{help_command_output_new}\n{end_code_pattern}\n{stop_comment_tag}"
readme_content = re.sub(comment_tag_pattern, help, readme_content, flags=re.DOTALL)
print("Content updated.")

# Set GitHub Actions output variable
with open(os.environ['GITHUB_OUTPUT'], 'a') as fh:
print(f'changed=true', file=fh)
else:
print("No change detected. Content remains unchanged.")
else:
print(f"Error: Unable to find {start_comment_tag} and {stop_comment_tag} in {readme_file}.")

with open(readme_file, "w") as f:
f.write(readme_content)
41 changes: 41 additions & 0 deletions .github/workflows/generate-help-output.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Generate --help output
# Generate `brew autoupdate --help output` for the README.md file
on:
push:

permissions:
contents: write

jobs:
generate-help-output:
name: Generate --help output
runs-on: macos-latest
steps:
- name: Set up Homebrew
id: set-up-homebrew
uses: Homebrew/actions/setup-homebrew@master
with:
debug: false
test-bot: false

- name: Checkout repository
uses: actions/checkout@v4
with:
ref: ${{ github.ref_name }}

- name: Install Python
uses: actions/setup-python@v4
with:
python-version: '3'

- name: Update README.md
id: generate
run: python .github/generate-help-output.py

- name: Commit changes
if: steps.generate.outputs.changed == 'true'
run: |
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config user.name "github-actions[bot]"
git commit -am "Update README.md with --help output"
git push
58 changes: 55 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,61 @@ Casks that have built-in auto-updates enabled by default will not be upgraded.

## Usage

Refer to the link below to find an in-depth description of the commands.
<!-- HELP-COMMAND-OUTPUT:START -->
```shell
Usage: brew autoupdate subcommand [interval] [options]

**[Homebrew Documentation autoupdate subcommand](https://docs.brew.sh/Manpage#autoupdate-subcommand-interval-options)**
An easy, convenient way to automatically update Homebrew.

This script will run brew update in the background once every 24 hours (by
default) until explicitly told to stop, utilising launchd.

brew autoupdate start [interval] [options]:
Start autoupdating either once every interval hours or once every 24
hours. Please note the interval has to be passed in seconds, so 12 hours would
be brew autoupdate start 43200. If you want to start the autoupdate
immediately and on system boot, pass --immediate. Pass --upgrade or
--cleanup to automatically run brew upgrade and/or brew cleanup
respectively.

brew autoupdate stop:
Stop autoupdating, but retain plist and logs.

brew autoupdate delete:
Cancel the autoupdate, delete the plist and logs.

brew autoupdate status:
Print the current status of this tool.

brew autoupdate version:
Output this tool's current version, and a short changelog.
--upgrade Automatically upgrade your installed
formulae. If the Caskroom exists locally then
casks will be upgraded as well. Must be
passed with start.
--greedy Upgrade casks with --greedy (include
auto-updating casks). Must be passed with
start.
--cleanup Automatically clean Homebrew's cache and
logs. Must be passed with start.
--enable-notification Notifications are enabled by default on macOS
Catalina and newer. This flag is no longer
required and can be safely dropped.
--immediate Starts the autoupdate command immediately and
on system boot, instead of waiting for one
interval (24 hours by default) to pass first.
Must be passed with start.
--sudo If a cask requires sudo, autoupdate will
open a GUI to ask for the password. Requires
https://formulae.brew.sh/formula/pinentry-mac
to be installed.
-d, --debug Display any debugging information.
-q, --quiet Make some output more quiet.
-v, --verbose Make some output more verbose.
-h, --help Show this message.
```
<!-- HELP-COMMAND-OUTPUT:END -->
**Logs of the performed operations can be found at:** `~/Library/Logs/com.github.domt4.homebrew-autoupdate`
Expand Down Expand Up @@ -77,4 +129,4 @@ the wider Homebrew leadership team in terms of maintenance/support requests.
## License
Code is under the [BSD 2 Clause (NetBSD) license](https://github.com/DomT4/homebrew-autoupdate/blob/master/LICENSE.txt).
Code is under the [BSD 2 Clause (NetBSD) license](https://github.com/DomT4/homebrew-autoupdate/blob/master/LICENSE.txt).

0 comments on commit e0cf8d7

Please sign in to comment.