diff options
author | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2024-03-31 23:56:27 +0200 |
---|---|---|
committer | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2024-04-01 00:00:14 +0200 |
commit | 4c7a6efdaa8c27d36343ce32be03ae916b7174c5 (patch) | |
tree | a0a30f681e29466fa79e9cc8a534737a1f1260cf | |
parent | build(cog.toml): Also update to the new SOTA (diff) | |
download | flake-templates-4c7a6efdaa8c27d36343ce32be03ae916b7174c5.tar.gz flake-templates-4c7a6efdaa8c27d36343ce32be03ae916b7174c5.zip |
refactor(common): Add common code to a shared directory
Diffstat (limited to '')
-rwxr-xr-x | common/renew_copyright_header.sh | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/common/renew_copyright_header.sh b/common/renew_copyright_header.sh new file mode 100755 index 0000000..edb0a65 --- /dev/null +++ b/common/renew_copyright_header.sh @@ -0,0 +1,86 @@ +#! /usr/bin/env sh + +# NOTE: This is the line length of the .licensure.yml header template **plus** the extra +# line after the template comment. +TEMPLATE_LINE_LENGTH=20 + +PROJECT_ROOT="$(git rev-parse --show-toplevel)" + +remove() { + extension="$1" + file="$2" + + # We need to differentiate, when removing the old copyright header, as some + # formatters do weird things to the file + case "$extension" in + # normal '#' comments (these are $TEMPLATE_LINE_LENGTH lines long) + "Makefile" | "toml" | "envrc" | "yml" | "gitignore" | "awk") + sed --in-place "1,${TEMPLATE_LINE_LENGTH}d" "$file" + ;; + # normal '/* ... */' like comments (these are $TEMPLATE_LINE_LENGTH + 2 lines long) + "c" | "h" | "md" | "rs") + length="$((TEMPLATE_LINE_LENGTH + 2))" + sed --in-place "1,${length}d;" "$file" + ;; + # alejandra (the nix formatter) removes the blank line after the comment, + # thus only $TEMPLATE_LINE_LENGTH - 1 lines + "nix") + length="$((TEMPLATE_LINE_LENGTH - 1))" + sed --in-place "1,${length}d;" "$file" + ;; + # Shell needs a shebang on the first line, only after the first line can we + # remove the $TEMPLATE_LINE_LENGTH lines + "sh") + sed --in-place "2,${TEMPLATE_LINE_LENGTH}d;" "$file" + licensure --in-place "$file" + + TEMPLATE_LINE_LENGTH_NEW="$(($(yq --raw-output '.licenses | map(.template) | join("")' "$PROJECT_ROOT/.licensure.yml" | wc -l) + $(yq '.comments | last | .commenter.trailing_lines' "$PROJECT_ROOT/.licensure.yml")))" + + # delete the current shebang + to="$((TEMPLATE_LINE_LENGTH_NEW + 1))" + sed --in-place "${TEMPLATE_LINE_LENGTH_NEW},${to}d;" "$file" + + # add a new one + sed --in-place "1i#! /usr/bin/env sh" "$file" + ;; + *) + echo "File '$file' with extension '$extension' is not know yet, please add it!" + ;; + esac +} + +list() { + echo "$extension -> $file" +} + +if [ -f "$1" ]; then + file="$(realpath "$1")" + filename="$(basename -- "$file")" + extension="${filename##*.}" + filename="${filename%.*}" + + if [ -n "$DRY_RUN" ]; then + list "$extension" "$file" + else + remove "$extension" "$file" + fi +else + fd --type file --hidden . | while read -r file; do + if grep --quiet 'SPDX-License-Identifier: GPL-3.0-or-later' "$file"; then + filename="$(basename -- "$file")" + extension="${filename##*.}" + filename="${filename%.*}" + + if [ -n "$DRY_RUN" ]; then + list "$extension" "$file" + else + remove "$extension" "$file" + fi + fi + done + + if [ -z "$DRY_RUN" ]; then + licensure --in-place --project + nix fmt + fi +fi |