|
| 1 | +name: Update README File List |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + paths: |
| 8 | + - '**.php' |
| 9 | + |
| 10 | +jobs: |
| 11 | + update-readme: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + permissions: |
| 14 | + contents: write |
| 15 | + steps: |
| 16 | + - name: Checkout code |
| 17 | + uses: actions/checkout@v4 |
| 18 | + |
| 19 | + - name: Generate File List |
| 20 | + id: file_list |
| 21 | + run: | |
| 22 | + echo "Generating file list..." |
| 23 | + # Find all .php files, sort them, and format as Markdown links |
| 24 | + # Format: - [Filename](URL) |
| 25 | + FILES=$(find . -maxdepth 1 -name "*.php" -type f | sort | sed 's|^\./||' | while read -r file; do echo "- [📄 $file](https://github.com/${{ github.repository }}/blob/main/$file)"; done) |
| 26 | + |
| 27 | + # Export to environment variable (multiline safe) |
| 28 | + echo "FILES<<EOF" >> $GITHUB_ENV |
| 29 | + echo "$FILES" >> $GITHUB_ENV |
| 30 | + echo "EOF" >> $GITHUB_ENV |
| 31 | +
|
| 32 | + - name: Update README.md |
| 33 | + uses: actions/github-script@v7 |
| 34 | + with: |
| 35 | + script: | |
| 36 | + const fs = require('fs'); |
| 37 | + const readmePath = 'README.md'; |
| 38 | + const startMarker = '<!-- FILES_START -->'; |
| 39 | + const endMarker = '<!-- FILES_END -->'; |
| 40 | + |
| 41 | + if (fs.existsSync(readmePath)) { |
| 42 | + let content = fs.readFileSync(readmePath, 'utf8'); |
| 43 | + const newContent = `${process.env.FILES}`; |
| 44 | + |
| 45 | + const regex = new RegExp(`${startMarker}[\\s\\S]*?${endMarker}`); |
| 46 | + if (regex.test(content)) { |
| 47 | + const updatedContent = content.replace(regex, `${startMarker}\n${newContent}\n${endMarker}`); |
| 48 | + fs.writeFileSync(readmePath, updatedContent); |
| 49 | + console.log('README.md updated successfully.'); |
| 50 | + } else { |
| 51 | + console.log('Markers not found in README.md'); |
| 52 | + } |
| 53 | + } else { |
| 54 | + console.log('README.md not found'); |
| 55 | + } |
| 56 | +
|
| 57 | + - name: Commit and Push changes |
| 58 | + run: | |
| 59 | + git config --global user.name "github-actions[bot]" |
| 60 | + git config --global user.email "github-actions[bot]@users.noreply.github.com" |
| 61 | + git add README.md |
| 62 | + # Only commit if there are changes |
| 63 | + git diff --quiet && git diff --staged --quiet || (git commit -m "docs: auto-update file list in README" && git push) |
0 commit comments