forked from material-components/material-components-ios
-
Notifications
You must be signed in to change notification settings - Fork 2
/
.kokoro
executable file
·419 lines (354 loc) · 13 KB
/
.kokoro
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#!/bin/bash
#
# Copyright 2017-present The Material Components for iOS Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Fail on any error.
set -e
KOKORO_RUNNER_VERSION="v4.*"
XCODE_MINIMUM_VERSION="8.3.0"
fix_bazel_imports() {
if [ -z "$KOKORO_BUILD_NUMBER" ]; then
tests_dir_prefix=""
else
tests_dir_prefix="github/repo/"
fi
private_components() {
if [ -z "$KOKORO_BUILD_NUMBER" ]; then
find "components/private" -type d | cut -d'/' -f3 | sort | uniq
else
find "github/repo/components/private" -type d | cut -d'/' -f5 | sort | uniq
fi
}
rewrite_tests() {
find "${stashed_dir}${tests_dir_prefix}"components/private/*/tests/unit -type f -name '*.swift' -exec perl -pi -e "$1" {} + || true
find "${stashed_dir}${tests_dir_prefix}"components/*/tests/unit -type f -name '*.swift' -exec perl -pi -e "$1" {} + || true
}
rewrite_source() {
find "${stashed_dir}${tests_dir_prefix}"components/private/*/src -type f -name '*.h' -exec perl -pi -e "$1" {} + || true
find "${stashed_dir}${tests_dir_prefix}"components/private/*/src -type f -name '*.m' -exec perl -pi -e "$1" {} + || true
find "${stashed_dir}${tests_dir_prefix}"components/*/src -type f -name '*.h' -exec perl -pi -e "$1" {} + || true
find "${stashed_dir}${tests_dir_prefix}"components/*/src -type f -name '*.m' -exec perl -pi -e "$1" {} + || true
}
stashed_dir=""
rewrite_tests "s/import MaterialComponents.Material(.+)_(.+)/import components_\1_\2/"
rewrite_tests "s/import MaterialComponents.Material(.+)Scheme/import components_schemes_\1_\1/"
private_components | while read private_component; do
if [ -z "$private_component" ]; then
continue
fi
rewrite_tests "s/import MaterialComponents.Material$private_component/import components_private_${private_component}_${private_component}/"
done
rewrite_tests "s/import MaterialComponents.Material(.+)/import components_\1_\1/"
rewrite_source "s/import <Motion(.+)\/Motion.+\.h>/import \"Motion\1.h\"/"
rewrite_source "s/import <MDF(.+)\/MDF.+\.h>/import \"MDF\1.h\"/"
rewrite_tests "s/import MDFTextAccessibility/import material_text_accessibility_ios_MDFTextAccessibility/"
rewrite_tests "s/import MDFInternationalization/import material_internationalization_ios_MDFInternationalization/"
stashed_dir="$(pwd)/"
reset_imports() {
# Undoes our source changes from above.
rewrite_tests "s/import components_schemes_(.+)_.+/import MaterialComponents.Material\1Scheme/"
rewrite_tests "s/import components_private_(.+)_.+/import MaterialComponents.Material\1/"
rewrite_tests "s/import components_(.+)_\1/import MaterialComponents.Material\1/"
rewrite_tests "s/import components_(.+)_(.+)/import MaterialComponents.Material\1_\2/"
rewrite_source "s/import \"Motion(.+)\.h\"/import <Motion\1\/Motion\1.h>/"
rewrite_source "s/import \"MDF(.+)\.h\"/import <MDF\1\/MDF\1.h>/"
rewrite_tests "s/import material_text_accessibility_ios_MDFTextAccessibility/import MDFTextAccessibility/"
rewrite_tests "s/import material_internationalization_ios_MDFInternationalization/import MDFInternationalization/"
}
trap reset_imports EXIT
}
version_as_number() {
padded_version="${1%.}" # Strip any trailing dots
# Pad with .0 until we get a M.m.p version string.
while [ $(grep -o "\." <<< "$padded_version" | wc -l) -lt "2" ]; do
padded_version=${padded_version}.0
done
echo "${padded_version//.}"
}
run_bazel() {
echo "Running bazel builds..."
fix_bazel_imports
if [ -n "$KOKORO_BUILD_NUMBER" ]; then
bazel version
fi
if [ -n "$VERBOSE_OUTPUT" ]; then
verbosity_args="-v"
fi
if [ -z "$COMMAND" ]; then
COMMAND="test"
fi
if [ -z "$TARGET" ]; then
TARGET="//components/..."
fi
if [ -n "$XCODE_VERSION" ]; then
xcode_versions="--xcode-version $XCODE_VERSION"
else
xcode_versions="--min-xcode-version $XCODE_MINIMUM_VERSION"
fi
./.kokoro-ios-runner/bazel.sh $COMMAND $TARGET \
$xcode_versions \
$verbosity_args \
--ios_minimum_os=8.0 \
--ios_multi_cpus=i386,x86_64
}
run_cocoapods() {
echo "Running cocoapods builds..."
xcode_min_version_as_number="$(version_as_number $XCODE_MINIMUM_VERSION)"
if [ -n "$KOKORO_BUILD_NUMBER" ]; then
# Move into our cloned repo
cd github/repo
gem install xcpretty cocoapods --no-rdoc --no-ri --no-document --quiet
pod --version
fi
BUILDS_TMP_PATH=$(mktemp -d)
SEEN_XCODES_FILE_PATH="$BUILDS_TMP_PATH/seen_xcodes"
touch "$SEEN_XCODES_FILE_PATH"
xcodes=$(ls /Applications/ | grep "Xcode")
for xcode_path in $xcodes; do
xcode_version=$(cat /Applications/$xcode_path/Contents/version.plist \
| grep "CFBundleShortVersionString" -A1 \
| grep string \
| cut -d'>' -f2 \
| cut -d'<' -f1)
xcode_version_as_number="$(version_as_number $xcode_version)"
# Ignore duplicate Xcode installations
if grep -xq "$xcode_version_as_number" "$SEEN_XCODES_FILE_PATH"; then
continue
fi
echo "$xcode_version_as_number" >> "$SEEN_XCODES_FILE_PATH"
if [ -n "$XCODE_VERSION" ]; then
if [ "$xcode_version_as_number" -ne "$(version_as_number $XCODE_VERSION)" ]; then
continue
fi
elif [ -n "$xcode_min_version_as_number" ]; then
if [ "$xcode_version_as_number" -lt "$xcode_min_version_as_number" ]; then
continue
fi
fi
sudo xcode-select --switch /Applications/$xcode_path/Contents/Developer
xcodebuild -version
# Resolves the following crash when switching Xcode versions:
# "Failed to locate a valid instance of CoreSimulatorService in the bootstrap"
launchctl remove com.apple.CoreSimulator.CoreSimulatorService || true
if [ "$DEPENDENCY_SYSTEM" = "cocoapods" ]; then
bash scripts/prep_all
bash scripts/build_all --verbose
if [ -n "$IS_RELEASE" ]; then
bash scripts/test_all
else
bash scripts/test_all catalog/MDCCatalog.xcworkspace:MDCUnitTests
fi
elif [ "$DEPENDENCY_SYSTEM" = "cocoapods-podspec" ]; then
pod lib lint MaterialComponents.podspec
fi
done
bash <(curl -s https://codecov.io/bash)
}
# For local runs, you must set the following environment variables:
#
# DANGER_GITHUB_API_TOKEN -> Create a token here: https://github.com/settings/tokens.
# Must have public_repo scope.
# DANGER_TEST_PR="###" -> The PR # you want to test danger against.
#
# And you'll likely have to install danger:
#
# yarn add danger
#
run_danger() {
if [ -n "$KOKORO_BUILD_NUMBER" ]; then
# Move into our cloned repo
cd github/repo
export DANGER_TEST_PR="$KOKORO_GITHUB_PULL_REQUEST_NUMBER"
# Install danger
yarn add danger
fi
# We're not a supported CI, so we have to pretend to be one.
export DANGER_FAKE_CI="YEP"
export DANGER_TEST_REPO="material-components/material-components-ios"
# Run Danger
yarn danger ci
}
generate_website() {
if [ -n "$KOKORO_BUILD_NUMBER" ]; then
# Move into our cloned repo
cd github/repo
./scripts/build_site.sh
gem install bundler --no-rdoc --no-ri --no-document --quiet
brew update
brew install yarn --without-node
fi
cd docsite-generator
bundle install
yarn install
cd ../
TMP_PATH=$(mktemp -d)
./scripts/build_site.sh 2>&1 | tee "$TMP_PATH/output"
if [[ $(grep "^Error:" "$TMP_PATH/output") ]]; then
# Make site generation errors be script failures. build_site.sh always exits with 0
# unfortunately.
exit 1
fi
}
# Will generate an API diff between the current branch and the merge-base from develop.
# The result will be posted to GitHub as a comment.
#
# For local runs, you must set the following environment variables:
#
# GITHUB_API_TOKEN -> Create a token here: https://github.com/settings/tokens.
# Must have public_repo scope.
# KOKORO_GITHUB_PULL_REQUEST_NUMBER="###" -> The PR # you want to post the API diff results to.
#
# And you'll likely have to install sourcekitten:
#
# brew install sourcekitten
#
generate_apidiff() {
if [ -n "$KOKORO_BUILD_NUMBER" ]; then
# Move into our cloned repo
cd github/repo
# Install sourcekitten, a dependency of the apidiff tool
brew install sourcekitten
fi
usage() {
echo "Usage: $0 -d apidiff"
echo
echo "Will generate an API diff between the current branch and the merge-base from develop."
echo "The result will be posted to GitHub as a comment."
echo
echo "Must set the following environment variables to run locally:"
echo
echo "GITHUB_API_TOKEN -> Create a token here: https://github.com/settings/tokens."
echo " Must have public_repo scope."
echo "KOKORO_GITHUB_PULL_REQUEST_NUMBER=\"###\" -> The PR # you want to post the API diff results to."
}
if [ -z "$GITHUB_API_TOKEN" ]; then
echo "GITHUB_API_TOKEN must be set to a github token with public_repo scope."
usage
exit 1
fi
if [ -z "$KOKORO_GITHUB_PULL_REQUEST_NUMBER" ]; then
echo "KOKORO_GITHUB_PULL_REQUEST_NUMBER must be set to a github pull request number."
usage
exit 1
fi
BUILDS_TMP_PATH=$(mktemp -d)
SEEN_XCODES_FILE_PATH="$BUILDS_TMP_PATH/seen_xcodes"
touch "$SEEN_XCODES_FILE_PATH"
xcodes=$(ls /Applications/ | grep "Xcode")
for xcode_path in $xcodes; do
xcode_version=$(cat /Applications/$xcode_path/Contents/version.plist \
| grep "CFBundleShortVersionString" -A1 \
| grep string \
| cut -d'>' -f2 \
| cut -d'<' -f1)
xcode_version_as_number="$(version_as_number $xcode_version)"
# Ignore duplicate Xcode installations
if grep -xq "$xcode_version_as_number" "$SEEN_XCODES_FILE_PATH"; then
continue
fi
echo "$xcode_version_as_number" >> "$SEEN_XCODES_FILE_PATH"
if [ "$xcode_version_as_number" -ne "$(version_as_number $XCODE_VERSION)" ]; then
continue
fi
sudo xcode-select --switch /Applications/$xcode_path/Contents/Developer
xcodebuild -version
# Resolves the following crash when switching Xcode versions:
# "Failed to locate a valid instance of CoreSimulatorService in the bootstrap"
launchctl remove com.apple.CoreSimulator.CoreSimulatorService || true
base_sha=$(git merge-base origin/develop HEAD)
./scripts/release apidiff "$base_sha" | tee "$BUILDS_TMP_PATH/api_diff"
changelog_path=$(cat "$BUILDS_TMP_PATH/api_diff" | grep "Changelog=" | cut -d'=' -f2)
error_path=$(cat "$BUILDS_TMP_PATH/api_diff" | grep "Errors=" | cut -d'=' -f2)
pushd scripts/github-comment >> /dev/null
if [ -f "$changelog_path" ]; then
echo "## API diff detected the following changes" > "$changelog_path.tmp"
cat "$changelog_path" >> "$changelog_path.tmp"
swift run github-comment \
--repo=material-components/material-components-ios \
--github_token="$GITHUB_API_TOKEN" \
--pull_request_number="$KOKORO_GITHUB_PULL_REQUEST_NUMBER" \
--identifier=apidiff \
--comment_body="$changelog_path.tmp"
else
# No changelog, so delete any existing comment
swift run github-comment \
--repo=material-components/material-components-ios \
--github_token="$GITHUB_API_TOKEN" \
--pull_request_number="$KOKORO_GITHUB_PULL_REQUEST_NUMBER" \
--identifier=apidiff \
--delete
fi
popd >> /dev/null
if [ -f "$error_path" ]; then
nested_error_path=$(cat "$error_path" | grep "stderr output is available in" | cut -d' ' -f6)
if [ -f "$nested_error_path" ]; then
echo
echo "Potential build errors:"
cat "$nested_error_path"
fi
fi
done
}
POSITIONAL=()
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-v|--verbose)
VERBOSE_OUTPUT="1"
shift
;;
-d|--dependency_system)
DEPENDENCY_SYSTEM="$2"
shift
shift
;;
-c|--command)
COMMAND="$2"
shift
shift
;;
-t|--target)
TARGET="$2"
shift
shift
;;
*)
POSITIONAL+=("$1")
shift
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
if [ -n "$VERBOSE_OUTPUT" ]; then
# Display commands to stderr.
set -x
fi
if [ ! -d .kokoro-ios-runner ]; then
git clone https://github.com/material-foundation/kokoro-ios-runner.git .kokoro-ios-runner
fi
pushd .kokoro-ios-runner
git fetch > /dev/null
TAG=$(git tag --sort=v:refname -l "$KOKORO_RUNNER_VERSION" | tail -n1)
git checkout "$TAG" > /dev/null
popd
case "$DEPENDENCY_SYSTEM" in
"bazel") run_bazel ;;
"cocoapods") run_cocoapods ;;
"cocoapods-podspec") run_cocoapods ;;
"website") generate_website ;;
"danger") run_danger ;;
"apidiff") generate_apidiff ;;
*) run_bazel ;;
esac
echo "Success!"