|
1 |
| -name:Directory writer |
2 |
| -on: |
3 |
| -push: |
4 |
| -branches: |
5 |
| - -master |
6 |
| -schedule: |
7 |
| -# ┌───────────── minute (0 - 59) |
8 |
| -# │ ┌───────────── hour (0 - 23) |
9 |
| -# │ │ ┌───────────── day of the month (1 - 31) |
10 |
| -# │ │ │ ┌───────────── month (1 - 12 or JAN-DEC) |
11 |
| -# │ │ │ │ ┌───────────── day of the week (0 - 6 or SUN-SAT) |
12 |
| -# │ │ │ │ │ |
13 |
| -# │ │ │ │ │ |
14 |
| -# │ │ │ │ │ |
15 |
| -# * * * * * |
16 |
| - -cron:'0 0 * * 1' |
| 1 | +name:update_directory_md |
| 2 | +on:[push] |
17 | 3 | jobs:
|
18 |
| -build: |
19 |
| -if:github.repository == 'TheAlgorithms/Ruby'# We only need this to run in our repository. |
| 4 | +update_directory_md: |
20 | 5 | runs-on:ubuntu-latest
|
21 | 6 | steps:
|
22 |
| - -uses:actions/checkout@v3 |
23 |
| -with: |
24 |
| -fetch-depth:0 |
25 |
| - -name:Build directory |
26 |
| -uses:TheAlgorithms/scripts/directory_md@main |
27 |
| -with: |
28 |
| -language:Ruby |
29 |
| -working-directory:. |
30 |
| -filetypes:.rb |
| 7 | + -uses:actions/checkout@master |
| 8 | + -uses:actions/setup-python@master |
| 9 | + -name:update_directory_md |
| 10 | +shell:python |
| 11 | +run:| |
| 12 | + import os |
| 13 | + from typing import Iterator |
| 14 | + URL_BASE = "https://github.com/TheAlgorithms/Ruby/blob/master" |
| 15 | + g_output = [] |
| 16 | +
|
| 17 | + def good_filepaths(top_dir: str = ".") -> Iterator[str]: |
| 18 | + for dirpath, dirnames, filenames in os.walk(top_dir): |
| 19 | + dirnames[:] = [d for d in dirnames if d[0] not in "._"] |
| 20 | + for filename in filenames: |
| 21 | + if os.path.splitext(filename)[1].lower() == ".rb": |
| 22 | + yield os.path.join(dirpath, filename).lstrip("./") |
| 23 | +
|
| 24 | + def md_prefix(i): |
| 25 | + return f"{i * ' '}*" if i else "\n##" |
| 26 | +
|
| 27 | + def print_path(old_path: str, new_path: str) -> str: |
| 28 | + global g_output |
| 29 | + old_parts = old_path.split(os.sep) |
| 30 | + for i, new_part in enumerate(new_path.split(os.sep)): |
| 31 | + if i + 1 > len(old_parts) or old_parts[i] != new_part: |
| 32 | + if new_part: |
| 33 | + g_output.append(f"{md_prefix(i)} {new_part.replace('_', ' ').title()}") |
| 34 | + return new_path |
| 35 | +
|
| 36 | + def build_directory_md(top_dir: str = ".") -> str: |
| 37 | + global g_output |
| 38 | + old_path = "" |
| 39 | + for filepath in sorted(good_filepaths(), key=str.lower): |
| 40 | + filepath, filename = os.path.split(filepath) |
| 41 | + if filepath != old_path: |
| 42 | + old_path = print_path(old_path, filepath) |
| 43 | + indent = (filepath.count(os.sep) + 1) if filepath else 0 |
| 44 | + url = "/".join((URL_BASE, filepath, filename)).replace(" ", "%20") |
| 45 | + filename = os.path.splitext(filename.replace("_", " ").title())[0] |
| 46 | + g_output.append(f"{md_prefix(indent)} [{filename}]({url})") |
| 47 | + return "\n".join(g_output) |
| 48 | + with open("DIRECTORY.md", "w") as out_file: |
| 49 | + out_file.write(build_directory_md(".") + "\n") |
| 50 | +
|
| 51 | + -name:Update DIRECTORY.md |
| 52 | +run:| |
| 53 | + cat DIRECTORY.md |
| 54 | + git config --global user.name github-actions |
| 55 | + git config --global user.email '${GITHUB_ACTOR}@users.noreply.github.com' |
| 56 | + git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY |
| 57 | + git add DIRECTORY.md |
| 58 | + git commit -am "updating DIRECTORY.md" ||true |
| 59 | + git push --force origin HEAD:$GITHUB_REF ||true |