This repository has been archived on 2024-05-26. You can view files and clone it, but cannot push or open issues or pull requests.
trixy/scripts/renew_copyright_header.sh

104 lines
3.4 KiB
Bash
Raw Normal View History

#! /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=20
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 19 lines long)
# 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"
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!"
;;
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
done
if [ -z "$DRY_RUN" ]; then
licensure --in-place --project
nix fmt
fi
fi