|
| 1 | +#!/bin/bash |
| 2 | +##===----------------------------------------------------------------------===## |
| 3 | +## |
| 4 | +## This source file is part of the SwiftNIO open source project |
| 5 | +## |
| 6 | +## Copyright (c) 2017-2019 Apple Inc. and the SwiftNIO project authors |
| 7 | +## Licensed under Apache License v2.0 |
| 8 | +## |
| 9 | +## See LICENSE.txt for license information |
| 10 | +## See CONTRIBUTORS.txt for the list of SwiftNIO project authors |
| 11 | +## |
| 12 | +## SPDX-License-Identifier: Apache-2.0 |
| 13 | +## |
| 14 | +##===----------------------------------------------------------------------===## |
| 15 | + |
| 16 | +SWIFT_FORMAT_VERSION=0.53.10 |
| 17 | + |
| 18 | +set -eu |
| 19 | +here="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" |
| 20 | + |
| 21 | +which swiftformat > /dev/null 2>&1 || (echo "swiftformat not installed. You can install it using 'brew install swiftformat'" ; exit -1) |
| 22 | + |
| 23 | +function replace_acceptable_years() { |
| 24 | + # this needs to replace all acceptable forms with 'YEARS' |
| 25 | + sed -e 's/20[12][0-9]-20[12][0-9]/YEARS/' -e 's/20[12][0-9]/YEARS/' -e '/^#!/ d' |
| 26 | +} |
| 27 | + |
| 28 | +printf "=> Checking format... " |
| 29 | +FIRST_OUT="$(git status --porcelain)" |
| 30 | +if [[ -n "${CI-""}" ]]; then |
| 31 | + printf "(using v$(mint run NickLockwood/SwiftFormat@"$SWIFT_FORMAT_VERSION" --version)) " |
| 32 | + mint run NickLockwood/SwiftFormat@"$SWIFT_FORMAT_VERSION" . > /dev/null 2>&1 |
| 33 | +else |
| 34 | + printf "(using v$(swiftformat --version)) " |
| 35 | + swiftformat . > /dev/null 2>&1 |
| 36 | +fi |
| 37 | +SECOND_OUT="$(git status --porcelain)" |
| 38 | +if [[ "$FIRST_OUT" != "$SECOND_OUT" ]]; then |
| 39 | + printf "\033[0;31mformatting issues!\033[0m\n" |
| 40 | + git --no-pager diff |
| 41 | + exit 1 |
| 42 | +else |
| 43 | + printf "\033[0;32mokay.\033[0m\n" |
| 44 | +fi |
| 45 | +exit |
| 46 | +printf "=> Checking license headers... " |
| 47 | +tmp=$(mktemp /tmp/.soto-core-sanity_XXXXXX) |
| 48 | + |
| 49 | +for language in swift-or-c; do |
| 50 | + declare -a matching_files |
| 51 | + declare -a exceptions |
| 52 | + expections=( ) |
| 53 | + matching_files=( -name '*' ) |
| 54 | + case "$language" in |
| 55 | + swift-or-c) |
| 56 | + exceptions=( -path '*Sources/INIParser/*' -o -path '*Sources/CSotoExpat/*' -o -path '*Benchmark/.build/*' -o -name Package.swift) |
| 57 | + matching_files=( -name '*.swift' -o -name '*.c' -o -name '*.h' ) |
| 58 | + cat > "$tmp" <<"EOF" |
| 59 | +//===----------------------------------------------------------------------===// |
| 60 | +// |
| 61 | +// This source file is part of the Hummingbird open source project |
| 62 | +// |
| 63 | +// Copyright (c) YEARS the Hummingbird authors |
| 64 | +// Licensed under Apache License v2.0 |
| 65 | +// |
| 66 | +// See LICENSE.txt for license information |
| 67 | +// See CONTRIBUTORS.txt for the list of Hummingbird authors |
| 68 | +// |
| 69 | +// SPDX-License-Identifier: Apache-2.0 |
| 70 | +// |
| 71 | +//===----------------------------------------------------------------------===// |
| 72 | +EOF |
| 73 | + ;; |
| 74 | + bash) |
| 75 | + matching_files=( -name '*.sh' ) |
| 76 | + cat > "$tmp" <<"EOF" |
| 77 | +##===----------------------------------------------------------------------===## |
| 78 | +## |
| 79 | +## This source file is part of the Hummingbird open source project |
| 80 | +## |
| 81 | +## Copyright (c) YEARS the Hummingbird authors |
| 82 | +## Licensed under Apache License v2.0 |
| 83 | +## |
| 84 | +## See LICENSE.txt for license information |
| 85 | +## See CONTRIBUTORS.txt for the list of Hummingbird authors |
| 86 | +## |
| 87 | +## SPDX-License-Identifier: Apache-2.0 |
| 88 | +## |
| 89 | +##===----------------------------------------------------------------------===## |
| 90 | +EOF |
| 91 | + ;; |
| 92 | + *) |
| 93 | + echo >&2 "ERROR: unknown language '$language'" |
| 94 | + ;; |
| 95 | + esac |
| 96 | + |
| 97 | + lines_to_compare=$(cat "$tmp" | wc -l | tr -d " ") |
| 98 | + # need to read one more line as we remove the '#!' line |
| 99 | + lines_to_read=$(expr "$lines_to_compare" + 1) |
| 100 | + expected_sha=$(cat "$tmp" | shasum) |
| 101 | + |
| 102 | + ( |
| 103 | + cd "$here/.." |
| 104 | + find . \ |
| 105 | + \( \! -path './.build/*' -a \ |
| 106 | + \( "${matching_files[@]}" \) -a \ |
| 107 | + \( \! \( "${exceptions[@]}" \) \) \) | while read line; do |
| 108 | + if [[ "$(cat "$line" | head -n $lines_to_read | replace_acceptable_years | head -n $lines_to_compare | shasum)" != "$expected_sha" ]]; then |
| 109 | + printf "\033[0;31mmissing headers in file '$line'!\033[0m\n" |
| 110 | + diff -u <(cat "$line" | head -n $lines_to_read | replace_acceptable_years | head -n $lines_to_compare) "$tmp" |
| 111 | + exit 1 |
| 112 | + fi |
| 113 | + done |
| 114 | + printf "\033[0;32mokay.\033[0m\n" |
| 115 | + ) |
| 116 | +done |
| 117 | + |
| 118 | +rm "$tmp" |
0 commit comments