Skip to content

Commit eaaf98d

Browse files
committed
add scripts
1 parent 97183bc commit eaaf98d

File tree

3 files changed

+149
-0
lines changed

3 files changed

+149
-0
lines changed

scripts/check_format.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
log() { printf -- "** %s\n" "$*" >&2; }
17+
error() { printf -- "** ERROR: %s\n" "$*" >&2; }
18+
fatal() { error "$@"; exit 1; }
19+
20+
21+
if [[ -f .swiftformatignore ]]; then
22+
log "Found swiftformatignore file..."
23+
24+
log "Running swift format format..."
25+
tr '\n' '\0' < .swiftformatignore| xargs -0 -I% printf '":(exclude)%" '| xargs git ls-files -z '*.swift' | xargs -0 swift format format --parallel --in-place
26+
27+
log "Running swift format lint..."
28+
29+
tr '\n' '\0' < .swiftformatignore | xargs -0 -I% printf '":(exclude)%" '| xargs git ls-files -z '*.swift' | xargs -0 swift format lint --strict --parallel
30+
else
31+
log "Running swift format format..."
32+
git ls-files -z '*.swift' | xargs -0 swift format format --parallel --in-place
33+
34+
log "Running swift format lint..."
35+
36+
git ls-files -z '*.swift' | xargs -0 swift format lint --strict --parallel
37+
fi
38+
39+
40+
41+
log "Checking for modified files..."
42+
43+
GIT_PAGER='' git diff --exit-code '*.swift'
44+
45+
log "✅ Found no formatting issues."

scripts/check_license.sh

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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."

yamllint.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
extends: default
2+
3+
rules:
4+
line-length: false
5+
document-start: false
6+
truthy:
7+
check-keys: false # Otherwise we get a false positive on GitHub action's `on` key

0 commit comments

Comments
 (0)