Skip to content

Commit

Permalink
Merge pull request #3 from ChengLuffy/option-o
Browse files Browse the repository at this point in the history
add Option `-o`
  • Loading branch information
ChengLuffy authored Mar 27, 2023
2 parents a9607f7 + 4fd81e0 commit bae9c10
Show file tree
Hide file tree
Showing 15 changed files with 259 additions and 39 deletions.
26 changes: 22 additions & 4 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'appcu alias org.gimp.gimp-2.10 gimp'",
"name": "Debug executable 'appcu -vno /Applications/Visual Studio Code.app/'",
"cargo": {
"args": [
"build",
Expand All @@ -19,7 +19,25 @@
"kind": "bin"
}
},
"args": ["alias org.gimp.gimp-2.10 gimp"],
"args": ["-vno", "/Applications/Visual Studio Code.app/"],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'appcu alias /Applications/GIMP.app/ gimp'",
"cargo": {
"args": [
"build",
"--bin=appcu",
"--package=appcu"
],
"filter": {
"name": "appcu",
"kind": "bin"
}
},
"args": ["alias", "/Applications/GIMP.app/", "gimp"],
"cwd": "${workspaceFolder}"
},
{
Expand Down Expand Up @@ -61,7 +79,7 @@
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'appcu generate_config'",
"name": "Debug executable 'appcu generate-config'",
"cargo": {
"args": [
"build",
Expand All @@ -73,7 +91,7 @@
"kind": "bin"
}
},
"args": ["generate_config"],
"args": ["generate-config"],
"cwd": "${workspaceFolder}"
},
{
Expand Down
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# CHANGELOG

## Unreleased
### Added
- 添加 `-o` 选项,当使用此选项时,输出结果打印应用路径或 MAS 链接,点击通知时打开应用或者 MAS 主页

## [0.1.6] - 2023-03-16
### Fixes
Expand Down Expand Up @@ -32,7 +34,7 @@
## [0.1.2] - 2023-02-22
### Fixes
- 修复 `--version` 输出错误的版本信息
- 修复 `generate_config` 即使输入 `y` 也无法正常运行的问题
- 修复 `generate-config` 即使输入 `y` 也无法正常运行的问题
- 修复 `ignore` `alias` 等命令无法按预期运行的问题
### Added
- 添加安装脚本
Expand Down
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ appcu
appcu /Applications/xx.app /Applications/yy.app
```

使用 `terminal-notifier` 发送检查更新结果
```
appcu -n
```

使用 `terminal-notifier` 发送检查更新结果时,点击通知打开应用或商店
```
appcu -no
```

### 参数说明
#### generate-config/gc
简写: `gc`,用于生成配置文件,详情请查看 `appcu help generate-config`
Expand Down Expand Up @@ -58,9 +68,14 @@ appcu alias /Applications/xx.app alias_name

结合 `crontab` 可以实现定时检查更新,例如每天早上10点运行
```
0 10 * * * appcu -n
0 10 * * * /path/to/appcu -n
```

#### -o
在不使用此选项时,输出结果默认是最新版本的下载链接,包括通知的点击事件

当使用此选项时,输出结果打印应用路径或 MAS 链接,点击通知时打开应用或者 MAS 主页

#### -v
输出所有详细信息,包括应用信息、应用匹配的检查更新方式、应用最新版本地址、应用版本号,**包括未发现新版本的应用信息**

Expand Down Expand Up @@ -148,5 +163,7 @@ macOS 安装应用方式多样,批量检查更新的有效方式很少,之
### 为什么采用 `Rust`
其实这样的应用采用 `Swift` 应该更好,但是之前看到好多安利 `Rust` 的文章,想着自己试一试这门语言,但是由于自己学习方法的不成熟,总是用写 iOS 应用的经验套用在 `Rust` 上,所以写的并不好。

如果您有兴趣查看我的代码并提出更改意见的,那将是对我的最大帮助

## License
应用采用 `MIT` 开源协议,详见 [LICENSE](LICENSE)
67 changes: 52 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,26 @@ pub mod request;
pub mod version_cmp;

lazy_static! {
/// 忽略的应用 bundle_id 集合
static ref IGNORES: Vec<String> = local::config::get_ignore_config();
/// homebrew 查询别名映射集合
static ref ALIAS: HashMap<String, String> = local::config::get_alias_config();
/// MAS 备用查询区域代码集合
static ref MASAREAS: Vec<String> = local::config::get_mas_areas();
/// 设备版本信息
static ref ARM_SYSTEM_NAME: String = local::plist::get_arm_system_version();
/// terminal-notifier 的安装路径
static ref TERMINAL_NOTIFIER_PATH: String = local::config::get_terminal_notifier_path();
/// 并发查询数量
static ref THREADNUMS: usize = local::config::get_thread_nums();
}

/// 检查更新配置
#[derive(Copy, Clone)]
pub struct CheckOperation {
pub notification: bool,
pub verbose: bool,
pub open_by_app: bool,
}

impl CheckOperation {
Expand All @@ -32,10 +40,12 @@ impl CheckOperation {
let path = Path::new(&item);
let buf = path.to_path_buf();
if let Some(app_info) = local::check_app_info(&buf) {
self.check_update(app_info)
self.check_update(app_info, path)
} else if self.notification {
// 通知发送应用信息读取失败
Notification::new_error_notification(format!("{item} 应用信息读取失败")).post()
} else {
// 打印应用信息读取失败
println!("+++++");
println!("{item} 应用信息读取失败");
println!("+++++\n");
Expand All @@ -54,28 +64,34 @@ impl CheckOperation {
pool.execute(move || match item {
Ok(path) => {
let app_info = local::check_app_info(&path.path());
// 这里不处理 else,原因是默认忽略以 `.` 开头的路径和不以 `.app` 结尾的路径
if let Some(info) = app_info {
temp_self.check_update(info);
temp_self.check_update(info, &path.path());
}
}
Err(error) => {
println!("+++++");
println!("{error:?}");
println!("+++++\n");
Notification::new_error_notification(format!("{error:?}")).post()
if temp_self.notification {
Notification::new_error_notification(format!("{error:?}")).post()
} else {
println!("+++++");
println!("{error:?}");
println!("+++++\n");
}
}
});
}
pool.join();
}

/// 根据应用类型查询更新并输出
pub fn check_update(&self, app_info: AppInfo) {
pub fn check_update(&self, app_info: AppInfo, path: &Path) {
let check_update_type = &app_info.check_update_type;
// 默认 version 为 `-1`,约定查询完毕后还是 `-1` 的话就是查询失败
let mut remote_info: RemoteInfo = RemoteInfo {
version: "-1".to_string(),
update_page_url: "".to_string(),
};
// 最多尝试五次
for _ in 0..5 {
remote_info = match check_update_type {
CheckUpType::Mas {
Expand All @@ -87,12 +103,12 @@ impl CheckOperation {
app_name,
bundle_id,
} => request::homebrew_check(app_name, bundle_id),
// _ => RemoteInfo { version: "-2".to_string(), update_page_url: String::new() }
};
if remote_info.version != *"-1" {
break;
}
}
// 处理查询失败的情况
if remote_info.version == *"-1" {
if self.notification {
Notification::new_remote_get_failed(&app_info).post()
Expand All @@ -108,8 +124,7 @@ impl CheckOperation {
// FIXME: 丑陋的代码,这一段代码变成这样的原因,Sparkle 应用各有各的写法,有的应用只有从 title 读取版本号,有的从 item 有的从 enclosure
// FIXME: 版本号也有问题,有的 sparkle:version 是 x.x.x 的形式,有的 sparkle:shortVersionString 是
// FIXME: homebrew 的接口也有点问题,比如 Version 是 4.0,通过接口查询会变成 4,比如有些应用本地查到是 7.0.2,接口查到是 7.0.2.7,但其实是一个版本
let local_cmp_version = if !app_info.short_version.is_empty()
&& !matches!(app_info.check_update_type, CheckUpType::Sparkle(_))
let local_cmp_version = if !app_info.short_version.is_empty() && !app_info.is_sparkle_app()
|| (remote_info.version.contains('.') && app_info.short_version.contains('.'))
{
&app_info.short_version
Expand All @@ -118,13 +133,20 @@ impl CheckOperation {
};
let ordering = cmp_version(local_cmp_version, &remote_info.version, false);
if ordering.is_lt() {
// if &remote_info.version != "-2" {
// 判断为有更新
if self.notification {
// 判断需要使用通知发送结果
Notification::new_update_notification(
app_info.name,
app_info.name.clone(),
local_cmp_version.to_string(),
remote_info.version,
remote_info.update_page_url,
// mas 应用还是打开 Mac App Store
if self.open_by_app && !&app_info.is_mas_app() {
format!("file://{}", path.to_str().unwrap_or_default())
} else {
remote_info.update_page_url
},
self.open_by_app,
)
.post()
} else {
Expand All @@ -134,15 +156,26 @@ impl CheckOperation {
println!("{}", app_info.check_update_type);
}
println!("{local_cmp_version} -> {}", remote_info.version);
println!("{}", remote_info.update_page_url);
if self.open_by_app {
println!("file://{}", path.to_str().unwrap_or_default());
} else {
println!("{}", remote_info.update_page_url);
}
println!("=====\n");
}
} else if self.verbose {
// 判断为没有更新但是需要详细输出
if self.notification {
Notification::new_verbose_notification(
&app_info,
local_cmp_version.to_string(),
&remote_info,
self.open_by_app,
Some(if self.open_by_app && !&app_info.is_mas_app() {
format!("file://{}", path.to_str().unwrap_or_default())
} else {
remote_info.update_page_url.to_owned()
}),
)
.post()
} else {
Expand All @@ -151,7 +184,11 @@ impl CheckOperation {
println!("{}", app_info.check_update_type);
println!("local version {local_cmp_version}");
println!("remote version {}", remote_info.version);
println!("{}", remote_info.update_page_url);
if self.open_by_app && !app_info.is_mas_app() {
println!("file://{}", path.to_str().unwrap_or_default());
} else {
println!("{}", remote_info.update_page_url);
}
println!("-----\n");
}
}
Expand Down
10 changes: 8 additions & 2 deletions src/local/config/mod.rs → src/local/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@ use std::{collections::HashMap, ffi::OsString, fs};

use crate::{local::check_app_info, IGNORES};

/// 配置文件结构体
#[derive(Debug, Serialize, Deserialize)]
pub struct Config {
/// 并发查询数
pub threads_num: usize,
/// terminal-notifier 安装路径
pub terminal_notifier_path: String,
/// Mac App Store 备用查询区域集合
pub mas_area: Vec<String>,
/// Homebrew 查询别名映射
pub alias: HashMap<String, String>,
/// 忽略查询应用集合
pub ignore: Vec<String>,
}

/// 实现 default trait
impl Default for Config {
fn default() -> Self {
let ignore = vec![
Expand Down Expand Up @@ -137,12 +144,11 @@ fn get_config() -> Option<Config> {
let mut path = dirs::home_dir().expect("未能定位到用户目录");
path.push(".config/appcu/config.yaml");
if path.exists() {
let content = fs::read_to_string(path).expect("读取配置文件时发生错误,`~/.config/appcu/config.yaml` 路径下不存在配置文件,您可以使用 `appcu generate_config` 生成一份默认配置文件");
let content = fs::read_to_string(path).expect("读取配置文件时发生错误,`~/.config/appcu/config.yaml` 路径下不存在配置文件,您可以使用 `appcu generate-config` 生成一份默认配置文件");
let config: Config =
serde_yaml::from_str(&content).expect("解析配置文件时发生错误,配置文件格式错误");
Some(config)
} else {
println!("未查到配置文件,您可以使用 `appcu generate_config` 生成一份默认配置文件");
None
}
}
Expand Down
Loading

0 comments on commit bae9c10

Please sign in to comment.