fix(scripts/renew_copyright_header): Correctly handle shebangs

This commit is contained in:
Benedikt Peetz 2024-03-26 19:47:45 +01:00
parent 3484ead8af
commit 3646e4aee8
Signed by: bpeetz
GPG Key ID: A5E94010C3A642AD
4 changed files with 125 additions and 38 deletions

View File

@ -1,10 +1,31 @@
# Copyright (C) 2023 - 2024:
# The Trinitrix Project <soispha@vhack.eu, antifallobst@systemausfall.org>
# SPDX-License-Identifier: LGPL-3.0-or-later
#
#
# This file is part of the Trixy crate for Trinitrix.
#
# Trixy is free software: you can redistribute it and/or modify
# it under the terms of the Lesser GNU General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# and the Lesser GNU General Public License along with this program.
# If not, see <https://www.gnu.org/licenses/>.
--- ---
# Regexes which if matched by a file path will always be excluded from # Regexes which if matched by a file path will always be excluded from
# getting a license header # getting a license header
excludes: excludes:
- .*lock - .*lock
- expected.md
- \.git/.* - \.git/.*
- \.licensure\.yml
- LICENSE.spdx - LICENSE.spdx
- COPYING - COPYING
- COPYING.LESSER - COPYING.LESSER

View File

@ -1,6 +1,8 @@
#(* #(*
# Copyright (C) 2023 - 2024: # Copyright (C) 2023 - 2024:
# The Trinitrix Project <soispha@vhack.eu, antifallobst@systemausfall.org> # The Trinitrix Project <soispha@vhack.eu, antifallobst@systemausfall.org>
# SPDX-License-Identifier: LGPL-3.0-or-later
#
# #
# This file is part of the Trixy crate for Trinitrix. # This file is part of the Trixy crate for Trinitrix.
# #
@ -19,7 +21,6 @@
# If not, see <https://www.gnu.org/licenses/>. # If not, see <https://www.gnu.org/licenses/>.
#*) #*)
# (* # (*
# Trixy is fully whitespace independent, this means that you can # Trixy is fully whitespace independent, this means that you can
# interleave whitespace in the definitions. # interleave whitespace in the definitions.

View File

@ -1,5 +1,7 @@
# Copyright (C) 2023 - 2024: # Copyright (C) 2023 - 2024:
# The Trinitrix Project <soispha@vhack.eu, antifallobst@systemausfall.org> # The Trinitrix Project <soispha@vhack.eu, antifallobst@systemausfall.org>
# SPDX-License-Identifier: LGPL-3.0-or-later
#
# #
# This file is part of the Trixy crate for Trinitrix. # This file is part of the Trixy crate for Trinitrix.
# #
@ -219,6 +221,9 @@
gdb gdb
ebnf2pdf.outputs.packages."${system}".default ebnf2pdf.outputs.packages."${system}".default
# for the ./scripts/renew_copyright_header.sh
yq
]; ];
inherit nativeBuildInputs buildInputs; inherit nativeBuildInputs buildInputs;
}; };

View File

