Skip to content

Commit

Permalink
Implemented autonomous dir tree in the README
Browse files Browse the repository at this point in the history
  • Loading branch information
Cdaprod committed Mar 24, 2024
1 parent cda6b0c commit 22d650f
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
30 changes: 30 additions & 0 deletions .github/scripts/update_readme.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import re

def read_file(filename):
with open(filename, 'r', encoding='utf-8') as file:
return file.read()

def write_file(filename, content):
with open(filename, 'w', encoding='utf-8') as file:
file.write(content)

def update_readme(directory_tree, readme_contents):
# Define the start and end markers
start_marker = "<!-- DIRECTORY_TREE_START -->"
end_marker = "<!-- DIRECTORY_TREE_END -->"

# Define the pattern to find the existing directory tree section
pattern = re.compile(re.escape(start_marker) + ".*?" + re.escape(end_marker), re.DOTALL)

# Define the replacement content, including the markers
replacement = f"{start_marker}\n```\n{directory_tree}\n```\n{end_marker}"

# Perform the replacement
updated_contents = re.sub(pattern, replacement, readme_contents)

return updated_contents

directory_tree = read_file('DIRECTORY_TREE.txt')
readme_contents = read_file('README.md')
updated_readme = update_readme(directory_tree, readme_contents)
write_file('README.md', updated_readme)
48 changes: 48 additions & 0 deletions .github/workflows/update_readme.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Update README with Directory Tree

on:
push:
branches:
- main # or your default branch

jobs:
update-readme:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
persist-credentials: false # avoids persisting GitHub credentials

- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: '3.x'

- name: Install tree command or alternative
run: |
sudo apt-get update
sudo apt-get install -y tree
- name: Generate Directory Tree
run: |
tree -I '.git|node_modules|other-directories-to-exclude' > DIRECTORY_TREE.txt
- name: Update README.md
run: python .github/scripts/update_readme.py

- name: Commit changes
run: |
git config --local user.email "[email protected]"
git config --local user.name "Cdaprod"
git add README.md
if git diff --staged --quiet; then
echo "No changes to commit."
else
git commit -m "Automated README update: directory tree"
fi
- name: Push changes
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GH_TOKEN }}
branch: ${{ github.ref }}
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@

[![Build and Push Docker Images](https://github.com/Cdaprod/cda-deploy-to-swarm/actions/workflows/build-latest.yml/badge.svg)](https://github.com/Cdaprod/cda-deploy-to-swarm/actions/workflows/build-latest.yml)
[![Deploy Services](https://github.com/Cdaprod/cda-deploy-to-swarm/actions/workflows/deploy-latest.yml/badge.svg)](https://github.com/Cdaprod/cda-deploy-to-swarm/actions/workflows/deploy-latest.yml)

<!-- DIRECTORY_TREE_START -->
```
```
<!-- DIRECTORY_TREE_END -->

0 comments on commit 22d650f

Please sign in to comment.