-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.sh
More file actions
179 lines (145 loc) · 4.97 KB
/
Copy pathsync.sh
File metadata and controls
179 lines (145 loc) · 4.97 KB
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
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env bash
set -euo pipefail
# Configure Git author details
configure_git() {
git config --global user.name "github-actions[bot]"
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
}
# Copy central template files to target repository clone
sync_repository_files() {
local clone_dir="$1"
local repo_name="$2"
local has_config="false"
if [ -f "$clone_dir/.github/config.json" ]; then
has_config="true"
mv "$clone_dir/.github/config.json" "$clone_dir/.github/config.json.bak"
fi
cp -a template/. "$clone_dir/"
if [ "$has_config" = "true" ]; then
mv "$clone_dir/.github/config.json.bak" "$clone_dir/.github/config.json"
else
# If the target repo didn't have config.json, populate the template package name with repo_name
sed -i "s/__PACKAGE__/${repo_name}/g" "$clone_dir/.github/config.json"
fi
}
# Resolve and write release-please version manifest
release_please() {
# Fetch tags and resolve release version if inside a git repository
local latest_tag=""
if git rev-parse --is-inside-work-tree &> /dev/null; then
git fetch --tags || true
latest_tag=$(git describe --tags --abbrev=0 2> /dev/null || echo "")
fi
local latest_version="${latest_tag#v}"
latest_version="${latest_version:-0.0.0}"
sed -i "s/__VERSION__/$latest_version/g" .github/release-please-manifest.json
# Inject version-files list from config.json if defined
if [ -f ".github/config.json" ]; then
local version_files_json
version_files_json=$(jq -c '.["version-files"] // empty' .github/config.json 2> /dev/null || echo "")
if [ -n "$version_files_json" ] && [ "$version_files_json" != "[]" ]; then
jq --argjson files "$version_files_json" \
'.packages["."]["extra-files"] = $files' \
.github/release-please-config.json > tmp.json &&
mv tmp.json .github/release-please-config.json
fi
fi
npx prettier --write .github/release-please-config.json
}
# Parse config.json and populate OS matrix in test.yml
test_workflow() {
local test_yml=".github/workflows/test.yml"
if [ -f "$test_yml" ]; then
# Fetch raw OS list from config.json
local raw_os
raw_os=$(jq -r '.os[]?' .github/config.json 2>/dev/null | xargs || true)
# Fallback to all three OSes if empty/null
if [ -z "$raw_os" ]; then
raw_os="linux macos windows"
fi
# Map raw OS values to GitHub runner names
local os_list=""
for os in $raw_os; do
case "$os" in
linux) os_list="$os_list, \"ubuntu-latest\"" ;;
macos) os_list="$os_list, \"macos-latest\"" ;;
windows) os_list="$os_list, \"windows-latest\"" ;;
esac
done
# Format as JSON array string: ["ubuntu-latest", "macos-latest"]
os_list="[${os_list#, }]"
# Fallback if no runner was mapped
if [ "$os_list" = "[]" ]; then
os_list='["ubuntu-latest"]'
fi
sed -i "s/\[\"__OS_LIST__\"\]/$os_list/g" "$test_yml"
fi
}
# Resolve and write dynamic links in CONTRIBUTING.md
contributing_md() {
local repo_name="$1"
if [ -f "CONTRIBUTING.md" ]; then
local package_name
package_name=$(jq -r '.package // empty' .github/config.json 2>/dev/null || echo "")
package_name="${package_name:-$repo_name}"
sed -i -e "s/__REPO__/${repo_name}/g" \
-e "s/__PACKAGE__/${package_name}/g" \
CONTRIBUTING.md
npx prettier --write CONTRIBUTING.md
fi
}
# Commit and push changes if any diffs exist
commit_and_push() {
local repo_name="$1"
git add -A
if ! git diff-index --quiet HEAD; then
git commit -m "chore: auto-sync"
git push origin main
else
echo "No changes found for $repo_name. Skipping push."
fi
}
# Core file sync and processing logic for a repository directory
sync_directory() {
local target_dir="$1"
local repo_name="$2"
local script_dir="$3"
# Copy template files
sync_repository_files "$target_dir" "$repo_name"
cd "$target_dir"
# Process configurations and workflows
bash "$script_dir/rockspec.sh" "$repo_name"
release_please
test_workflow
bash "$script_dir/readme.sh" "$repo_name"
contributing_md "$repo_name"
}
main() {
local script_dir
script_dir=$(cd "$(dirname "$0")" && pwd)
# Discover target repositories (active repos, excluding .github and site repos)
local repos
repos=$(gh repo list BlueLua \
--limit 1000 \
--json name,isArchived \
-q '.[] | select(.isArchived == false and .name != ".github" and .name != "bluelua.github.io") | .name')
configure_git
for r in $repos; do
echo "=== Syncing repository: $r ==="
local clone_dir="/tmp/$r"
rm -rf "$clone_dir"
# Clone using the PAT token
git clone "https://x-access-token:${GH_TOKEN}@github.com/BlueLua/$r.git" "$clone_dir"
# Sync and process the repository files
sync_directory "$clone_dir" "$r" "$script_dir"
# Commit & push changes
cd "$clone_dir"
commit_and_push "$r"
# Clean up local clone
cd "$GITHUB_WORKSPACE"
rm -rf "$clone_dir"
done
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi