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