Skip to content

Commit 6f06de1

Browse files
authored
Create update_directory_md.yml
1 parent 01283e3 commit 6f06de1

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
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_md
3+
on: [push]
4+
jobs:
5+
update_directory_md:
6+
runs-on: ubuntu-latest
7+
steps:
8+
- uses: actions/checkout@v1
9+
- shell: python # Legacy Python 2.7.15 :-(
10+
run: import sys ; print("Python {}.{}.{}".format(*sys.version_info))
11+
- shell: bash -c "$RUNNER_TOOL_CACHE/Python/3.8.0/x64/python {0}"
12+
run: import sys ; print("Python {}.{}.{}".format(*sys.version_info))
13+
- shell: bash -c "$RUNNER_TOOL_CACHE/Python/3.8.0/x64/python {0}"
14+
run: |
15+
import os
16+
from typing import Iterator
17+
18+
URL_BASE = "https://github.com/TheAlgorithms/Java/blob/master"
19+
g_output = []
20+
21+
22+
def good_filepaths(top_dir: str = ".") -> Iterator[str]:
23+
for dirpath, dirnames, filenames in os.walk(top_dir):
24+
dirnames[:] = [d for d in dirnames if d[0] not in "._"]
25+
for filename in filenames:
26+
if os.path.splitext(filename)[1].lower() == ".java":
27+
yield os.path.join(dirpath, filename).lstrip("./")
28+
29+
30+
def md_prefix(i):
31+
return f"{i * ' '}*" if i else "\n##"
32+
33+
34+
def print_path(old_path: str, new_path: str) -> str:
35+
global g_output
36+
old_parts = old_path.split(os.sep)
37+
for i, new_part in enumerate(new_path.split(os.sep)):
38+
if i + 1 > len(old_parts) or old_parts[i] != new_part:
39+
if new_part:
40+
g_output.append(f"{md_prefix(i)} {new_part.replace('_', ' ')}")
41+
return new_path
42+
43+
44+
def build_directory_md(top_dir: str = ".") -> str:
45+
global g_output
46+
old_path = ""
47+
for filepath in sorted(good_filepaths(), key=str.lower):
48+
filepath, filename = os.path.split(filepath)
49+
if filepath != old_path:
50+
old_path = print_path(old_path, filepath)
51+
indent = (filepath.count(os.sep) + 1) if filepath else 0
52+
url = "/".join((URL_BASE, filepath, filename)).replace(" ", "%20")
53+
filename = os.path.splitext(filename.replace("_", " "))[0]
54+
g_output.append(f"{md_prefix(indent)} [{filename}]({url})")
55+
return "\n".join(g_output)
56+
57+
58+
with open("DIRECTORY.md", "w") as out_file:
59+
out_file.write(build_directory_md(".") + "\n")
60+
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 "updating DIRECTORY.md" || true
69+
git push --force origin HEAD:$GITHUB_REF || true

0 commit comments

Comments
 (0)