-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #135 from swissbuechi/generate-help-command-output…
…-for-readme Generate help command output for readme with github action
- Loading branch information
Showing
3 changed files
with
153 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters