|
| 1 | +name: directory_md |
| 2 | +on: [push, pull_request] |
| 3 | + |
| 4 | +jobs: |
| 5 | + MainSequence: |
| 6 | + name: DIRECTORY.md |
| 7 | + runs-on: ubuntu-latest |
| 8 | + steps: |
| 9 | + - uses: actions/checkout@v1 # v2 is broken for git diff |
| 10 | + - uses: actions/setup-python@v2 |
| 11 | + - name: Setup Git Specs |
| 12 | + run: | |
| 13 | + git config --global user.name github-actions |
| 14 | + git config --global user.email '${GITHUB_ACTOR}@users.noreply.github.com' |
| 15 | + git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY |
| 16 | + - name: Update DIRECTORY.md |
| 17 | + shell: python |
| 18 | + run: | |
| 19 | + import os |
| 20 | + from typing import Iterator |
| 21 | + URL_BASE = "https://github.com/TheAlgorithms/MATLAB-Octave/blob/master" |
| 22 | + g_output = [] |
| 23 | + def good_filepaths(top_dir: str = ".") -> Iterator[str]: |
| 24 | + fs_exts = tuple(".m".split()) |
| 25 | + for dirpath, dirnames, filenames in os.walk(top_dir): |
| 26 | + dirnames[:] = [d for d in dirnames if d[0] not in "._"] |
| 27 | + for filename in filenames: |
| 28 | + if os.path.splitext(filename)[1].lower() in fs_exts: |
| 29 | + yield os.path.join(dirpath, filename).lstrip("./") |
| 30 | + def md_prefix(i): |
| 31 | + return f"{i * ' '}*" if i else "\n##" |
| 32 | + def print_path(old_path: str, new_path: str) -> str: |
| 33 | + global g_output |
| 34 | + old_parts = old_path.split(os.sep) |
| 35 | + for i, new_part in enumerate(new_path.split(os.sep)): |
| 36 | + if i + 1 > len(old_parts) or old_parts[i] != new_part: |
| 37 | + if new_part: |
| 38 | + g_output.append(f"{md_prefix(i)} {new_part.replace('_', ' ').title()}") |
| 39 | + return new_path |
| 40 | + def build_directory_md(top_dir: str = ".") -> str: |
| 41 | + global g_output |
| 42 | + old_path = "" |
| 43 | + for filepath in sorted(good_filepaths(), key=str.lower): |
| 44 | + filepath, filename = os.path.split(filepath) |
| 45 | + if filepath != old_path: |
| 46 | + old_path = print_path(old_path, filepath) |
| 47 | + indent = (filepath.count(os.sep) + 1) if filepath else 0 |
| 48 | + url = "/".join((URL_BASE, filepath, filename)).replace(" ", "%20") |
| 49 | + filename = os.path.splitext(filename.replace("_", " ").title())[0] |
| 50 | + g_output.append(f"{md_prefix(indent)} [{filename}]({url})") |
| 51 | + return "# List of all files\n" + "\n".join(g_output) |
| 52 | + with open("DIRECTORY.md", "w") as out_file: |
| 53 | + out_file.write(build_directory_md(".") + "\n") |
| 54 | + - name: Commit DIRECTORY.md |
| 55 | + run: | |
| 56 | + git commit -m "updating DIRECTORY.md" DIRECTORY.md || true |
| 57 | + git diff DIRECTORY.md |
| 58 | + git push --force origin HEAD:$GITHUB_REF || true |
0 commit comments