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,18 +1,37 @@
#! /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() {
fd --type file . | while read -r file; do extension="$1"
if grep --quiet 'The Trinitrix Project <soispha@vhack.eu, antifallobst@systemausfall.org>' "$file"; then file="$2"
filename="$(basename -- "$file")"
extension="${filename##*.}"
filename="${filename%.*}"
# We need to differentiate, when removing the old copyright header, as some # We need to differentiate, when removing the old copyright header, as some
# formatters do weird things to the file # formatters do weird things to the file
case "$extension" in case "$extension" in
# normal '#' comments (these are 19 lines long) # normal '#' comments (these are 19 lines long)
"Makefile" | "toml") # 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" sed --in-place "1,${TEMPLATE_LINE_LENGHT}d" "$file"
;; ;;
# normal '/* ... */' like comments (these are 21 lines long--'#' + 2) # normal '/* ... */' like comments (these are 21 lines long--'#' + 2)
@ -30,14 +49,55 @@ fd --type file . | while read -r file; do
# remove the 19 lines # remove the 19 lines
"sh") "sh")
sed --in-place "2,${TEMPLATE_LINE_LENGHT}d;" "$file" sed --in-place "2,${TEMPLATE_LINE_LENGHT}d;" "$file"
licensure --in-place "$file"
TEMPLATE_LINE_LENGHT_NEW="$(($(yq --raw-output '.licenses | map(.template) | join("")' ./.licensure.yml | wc -l) + $(yq '.comments | last | .commenter.trailing_lines' ./.licensure.yml)))"
# delete the current shebang
to="$((TEMPLATE_LINE_LENGHT_NEW + 1))"
sed --in-place "${TEMPLATE_LINE_LENGHT_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!" echo "File '$file' with extension '$extension' is not know yet, please add it!"
;; ;;
esac 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 'The Trinitrix Project <soispha@vhack.eu, antifallobst@systemausfall.org>' "$file"; then
filename="$(basename -- "$file")"
extension="${filename##*.}"
filename="${filename%.*}"
if [ -n "$DRY_RUN" ]; then
list "$extension" "$file"
else
remove "$extension" "$file"
fi
fi fi
done done
nix fmt if [ -z "$DRY_RUN" ]; then
licensure --in-place --project licensure --in-place --project
nix fmt
fi
fi