Skip to content

Commit

Permalink
fix bash script handling
Browse files Browse the repository at this point in the history
  • Loading branch information
blitter committed Mar 27, 2024
1 parent 9d031cd commit a1583d8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
18 changes: 8 additions & 10 deletions modules/apps/kate/check-theme-name-free.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ NORMAL=$(tput sgr0)
# #
# # https://www.baeldung.com/linux/bash-check-script-arguments

if [[ "$#" -ne 2 ]]; then
if [[ "$#" -ne 1 ]]; then
# https://stackoverflow.com/questions/3005963/how-can-i-have-a-newline-in-a-string-in-sh/3182519#3182519
>&2 printf "${RED}${BOLD}Incorrect number of arguments.${NORMAL}${RED} Expected three:\n * Name of the theme that should not already be in use\n * the path to the jq executable"
>&2 printf "%sIncorrect number of arguments.%s Expected one: The name of the theme that should not already be in use" "${RED}${BOLD}" "${NORMAL}${RED}"
exit 1
fi

THEMENAME=$1
jqexec=$2


# =====================================
Expand All @@ -30,17 +29,16 @@ jqexec=$2

THEME_DIR="${XDG_DATA_HOME:-$HOME/.local/share}/org.kde.syntax-highlighting/themes/"

ls "${THEME_DIR}" | while read -r themefile; do
FULL_PATH="${THEME_DIR}/${themefile}"
THIS_THEMENAME=$(${jqexec} -r .metadata.name "${FULL_PATH}")
# TODO skip if file is a symlink to /nix/store"
# TODO and symlinks! (or: skip over dirs)
find "${THEME_DIR}" \( -type f -o -type l \) | while read -r themefile; do
THIS_THEMENAME=$(jq -r .metadata.name "${themefile}")

if [[ "${THIS_THEMENAME}" == "${THEMENAME}" ]]; then
if [[ "${THIS_THEMENAME}" == "${themefile}" ]]; then
# make sure to not look at symbolic links to the nix store
# https://stackoverflow.com/questions/17918367/linux-shell-verify-whether-a-file-exists-and-is-a-soft-link/17918442#17918442
# https://stackoverflow.com/questions/2172352/in-bash-how-can-i-check-if-a-string-begins-with-some-value/2172367#2172367
if [[ ! ( -L "${FULL_PATH}" && $(readlink -f "${FULL_PATH}") == /nix/store/* ) ]]; then
>&2 printf "${RED}${BOLD}In ${THEME_DIR} there is already a theme with the name ${THEMENAME} (${themefile}).${NORMAL}${RED} You could rename the theme given in config.programs.kate.editor.theme.src by changing the value for metadta.name inside the theme."
if [[ ! ( -L "${themefile}" && $(readlink -f "${themefile}") == /nix/store/* ) ]]; then
>&2 printf "%s In %s there is already a theme with the name %s (%s).%s You could rename the theme given in config.programs.kate.editor.theme.src by changing the value for metadata.name inside the theme." "${RED}${BOLD}" "${THEME_DIR}" "${THEMENAME}" "${themefile}" "${NORMAL}${RED}"
exit 1 # even on dryrun
fi
fi
Expand Down
19 changes: 18 additions & 1 deletion modules/apps/kate/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ let
if (indentSettings.undoByShiftTab && indentSettings.tabFromEverywhere) then 1 else
2
);

checkThemeNameScript = pkgs.writeShellApplication {
name = "checkThemeName";
runtimeInputs = with pkgs; [ jq ];
text = builtins.readFile ./check-theme-name-free.sh;
};

checkThemeName = name:
''
${checkThemeNameScript}/bin/checkThemeName ${name}
'';

script = pkgs.writeScript "kate-check" (checkThemeName cfg.editor.theme.name);
in
{
options.programs.kate = {
Expand Down Expand Up @@ -167,9 +180,13 @@ in
# In case of using a custom theme, check that there is no name collision
home.activation.checkKateTheme = lib.mkIf (cfg.enable && cfg.editor.theme.src != null) (lib.hm.dag.entryBefore [ "writeBoundary" ]
''
${./check-theme-name-free.sh} ${cfg.editor.theme.name} ${pkgs.jq}/bin/jq
$DRY_RUN_CMD ${script}
'');
# ${./check-theme-name-free.sh} ${cfg.editor.theme.name} ${pkgs.jq}/bin/jq

# In case of using a system theme, there should be a check that there exists such a theme
# but I could not figure out where to find them
# That's why there is no check for now
# See also [the original PR](https://github.com/pjones/plasma-manager/pull/95#issue-2206192839)
};
}

0 comments on commit a1583d8

Please sign in to comment.