Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: auto set remote url based on locale #141

Merged
merged 2 commits into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -458,4 +458,5 @@ url = "https://github.com/MaaAssistantArknights/MaaResource.git"
- 即使启动了资源热更新,你依然需要安装 `MaaCore` 的资源,因为资源热更新并不包含所有的资源文件,只是包含部份可更新的资源文件,基础资源文件仍然需要安装。
- 资源热更新是通过 Git 来拉取远程仓库,如果后端设置为 `git` 那么 `git` 命令行工具必须可用。
- 如果你想要使用 SSH 协议来拉取远程仓库,你必须配置 `ssh_key` 字段,这个字段应该是一个路径,指向你的 SSH 私钥。
- `url` 设置目前只对首次安装资源有效,如果你想要更改远程仓库的地址,你需要通过 `git` 命令行工具手动更改,或者删除对应的仓库。仓库所在位置可以通过 `maa dir hot-update` 获取。
- 远程仓库的 `url` 设置目前只对首次安装资源有效,如果你想要更改远程仓库的地址,你需要通过 `git` 命令行工具手动更改,或者删除对应的仓库。仓库所在位置可以通过 `maa dir hot-update` 获取。
- 远程仓库的`url` 会根据你本机的语言自动设置,如果你的语言是简体中文,那么远程仓库的 `url` 将会被设置为国内的镜像 `https://git.maa-org.net/MAA/MaaResource.git`, 在其他情况则会被设置为 Github。如果你在国内但是使用的不是简体中文,或者在国外使用简体中文,那么你可能需要手动设置以获得最佳的体验。
1 change: 1 addition & 0 deletions maa-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ anyhow = "1"
signal-hook = "0.3.17"
dunce = "1.0.4"
lazy_static = "1.4.0"
sys-locale = "0.3.1"
prettytable = { version = "0.10.0", default-features = false }

clap = { version = "4.4", features = ["derive"] }
Expand Down
26 changes: 21 additions & 5 deletions maa-cli/src/config/cli/resource.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::path::{Path, PathBuf};

use serde::Deserialize;
use sys_locale::get_locale;

#[cfg_attr(test, derive(Debug, PartialEq))]
#[derive(Deserialize, Default, Clone)]
Expand Down Expand Up @@ -65,7 +66,16 @@
}

fn default_url() -> String {
String::from("https://github.com/MaaAssistantArknights/MaaResource.git")
if get_locale().is_some_and(check_zh_cn) {
String::from("https://git.maa-org.net/MAA/MaaResource.git")

Check warning on line 70 in maa-cli/src/config/cli/resource.rs

View check run for this annotation

Codecov / codecov/patch

maa-cli/src/config/cli/resource.rs#L70

Added line #L70 was not covered by tests
} else {
String::from("https://github.com/MaaAssistantArknights/MaaResource.git")
}
}

/// Check if locale is zh-CN
fn check_zh_cn(locale: impl AsRef<str>) -> bool {
matches!(locale.as_ref(), "zh-CN" | "zh-Hans" | "zh-Hans-CN")
}

impl Remote {
Expand Down Expand Up @@ -210,10 +220,7 @@

#[test]
fn url() {
assert_eq!(
Remote::default().url(),
"https://github.com/MaaAssistantArknights/MaaResource.git",
);
assert_eq!(Remote::default().url(), default_url());

assert_eq!(
Remote {
Expand Down Expand Up @@ -254,4 +261,13 @@
Path::new("~/.ssh/id_ed25519")
);
}

#[test]
fn test_check_zh_cn() {
assert!(check_zh_cn("zh-CN"));
assert!(check_zh_cn("zh-Hans"));
assert!(check_zh_cn("zh-Hans-CN"));
assert!(!check_zh_cn("zh-TW"));
assert!(!check_zh_cn("en-US"));
}
}