knotfiles/dotfiles

132 lines
3.7 KiB
Plaintext
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env nu
# dotfiles -- 操作 dotfiles 相关环境的 CLI 工具
#
# 使用方式dotfiles <action>
#
# Copyright (C) 2022 KAAAsS
use knotfiles/log.nu
use knotfiles/dotfile
use active_config.nu
# 安装相关环境
def install [
modules
] {
log status $"安装路径:($env.HOME_DIR)\n"
# 安装依赖包
log succ "开始安装依赖包"; log info ""
dotfile do_hook $modules "install_dep" "依赖包安装"
# 运行安装前钩子
log succ "开始运行安装前钩子"; log info ""
dotfile do_hook $modules "pre_install" "安装前钩子"
# 链接
log succ "开始进行链接"; log info ""
dotfile do_link $modules
# 运行安装后钩子
log succ "开始运行安装后钩子"; log info ""
dotfile do_hook $modules "post_install" "安装后钩子"
log succ "安装完成!"
}
# 卸载相关环境
def uninstall [
modules
] {
log status $"安装路径:($env.HOME_DIR)\n"
# 运行卸载前钩子
log succ "开始运行卸载前钩子"; log info ""
dotfile do_hook $modules "pre_uninstall" "卸载前钩子"
# 清理
log succ "开始清理文件"; log info ""
dotfile do_unlink $modules
# 运行卸载后钩子
log succ "开始运行卸载后钩子"; log info ""
dotfile do_hook $modules "post_uninstall" "卸载后钩子"
# 卸载依赖包
log succ "开始卸载依赖包"; log info ""
dotfile do_hook $modules "uninstall_dep" "依赖包卸载"
log succ "卸载完成!"
}
# 同步配置文件
def sync [
modules
] {
# 链接
log succ "开始进行链接"; log info ""
dotfile do_link $modules
}
# 列举管理的配置文件
def list [
modules,
list_installed,
list_module,
] {
if ($list_module) {
dotfile filtered_modules $modules
} else {
let files = (
dotfile list_files $modules | select module dest | rename module path
)
if ($list_installed) {
$files | where ($it.path | path type) == "symlink"
} else {
$files
}
}
}
# 列举并检查所有环境约束
def constraints [] {
dotfile check_all_constraints
}
# knotfiles -- 操作 dotfiles 相关环境的 CLI 工具
def main [
action: string, # 待进行的操作。支持: install, uninstall, sync, list (ls), constraints
...modules: string, # 欲操作的模块,为空则处理全部模块
--home-dir (-H): string, # 家目录(即安装目录)
--verbose (-v), # 打印调试信息
--less, # 减少输出的内容
--list-installed (-I), # (list) 仅显示已安装的文件
--list-module (-M), # (list) 仅列举模块
--no-confirm, # 所有询问选择默认选项
--ignore-constraint (-C), # 忽略模块的约束检查
] {
# 家目录
$env.HOME_DIR = (
if ($home_dir != null) { $home_dir } else { $env.HOME }
| path expand
)
# 日志等级
$env.KNOT_LOG_LEVEL = (
if ($verbose) { 0 }
else { if ($less) { 12 } else { 10 } }
)
# 确认选项
$env.NO_CONFIRM = $no_confirm
# 约束检查
$env.IGNORE_CONSTRAINT = $ignore_constraint
# 加载配置集
active_config register
# 运行操作
if ($action == install) {
install $modules
} else if ($action == sync) {
sync $modules
} else if ($action == uninstall) {
uninstall $modules
} else if ($action == list or $action == ls) {
list $modules $list_installed $list_module
} else if ($action == constraints) {
constraints
} else {
log error "操作不存在!"
exit 1
}
}