From e2fa66f87ad9bc404450f51ba96a336976d710b6 Mon Sep 17 00:00:00 2001 From: Nuclear Squid Date: Wed, 19 Mar 2025 17:47:07 +0100 Subject: [PATCH] feat(qmk): made the install script safer on rerun. --- qmk/arsenik-qmk.sh | 79 +++++++++++++++++++++++++++++++++++++++++--- qmk/arsenik_config.h | 2 +- 2 files changed, 75 insertions(+), 6 deletions(-) diff --git a/qmk/arsenik-qmk.sh b/qmk/arsenik-qmk.sh index 36e5e36..408664c 100755 --- a/qmk/arsenik-qmk.sh +++ b/qmk/arsenik-qmk.sh @@ -7,8 +7,8 @@ if [ "$#" -lt 1 ]; then echo "$0 [-n] [-b,-f]" echo " # Install Arsenik for your keyboard, opens your editor to edit the config" echo ' # -n: no editor: doesn’t open the config with your $EDITOR' - echo " # -b: build the newly created keymap (after you edited the config)" - echo " # -f: build the newly created keymap (after you edited the config) then flash" + echo " # -c: compile the newly created keymap (after you edited the config)" + echo " # -f: compile the newly created keymap (after you edited the config) then flash" exit 0 fi @@ -39,6 +39,74 @@ function get_keymaps_folder() { echo "$QMK_PATH/keyboards/$keyboard_name/keymaps" } +function handle_keymap_conflict() { + local arsenik_folder="$1" + local script_name=$(basename $0) + + echo -e "\x1b[1;31mError :\x1b[0m The Arsenik keymap already exists for this keyboard." + echo -e "$script_name should \x1b[4mnot\x1b[0m be used to recompile your keymap, this is an \x1b[4minstall\x1b[0m script." + echo "This is because some keyboards may need extra info to properly compile or flash the" + echo "firmware (like if you need to specify the bootloader)" + echo + + local old_arsenik_keymap_folder="${arsenik_folder::-1}" + local old_arsenik_keymap_id=1 + + while [ -d "$old_arsenik_keymap_folder"_"$old_arsenik_keymap_id" ]; do + old_arsenik_keymap_id=$((old_arsenik_keymap_id + 1)) + done + + local keymap_backup_folder="$old_arsenik_keymap_folder"_"$old_arsenik_keymap_id" + + echo "If you want to reinstall the Arsenik keymap, your old keymap will be moved to this folder:" + echo " \`$keymap_backup_folder\`" + echo + + echo "If you want to recompile and flash your keymap, you should do so with the commands provided by QMK" + echo " Using the QMK CLI: \`qmk flash -kb $keyboard_name -km arsenik\`" + echo " (docs: https://docs.qmk.fm/cli_commands)" + echo " Using the \`make\` command: \`make $keyboard_name:arsenik:flash\` run in $QMK_PATH" + echo " (docs: https://docs.qmk.fm/getting_started_make_guide)" + echo + echo "Note: Those commands may not work out of the box, some keyboards need extra cmdline arguments" + echo "(for instance if you need to specify the bootloader explicitely)" + echo "If you have an error, check the docs mentionned above." + echo + + echo "What do you want to do?" + COLUMNS=1 # Display options from select in a single column + select cmd in \ + "Backup keymap and reinstall Arsenik" \ + "Recompile and flash keymap with the QMK CLI" \ + "Recompile and flash keymap with \`make\`" \ + "Abort" + do + case $cmd in + "Backup keymap and reinstall Arsenik") + mv "$arsenik_folder" "$keymap_backup_folder" + return 0 + ;; + "Recompile and flash keymap with the QMK CLI") + cd "$QMK_PATH" + make "$keyboard_name":arsenik:flash + return 1 + ;; + "Recompile and flash keymap with \`make\`") + qmk flash -kb $keyboard_name -km arsenik + return 1 + ;; + "Abort") + echo "Aborting." + return 1 + ;; + *) + echo "Unknown command chosen. Aborting." + return 1 + ;; + esac + done +} + function make_new_arsenik_keymap() { local keyboard_name="$1" local qmk_cmd="$2" @@ -48,8 +116,9 @@ function make_new_arsenik_keymap() { local arsenik_folder="$keymap_folder/arsenik/" local default_keymap_folder="$keymap_folder/default" + [ -d "$arsenik_folder" ] && handle_keymap_conflict "$arsenik_folder" + cp -r "$default_keymap_folder" "$arsenik_folder" - ls -l "$arsenik_folder" local layout="" case $(ls "$default_keymap_folder"/keymap.* | sed 's/.*\(keymap.*\)/\1/') in @@ -78,7 +147,7 @@ function make_new_arsenik_keymap() { case "$qmk_cmd" in "none") ;; - "build") cd "$QMK_PATH" && make "$keyboard_name:arsenik";; + "compile") cd "$QMK_PATH" && make "$keyboard_name:arsenik";; "flash") cd "$QMK_PATH" && make "$keyboard_name:arsenik:flash";; esac } @@ -89,7 +158,7 @@ qmk_cmd="none" for arg in "${@:2}"; do case "$arg" in "-n") no_editor=true;; - "-b") qmk_cmd="build";; + "-c") qmk_cmd="compile";; "-f") qmk_cmd="flash";; *) echo "Unknown argument $arg."; exit 1;; esac diff --git a/qmk/arsenik_config.h b/qmk/arsenik_config.h index 506653a..3cda53c 100644 --- a/qmk/arsenik_config.h +++ b/qmk/arsenik_config.h @@ -120,7 +120,7 @@ // Lists of layouts supported by Arsenik. Some parts of the config are dependent -// on keyboard layout used on your computer. If they don’t match up some +// on the keyboard layout used on your computer. If they don’t match up some // characters may not be correctly placed or missing entirely. If multiple // options are toggled at the same time, the first one is chosen. #define ARSENIK_HOST_LAYOUT_QWERTY