|
| 1 | +name: Update README with Table |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + workflow_dispatch: # Allows manual triggering of the workflow |
| 8 | + |
| 9 | +jobs: |
| 10 | + update-readme: |
| 11 | + runs-on: ubuntu-latest |
| 12 | + |
| 13 | + steps: |
| 14 | + - name: Checkout repository |
| 15 | + uses: actions/checkout@v2 |
| 16 | + |
| 17 | + - name: Set up Node.js |
| 18 | + uses: actions/setup-node@v2 |
| 19 | + with: |
| 20 | + node-version: '14' |
| 21 | + |
| 22 | + - name: Generate repository folder information |
| 23 | + id: generate_table |
| 24 | + run: | |
| 25 | + # Set the starting "from" value at 50000 and maximum at 99999 |
| 26 | + START_FROM=50000 |
| 27 | + MAX_TO=99999 |
| 28 | +
|
| 29 | + # Create an empty array to store the rows |
| 30 | + ROWS=() |
| 31 | +
|
| 32 | + # Generate and sort rows on the fly |
| 33 | + mapfile -t SORTED_ROWS < <( |
| 34 | + find . -maxdepth 2 -type f -name "app.json" -exec dirname {} \; | while IFS= read -r folder; do |
| 35 | + # Extract "from", "to", and "name" values from app.json |
| 36 | + FROM=$(grep -m 1 '"from":' "$folder/app.json" | sed 's/[^0-9]*//g') |
| 37 | + TO=$(grep -m 1 '"to":' "$folder/app.json" | sed 's/[^0-9]*//g') |
| 38 | + NAME=$(grep -m 1 '"name":' "$folder/app.json" | sed 's/.*"name": "\(.*\)".*/\1/') |
| 39 | + |
| 40 | + if [ "$FROM" ] && [ "$TO" ] && [ "$NAME" ]; then |
| 41 | + # Print each row for sorting |
| 42 | + echo "$FROM:$TO:$NAME" |
| 43 | + fi |
| 44 | + done | sort -t':' -k1,1n |
| 45 | + ) |
| 46 | +
|
| 47 | + # Start building the table, keeping track of gaps |
| 48 | + TABLE="| App | From | To |\n|------------------------------------|--------|--------|" |
| 49 | + |
| 50 | + # Initialize the previous "to" to the starting point 50000 |
| 51 | + PREV_TO=$((START_FROM - 1)) |
| 52 | + |
| 53 | + for row in "${SORTED_ROWS[@]}"; do |
| 54 | + FROM=$(echo "$row" | cut -d':' -f1) |
| 55 | + TO=$(echo "$row" | cut -d':' -f2) |
| 56 | + NAME=$(echo "$row" | cut -d':' -f3) |
| 57 | +
|
| 58 | + # Check if there is a gap between the previous "to" and current "from" |
| 59 | + if [ "$PREV_TO" -lt "$((FROM - 1))" ]; then |
| 60 | + GAP_FROM=$((PREV_TO + 1)) |
| 61 | + GAP_TO=$((FROM - 1)) |
| 62 | + # Add an empty row for the gap |
| 63 | + TABLE+="\n| (empty) | $GAP_FROM | $GAP_TO |" |
| 64 | + fi |
| 65 | +
|
| 66 | + # Add the current row |
| 67 | + TABLE+="\n| $NAME | $FROM | $TO |" |
| 68 | + |
| 69 | + # Update PREV_TO |
| 70 | + PREV_TO=$TO |
| 71 | + done |
| 72 | +
|
| 73 | + # Check if there's a gap between the last "to" and the maximum (99999) |
| 74 | + if [ "$PREV_TO" -lt "$MAX_TO" ]; then |
| 75 | + GAP_FROM=$((PREV_TO + 1)) |
| 76 | + GAP_TO=$MAX_TO |
| 77 | + TABLE+="\n| (empty) | $GAP_FROM | $GAP_TO |" |
| 78 | + fi |
| 79 | +
|
| 80 | + # Write the table to table.md |
| 81 | + echo -e "$TABLE" > table.md |
| 82 | + cat table.md |
| 83 | + shell: bash |
| 84 | + |
| 85 | + - name: Create README.md if not exists |
| 86 | + run: | |
| 87 | + if [ ! -f README.md ]; then |
| 88 | + # Use echo -e to interpret \n as new lines |
| 89 | + echo -e "# This project contains several PTE modules." > README.md |
| 90 | + fi |
| 91 | +
|
| 92 | + - name: Update README.md with placeholder |
| 93 | + run: | |
| 94 | + # Log the current state of README.md for troubleshooting |
| 95 | + echo "Current README.md before update:" |
| 96 | + cat README.md |
| 97 | +
|
| 98 | + # Remove the old table (from start_table comment to end_table comment) and insert placeholder |
| 99 | + sed -i '/\[comment\]: <> (start_table)/,/\[comment\]: <> (end_table)/c [TABLE_PLACEHOLDER]' README.md |
| 100 | + |
| 101 | + # Verify if the placeholder has been inserted |
| 102 | + echo "README.md after inserting placeholder:" |
| 103 | + cat README.md |
| 104 | + |
| 105 | + # Insert the new table with the start and end comments around the placeholder |
| 106 | + sed -i '/\[TABLE_PLACEHOLDER\]/{ |
| 107 | + a [comment]: <> (start_table)\n |
| 108 | + r table.md |
| 109 | + a \\ |
| 110 | + a [comment]: <> (end_table) |
| 111 | + d |
| 112 | + }' README.md |
| 113 | + |
| 114 | + # Log the updated README.md to check if changes are made |
| 115 | + echo "README.md after updating table:" |
| 116 | + cat README.md |
| 117 | + |
| 118 | + # Forcefully stage README.md for commit |
| 119 | + git add README.md |
| 120 | + git diff --cached |
| 121 | + |
| 122 | + # Check if any changes have been made |
| 123 | + git status |
| 124 | +
|
| 125 | + - name: Check if README.md has changed |
| 126 | + id: check_diff |
| 127 | + run: | |
| 128 | + # Check if README.md has changes |
| 129 | + git diff --exit-code README.md || echo "README.md has been modified" |
| 130 | +
|
| 131 | + - name: Commit and push changes if README.md has changed |
| 132 | + if: steps.check_diff.outcome == 'success' |
| 133 | + run: | |
| 134 | + git config --local user.email "github-actions[bot]@users.noreply.github.com" |
| 135 | + git config --local user.name "GitHub Actions Bot" |
| 136 | + git commit -m "Update README.md with generated table" || echo "No changes to commit" |
| 137 | + git push || echo "Nothing to push" |
| 138 | + env: |
| 139 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments