|
| 1 | +#!/bin/bash |
| 2 | +# ===----------------------------------------------------------------------===// |
| 3 | +# |
| 4 | +# This source file is part of the Swift.org open source project |
| 5 | +# |
| 6 | +# Copyright (c) 2024 Apple Inc. and the Swift project authors |
| 7 | +# Licensed under Apache License v2.0 with Runtime Library Exception |
| 8 | +# |
| 9 | +# See https://swift.org/LICENSE.txt for license information |
| 10 | +# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 11 | +# |
| 12 | +# ===----------------------------------------------------------------------===// |
| 13 | + |
| 14 | +set -euo pipefail |
| 15 | + |
| 16 | +set +x |
| 17 | + |
| 18 | +log() { printf -- "** %s\n" "$*" >&2; } |
| 19 | +error() { printf -- "** ERROR: %s\n" "$*" >&2; } |
| 20 | +fatal() { error "$@"; exit 1; } |
| 21 | + |
| 22 | +test -n "${PROJECT_NAME:-}" || fatal "PROJECT_NAME unset" |
| 23 | + |
| 24 | +if [ -f .license_header_template ]; then |
| 25 | + # allow projects to override the license header template |
| 26 | + expected_file_header_template=$(cat .license_header_template) |
| 27 | +else |
| 28 | + expected_file_header_template="@@===----------------------------------------------------------------------===@@ |
| 29 | +@@ |
| 30 | +@@ This source file is part of the ${PROJECT_NAME} open source project |
| 31 | +@@ |
| 32 | +@@ Copyright (c) YEARS Apple Inc. and the ${PROJECT_NAME} project authors |
| 33 | +@@ Licensed under Apache License v2.0 |
| 34 | +@@ |
| 35 | +@@ See LICENSE.txt for license information |
| 36 | +@@ See CONTRIBUTORS.txt for the list of ${PROJECT_NAME} project authors |
| 37 | +@@ |
| 38 | +@@ SPDX-License-Identifier: Apache-2.0 |
| 39 | +@@ |
| 40 | +@@===----------------------------------------------------------------------===@@" |
| 41 | +fi |
| 42 | + |
| 43 | +paths_with_missing_license=( ) |
| 44 | + |
| 45 | +# file_excludes=".license_header_template |
| 46 | +# .licenseignore" |
| 47 | +# if [ -f .licenseignore ]; then |
| 48 | +# file_excludes=$(printf '%s\n%s' "$file_excludes" "$(cat .licenseignore)") |
| 49 | +# fi |
| 50 | +# file_paths=$(echo "$file_excludes" | tr '\n' '\0' | xargs -0 -I% printf '":(exclude)%" '| xargs git ls-files) |
| 51 | +file_paths=$(tr '\n' '\0' < .licenseignore | xargs -0 -I% printf '":(exclude)%" '| xargs git ls-files ":(exclude).licenseignore" ":(exclude).license_header_template" ) |
| 52 | +echo $file_paths |
| 53 | + |
| 54 | +while IFS= read -r file_path; do |
| 55 | + file_basename=$(basename -- "${file_path}") |
| 56 | + file_extension="${file_basename##*.}" |
| 57 | + |
| 58 | + # shellcheck disable=SC2001 # We prefer to use sed here instead of bash search/replace |
| 59 | + case "${file_extension}" in |
| 60 | + swift) expected_file_header=$(sed -e 's|@@|//|g' <<<"${expected_file_header_template}") ;; |
| 61 | + h) expected_file_header=$(sed -e 's|@@|//|g' <<<"${expected_file_header_template}") ;; |
| 62 | + c) expected_file_header=$(sed -e 's|@@|//|g' <<<"${expected_file_header_template}") ;; |
| 63 | + sh) expected_file_header=$(cat <(echo '#!/bin/bash') <(sed -e 's|@@|##|g' <<<"${expected_file_header_template}")) ;; |
| 64 | + kts) expected_file_header=$(sed -e 's|@@|//|g' <<<"${expected_file_header_template}") ;; |
| 65 | + gradle) expected_file_header=$(sed -e 's|@@|//|g' <<<"${expected_file_header_template}") ;; |
| 66 | + groovy) expected_file_header=$(sed -e 's|@@|//|g' <<<"${expected_file_header_template}") ;; |
| 67 | + java) expected_file_header=$(sed -e 's|@@|//|g' <<<"${expected_file_header_template}") ;; |
| 68 | + py) expected_file_header=$(cat <(echo '#!/usr/bin/env python3') <(sed -e 's|@@|##|g' <<<"${expected_file_header_template}")) ;; |
| 69 | + rb) expected_file_header=$(cat <(echo '#!/usr/bin/env ruby') <(sed -e 's|@@|##|g' <<<"${expected_file_header_template}")) ;; |
| 70 | + in) expected_file_header=$(sed -e 's|@@|##|g' <<<"${expected_file_header_template}") ;; |
| 71 | + cmake) expected_file_header=$(sed -e 's|@@|##|g' <<<"${expected_file_header_template}") ;; |
| 72 | + *) |
| 73 | + error "Unsupported file extension ${file_extension} for file (exclude or update this script): ${file_path}" |
| 74 | + paths_with_missing_license+=("${file_path} ") |
| 75 | + ;; |
| 76 | + esac |
| 77 | + expected_file_header_linecount=$(wc -l <<<"${expected_file_header}") |
| 78 | + |
| 79 | + file_header=$(head -n "${expected_file_header_linecount}" "${file_path}") |
| 80 | + normalized_file_header=$( |
| 81 | + echo "${file_header}" \ |
| 82 | + | sed -e 's/20[12][0123456789]-20[12][0123456789]/YEARS/' -e 's/20[12][0123456789]/YEARS/' \ |
| 83 | + ) |
| 84 | + |
| 85 | + if ! diff -u \ |
| 86 | + --label "Expected header" <(echo "${expected_file_header}") \ |
| 87 | + --label "${file_path}" <(echo "${normalized_file_header}") |
| 88 | + then |
| 89 | + paths_with_missing_license+=("${file_path} ") |
| 90 | + fi |
| 91 | +done <<< "$file_paths" |
| 92 | + |
| 93 | +if [ "${#paths_with_missing_license[@]}" -gt 0 ]; then |
| 94 | + fatal "❌ Found missing license header in files: ${paths_with_missing_license[*]}." |
| 95 | +fi |
| 96 | + |
| 97 | +log "✅ Found no files with missing license header." |
0 commit comments