@ -1,43 +1,103 @@
#! /usr/bin/env sh #! /usr/bin/env sh
# Copyright (C) 2023 - 2024:
# The Trinitrix Project <soispha@vhack.eu, antifallobst@systemausfall.org>
# SPDX-License-Identifier: LGPL-3.0-or-later
#
#
# This file is part of the Trixy crate for Trinitrix.
#
# Trixy is free software: you can redistribute it and/or modify
# it under the terms of the Lesser GNU General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# and the Lesser GNU General Public License along with this program.
# If not, see <https://www.gnu.org/licenses/>.
TEMPLATE_LINE_LENGHT=19 TEMPLATE_LINE_LENGHT=20
remove() {
extension="$1"
file="$2"
fd --type file . | while read -r file; do # We need to differentiate, when removing the old copyright header, as some
if grep --quiet 'The Trinitrix Project <soispha@vhack.eu, antifallobst@systemausfall.org>' "$file"; then # formatters do weird things to the file
filename="$(basename -- "$file")" case "$extension" in
extension="${filename##*.}" # normal '#' comments (these are 19 lines long)
filename="${filename%.*}" # the `config` file is the cargo config at `./example/main/.cargo/config`, which is
# toml
"Makefile" | "toml" | "envrc" | "config" | "yml")
sed --in-place "1,${TEMPLATE_LINE_LENGHT}d" "$file"
;;
# normal '/* ... */' like comments (these are 21 lines long--'#' + 2)
"c" | "ebnf" | "h" | "md" | "rs" | "tri")
lenght="$((TEMPLATE_LINE_LENGHT + 2))"
sed --in-place "1,${lenght}d;" "$file"
;;
# alejandra (the nix formatter) removes the blank line after the comment,
# thus only 18 lines
"nix")
lenght="$((TEMPLATE_LINE_LENGHT - 1))"
sed --in-place "1,${lenght}d;" "$file"
;;
# Shell needs a shebang on the first line, only after the first line can we
# remove the 19 lines
"sh")
sed --in-place "2,${TEMPLATE_LINE_LENGHT}d;" "$file"
licensure --in-place "$file"
# We need to differentiate, when removing the old copyright header, as some TEMPLATE_LINE_LENGHT_NEW="$(($(yq --raw-output '.licenses | map(.template) | join("")' ./.licensure.yml | wc -l) + $(yq '.comments | last | .commenter.trailing_lines' ./.licensure.yml)))"
# formatters do weird things to the file
case "$extension" in # delete the current shebang
# normal '#' comments (these are 19 lines long) to="$((TEMPLATE_LINE_LENGHT_NEW + 1))"
"Makefile" | "toml") sed --in-place "${TEMPLATE_LINE_LENGHT_NEW},${to}d;" "$file"
sed --in-place "1,${TEMPLATE_LINE_LENGHT}d" "$file"
;; # add a new one
# normal '/* ... */' like comments (these are 21 lines long--'#' + 2) sed --in-place "1i#! /usr/bin/env sh" "$file"
"c" | "ebnf" | "h" | "md" | "rs" | "tri") ;;
lenght="$((TEMPLATE_LINE_LENGHT + 2))" *)
sed --in-place "1,${lenght}d;" "$file" echo "File '$file' with extension '$extension' is not know yet, please add it!"
;; ;;
# alejandra (the nix formatter) removes the blank line after the comment, esac
# thus only 18 lines }
"nix")
lenght="$((TEMPLATE_LINE_LENGHT - 1))" list() {
sed --in-place "1,${lenght}d;" "$file" echo "$extension -> $file"
;; }
# Shell needs a shebang on the first line, only after the first line can we
# remove the 19 lines if [ -f "$1" ]; then
"sh") file="$(realpath "$1")"
sed --in-place "2,${TEMPLATE_LINE_LENGHT}d;" "$file" filename="$(basename -- "$file")"
;; extension="${filename##*.}"
*) filename="${filename%.*}"
echo "File '$file' with extension '$extension' is not know yet, please add it!"
;; if [ -n "$DRY_RUN" ]; then
esac list "$extension" "$file"
else
remove "$extension" "$file"
fi fi
done else
fd --type file --hidden . | while read -r file; do
if grep --quiet 'The Trinitrix Project <soispha@vhack.eu, antifallobst@systemausfall.org>' "$file"; then
filename="$(basename -- "$file")"
extension="${filename##*.}"
filename="${filename%.*}"
nix fmt if [ -n "$DRY_RUN" ]; then
list "$extension" "$file"
else
remove "$extension" "$file"
fi
fi
done
licensure --in-place --project if [ -z "$DRY_RUN" ]; then
licensure --in-place --project
nix fmt
fi
fi