-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate-dash-user-contributions.sh
More file actions
executable file
·230 lines (192 loc) · 6.88 KB
/
update-dash-user-contributions.sh
File metadata and controls
executable file
·230 lines (192 loc) · 6.88 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env bash
set -euo pipefail
# Automatically update the PHP docsets to the Kapeli/Dash-User-Contributions repository.
# This script is designed to be executed both locally and within a GitHub workflow.
ROOT="$(cd "$(dirname "$0")" && pwd)"
source "$ROOT/lib/common.sh"
FORK_REPO="${FORK_REPO:-ElfSundae/Dash-User-Contributions}"
UPSTREAM_REPO="Kapeli/Dash-User-Contributions"
OUTPUT="$ROOT/output"
DEV_MODE=false
if [[ " $* " == *" --dev "* ]]; then
DEV_MODE=true
# Use the fork as upstream in dev mode
UPSTREAM_REPO="$FORK_REPO"
fi
require_command md5sum tar jq gh
fork_owner="${FORK_REPO%%/*}"
fork_path="$OUTPUT/${FORK_REPO##*/}"
branch="update-php-docsets"
msg_main "Preparing the fork repository (${FORK_REPO})..."
if [[ ! -d "$fork_path" ]]; then
git clone "https://${GH_TOKEN:+"x-access-token:${GH_TOKEN}@"}github.com/${FORK_REPO}.git" "$fork_path"
else
(
cd "$fork_path"
git reset --hard
git clean -dxfq
)
fi
# Sync fork, checkout master branch
(
cd "$fork_path"
upstream_url="https://github.com/${UPSTREAM_REPO}.git"
if git remote get-url upstream >/dev/null 2>&1; then
git remote set-url upstream "$upstream_url"
else
git remote add upstream "$upstream_url"
fi
git fetch --all
git checkout master
git reset --hard upstream/master
git push origin master --force
)
# Clean up old branch
(
cd "$fork_path"
git branch -D "$branch" 2>/dev/null || true
exit_code=0
git push origin --delete "$branch" 2>/dev/null || exit_code=$?
# exit_code 0: branch deleted; 1: branch does not exist
if [[ $exit_code -eq 0 || $exit_code -eq 1 ]]; then
exit 0
fi
# Network or authentication error while deleting branch
exit $exit_code
) || {
msg_error "Failed to clean up old branch '${branch}', maybe due to network or authentication error."
exit 1
}
# Checkout new branch
(
cd "$fork_path"
git checkout -b "$branch"
)
commit_message="Update PHP docsets"
pr_body=$(cat <<EOF
This pull request updates the following PHP docsets:
| Docset | Version |
|--------|---------|
EOF
)
for lang in "${LANG_CODES[@]}"; do
if [[ "$lang" == "en" ]]; then
continue;
fi
docset_name="PHP_${lang}"
docset_filename="${docset_name}.docset"
docset_archive="${docset_name}.tgz"
docset="$OUTPUT/$docset_filename"
msg_main "Updating ${docset_filename}..."
if [[ "$DEV_MODE" == false || ! -d "$docset" ]]; then
if ! "$ROOT/generate.sh" "$lang" "$@" --output "$OUTPUT"; then
msg_error "Failed to generate Dash docset for language: $lang"
docset_archive_url="https://github.com/ElfSundae/dash-php/releases/download/docsets/${docset_archive}"
docset_archive_path="$OUTPUT/$docset_archive"
msg_main "Try to download existing docset: ${docset_archive_url}..."
if ! curl -fsL -o "$docset_archive_path" "$docset_archive_url"; then
msg_error "Failed to download existing docset: $docset_archive"
continue
fi
msg_main "Unarchiving ${docset_archive}..."
tar -xf "$docset_archive_path" -C "$OUTPUT" || {
msg_error "Failed to unarchive $docset_archive"
continue
}
fi
fi
docset_bundle_name=$(get_docset_bundle_name "$docset")
if [[ -z "$docset_bundle_name" ]]; then
msg_error "Failed to obtain docset bundle name."
continue
fi
msg_sub "$docset_filename bundle name: $docset_bundle_name"
localized_manual_title=$(xmllint --html --xpath 'string(//h1[@class="title"][1])' \
"$docset/Contents/Resources/Documents/index.html" 2>/dev/null) || {
msg_error "Failed to obtain localized manual title."
continue
}
msg_sub "$docset_filename localized manual title: $localized_manual_title"
msg_sub "Obtaining the docset version..."
version=$(get_docset_version "$docset")
if [[ -z "$version" ]]; then
msg_error "Failed to obtain the docset version."
continue
fi
msg_sub "$docset_filename version: $version"
# Check existing version in fork repository; skip if same version
existing_json="$fork_path/docsets/$docset_name/docset.json"
if [[ -f "$existing_json" ]]; then
existing_version=$(jq -r '.version // empty' "$existing_json" 2>/dev/null || true)
if [[ -n "$existing_version" && "$existing_version" == "$version" ]]; then
msg_sub "Same version ($version) already exists in fork, skipping ${docset_filename}."
continue
fi
fi
msg_sub "Archiving ${docset_filename}..."
(
cd "$OUTPUT"
rm -rf "$docset_archive"
tar --exclude='.DS_Store' --exclude='optimizedIndex.dsidx' -czf "$docset_archive" "$docset_filename"
) || {
msg_error "Failed to archive $docset_filename"
continue
}
msg_sub "Archived $docset_filename to $docset_archive"
msg_sub "Updating $docset_archive in the fork repository..."
(
cd "$fork_path"
root="docsets/$docset_name"
mkdir -p "$root"
cp -f "$OUTPUT/$docset_archive" "$root/"
cp -f "$docset/icon.png" "$root/"
cp -f "$docset/icon@2x.png" "$root/"
cp -f "$ROOT/assets/Dash-User-Contributions/README.md" "$root/"
jq --indent 4 -n \
--arg name "$docset_bundle_name" \
--arg version "$version" \
--arg archive "$docset_archive" \
'{
"name": $name,
"version": $version,
"archive": $archive,
"author": {
"name": "Elf Sundae",
"link": "https://github.com/ElfSundae"
},
"aliases": ["PHP", "PHP Manual", "PHP Documentation"]
}' > "$root/docset.json"
jq --indent 4 --arg title "$localized_manual_title" \
'.aliases |= (if index($title) then . else . + [$title] end)' "$root/docset.json" \
> temp.json && mv temp.json "$root/docset.json"
cat "$root/docset.json"
git add -A "$root"
) || {
msg_error "Failed to update $docset_archive in the fork repository."
continue
}
if ! git -C "$fork_path" diff --cached --quiet -- "docsets/$docset_name"; then
pr_body+=$'\n'"| ${docset_bundle_name} | \`${version}\` |"
fi
done
if git -C "$fork_path" diff --cached --quiet; then
msg_main "No changes detected, skipping pull request creation."
exit 0
fi
(
cd "$fork_path"
git commit -m "$commit_message"
git push -u origin "$branch"
)
msg_main "Creating pull request on ${UPSTREAM_REPO}..."
if ! pr_url=$(gh pr create \
--repo "$UPSTREAM_REPO" \
--base master \
--title "$commit_message" \
--body "$pr_body" \
--head "$fork_owner:$branch"
); then
msg_error "Failed to create pull request."
exit 9
fi
msg_sub "Pull request created: $pr_url"