blob: 7038c231d46e7417b88beb7d454d37a163dd74ca [file] [log] [blame] [edit]
#!/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
COCOAPODS_VERSION="1.6.0"
# Fix permissions issue with Ruby Gems (b/128326105)
if [ -n "$KOKORO_BUILD_NUMBER" ]; then
sudo chown -Rh $(whoami) /Library/Ruby/Gems
fi
if [ -n "$KOKORO_BUILD_NUMBER" ]; then
repo_dir='github/repo'
else
repo_dir="$(dirname $0)"
fi
repo_dir="$(cd $repo_dir; pwd)"
scripts_dir="${repo_dir}/scripts/lib"
source "${scripts_dir}/github_comments.sh"
source "${scripts_dir}/select_xcode.sh"
source "${scripts_dir}/package_managers.sh"
# To install homebrew formulas at specific versions we need to point directly
# to the desired sha in the homebrew formula repository.
# See https://danepowell.com/blog/homebrew-formula-versions for more details.
# This currently points to version 0.21.3, the last version that supports Xcode 9
SOURCEKITTEN_FORMULA="https://raw.githubusercontent.com/Homebrew/homebrew-core/c5a8a094f9e1dd8e41ed24785acef07ef7092d0d/Formula/sourcekitten.rb"
fix_bazel_imports() {
echo "Rewriting imports for bazel..."
private_components() {
find "components/private" -type d | cut -d'/' -f3 | sort | uniq
}
rewrite_tests() {
find "${stashed_dir}"components/private/*/tests -type f -name '*.swift' -exec perl -pi -e "$1" {} + || true
find "${stashed_dir}"components/private/*/tests -type f -name '*.m' -exec perl -pi -e "$1" {} + || true
find "${stashed_dir}"components/*/tests -type f -name '*.swift' -exec perl -pi -e "$1" {} + || true
find "${stashed_dir}"components/*/tests -type f -name '*.m' -exec perl -pi -e "$1" {} + || true
find "${stashed_dir}"components/schemes/*/tests -type f -name '*.m' -exec perl -pi -e "$1" {} + || true
}
rewrite_examples() {
find "${stashed_dir}"components/private/*/examples -type f -name '*.swift' -exec perl -pi -e "$1" {} + || true
find "${stashed_dir}"components/private/*/examples -type f -name '*.h' -exec perl -pi -e "$1" {} + || true
find "${stashed_dir}"components/private/*/examples -type f -name '*.m' -exec perl -pi -e "$1" {} + || true
find "${stashed_dir}"components/*/examples -type f -name '*.swift' -exec perl -pi -e "$1" {} + || true
find "${stashed_dir}"components/*/examples -type f -name '*.h' -exec perl -pi -e "$1" {} + || true
find "${stashed_dir}"components/*/examples -type f -name '*.m' -exec perl -pi -e "$1" {} + || true
}
rewrite_source() {
find "${stashed_dir}"components/private/*/src -type f -name '*.h' -exec perl -pi -e "$1" {} + || true
find "${stashed_dir}"components/private/*/src -type f -name '*.m' -exec perl -pi -e "$1" {} + || true
find "${stashed_dir}"components/*/src -type f -name '*.h' -exec perl -pi -e "$1" {} + || true
find "${stashed_dir}"components/*/src -type f -name '*.m' -exec perl -pi -e "$1" {} + || true
find "${stashed_dir}"components/schemes/*/src -type f -name '*.h' -exec perl -pi -e "$1" {} + || true
find "${stashed_dir}"components/schemes/*/src -type f -name '*.m' -exec perl -pi -e "$1" {} + || true
}
rewrite_catalogs() {
find "${stashed_dir}"catalog/MDC* -type f -name '*.swift' -exec perl -pi -e "$1" {} + || true
find "${stashed_dir}"catalog/MDC* -type f -name '*.m' -exec perl -pi -e "$1" {} + || true
find "${stashed_dir}"catalog/MDC* -type f -name '*.h' -exec perl -pi -e "$1" {} + || true
}
stashed_dir=""
rewrite_tests "s/import MaterialComponents.Material(.+)_(.+)/import components_\1_\2/"
rewrite_tests "s/import MaterialComponentsBeta.Material(.+)Beta/import components_\1_\1Beta \/\/ BetaSplit/"
rewrite_tests "s/import MaterialComponents.Material(.+)Scheme/import components_schemes_\1_\1/"
rewrite_catalogs "s/import MaterialCatalog/import catalog_MaterialCatalog/"
rewrite_catalogs "s/import MaterialComponents.MaterialIcons_(.+)/import components_private_Icons_icons_\1_\1/"
rewrite_catalogs "s/import MaterialComponents.Material(.+)_(.+)/import components_\1_\2/"
rewrite_catalogs "s/import MaterialComponents.Material(.+)Scheme/import components_schemes_\1_\1/"
rewrite_examples "s/import <MaterialComponentsBeta\/(.+)>/import\/\*Material beta framework import\*\/ \"\1\"/"
rewrite_examples "s/import MaterialComponents.Material(.+)_Private/import components_\1_Private/"
rewrite_examples "s/import MaterialComponents.Material(.+)_(.+)/import components_\1_\2/"
rewrite_examples "s/import MaterialComponentsBeta.Material(.+)Beta/import components_\1_\1Beta \/\/ BetaSplit/"
rewrite_examples "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}/"
rewrite_examples "s/import MaterialComponents.Material$private_component/import components_private_${private_component}_${private_component}/"
rewrite_catalogs "s/import MaterialComponents.Material$private_component\\b/import components_private_${private_component}_${private_component}/"
done
rewrite_tests "s/import MaterialComponentsBeta.Material(.+)_(.+)/import components_\1_\2 \/\/ Beta/"
rewrite_tests "s/import MaterialComponentsBeta.Material(.+)/import components_\1_\1 \/\/ Beta/"
rewrite_tests "s/import MaterialComponents.Material(.+)/import components_\1_\1/"
rewrite_catalogs "s/import MaterialComponentsBeta.Material(.+)_(.+)/import components_\1_\2 \/\/ Beta/"
rewrite_catalogs "s/import MaterialComponentsBeta.Material(.+)/import components_\1_\1 \/\/ Beta/"
rewrite_catalogs "s/import MaterialComponents.Material(.+)/import components_\1_\1/"
rewrite_catalogs "s/import CatalogByConvention/import catalog_by_convention_CatalogByConvention/"
rewrite_catalogs "s/import MDFInternationalization/import material_internationalization_ios_MDFInternationalization/"
rewrite_catalogs "s/import MDFTextAccessibility/import material_text_accessibility_ios_MDFTextAccessibility/"
rewrite_catalogs "s/import <CatalogByConvention\/CatalogByConvention\.h>/import\/\*Material prefix framework import\*\/ \"CatalogByConvention.h\"/"
rewrite_examples "s/import <MaterialComponents\/Material(.+)\.h>/import\/\*Material prefix framework import\*\/ \"Material\1.h\"/"
rewrite_examples "s/import MaterialComponentsBeta.Material(.+)_(.+)/import components_\1_\2 \/\/ Beta/"
rewrite_examples "s/import MaterialComponentsBeta.Material(.+)/import components_\1_\1 \/\/ Beta/"
rewrite_examples "s/import MaterialComponents.Material(.+)/import components_\1_\1/"
rewrite_examples "s/import MDFTextAccessibility/import material_text_accessibility_ios_MDFTextAccessibility/"
rewrite_source "s/import <MaterialComponentsBeta\/(.+)>/import\/\*Material beta framework import\*\/ \"\1\"/"
rewrite_source "s/import <Motion(.+)\/Motion.+\.h>/import \"Motion\1.h\"/"
rewrite_source "s/import <MDFTextAccessibility\/MDFTextAccessibility\.h>/import \"MDFTextAccessibility.h\"/"
rewrite_source "s/import <MaterialComponents\/Material(.+)\.h>/import\/\*Material prefix framework import\*\/ \"Material\1.h\"/"
rewrite_source "s/import <MaterialComponents\/MDC(.+)\.h>/import\/\*MDC prefix framework import\*\/ \"MDC\1.h\"/"
rewrite_tests "s/import <MaterialComponentsBeta\/Material(.+)Beta\.h>/import\/\*Material beta prefix framework import\*\/ \"Material\1Beta.h\"/"
rewrite_tests "s/import <MaterialComponents\/Material(.+)\.h>/import\/\*Material prefix framework import\*\/ \"Material\1.h\"/"
rewrite_tests "s/import <MDFTesting\/MDFTesting\.h>/import \"MDFTesting.h\"/"
rewrite_tests "s/import MDFTesting/import material_testing_ios_MDFTesting/"
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() {
echo "Undoing import rewrites for bazel..."
# Undoes our source changes from above.
rewrite_tests "s/import\/\*Material beta prefix framework import\*\/ \"Material(.+).h\"/import <MaterialComponentsBeta\/Material\1\.h>/"
rewrite_tests "s/import components_(.+)_\1 \/\/ Beta/import MaterialComponentsBeta.Material\1/"
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_(.+)_\1Beta \/\/ BetaSplit/import MaterialComponentsBeta.Material\1Beta/"
rewrite_tests "s/import components_(.+)_(.+) \/\/ Beta/import MaterialComponentsBeta.Material\1_\2/"
rewrite_tests "s/import components_(.+)_(.+)/import MaterialComponents.Material\1_\2/"
rewrite_catalogs "s/import\/\*Material prefix framework import\*\/ \"CatalogByConvention\.h\"/import <CatalogByConvention\/CatalogByConvention\.h>/"
rewrite_catalogs "s/import catalog_MaterialCatalog/import MaterialCatalog/"
rewrite_catalogs "s/import components_private_Icons_icons_(.+)_\1/import MaterialComponents.MaterialIcons_\1/"
rewrite_catalogs "s/import components_(.+)_\1 \/\/ Beta/import MaterialComponentsBeta.Material\1/"
rewrite_catalogs "s/import components_schemes_(.+)_.+/import MaterialComponents.Material\1Scheme/"
rewrite_catalogs "s/import components_private_(.+)_.+/import MaterialComponents.Material\1/"
rewrite_catalogs "s/import components_(.+)_\1/import MaterialComponents.Material\1/"
rewrite_catalogs "s/import components_(.+)_(.+) \/\/ Beta/import MaterialComponentsBeta.Material\1_\2/"
rewrite_catalogs "s/import components_(.+)_(.+)/import MaterialComponents.Material\1_\2/"
rewrite_catalogs "s/import catalog_by_convention_CatalogByConvention/import CatalogByConvention/"
rewrite_catalogs "s/import material_internationalization_ios_MDFInternationalization/import MDFInternationalization/"
rewrite_catalogs "s/import material_text_accessibility_ios_MDFTextAccessibility/import MDFTextAccessibility/"
rewrite_examples "s/import\/\*Material beta framework import\*\/ \"(.+)\"/import <MaterialComponentsBeta\/\1>/"
rewrite_examples "s/import components_(.+)_\1Beta \/\/ BetaSplit/import MaterialComponentsBeta.Material\1Beta/"
rewrite_examples "s/import components_(.+)_\1 \/\/ Beta/import MaterialComponentsBeta.Material\1/"
rewrite_examples "s/import components_schemes_(.+)_.+/import MaterialComponents.Material\1Scheme/"
rewrite_examples "s/import components_private_(.+)_.+/import MaterialComponents.Material\1/"
rewrite_examples "s/import components_(.+)_Private/import MaterialComponents.Material\1_Private/"
rewrite_examples "s/import components_(.+)_\1/import MaterialComponents.Material\1/"
rewrite_examples "s/import components_(.+)_(.+) \/\/ Beta/import MaterialComponentsBeta.Material\1_\2/"
rewrite_examples "s/import components_(.+)_(.+)/import MaterialComponents.Material\1_\2/"
rewrite_examples "s/import material_text_accessibility_ios_MDFTextAccessibility/import MDFTextAccessibility/"
rewrite_examples "s/import\/\*Material prefix framework import\*\/ \"Material(.+)\.h\"/import <MaterialComponents\/Material\1\.h>/"
rewrite_source "s/import\/\*Material beta framework import\*\/ \"(.+)\"/import <MaterialComponentsBeta\/\1>/"
rewrite_source "s/import \"Motion(.+)\.h\"/import <Motion\1\/Motion\1.h>/"
rewrite_source "s/import \"MDFTextAccessibility\.h\"/import <MDFTextAccessibility\/MDFTextAccessibility.h>/"
rewrite_source "s/import\/\*Material prefix framework import\*\/ \"Material(.+)\.h\"/import <MaterialComponents\/Material\1\.h>/"
rewrite_source "s/import\/\*MDC prefix framework import\*\/ \"MDC(.+)\.h\"/import <MaterialComponents\/MDC\1\.h>/"
rewrite_tests "s/import\/\*Material prefix framework import\*\/ \"Material(.+)\.h\"/import <MaterialComponents\/Material\1\.h>/"
rewrite_tests "s/import \"MDFTesting\.h\"/import <MDFTesting\/MDFTesting.h>/"
rewrite_tests "s/import material_testing_ios_MDFTesting/import MDFTesting/"
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
}
move_derived_data_to_tmp() {
targetDir="${HOME}/Library/Developer/Xcode/DerivedData"
if [[ -d "$targetDir" ]]; then
mv "$targetDir" /tmpfs/
ln -sf /tmpfs/DerivedData "$targetDir"
fi
}
# Uploads all of the bazel test artifacts to Kokoro's artifacts storage.
upload_bazel_test_artifacts() {
logs_dir="$KOKORO_ARTIFACTS_DIR/bazel_test_logs"
mkdir -p "$logs_dir"
# Copies each file from stdin to $KOKORO_ARTIFACTS_DIR and preserves the directory structure
copy_to_artifacts() {
cat - | while read file; do
directory="$logs_dir/$(dirname $file)"
mkdir -p "$directory"
cp "$file" "$logs_dir/$file"
done
}
brew_install rename
# rename all test.log to sponge_log.log and then copy them to the kokoro
# artifacts directory.
find -L . -name "test.log" -type f -exec rename 's/test.log/sponge_log.log/' {} \;
find -L . -name "sponge_log.log" -type f | copy_to_artifacts
# rename all test.xml to sponge_log.xml and then copy them to kokoro
# artifacts directory.
find -L . -name "test.xml" -type f -exec rename 's/test.xml/sponge_log.xml/' {} \;
find -L . -name "sponge_log.xml" -type f | copy_to_artifacts
}
# Upgrades kokoro's current bazel version to at least 0.20.0
upgrade_kokoro_bazel_if_needed() {
bazel version
if [ -f "$KOKORO_GFILE_DIR/use_bazel.sh" ]; then
bash "$KOKORO_GFILE_DIR/use_bazel.sh" 0.20.0
else
use_bazel.sh 0.20.0
fi
bazel version
}
run_bazel_affected() {
echo "Checking affected targets..."
if [ -z "$COMMAND" ]; then
COMMAND="test"
fi
if [ -z "$TARGET" ]; then
if [ "$COMMAND" == "test" ]; then
# Only return test targets.
bazel_kind="test"
else
# Return all affected targets.
bazel_kind="rule"
fi
if [ -n "$KOKORO_BUILD_NUMBER" ]; then
pushd github/repo >> /dev/null
fi
# `target_branch` is either the Pull Request destination branch (where it will be merged) or if
# none can be determined, defaults to `develop`.
if [ -n "$KOKORO_GITHUB_PULL_REQUEST_TARGET_BRANCH" ]; then
target_branch="$KOKORO_GITHUB_PULL_REQUEST_TARGET_BRANCH"
else
target_branch=develop
fi
# `base_sha` is the merge base of `target_branch` and the current HEAD.
base_sha=$(git merge-base "$target_branch" HEAD)
# We must upgrade bazel prior to running bazel query because we rely on features in bazel
# 0.20.
if [ -n "$KOKORO_BUILD_NUMBER" ]; then
upgrade_kokoro_bazel_if_needed
fi
# Output the affected targets to the logs. This also gives the affected_targets script the
# opportunity to fail.
scripts/affected_targets "$bazel_kind" "$base_sha...HEAD"
# `TARGET` is then all bazel targets affected by changes in the commit range between `base_sha`
# and `HEAD`. By default, that becomes all changes between the `develop` branch and the latest
# commit.
TARGET="$(scripts/affected_targets $bazel_kind $base_sha...HEAD)"
if [ -n "$KOKORO_BUILD_NUMBER" ]; then
popd >> /dev/null
fi
fi
if [ -z "$TARGET" ]; then
echo "Nothing to build."
exit 0
fi
comment_identifier="affected-targets"
if [ -z "$TARGET" ]; then
echo "Nothing to build."
if [ -n "$GITHUB_API_TOKEN" ]; then
delete_comment "$comment_identifier"
fi
exit 0
else
if [ -n "$GITHUB_API_TOKEN" ]; then
comment_tmp_path=$(mktemp -d)
comment_tmp_file="$comment_tmp_path/comment.tmp"
echo "bazel detected changes to the following targets:" > "$comment_tmp_file"
echo >> "$comment_tmp_file"
echo '```' >> "$comment_tmp_file"
echo "$TARGET" >> "$comment_tmp_file"
echo '```' >> "$comment_tmp_file"
post_comment "$comment_identifier" "$comment_tmp_file"
fi
fi
run_bazel
}
run_bazel() {
echo "Running bazel builds..."
if [ -n "$KOKORO_BUILD_NUMBER" ]; then
move_derived_data_to_tmp
fi
if [ -n "$KOKORO_BUILD_NUMBER" ]; then
upgrade_kokoro_bazel_if_needed
fi
if [ -n "$VERBOSE_OUTPUT" ]; then
verbosity_args="-s"
fi
if [ -z "$COMMAND" ]; then
COMMAND="test"
fi
if [ -z "$TARGET" ]; then
TARGET="//..."
fi
if [ -n "$KOKORO_BUILD_NUMBER" ]; then
select_xcode "$XCODE_VERSION"
# Move into our cloned repo
cd github/repo
fi
# Run against whichever Xcode is currently selected.
selected_xcode_developer_path=$(xcode-select -p)
selected_xcode_contents_path=$(dirname "$selected_xcode_developer_path")
xcode_version=$(cat "$selected_xcode_contents_path/version.plist" \
| grep "CFBundleShortVersionString" -A1 \
| grep string \
| cut -d'>' -f2 \
| cut -d'<' -f1)
if [ "$COMMAND" == "build" ]; then
echo "🏗️ $COMMAND with Xcode $xcode_version..."
elif [ "$COMMAND" == "test" ]; then
echo "🛠️ $COMMAND with Xcode $xcode_version..."
if [ -n "$VERBOSE_OUTPUT" ]; then
extra_args="--test_output=all"
else
extra_args="--test_output=errors"
fi
fi
fix_bazel_imports
if [ -n "$KOKORO_ARTIFACTS_DIR" ]; then
# Especially in the event of failure, we want our test artifacts to be uploaded.
trap upload_bazel_test_artifacts EXIT
fi
snapshot_dir="${repo_dir}/snapshot_test_goldens/goldens"
tmp_img_dir="$(mktemp -d)"
# Install git lfs if we're performing tests on kokoro
if [ -n "$KOKORO_BUILD_NUMBER" ] || [ -n "$AUTOBOT_BUILD_NUMBER" ]; then
if [ "$COMMAND" == "test" ]; then
brew_install git-lfs
git lfs install
git lfs pull
# Reset the simulators to prepare for hosted snapshot tests
sim_cleaner_tmp=$(mktemp -d)
git clone https://github.com/material-foundation/ios-simulator-utils.git "$sim_cleaner_tmp"
source "${sim_cleaner_tmp}/scripts/cleanup_simulators.sh"
perform_pre_test_cleanup "iPhone"
fi
fi
# Configure CI mode
if [ -n "$KOKORO_BUILD_NUMBER" ]; then
ci_mode="kokoro"
elif [ -n "$AUTOBOT_BUILD_NUMBER" ]; then
ci_mode="autobot"
fi
bazel $COMMAND $TARGET --xcode_version $xcode_version --ios_minimum_os=9.0 \
--ios_multi_cpus=i386,x86_64 $extra_args $verbosity_args \
--test_env="FB_REFERENCE_IMAGE_DIR=$snapshot_dir" \
--test_env="IMAGE_DIFF_DIR=$tmp_img_dir" \
--define ci_mode=$ci_mode
}
run_cocoapods() {
echo "Running cocoapods builds..."
if [ -n "$KOKORO_BUILD_NUMBER" ]; then
move_derived_data_to_tmp
fi
if [ -n "$KOKORO_BUILD_NUMBER" ]; then
select_xcode "$XCODE_VERSION"
# Move into our cloned repo
cd github/repo
gem_install xcpretty
# TODO(https://github.com/material-components/material-components-ios/issues/6356 ): Move to 1.6
gem install cocoapods -v "$COCOAPODS_VERSION"
pod --version
# Install git-lfs
brew_install git-lfs
git lfs install
git lfs pull
fi
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:MDCCatalog
fi
elif [ "$DEPENDENCY_SYSTEM" = "cocoapods-podspec" ]; then
pod lib lint MaterialComponents.podspec --skip-tests
fi
if [ -n "$CODECOV_TOKEN" ]; then
bash <(curl -s https://codecov.io/bash)
fi
}
# 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
brew_install yarn --ignore-dependencies
# The above installations update node to v10, but the docsite generator
# relies on v8.
brew unlink node
brew_install node@8
# Required because node@8 is keg-only, meaning it isn't linked automatically.
brew link --force --overwrite node@8
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
select_xcode "$XCODE_VERSION"
# Move into our cloned repo
cd github/repo
# Install sourcekitten, a dependency of the apidiff tool
brew_install "$SOURCEKITTEN_FORMULA"
sourcekitten version
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)
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)
if [ ! -f scripts/external/github-comment/.git ]; then
git submodule update --init --recursive scripts/external/github-comment
fi
pushd scripts/external/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
}
# This command can be used when a pull request should always be put into a failure state.
# For example, we do not want to allow pull requests to be merged into stable.
fail_immediately() {
exit 1
}
# This command protects the stable branch from being merged with anything other than
# release-candidate branches.
protect_stable_from_undesired_merges() {
if [ -z "$KOKORO_GITHUB_PULL_REQUEST_NUMBER" ]; then
echo "KOKORO_GITHUB_PULL_REQUEST_NUMBER has not been defined."
echo "Please set this variable to a valid GitHub pull request number."
exit 1
fi
pull_request_api_url="https://api.github.com/repos/material-components/material-components-ios/pulls/$KOKORO_GITHUB_PULL_REQUEST_NUMBER"
tmp_path=$(mktemp -d)
pull_request_metadata_file="$tmp_path/output"
curl -s "$pull_request_api_url" > "$pull_request_metadata_file"
if ! grep --quiet '^ "label": "material-components:stable",$' "$pull_request_metadata_file"; then
echo "This job should only be used to validate pull requests to stable."
exit 1;
fi
if ! grep --quiet '^ "label": "material-components:release-candidate",$' "$pull_request_metadata_file"; then
echo "Only release-candidate branches can be merged into stable."
exit 1;
fi
echo "Validated that this branch is a release-candidate being merged in to stable."
}
# The fall-through case for unknown jobs.
unknown_job() {
if [ -n "$DEPENDENCY_SYSTEM" ]; then
echo "The following job is unknown and will not be run: $DEPENDENCY_SYSTEM"
else
echo "You must provide a depenency system with '-d' or the environment "
echo "variable 'DEPENDENCY_SYSTEM'."
fi
exit 1
}
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
case "$DEPENDENCY_SYSTEM" in
"bazel") run_bazel ;;
"bazel-affected") run_bazel_affected ;;
"cocoapods") run_cocoapods ;;
"cocoapods-podspec") run_cocoapods ;;
"website") generate_website ;;
"danger") run_danger ;;
"apidiff") generate_apidiff ;;
"fail") fail_immediately ;;
"protect-stable-from-undesired-merges") protect_stable_from_undesired_merges ;;
*) unknown_job ;;
esac
echo "Success!"