Skip to content

Commit 94182ad

Browse files
Copilotswissspidy
andauthored
Convert PHP cloning scripts to bash, detect CPU cores for parallelism
Agent-Logs-Url: https://github.com/wp-cli/wp-cli-dev/sessions/28119b52-0b7a-445b-b43e-f3189c004e28 Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 55e87f8 commit 94182ad

5 files changed

Lines changed: 105 additions & 74 deletions

File tree

.maintenance/clone-all-repositories.php

Lines changed: 0 additions & 61 deletions
This file was deleted.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
6+
7+
SKIP_LIST=(
8+
"autoload-splitter"
9+
"composer-changelogs"
10+
"dash-docset-generator"
11+
"ideas"
12+
"package-index"
13+
"regenerate-readme"
14+
"sample-plugin"
15+
"wp-cli-dev"
16+
"wp-cli-roadmap"
17+
)
18+
19+
# Detect number of CPU cores, defaulting to 4.
20+
if command -v nproc &>/dev/null; then
21+
CORES=$(nproc)
22+
elif command -v sysctl &>/dev/null; then
23+
CORES=$(sysctl -n hw.logicalcpu 2>/dev/null || echo 4)
24+
else
25+
CORES=4
26+
fi
27+
28+
# Fetch repository list from the GitHub API.
29+
CURL_OPTS=(-s)
30+
if [[ -n "${GITHUB_TOKEN:-}" ]]; then
31+
CURL_OPTS+=(--header "Authorization: Bearer ${GITHUB_TOKEN}")
32+
fi
33+
34+
RESPONSE=$(curl "${CURL_OPTS[@]}" 'https://api.github.com/orgs/wp-cli/repos?per_page=100')
35+
36+
# Detect API errors such as rate limiting.
37+
if echo "${RESPONSE}" | jq -e '.message' &>/dev/null; then
38+
MESSAGE=$(echo "${RESPONSE}" | jq -r '.message')
39+
echo "GitHub responded with: ${MESSAGE}"
40+
echo "If you are running into a rate limiting issue during large events please set GITHUB_TOKEN environment variable."
41+
echo "See https://github.com/settings/tokens"
42+
exit 1
43+
fi
44+
45+
is_skipped() {
46+
local name="$1"
47+
for skip in "${SKIP_LIST[@]}"; do
48+
[[ "${skip}" == "${name}" ]] && return 0
49+
done
50+
return 1
51+
}
52+
53+
get_destination() {
54+
local name="$1"
55+
if [[ "${name}" == ".github" ]]; then
56+
echo "dot-github"
57+
else
58+
echo "${name}"
59+
fi
60+
}
61+
62+
CLONE_LIST=()
63+
UPDATE_FOLDERS=()
64+
65+
while IFS=$'\t' read -r name clone_url ssh_url; do
66+
if is_skipped "${name}"; then
67+
continue
68+
fi
69+
70+
destination=$(get_destination "${name}")
71+
72+
if [[ ! -d "${destination}" ]]; then
73+
if [[ -n "${GITHUB_ACTION:-}" ]]; then
74+
CLONE_LIST+=("${destination}"$'\t'"${clone_url}")
75+
else
76+
CLONE_LIST+=("${destination}"$'\t'"${ssh_url}")
77+
fi
78+
fi
79+
80+
UPDATE_FOLDERS+=("${destination}")
81+
done < <(echo "${RESPONSE}" | jq -r '.[] | [.name, .clone_url, .ssh_url] | @tsv')
82+
83+
if [[ ${#CLONE_LIST[@]} -gt 0 ]]; then
84+
printf '%s\n' "${CLONE_LIST[@]}" | xargs -n2 -P"${CORES}" bash "${SCRIPT_DIR}/clone-repository.sh"
85+
fi
86+
87+
if [[ ${#UPDATE_FOLDERS[@]} -gt 0 ]]; then
88+
printf '%s\n' "${UPDATE_FOLDERS[@]}" | xargs -n1 -P"${CORES}" -I% php "${SCRIPT_DIR}/refresh-repository.php" %
89+
fi

.maintenance/clone-repository.php

Lines changed: 0 additions & 11 deletions
This file was deleted.

.maintenance/clone-repository.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
if [[ $# -lt 2 ]]; then
6+
echo "Usage: clone-repository.sh <destination> <clone_url>" >&2
7+
exit 1
8+
fi
9+
10+
destination="$1"
11+
clone_url="$2"
12+
13+
printf "Fetching \033[32m%s\033[0m...\n" "${destination}"
14+
git clone "${clone_url}" "${destination}"

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,8 @@
319319
"minimum-stability": "dev",
320320
"prefer-stable": true,
321321
"scripts": {
322-
"pre-install-cmd": "php .maintenance/clone-all-repositories.php",
323-
"pre-update-cmd": "php .maintenance/clone-all-repositories.php",
322+
"pre-install-cmd": "bash .maintenance/clone-all-repositories.sh",
323+
"pre-update-cmd": "bash .maintenance/clone-all-repositories.sh",
324324
"post-install-cmd": [
325325
"php .maintenance/symlink-vendor-folders.php",
326326
"php .maintenance/phpstorm.exclude-recursive-folders.php"

0 commit comments

Comments
 (0)