Compare commits

...

3 Commits

Author SHA1 Message Date
38850ebf8e
refactor: 支持 0.85.0 2023-10-20 16:13:28 +08:00
91efeb74b8
feat(constraints): gui 判断增加 MacOS 2023-10-17 11:38:32 +08:00
477a579044
feat(autostart): 支持 MacOS 2023-10-17 11:38:03 +08:00
10 changed files with 110 additions and 33 deletions

View File

@ -98,19 +98,19 @@ def main [
--ignore-constraint (-C), # 忽略模块的约束检查
] {
# 家目录
let-env HOME_DIR = (
$env.HOME_DIR = (
if ($home_dir != null) { $home_dir } else { $env.HOME }
| path expand
)
# 日志等级
let-env LOG_LEVEL = (
$env.LOG_LEVEL = (
if ($verbose) { 0 }
else { if ($less) { 12 } else { 10 } }
)
# 确认选项
let-env NO_CONFIRM = $no_confirm
$env.NO_CONFIRM = $no_confirm
# 约束检查
let-env IGNORE_CONSTRAINT = $ignore_constraint
$env.IGNORE_CONSTRAINT = $ignore_constraint
# 加载配置集
active_config register
# 运行操作

View File

@ -2,13 +2,15 @@
#
# Copyright (C) 2022 KAAAsS
# 设置自启脚本
export def install [
path: path
] {
let filename = ($path | path basename)
mkdir $"($env.HOME_DIR)/.config/autostart"
echo $"[Desktop Entry]
use constraints.nu
module linux {
export def install [
path: path
] {
let filename = ($path | path basename)
mkdir $"($env.HOME_DIR)/.config/autostart"
echo $"[Desktop Entry]
Exec=($path)
Icon=dialog-scripts
Name=($filename)
@ -16,6 +18,68 @@ Path=
Type=Application
X-KDE-AutostartScript=true
" | save -f $"($env.HOME_DIR)/.config/autostart/($filename).desktop"
null
}
export def uninstall [
path: path
] {
let filename = ($path | path basename)
rm -f $"($env.HOME_DIR)/.config/autostart/($filename).desktop"
null
}
}
module darwin {
export def install [
path: path
] {
let filename = ($path | path basename)
mkdir $"($env.HOME_DIR)/Library/LaunchAgents"
echo $"<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
<plist version=\"1.0\">
<dict>
<key>Label</key>
<string>($filename)</string>
<key>ProgramArguments</key>
<array>
<string>($path)</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
" | save -f $"($env.HOME_DIR)/Library/LaunchAgents/($filename).plist"
launchctl load $"($env.HOME_DIR)/Library/LaunchAgents/($filename).plist"
null
}
export def uninstall [
path: path
] {
let filename = ($path | path basename)
launchctl stop $"($filename)"
launchctl unload $"($env.HOME_DIR)/Library/LaunchAgents/($filename).plist"
rm -f $"($env.HOME_DIR)/Library/LaunchAgents/($filename).plist"
null
}
}
use linux
use darwin
# 设置自启脚本
export def install [
path: path
] {
if (constraints os linux) {
linux install $path
} else if (constraints os darwin) {
darwin install $path
} else {
error make { msg: "当前系统不支持自启脚本" }
}
null
}
@ -23,7 +87,12 @@ X-KDE-AutostartScript=true
export def uninstall [
path: path
] {
let filename = ($path | path basename)
rm -f $"($env.HOME_DIR)/.config/autostart/($filename).desktop"
if (constraints os linux) {
linux uninstall $path
} else if (constraints os darwin) {
darwin uninstall $path
} else {
error make { msg: "当前系统不支持自启脚本" }
}
null
}

View File

@ -21,15 +21,23 @@ module os {
}
}
export use os
# 图形界面约束
export def gui [] {
use pkg.nu
pkg check_install? libxrandr
if (os linux) {
# Linux 暂时只考虑 X11
pkg check_install? libxrandr
} else if (os darwin) {
# MacOS 都是图形界面
true
} else {
false
}
}
export use os
# 获得默认的约束
export def default_constraints [] {
{

View File

@ -9,7 +9,7 @@ use global_conf.nu
# 注册自定义约束
export def-env register_custom_constraint [
name: string, # 约束名
fn: block, # 约束检查函数,返回 bool
fn: closure, # 约束检查函数,返回 bool
] {
log debug $"自定义约束: ($name): ($fn)"
let constraints = (global_conf get_config "_" "constraints")
@ -62,7 +62,7 @@ export def check_constraint [
export def check_all_constraints [] {
let constraints = (global_conf get_config "_" "constraints")
mut result = null
mut result = []
for $cons in ($constraints | transpose key).key {
$result = ($result | append {
"constraint": $cons,

View File

@ -6,7 +6,7 @@ use ../constraints.nu
export-env {
# 全局配置
let-env modules_config = { "_":
$env.modules_config = { "_":
{ "constraints": (constraints default_constraints) }
}
}
@ -57,7 +57,7 @@ export def-env set_config [
$old_config
| merge { $config : $value }
)
let-env modules_config = (
$env.modules_config = (
$modules_config
| merge { $module : $config }
)

View File

@ -36,12 +36,12 @@ export def-env register_post_install [module: string, hook: closure] {
}
# 注册模块卸载前运行钩子
export def-env register_pre_uninstall [module: string, hook: block] {
export def-env register_pre_uninstall [module: string, hook: closure] {
register_hook "pre_uninstall" $module $hook
}
# 注册模块卸载后运行钩子
export def-env register_post_uninstall [module: string, hook: block] {
export def-env register_post_uninstall [module: string, hook: closure] {
register_hook "post_uninstall" $module $hook
}

View File

@ -18,7 +18,7 @@ export def filtered_modules [
$modules
} else {
# 检查模块是否存在
mut result = null
mut result = []
for $module in $want_modules {
if (not ($module in $modules)) {
log error $"模块 ($module) 不存在!"
@ -34,7 +34,7 @@ export def filtered_modules [
if ("IGNORE_CONSTRAINT" in $env and $env.IGNORE_CONSTRAINT) {
return $modules
}
mut result = null
mut result = []
for $module in $modules {
let constraints = (global_conf get_config $module "constraints" [])
let check = (

View File

@ -21,7 +21,7 @@ def get_base_dir [local: path, dest: path] {
# 声明文件及其目标路径
export def-env declare [
module: string, # 模块名称
local: path, # 本地路径,以项目根目录为起点的相对路径
local: any, # 本地路径,以项目根目录为起点的相对路径
dest: string, # 目标路径,以用户家目录为起点的相对路径
--local-only: bool, # 是否是本地特有的文件。若文件不存在则会自动从 example 创建本地文件
] {
@ -33,7 +33,7 @@ export def-env declare [
}
# 遍历 local 路径,获得映射
mut new_maps = null
mut new_maps = []
for $local in $locals {
let dest = if ($type == "list<string>") {
# 对于文件列表(如 glob把文件名追加在最后

View File

@ -3,7 +3,7 @@
# Copyright (C) 2022 KAAAsS
def-env confirm_loop [ prompt: string, default: string ] {
let-env result = if ($env.result == null) {
$env.result = if ($env.result == null) {
let got = (input $prompt)
if ($got == "y" or $got == "n") {
$got
@ -43,7 +43,7 @@ export def confirm [
$default == "y"
} else {
# 尝试获得结果
let-env result = null
$env.result = null
# 最多尝试 5 次(因为 nushell 的限制暂时无法在循环里编辑环境)
confirm_loop $prompt $default
@ -67,7 +67,7 @@ def main [] {
print (confirm "测试 1")
print (confirm "测试 2" "y")
print (confirm "测试 3" "n")
let-env NO_CONFIRM = true
$env.NO_CONFIRM = true
print (confirm "测试 2" "y")
print (confirm "测试 3" "n")
print (confirm "测试 1")

View File

@ -6,13 +6,13 @@ use pkgs/homebrew.nu
export-env {
# 所有可用的包管理器
let-env package_managers = [
$env.package_managers = [
(pacman pack),
(homebrew pack),
(fallback_manager)
]
# 计算默认包管理器
let-env default_package_manager = (select_managers)
$env.default_package_manager = (select_managers)
}
def select_managers [
@ -184,7 +184,7 @@ export def test [] {
use testing *
# 增加 Mock 管理器
let-env package_managers = [
$env.package_managers = [
{
"name": "unavailable",
"available?": { false }
@ -202,7 +202,7 @@ export def test [] {
"check_install?": {|p| $p == "mock2" },
}
]
let-env default_package_manager = (select_managers)
$env.default_package_manager = (select_managers)
# select_managers
let mock = (select_managers "mock2")