Skip to content

Commit 941777e

Browse files
committed
Refactor the license header check script to not repeat the expected license header computation
1 parent 937e178 commit 941777e

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

.github/workflows/scripts/check-license-header.sh

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -56,39 +56,44 @@ while IFS= read -r file_path; do
5656
continue # Ignore symbolic links
5757
fi
5858

59+
# The characters that are used to start a line comment and that replace '@@' in the license header template
60+
comment_marker=''
61+
# A line that we expect before the license header. This should end with a newline if it is not empty
62+
header_prefix=''
5963
# shellcheck disable=SC2001 # We prefer to use sed here instead of bash search/replace
6064
case "${file_extension}" in
61-
bazel) expected_file_header=$(sed -e 's|@@|##|g' <<<"${expected_file_header_template}") ;;
62-
bzl) expected_file_header=$(sed -e 's|@@|##|g' <<<"${expected_file_header_template}") ;;
63-
bazelrc) 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-
cmake) expected_file_header=$(sed -e 's|@@|##|g' <<<"${expected_file_header_template}") ;;
66-
editorconfig) 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-
h) expected_file_header=$(sed -e 's|@@|//|g' <<<"${expected_file_header_template}") ;;
70-
in) expected_file_header=$(sed -e 's|@@|##|g' <<<"${expected_file_header_template}") ;;
71-
java) expected_file_header=$(sed -e 's|@@|//|g' <<<"${expected_file_header_template}") ;;
72-
js) expected_file_header=$(sed -e 's|@@|//|g' <<<"${expected_file_header_template}") ;;
65+
bazel) comment_marker='##' ;;
66+
bzl) comment_marker='##' ;;
67+
bazelrc) comment_marker='##' ;;
68+
c) comment_marker='//' ;;
69+
cmake) comment_marker='##' ;;
70+
editorconfig) comment_marker='##' ;;
71+
gradle) comment_marker='//' ;;
72+
groovy) comment_marker='//' ;;
73+
h) comment_marker='//' ;;
74+
in) comment_marker='##' ;;
75+
java) comment_marker='//' ;;
76+
js) comment_marker='//' ;;
7377
json) continue ;; # JSON doesn't support comments
74-
jsx) expected_file_header=$(sed -e 's|@@|//|g' <<<"${expected_file_header_template}") ;;
75-
kts) expected_file_header=$(sed -e 's|@@|//|g' <<<"${expected_file_header_template}") ;;
76-
proto) expected_file_header=$(sed -e 's|@@|//|g' <<<"${expected_file_header_template}") ;;
77-
ps1) expected_file_header=$(sed -e 's|@@|##|g' <<<"${expected_file_header_template}") ;;
78-
py) expected_file_header=$(cat <(echo '#!/usr/bin/env python3') <(sed -e 's|@@|##|g' <<<"${expected_file_header_template}")) ;;
79-
rb) expected_file_header=$(cat <(echo '#!/usr/bin/env ruby') <(sed -e 's|@@|##|g' <<<"${expected_file_header_template}")) ;;
80-
sh) expected_file_header=$(cat <(echo '#!/bin/bash') <(sed -e 's|@@|##|g' <<<"${expected_file_header_template}")) ;;
81-
swift) expected_file_header=$(sed -e 's|@@|//|g' <<<"${expected_file_header_template}") ;;
78+
jsx) comment_marker='//' ;;
79+
kts) comment_marker='//' ;;
80+
proto) comment_marker='//' ;;
81+
ps1) comment_marker='##' ;;
82+
py) comment_marker='##'; header_prefix=$'#!/usr/bin/env python3\n' ;;
83+
rb) comment_marker='##'; header_prefix=$'#!/usr/bin/env ruby\n' ;;
84+
sh) comment_marker='##'; header_prefix=$'#!/bin/bash\n' ;;
85+
swift) comment_marker='//' ;;
8286
swift-format) continue ;; # .swift-format is JSON and doesn't support comments
83-
ts) expected_file_header=$(sed -e 's|@@|//|g' <<<"${expected_file_header_template}") ;;
84-
tsx) expected_file_header=$(sed -e 's|@@|//|g' <<<"${expected_file_header_template}") ;;
87+
ts) comment_marker='//' ;;
88+
tsx) comment_marker='//' ;;
8589
*)
8690
error "Unsupported file extension ${file_extension} for file (exclude or update this script): ${file_path}"
8791
paths_with_missing_license+=("${file_path} ")
8892
continue
8993
;;
9094
esac
91-
expected_file_header_linecount=$(wc -l <<<"${expected_file_header}")
95+
expected_file_header=$(echo "${header_prefix}${expected_file_header_template}" | sed -e "s|@@|$comment_marker|g")
96+
expected_file_header_linecount=$(echo "${expected_file_header}" | wc -l)
9297

9398
file_header=$(head -n "${expected_file_header_linecount}" "${file_path}")
9499
normalized_file_header=$(

0 commit comments

Comments
 (0)