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