diff options
Diffstat (limited to 'scripts/migrate-hypr-to-lua.sh')
| -rwxr-xr-x | scripts/migrate-hypr-to-lua.sh | 82 |
1 files changed, 68 insertions, 14 deletions
diff --git a/scripts/migrate-hypr-to-lua.sh b/scripts/migrate-hypr-to-lua.sh index ccbd6ed9..a3e4c78c 100755 --- a/scripts/migrate-hypr-to-lua.sh +++ b/scripts/migrate-hypr-to-lua.sh @@ -12,7 +12,7 @@ set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" REPO_DIR="$(cd "$SCRIPT_DIR/.." && pwd)" SRC_HYPR_DIR="$REPO_DIR/config/hypr" -DEST_HYPR_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/hypr" +DEST_HYPR_DIR="${XDG_CONFIG_HOME:-${XDG_CONFIG_HOME:-$HOME/.config}}/hypr" BACKUP_DIR="${DEST_HYPR_DIR}-backup-lua-$(date +%Y%m%d-%H%M%S)" MIGRATION_TS="$(date +%Y%m%d-%H%M%S)" DRY_RUN=0 @@ -49,6 +49,7 @@ DEST_WORKSPACES_CONF="$DEST_HYPR_DIR/workspaces.conf" DEST_LUA_WORKSPACES="$DEST_HYPR_DIR/lua/workspaces.lua" SOURCE_LUA_ENTRY_ENABLED="$SRC_HYPR_DIR/hyprland.lua" SOURCE_LUA_ENTRY_DISABLED="$SRC_HYPR_DIR/hyprland.lua.disable" +SRC_USER_LUA_TEMPLATES_DIR="$SRC_HYPR_DIR/UserConfigs" DEST_LUA_ENTRY="$DEST_HYPR_DIR/hyprland.lua" DEST_LUA_ENTRY_DISABLED="$DEST_HYPR_DIR/hyprland.lua.disable" SOURCE_LUA_ENTRY="" @@ -163,6 +164,51 @@ restore_latest_conf_backup() { fi } +has_active_hyprlang_content() { + local source_conf="$1" + [ -f "$source_conf" ] || return 1 + grep -Eq '^[[:space:]]*[^#[:space:]]' "$source_conf" +} + +lua_file_is_generated() { + local lua_path="$1" + [ -f "$lua_path" ] || return 1 + grep -Eq 'auto-generated|Converted from|No active entries were found|Source reference from' "$lua_path" +} + +ensure_template_for_empty_user_conf() { + local source_conf="$1" + local target_lua="$2" + local template_lua="$3" + + [ -f "$template_lua" ] || return 0 + + if has_active_hyprlang_content "$source_conf"; then + return 0 + fi + + if [ -f "$target_lua" ] && ! lua_file_is_generated "$target_lua"; then + echo "[INFO] Preserving customized Lua file for empty/default source: $target_lua" + return 0 + fi + + cp -f "$template_lua" "$target_lua" + echo "[OK] Ensured template for empty/default source: $source_conf -> $target_lua" +} + +ensure_templates_for_empty_user_configs() { + ensure_template_for_empty_user_conf "$USER_ENV_VARS" "$USER_CONFIGS_DIR/user_env.lua" "$SRC_USER_LUA_TEMPLATES_DIR/user_env.lua" + ensure_template_for_empty_user_conf "$USER_STARTUP_APPS" "$USER_CONFIGS_DIR/user_startup.lua" "$SRC_USER_LUA_TEMPLATES_DIR/user_startup.lua" + ensure_template_for_empty_user_conf "$USER_WINDOW_RULES" "$USER_CONFIGS_DIR/user_window_rules.lua" "$SRC_USER_LUA_TEMPLATES_DIR/user_window_rules.lua" + ensure_template_for_empty_user_conf "$USER_LAYER_RULES" "$USER_CONFIGS_DIR/user_layer_rules.lua" "$SRC_USER_LUA_TEMPLATES_DIR/user_layer_rules.lua" + ensure_template_for_empty_user_conf "$USER_KEYBINDS" "$USER_CONFIGS_DIR/user_keybinds.lua" "$SRC_USER_LUA_TEMPLATES_DIR/user_keybinds.lua" + ensure_template_for_empty_user_conf "$USER_SETTINGS" "$USER_CONFIGS_DIR/user_settings.lua" "$SRC_USER_LUA_TEMPLATES_DIR/user_settings.lua" + ensure_template_for_empty_user_conf "$USER_DECORATIONS" "$USER_CONFIGS_DIR/user_decorations.lua" "$SRC_USER_LUA_TEMPLATES_DIR/user_decorations.lua" + ensure_template_for_empty_user_conf "$USER_ANIMATIONS" "$USER_CONFIGS_DIR/user_animations.lua" "$SRC_USER_LUA_TEMPLATES_DIR/user_animations.lua" + ensure_template_for_empty_user_conf "$USER_LAPTOPS" "$USER_CONFIGS_DIR/user_laptops.lua" "$SRC_USER_LUA_TEMPLATES_DIR/user_laptops.lua" + ensure_template_for_empty_user_conf "$USER_CONFIGS_DIR/01-UserDefaults.conf" "$USER_CONFIGS_DIR/user_defaults.lua" "$SRC_USER_LUA_TEMPLATES_DIR/user_defaults.lua" +} + if [ "$YES" -eq 0 ]; then if [ "$REVERT" -eq 1 ]; then printf "[ACTION] Continue and revert Lua migration using latest legacy archives? [y/N] " @@ -199,10 +245,15 @@ if [ "$DRY_RUN" -eq 1 ]; then fi if [ -f "$DEST_LUA_ENTRY_DISABLED" ]; then echo "[DRY-RUN] Would enable Lua entrypoint: $DEST_LUA_ENTRY_DISABLED -> $DEST_LUA_ENTRY" + fi + if [ -n "$SOURCE_LUA_ENTRY" ]; then + if [ -f "$DEST_LUA_ENTRY" ]; then + echo "[DRY-RUN] Would refresh Lua entrypoint from template: $SOURCE_LUA_ENTRY -> $DEST_LUA_ENTRY" + else + echo "[DRY-RUN] Would install Lua entrypoint: $SOURCE_LUA_ENTRY -> $DEST_LUA_ENTRY" + fi elif [ -f "$DEST_LUA_ENTRY" ]; then - echo "[DRY-RUN] Lua entrypoint already enabled: $DEST_LUA_ENTRY" - elif [ -n "$SOURCE_LUA_ENTRY" ]; then - echo "[DRY-RUN] Would install Lua entrypoint: $SOURCE_LUA_ENTRY -> $DEST_LUA_ENTRY" + echo "[DRY-RUN] Lua entrypoint already enabled: $DEST_LUA_ENTRY (no source template available to refresh)" fi echo "[DRY-RUN] Would replace Lua module directory: $DEST_HYPR_DIR/lua" echo "[DRY-RUN] Would generate split configs/UserConfigs Lua overlays:" @@ -223,6 +274,7 @@ if [ "$DRY_RUN" -eq 1 ]; then echo "[DRY-RUN] - $USER_CONFIGS_DIR/user_animations.lua" echo "[DRY-RUN] - $USER_CONFIGS_DIR/user_laptops.lua" echo "[DRY-RUN] - $USER_CONFIGS_DIR/user_defaults.lua" + echo "[DRY-RUN] Would ensure repo Lua templates for empty/default UserConfigs/*.conf files." echo "[DRY-RUN] - $DEST_LUA_MONITORS (generated from $DEST_MONITORS_CONF)" echo "[DRY-RUN] - $DEST_LUA_WORKSPACES (generated from $DEST_WORKSPACES_CONF)" if [ -d "$USER_CONFIGS_DIR" ]; then @@ -264,11 +316,12 @@ if [ -f "$DEST_LUA_ENTRY_DISABLED" ]; then mv "$DEST_LUA_ENTRY_DISABLED" "$DEST_LUA_ENTRY" echo "[OK] Enabled Lua entrypoint: $DEST_LUA_ENTRY" fi -elif [ -f "$DEST_LUA_ENTRY" ]; then - echo "[INFO] Lua entrypoint already enabled: $DEST_LUA_ENTRY" -elif [ -n "$SOURCE_LUA_ENTRY" ]; then +fi +if [ -n "$SOURCE_LUA_ENTRY" ]; then cp -f "$SOURCE_LUA_ENTRY" "$DEST_LUA_ENTRY" - echo "[OK] Installed Lua entrypoint: $SOURCE_LUA_ENTRY -> $DEST_LUA_ENTRY" + echo "[OK] Installed/updated Lua entrypoint: $SOURCE_LUA_ENTRY -> $DEST_LUA_ENTRY" +elif [ -f "$DEST_LUA_ENTRY" ]; then + echo "[INFO] Lua entrypoint already enabled: $DEST_LUA_ENTRY (source template unavailable; keeping existing entrypoint)" else echo "[ERROR] Unable to locate a Lua entrypoint to enable." >&2 exit 1 @@ -1483,16 +1536,16 @@ system_keybind_lines = [ " end", " if name == \"cyclenext\" then", " if args == \"prev\" or args == \"b\" then", - " return exec_cmd(\"$HOME/.config/hypr/scripts/LuaCycleWindow.sh previous\")", + " return exec_cmd(\"${XDG_CONFIG_HOME:-$HOME/.config}/hypr/scripts/LuaCycleWindow.sh previous\")", " end", - " return exec_cmd(\"$HOME/.config/hypr/scripts/LuaCycleWindow.sh next\")", + " return exec_cmd(\"${XDG_CONFIG_HOME:-$HOME/.config}/hypr/scripts/LuaCycleWindow.sh next\")", " end", " if name == \"swapwindow\" then", " local swap_direction = trim(args)", " if swap_direction == \"\" then", " return nil", " end", - " return exec_cmd(\"$HOME/.config/hypr/scripts/LuaSwapWindow.sh \" .. swap_direction)", + " return exec_cmd(\"${XDG_CONFIG_HOME:-$HOME/.config}/hypr/scripts/LuaSwapWindow.sh \" .. swap_direction)", " end", " if name == \"workspace\" and dsp and dsp.focus then", " return function() hl.dispatch(dsp.focus({ workspace = workspace_value(args) })) end", @@ -1688,7 +1741,7 @@ startup_lines = [ "-- User startup overrides (auto-generated).", "-- Add commands with exec_once(\"your command\")", "-- Example:", - "-- exec_once(\"$HOME/.config/hypr/UserScripts/MyStartup.sh\")", + "-- exec_once(\"${XDG_CONFIG_HOME:-$HOME/.config}/hypr/UserScripts/MyStartup.sh\")", "", "local user_startup_helper = nil", "do", @@ -1870,7 +1923,7 @@ keybind_lines = [ "-- Add keybinds with bind(\"MODS\", \"KEY\", fn, opts).", "-- Example:", "-- bind(\"SUPER\", \"Z\", exec_cmd(\"ghostty\"), { description = \"Launch ghostty\" })", - "-- Helper functions live in ~/.config/hypr/lua/user_keybinds_helper.lua so they can be updated separately.", + "-- Helper functions live in ${XDG_CONFIG_HOME:-$HOME/.config}/hypr/lua/user_keybinds_helper.lua so they can be updated separately.", "local user_keybinds_helper = nil", "do", " local source = (debug.getinfo(1, \"S\") or {}).source or \"\"", @@ -2085,6 +2138,7 @@ for name, source in [ lines.append(f"-- No active entries were found in {source.name}.") write_file(files_out[name], lines) PY +ensure_templates_for_empty_user_configs cat > "$USER_OVERRIDES_SHIM" <<'LUA' -- ================================================== @@ -2094,7 +2148,7 @@ cat > "$USER_OVERRIDES_SHIM" <<'LUA' -- SPDX-License-Identifier: GPL-3.0-or-later -- ================================================== -- Auto-generated by scripts/migrate-hypr-to-lua.sh. --- Loads split system/user Lua files from ~/.config/hypr/configs and ~/.config/hypr/UserConfigs. +-- Loads split system/user Lua files from ${XDG_CONFIG_HOME:-$HOME/.config}/hypr/configs and ${XDG_CONFIG_HOME:-$HOME/.config}/hypr/UserConfigs. local configHome = os.getenv("XDG_CONFIG_HOME") or ((os.getenv("HOME") or "") .. "/.config") local hyprDir = configHome .. "/hypr" local systemDir = hyprDir .. "/configs" |
