From 4fec87e212697a3f58245eb71a054357aa9f0898 Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Mon, 1 Apr 2024 12:56:56 +0200 Subject: refactor(templates): Instantiate all symlinks Symlinks are copied verbatim, when nix initializes a template, thus symlinking common files does not work. --- templates/awk/.licensure.yml | 18 +++++ templates/awk/scripts/renew_copyright_header.sh | 87 ++++++++++++++++++++++- templates/awk/treefmt.nix | 74 ++++++++++++++++++- templates/c/.licensure.yml | 18 +++++ templates/c/scripts/renew_copyright_header.sh | 87 ++++++++++++++++++++++- templates/c/treefmt.nix | 74 ++++++++++++++++++- templates/latex/.licensure.yml | 43 +++++++++-- templates/latex/scripts/renew_copyright_header.sh | 87 ++++++++++++++++++++++- templates/latex/treefmt.nix | 74 ++++++++++++++++++- templates/rust/.licensure.yml | 18 +++++ templates/rust/scripts/renew_copyright_header.sh | 87 ++++++++++++++++++++++- templates/rust/treefmt.nix | 74 ++++++++++++++++++- templates/shell/.licensure.yml | 18 +++++ templates/shell/scripts/renew_copyright_header.sh | 87 ++++++++++++++++++++++- templates/shell/treefmt.nix | 74 ++++++++++++++++++- 15 files changed, 903 insertions(+), 17 deletions(-) mode change 120000 => 100755 templates/awk/scripts/renew_copyright_header.sh mode change 120000 => 100644 templates/awk/treefmt.nix mode change 120000 => 100755 templates/c/scripts/renew_copyright_header.sh mode change 120000 => 100644 templates/c/treefmt.nix mode change 120000 => 100755 templates/latex/scripts/renew_copyright_header.sh mode change 120000 => 100644 templates/latex/treefmt.nix mode change 120000 => 100755 templates/rust/scripts/renew_copyright_header.sh mode change 120000 => 100644 templates/rust/treefmt.nix mode change 120000 => 100755 templates/shell/scripts/renew_copyright_header.sh mode change 120000 => 100644 templates/shell/treefmt.nix (limited to 'templates') diff --git a/templates/awk/.licensure.yml b/templates/awk/.licensure.yml index e1f5a01..b7f963a 100644 --- a/templates/awk/.licensure.yml +++ b/templates/awk/.licensure.yml @@ -14,6 +14,24 @@ excludes: # No default license configuration is provided. This section must be # configured by the user. licenses: + - files: tex + ident: CC-BY-SA-4.0 + authors: + - name: Benedikt Peetz + email: "benedikt.peetz@b-peetz.de" + + template: | + 2024 - [year] (C) by [name of author] + SPDX-License-Identifier: CC-BY-SA-4.0 + + is licensed under a + Creative Commons Attribution-ShareAlike 4.0 International License. + + You should have received a copy of the license along with this + work. If not, see . + + unwrap_text: false + # Either a regex or the string "any" to determine to what files this # license should apply. It is common for projects to have files # under multiple licenses or with multiple copyright holders. This diff --git a/templates/awk/scripts/renew_copyright_header.sh b/templates/awk/scripts/renew_copyright_header.sh deleted file mode 120000 index f286da9..0000000 --- a/templates/awk/scripts/renew_copyright_header.sh +++ /dev/null @@ -1 +0,0 @@ -../../../common/renew_copyright_header.sh \ No newline at end of file diff --git a/templates/awk/scripts/renew_copyright_header.sh b/templates/awk/scripts/renew_copyright_header.sh new file mode 100755 index 0000000..edb0a65 --- /dev/null +++ b/templates/awk/scripts/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 diff --git a/templates/awk/treefmt.nix b/templates/awk/treefmt.nix deleted file mode 120000 index 6a4fe7d..0000000 --- a/templates/awk/treefmt.nix +++ /dev/null @@ -1 +0,0 @@ -../../treefmt.nix \ No newline at end of file diff --git a/templates/awk/treefmt.nix b/templates/awk/treefmt.nix new file mode 100644 index 0000000..1cbab40 --- /dev/null +++ b/templates/awk/treefmt.nix @@ -0,0 +1,73 @@ +{ + treefmt-nix, + pkgs, +}: +treefmt-nix.lib.evalModule pkgs ( + {pkgs, ...}: { + # Used to find the project root + projectRootFile = "flake.nix"; + + programs = { + alejandra.enable = true; + rustfmt.enable = true; + clang-format.enable = true; + mdformat.enable = true; + shfmt = { + enable = true; + indent_size = 4; + }; + shellcheck.enable = true; + prettier = { + settings = { + arrowParens = "always"; + bracketSameLine = false; + bracketSpacing = true; + editorconfig = true; + embeddedLanguageFormatting = "auto"; + endOfLine = "lf"; + # experimentalTernaries = false; + htmlWhitespaceSensitivity = "css"; + insertPragma = false; + jsxSingleQuote = true; + printWidth = 80; + proseWrap = "always"; + quoteProps = "consistent"; + requirePragma = false; + semi = true; + singleAttributePerLine = true; + singleQuote = true; + trailingComma = "all"; + useTabs = false; + vueIndentScriptAndStyle = false; + + tabWidth = 4; + overrides = { + files = ["*.js"]; + options.tabwidth = 2; + }; + }; + }; + stylua.enable = true; + ruff = { + enable = true; + format = true; + }; + taplo.enable = true; + }; + + settings = { + global.excludes = [ + "CHANGELOG.md" + "NEWS.md" + ]; + formatter = { + clang-format = { + options = ["--style" "GNU"]; + }; + shfmt = { + includes = ["*.bash"]; + }; + }; + }; + } +) diff --git a/templates/c/.licensure.yml b/templates/c/.licensure.yml index e1f5a01..b7f963a 100644 --- a/templates/c/.licensure.yml +++ b/templates/c/.licensure.yml @@ -14,6 +14,24 @@ excludes: # No default license configuration is provided. This section must be # configured by the user. licenses: + - files: tex + ident: CC-BY-SA-4.0 + authors: + - name: Benedikt Peetz + email: "benedikt.peetz@b-peetz.de" + + template: | + 2024 - [year] (C) by [name of author] + SPDX-License-Identifier: CC-BY-SA-4.0 + + is licensed under a + Creative Commons Attribution-ShareAlike 4.0 International License. + + You should have received a copy of the license along with this + work. If not, see . + + unwrap_text: false + # Either a regex or the string "any" to determine to what files this # license should apply. It is common for projects to have files # under multiple licenses or with multiple copyright holders. This diff --git a/templates/c/scripts/renew_copyright_header.sh b/templates/c/scripts/renew_copyright_header.sh deleted file mode 120000 index f286da9..0000000 --- a/templates/c/scripts/renew_copyright_header.sh +++ /dev/null @@ -1 +0,0 @@ -../../../common/renew_copyright_header.sh \ No newline at end of file diff --git a/templates/c/scripts/renew_copyright_header.sh b/templates/c/scripts/renew_copyright_header.sh new file mode 100755 index 0000000..edb0a65 --- /dev/null +++ b/templates/c/scripts/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 diff --git a/templates/c/treefmt.nix b/templates/c/treefmt.nix deleted file mode 120000 index 6a4fe7d..0000000 --- a/templates/c/treefmt.nix +++ /dev/null @@ -1 +0,0 @@ -../../treefmt.nix \ No newline at end of file diff --git a/templates/c/treefmt.nix b/templates/c/treefmt.nix new file mode 100644 index 0000000..1cbab40 --- /dev/null +++ b/templates/c/treefmt.nix @@ -0,0 +1,73 @@ +{ + treefmt-nix, + pkgs, +}: +treefmt-nix.lib.evalModule pkgs ( + {pkgs, ...}: { + # Used to find the project root + projectRootFile = "flake.nix"; + + programs = { + alejandra.enable = true; + rustfmt.enable = true; + clang-format.enable = true; + mdformat.enable = true; + shfmt = { + enable = true; + indent_size = 4; + }; + shellcheck.enable = true; + prettier = { + settings = { + arrowParens = "always"; + bracketSameLine = false; + bracketSpacing = true; + editorconfig = true; + embeddedLanguageFormatting = "auto"; + endOfLine = "lf"; + # experimentalTernaries = false; + htmlWhitespaceSensitivity = "css"; + insertPragma = false; + jsxSingleQuote = true; + printWidth = 80; + proseWrap = "always"; + quoteProps = "consistent"; + requirePragma = false; + semi = true; + singleAttributePerLine = true; + singleQuote = true; + trailingComma = "all"; + useTabs = false; + vueIndentScriptAndStyle = false; + + tabWidth = 4; + overrides = { + files = ["*.js"]; + options.tabwidth = 2; + }; + }; + }; + stylua.enable = true; + ruff = { + enable = true; + format = true; + }; + taplo.enable = true; + }; + + settings = { + global.excludes = [ + "CHANGELOG.md" + "NEWS.md" + ]; + formatter = { + clang-format = { + options = ["--style" "GNU"]; + }; + shfmt = { + includes = ["*.bash"]; + }; + }; + }; + } +) diff --git a/templates/latex/.licensure.yml b/templates/latex/.licensure.yml index 551ddfc..b7f963a 100644 --- a/templates/latex/.licensure.yml +++ b/templates/latex/.licensure.yml @@ -14,6 +14,24 @@ excludes: # No default license configuration is provided. This section must be # configured by the user. licenses: + - files: tex + ident: CC-BY-SA-4.0 + authors: + - name: Benedikt Peetz + email: "benedikt.peetz@b-peetz.de" + + template: | + 2024 - [year] (C) by [name of author] + SPDX-License-Identifier: CC-BY-SA-4.0 + + is licensed under a + Creative Commons Attribution-ShareAlike 4.0 International License. + + You should have received a copy of the license along with this + work. If not, see . + + unwrap_text: false + # Either a regex or the string "any" to determine to what files this # license should apply. It is common for projects to have files # under multiple licenses or with multiple copyright holders. This @@ -27,7 +45,7 @@ licenses: # found at: https://spdx.org/licenses/ but existence of the ident # in this list it is not enforced unless auto_template is set to # true. - ident: CC-BY-SA-4.0 + ident: GPL-3.0-or-later # # A list of authors who hold copyright over these files authors: @@ -44,14 +62,25 @@ licenses: # Name . If multiple authors are provided the # list is concatenated together with commas. template: | - 2024 - [year] (C) by [name of author] - SPDX-License-Identifier: CC-BY-SA-4.0 + Copyright (C) 2023 - [year]: + [name of author] + SPDX-License-Identifier: GPL-3.0-or-later - is licensed under a - Creative Commons Attribution-ShareAlike 4.0 International License. + This file is part of the TODO. - You should have received a copy of the license along with this - work. If not, see . + 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 . # If auto_template is true then template is ignored and the SPDX diff --git a/templates/latex/scripts/renew_copyright_header.sh b/templates/latex/scripts/renew_copyright_header.sh deleted file mode 120000 index f286da9..0000000 --- a/templates/latex/scripts/renew_copyright_header.sh +++ /dev/null @@ -1 +0,0 @@ -../../../common/renew_copyright_header.sh \ No newline at end of file diff --git a/templates/latex/scripts/renew_copyright_header.sh b/templates/latex/scripts/renew_copyright_header.sh new file mode 100755 index 0000000..edb0a65 --- /dev/null +++ b/templates/latex/scripts/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 diff --git a/templates/latex/treefmt.nix b/templates/latex/treefmt.nix deleted file mode 120000 index 6a4fe7d..0000000 --- a/templates/latex/treefmt.nix +++ /dev/null @@ -1 +0,0 @@ -../../treefmt.nix \ No newline at end of file diff --git a/templates/latex/treefmt.nix b/templates/latex/treefmt.nix new file mode 100644 index 0000000..1cbab40 --- /dev/null +++ b/templates/latex/treefmt.nix @@ -0,0 +1,73 @@ +{ + treefmt-nix, + pkgs, +}: +treefmt-nix.lib.evalModule pkgs ( + {pkgs, ...}: { + # Used to find the project root + projectRootFile = "flake.nix"; + + programs = { + alejandra.enable = true; + rustfmt.enable = true; + clang-format.enable = true; + mdformat.enable = true; + shfmt = { + enable = true; + indent_size = 4; + }; + shellcheck.enable = true; + prettier = { + settings = { + arrowParens = "always"; + bracketSameLine = false; + bracketSpacing = true; + editorconfig = true; + embeddedLanguageFormatting = "auto"; + endOfLine = "lf"; + # experimentalTernaries = false; + htmlWhitespaceSensitivity = "css"; + insertPragma = false; + jsxSingleQuote = true; + printWidth = 80; + proseWrap = "always"; + quoteProps = "consistent"; + requirePragma = false; + semi = true; + singleAttributePerLine = true; + singleQuote = true; + trailingComma = "all"; + useTabs = false; + vueIndentScriptAndStyle = false; + + tabWidth = 4; + overrides = { + files = ["*.js"]; + options.tabwidth = 2; + }; + }; + }; + stylua.enable = true; + ruff = { + enable = true; + format = true; + }; + taplo.enable = true; + }; + + settings = { + global.excludes = [ + "CHANGELOG.md" + "NEWS.md" + ]; + formatter = { + clang-format = { + options = ["--style" "GNU"]; + }; + shfmt = { + includes = ["*.bash"]; + }; + }; + }; + } +) diff --git a/templates/rust/.licensure.yml b/templates/rust/.licensure.yml index e1f5a01..b7f963a 100644 --- a/templates/rust/.licensure.yml +++ b/templates/rust/.licensure.yml @@ -14,6 +14,24 @@ excludes: # No default license configuration is provided. This section must be # configured by the user. licenses: + - files: tex + ident: CC-BY-SA-4.0 + authors: + - name: Benedikt Peetz + email: "benedikt.peetz@b-peetz.de" + + template: | + 2024 - [year] (C) by [name of author] + SPDX-License-Identifier: CC-BY-SA-4.0 + + is licensed under a + Creative Commons Attribution-ShareAlike 4.0 International License. + + You should have received a copy of the license along with this + work. If not, see . + + unwrap_text: false + # Either a regex or the string "any" to determine to what files this # license should apply. It is common for projects to have files # under multiple licenses or with multiple copyright holders. This diff --git a/templates/rust/scripts/renew_copyright_header.sh b/templates/rust/scripts/renew_copyright_header.sh deleted file mode 120000 index f286da9..0000000 --- a/templates/rust/scripts/renew_copyright_header.sh +++ /dev/null @@ -1 +0,0 @@ -../../../common/renew_copyright_header.sh \ No newline at end of file diff --git a/templates/rust/scripts/renew_copyright_header.sh b/templates/rust/scripts/renew_copyright_header.sh new file mode 100755 index 0000000..edb0a65 --- /dev/null +++ b/templates/rust/scripts/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 diff --git a/templates/rust/treefmt.nix b/templates/rust/treefmt.nix deleted file mode 120000 index 6a4fe7d..0000000 --- a/templates/rust/treefmt.nix +++ /dev/null @@ -1 +0,0 @@ -../../treefmt.nix \ No newline at end of file diff --git a/templates/rust/treefmt.nix b/templates/rust/treefmt.nix new file mode 100644 index 0000000..1cbab40 --- /dev/null +++ b/templates/rust/treefmt.nix @@ -0,0 +1,73 @@ +{ + treefmt-nix, + pkgs, +}: +treefmt-nix.lib.evalModule pkgs ( + {pkgs, ...}: { + # Used to find the project root + projectRootFile = "flake.nix"; + + programs = { + alejandra.enable = true; + rustfmt.enable = true; + clang-format.enable = true; + mdformat.enable = true; + shfmt = { + enable = true; + indent_size = 4; + }; + shellcheck.enable = true; + prettier = { + settings = { + arrowParens = "always"; + bracketSameLine = false; + bracketSpacing = true; + editorconfig = true; + embeddedLanguageFormatting = "auto"; + endOfLine = "lf"; + # experimentalTernaries = false; + htmlWhitespaceSensitivity = "css"; + insertPragma = false; + jsxSingleQuote = true; + printWidth = 80; + proseWrap = "always"; + quoteProps = "consistent"; + requirePragma = false; + semi = true; + singleAttributePerLine = true; + singleQuote = true; + trailingComma = "all"; + useTabs = false; + vueIndentScriptAndStyle = false; + + tabWidth = 4; + overrides = { + files = ["*.js"]; + options.tabwidth = 2; + }; + }; + }; + stylua.enable = true; + ruff = { + enable = true; + format = true; + }; + taplo.enable = true; + }; + + settings = { + global.excludes = [ + "CHANGELOG.md" + "NEWS.md" + ]; + formatter = { + clang-format = { + options = ["--style" "GNU"]; + }; + shfmt = { + includes = ["*.bash"]; + }; + }; + }; + } +) diff --git a/templates/shell/.licensure.yml b/templates/shell/.licensure.yml index e1f5a01..b7f963a 100644 --- a/templates/shell/.licensure.yml +++ b/templates/shell/.licensure.yml @@ -14,6 +14,24 @@ excludes: # No default license configuration is provided. This section must be # configured by the user. licenses: + - files: tex + ident: CC-BY-SA-4.0 + authors: + - name: Benedikt Peetz + email: "benedikt.peetz@b-peetz.de" + + template: | + 2024 - [year] (C) by [name of author] + SPDX-License-Identifier: CC-BY-SA-4.0 + + is licensed under a + Creative Commons Attribution-ShareAlike 4.0 International License. + + You should have received a copy of the license along with this + work. If not, see . + + unwrap_text: false + # Either a regex or the string "any" to determine to what files this # license should apply. It is common for projects to have files # under multiple licenses or with multiple copyright holders. This diff --git a/templates/shell/scripts/renew_copyright_header.sh b/templates/shell/scripts/renew_copyright_header.sh deleted file mode 120000 index f286da9..0000000 --- a/templates/shell/scripts/renew_copyright_header.sh +++ /dev/null @@ -1 +0,0 @@ -../../../common/renew_copyright_header.sh \ No newline at end of file diff --git a/templates/shell/scripts/renew_copyright_header.sh b/templates/shell/scripts/renew_copyright_header.sh new file mode 100755 index 0000000..edb0a65 --- /dev/null +++ b/templates/shell/scripts/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 diff --git a/templates/shell/treefmt.nix b/templates/shell/treefmt.nix deleted file mode 120000 index 6a4fe7d..0000000 --- a/templates/shell/treefmt.nix +++ /dev/null @@ -1 +0,0 @@ -../../treefmt.nix \ No newline at end of file diff --git a/templates/shell/treefmt.nix b/templates/shell/treefmt.nix new file mode 100644 index 0000000..1cbab40 --- /dev/null +++ b/templates/shell/treefmt.nix @@ -0,0 +1,73 @@ +{ + treefmt-nix, + pkgs, +}: +treefmt-nix.lib.evalModule pkgs ( + {pkgs, ...}: { + # Used to find the project root + projectRootFile = "flake.nix"; + + programs = { + alejandra.enable = true; + rustfmt.enable = true; + clang-format.enable = true; + mdformat.enable = true; + shfmt = { + enable = true; + indent_size = 4; + }; + shellcheck.enable = true; + prettier = { + settings = { + arrowParens = "always"; + bracketSameLine = false; + bracketSpacing = true; + editorconfig = true; + embeddedLanguageFormatting = "auto"; + endOfLine = "lf"; + # experimentalTernaries = false; + htmlWhitespaceSensitivity = "css"; + insertPragma = false; + jsxSingleQuote = true; + printWidth = 80; + proseWrap = "always"; + quoteProps = "consistent"; + requirePragma = false; + semi = true; + singleAttributePerLine = true; + singleQuote = true; + trailingComma = "all"; + useTabs = false; + vueIndentScriptAndStyle = false; + + tabWidth = 4; + overrides = { + files = ["*.js"]; + options.tabwidth = 2; + }; + }; + }; + stylua.enable = true; + ruff = { + enable = true; + format = true; + }; + taplo.enable = true; + }; + + settings = { + global.excludes = [ + "CHANGELOG.md" + "NEWS.md" + ]; + formatter = { + clang-format = { + options = ["--style" "GNU"]; + }; + shfmt = { + includes = ["*.bash"]; + }; + }; + }; + } +) -- cgit 1.4.1