Files
firebase-ios-sdk/scripts/style.sh
T

202 lines
5.2 KiB
Bash
Raw Normal View History

#!/bin/bash
2020-08-12 14:50:37 -07:00
# Copyright 2017 Google LLC
#
# 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
2020-08-12 14:50:37 -07:00
#
# http://www.apache.org/licenses/LICENSE-2.0
2020-08-12 14:50:37 -07:00
#
# 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.
# Usage:
# ./scripts/style.sh [branch-name | filenames]
#
# With no arguments, formats all eligible files in the repo
# Pass a branch name to format all eligible files changed since that branch
# Pass a specific file or directory name to format just files found there
#
# Commonly
# ./scripts/style.sh master
2018-06-06 12:31:37 -07:00
# Strip the clang-format version output down to the major version. Examples:
# clang-format version 7.0.0 (tags/google/stable/2018-01-11)
# clang-format version google3-trunk (trunk r333779)
2018-03-07 09:59:38 -08:00
version=$(clang-format --version)
2018-08-15 11:00:31 -07:00
# Log the version in non-interactive use as it can be useful in travis logs.
if [[ ! -t 1 ]]; then
echo "Found: $version"
fi
2018-06-06 12:31:37 -07:00
# Remove leading "clang-format version"
version="${version/*version /}"
# Remove trailing parenthetical version details
version="${version/ (*)/}"
# Remove everything after the first dot (note this is a glob, not a regex)
version="${version/.*/}"
case "$version" in
2020-06-26 14:34:27 -07:00
10)
2018-06-06 12:31:37 -07:00
;;
google3-trunk)
echo "Please use a publicly released clang-format; a recent LLVM release"
echo "from http://releases.llvm.org/download.html will work."
echo "Prepend to PATH when running style.sh."
exit 1
;;
*)
2020-06-26 14:34:27 -07:00
echo "Please upgrade to clang-format version 10."
echo "If it's installed via homebrew you can run:"
echo "brew upgrade clang-format"
2018-06-06 12:31:37 -07:00
exit 1
;;
esac
2020-08-12 14:50:37 -07:00
# Ensure that tools in `Mintfile` are installed locally to avoid permissions
# problems that would otherwise arise from the default of installing in
# /usr/local.
export MINT_PATH=Mint
2018-06-06 12:31:37 -07:00
system=$(uname -s)
2018-02-26 10:51:58 -08:00
# Joins the given arguments with the separator given as the first argument.
function join() {
local IFS="$1"
shift
echo "$*"
}
clang_options=(-style=file)
# Rules to disable in swiftformat:
swift_disable=(
# sortedImports is broken, sorting into the middle of the copyright notice.
sortedImports
# Too many of our swift files have simplistic examples. While technically
# it's correct to remove the unused argument labels, it makes our examples
# look wrong.
unusedArguments
# We prefer trailing braces.
wrapMultilineStatementBraces
2018-02-26 10:51:58 -08:00
)
swift_options=(
# Mimic Objective-C style.
--indent 2
2020-04-08 10:48:24 -07:00
--maxwidth 100
--wrapparameters afterfirst
2018-02-26 10:51:58 -08:00
--disable $(join , "${swift_disable[@]}")
)
if [[ $# -gt 0 && "$1" == "test-only" ]]; then
2018-01-10 19:06:42 -08:00
test_only=true
2018-02-26 10:51:58 -08:00
clang_options+=(-output-replacements-xml)
swift_options+=(--dryrun)
2018-01-17 10:02:42 -08:00
shift
2018-01-10 19:06:42 -08:00
else
test_only=false
2018-02-26 10:51:58 -08:00
clang_options+=(-i)
2018-01-10 19:06:42 -08:00
fi
#TODO(#2223) - Find a way to handle spaces in filenames
files=$(
(
2018-01-17 10:02:42 -08:00
if [[ $# -gt 0 ]]; then
if git rev-parse "$1" -- >& /dev/null; then
# Argument was a branch name show files changed since that branch
2018-01-17 10:02:42 -08:00
git diff --name-only --relative --diff-filter=ACMR "$1"
else
# Otherwise assume the passed things are files or directories
find "$@" -type f
fi
else
# Do everything by default
find . -type f
fi
) | sed -E -n '
2018-03-15 23:28:52 -07:00
# find . includes a leading "./" that git does not include
s%^./%%
# Build outputs
\%/Pods/% d
2018-03-22 19:46:20 -04:00
\%^build/% d
\%^Debug/% d
\%^Release/% d
\%^cmake-build-debug/% d
2020-07-30 06:30:24 -07:00
\%^.build/% d
\%^.swiftpm/% d
# pod gen output
\%^gen/% d
# FirestoreEncoder is under 'third_party' for licensing reasons but should be
2019-06-19 10:00:17 -04:00
# formatted.
\%Firestore/third_party/FirestoreEncoder/.*\.swift% p
2019-06-19 10:00:17 -04:00
# Sources controlled outside this tree
\%/third_party/% d
2020-08-06 14:08:25 -07:00
# Public headers for closed sourced FirebaseAnalytics
\%^(FirebaseAnalyticsWrapper)/% d
# Generated source
\%/Firestore/core/src/util/config.h% d
# Sources pulled in by travis bundler, with and without a leading slash
\%^/?vendor/bundle/% d
2020-08-12 14:50:37 -07:00
# Sources pulled in by the Mint package manager
\%^Mint% d
2020-03-17 10:22:29 -07:00
# Auth Sample is not subject to formatting
\%^(FirebaseAuth/Tests/Sample)/% d
2019-10-08 14:57:07 -07:00
# Keep Firebase.h indenting
\%^CoreOnly/Sources/Firebase.h% d
2019-10-08 14:57:07 -07:00
# Checked-in generated code
\%\.pb(objc|rpc)\.% d
2018-02-02 14:35:34 -05:00
\%\.pb\.% d
\%\.nanopb\.% d
# Format C-ish sources only
2018-02-26 10:51:58 -08:00
\%\.(h|m|mm|cc|swift)$% p
'
)
2018-02-26 10:51:58 -08:00
needs_formatting=false
for f in $files; do
2018-02-26 10:51:58 -08:00
if [[ "${f: -6}" == '.swift' ]]; then
if [[ "$system" == 'Darwin' ]]; then
2019-10-23 10:58:50 -07:00
# Match output that says:
# 1/1 files would have been formatted. (with --dryrun)
# 1/1 files formatted. (without --dryrun)
2020-08-12 14:50:37 -07:00
mint run swiftformat "${swift_options[@]}" "$f" 2>&1 | grep '^1/1 files' > /dev/null
2018-02-26 10:51:58 -08:00
else
false
fi
else
clang-format "${clang_options[@]}" "$f" | grep "<replacement " > /dev/null
fi
if [[ "$test_only" == true && $? -ne 1 ]]; then
echo "$f needs formatting."
needs_formatting=true
fi
done
2018-01-10 19:06:42 -08:00
2018-02-26 10:51:58 -08:00
if [[ "$needs_formatting" == true ]]; then
2018-01-17 10:02:42 -08:00
echo "Proposed commit is not style compliant."
echo "Run scripts/style.sh and git add the result."
2018-01-10 19:06:42 -08:00
exit 1
fi