Skip to content

Commit 21912ab

Browse files
authored
Create update_directory.yml
1 parent 089590a commit 21912ab

1 file changed

Lines changed: 69 additions & 0 deletions

File tree

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# This GitHub Action updates the DIRECTORY.md file (if needed) when doing a git push
2+
name: Update Directory
3+
on:
4+
push:
5+
paths:
6+
- 'src/**'
7+
- '**.yml'
8+
- '**.xml'
9+
- '**.Dockerfile'
10+
pull_request:
11+
paths:
12+
- 'src/**'
13+
- '**.yml'
14+
- '**.xml'
15+
- '**.Dockerfile'
16+
jobs:
17+
update_directory_md:
18+
runs-on: ubuntu-latest
19+
steps:
20+
- uses: actions/checkout@master
21+
- uses: actions/setup-python@v4
22+
with:
23+
python-version: '3.10'
24+
- name: Update Directory
25+
shell: python
26+
run: |
27+
import os
28+
from typing import Iterator
29+
URL_BASE = "https://github.com/hoangtien2k3qx1/Java/blob/main"
30+
g_output = []
31+
def good_filepaths(top_dir: str = ".") -> Iterator[str]:
32+
for dirpath, dirnames, filenames in os.walk(top_dir):
33+
dirnames[:] = [d for d in dirnames if d[0] not in "._"]
34+
for filename in filenames:
35+
if os.path.splitext(filename)[1].lower() == ".java":
36+
yield os.path.join(dirpath, filename).lstrip("./")
37+
def md_prefix(i):
38+
return f"{i * ' '}*" if i else "\n##"
39+
def print_path(old_path: str, new_path: str) -> str:
40+
global g_output
41+
old_parts = old_path.split(os.sep)
42+
for i, new_part in enumerate(new_path.split(os.sep)):
43+
if i + 1 > len(old_parts) or old_parts[i] != new_part:
44+
if new_part:
45+
g_output.append(f"{md_prefix(i)} {new_part.replace('_', ' ')}")
46+
return new_path
47+
def build_directory_md(top_dir: str = ".") -> str:
48+
global g_output
49+
old_path = ""
50+
for filepath in sorted(good_filepaths(), key=str.lower):
51+
filepath, filename = os.path.split(filepath)
52+
if filepath != old_path:
53+
old_path = print_path(old_path, filepath)
54+
indent = (filepath.count(os.sep) + 1) if filepath else 0
55+
url = "/".join((URL_BASE, filepath, filename)).replace(" ", "%20")
56+
filename = os.path.splitext(filename.replace("_", " "))[0]
57+
g_output.append(f"{md_prefix(indent)} [{filename}]({url})")
58+
return "\n".join(g_output)
59+
with open("DIRECTORY.md", "w") as out_file:
60+
out_file.write(build_directory_md(".") + "\n")
61+
- name: Update DIRECTORY.md
62+
run: |
63+
cat DIRECTORY.md
64+
git config --global user.name github-actions
65+
git config --global user.email '${GITHUB_ACTOR}@users.noreply.github.com'
66+
git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY
67+
git add DIRECTORY.md
68+
git commit -am "Directory.md" || true
69+
git push --force origin HEAD:$GITHUB_REF || true

0 commit comments

Comments
 (0)