Skip to content
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ xy
fw
README.md.bak*
*.tmp
chsrc_tmp_test.txt
chsrc_tmp_test.txt.bak

# Generated when testing (when there's a bug)
nul
Expand Down
171 changes: 95 additions & 76 deletions src/recipe/lang/Rust/Cargo.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ pl_rust_cargo_prelude (void)
chef_prep_this (pl_rust_cargo, gsr);

chef_set_created_on (this, "2023-08-30");
chef_set_last_updated (this, "2025-09-18");
chef_set_last_updated (this, "2025-10-06");
chef_set_sources_last_updated (this, "2025-06-18");

chef_set_chef (this, NULL);
chef_set_cooks (this, 2, "@happy-game", "@ccmywish");
chef_set_sauciers (this, 1, "@Mikachu2333");
chef_set_cooks (this, 2, "@ccmywish", "@Mikachu2333");
chef_set_sauciers (this, 1, "@happy-game");

chef_allow_local_mode (this, PartiallyCan, "可以基于本项目换源吗?请帮助确认", "Can it change sources based on this project? Please help confirm");
chef_forbid_english (this);
chef_allow_user_define(this);
chef_allow_user_define (this);


// 以下都支持稀疏索引,我们换源时都将默认添加 `sparse+`。链接末尾的 `/` 不能缺少
Expand Down Expand Up @@ -48,55 +48,73 @@ pl_rust_cargo_prelude (void)
}


void
pl_rust_cargo_note_get_src_default ()
{
if (ENGLISH)
chsrc_note2 ("No source configured in Cargo, showing default upstream source:");
else
chsrc_note2 ("Cargo 中未自定义源,显示默认源:");

Source_t default_source = chsrc_yield_source (&pl_rust_cargo_target, "upstream");
say (default_source.url);
}

void
pl_rust_cargo_note_get_src_mirror (char *url, bool sparse)
{
chsrc_note2 (ENGLISH ? "Custom source found: " : "已找到自定义源:");
say (xy_2strcat (url, sparse ? " (sparse)" : ""));
}

void
pl_rust_cargo_getsrc (char *option)
{
char *cargo_config_file = xy_normalize_path ("~/.cargo/config.toml");

if (xy_file_exist (cargo_config_file))

char *raw_content = xy_file_to_str (cargo_config_file);
char *formatted_content = xy_str_gsub (raw_content, " ", "");
formatted_content = xy_str_gsub (formatted_content, "'", "\"");
free (raw_content);

XyStrFindResult_t result_has_mirror = xy_str_find (formatted_content, "replace-with");
if (result_has_mirror.found)
{
// 尝试提取 [source.mirror] 下的 registry URL
char *grep_cmd = xy_str_gsub ("grep -A1 '\\[source\\.mirror\\]' '@f@' | grep 'registry' | sed 's/[^\"]*\"\\([^\"]*\\)\".*/\\1/'", "@f@", cargo_config_file);
chsrc_ensure_program ("grep");
chsrc_ensure_program ("sed");

char *mirror_url;
int status = xy_run_get_stdout (grep_cmd, &mirror_url);
char *stripped_url = (mirror_url) ? xy_str_strip(mirror_url) : "";

if (0 == status && stripped_url && strstr(stripped_url, "http"))
char *mirror_name = xy_str_take_until_newline (formatted_content + result_has_mirror.end + 1);
mirror_name = xy_str_delete_prefix (mirror_name, "=\"");
mirror_name = xy_str_delete_suffix (mirror_name, "\"");

XyStrFindResult_t result_mirror = xy_str_find (formatted_content, xy_strcat (3, "[source.", mirror_name, "]"));
if (!result_mirror.found)
{
// 找到配置的镜像源,如果存在 sparse+ 前缀则去除
char *clean_url = (strstr(stripped_url, "sparse+")) ?
stripped_url + 7 : stripped_url;
say (clean_url);
pl_rust_cargo_note_get_src_default();
return;
}
else
char *mirror_url = xy_str_take_until_newline (formatted_content + result_mirror.end + 1);
mirror_url = xy_str_delete_prefix (mirror_url, "registry=\"");
mirror_url = xy_str_delete_suffix (mirror_url, "\"");
if (xy_str_find (mirror_url, "sparse+").found)
{
// 配置文件存在但没有找到镜像源配置,显示默认上游源
if (ENGLISH)
chsrc_note2 ("Config file exists but no mirror source found, showing default upstream source:");
else
chsrc_note2 ("配置文件存在但未找到镜像源配置,显示默认上游源:");

Source_t default_source = chsrc_yield_source (&pl_rust_cargo_target, "upstream");
say (default_source.url);
pl_rust_cargo_note_get_src_mirror (xy_str_delete_prefix (mirror_url, "sparse+"), true);
}
}
else
{
// 配置文件不存在,显示默认上游源
if (ENGLISH)
chsrc_note2 ("No source configured in Cargo, showing default upstream source:");
else
chsrc_note2 ("Cargo 中未配置源,显示默认上游源:");

Source_t default_source = chsrc_yield_source (&pl_rust_cargo_target, "upstream");
say (default_source.url);
pl_rust_cargo_note_get_src_default();
}
}


void
pl_write_rust_config (const char *path, const char *url)
{
remove (path);
char *content = RAWSTR_pl_rust_cargo_config;
content = xy_str_gsub (content, "@url@", url);
chsrc_overwrite_file (content, path);
free (content);
}

/**
* @consult https://mirrors.tuna.tsinghua.edu.cn/help/crates.io-index/
* @consult https://help.mirrors.cernet.edu.cn/crates.io-index
Expand All @@ -105,62 +123,63 @@ void
pl_rust_cargo_setsrc (char *option)
{
chsrc_ensure_program ("cargo");

chsrc_use_this_source (pl_rust_cargo);

char *default_content = RAWSTR_pl_rust_cargo_config;
char *cargo_config_dir = "~/.cargo/";
char *cargo_config_file = xy_2strcat (cargo_config_dir, "config.toml");

chsrc_ensure_dir (cargo_config_dir);

cargo_config_file = xy_normalize_path (cargo_config_file);

if (xy_file_exist (cargo_config_file))
{
chsrc_backup (cargo_config_file);
}

char *content = RAWSTR_pl_rust_cargo_config;
content = xy_str_gsub (content, "@url@", source.url);
char *raw_content = xy_file_to_str (cargo_config_file);

if (xy_file_exist (cargo_config_file))
{
char *check_cmd = xy_str_gsub (RAWSTR_pl_rust_cargo_check_config, "@f@", cargo_config_file);
chsrc_ensure_program ("grep");
int status = chsrc_run_directly (check_cmd);

if (0 == status)
XyStrFindResult_t result_has_mirror = xy_str_find (raw_content, "replace-with");
if (!result_has_mirror.found)
{
pl_write_rust_config (cargo_config_file, source.url);
goto finish;
}

char *mirror_name = xy_str_take_until_newline (raw_content + result_has_mirror.end + 1);
mirror_name = xy_str_gsub (mirror_name, " ", "");
mirror_name = xy_str_gsub (mirror_name, "'", "\"");
mirror_name = xy_str_delete_prefix (mirror_name, "=\"");
mirror_name = xy_str_delete_suffix (mirror_name, "\"");

XyStrFindResult_t result_mirror = xy_str_find (raw_content, xy_strcat (3, "[source.", mirror_name, "]"));
if (!result_mirror.found)
{
char *sed_cmd;
if (xy.on_bsd || xy.on_macos)
{
sed_cmd = "sed -i '' ";
}
else
{
sed_cmd = "sed -i ";
}

char *update_cmd = xy_str_gsub (RAWSTR_pl_rust_cargo_update_replace_with, "@sed@", sed_cmd);
update_cmd = xy_str_gsub (update_cmd, "@f@", cargo_config_file);
chsrc_run (update_cmd, RunOpt_Default);

update_cmd = xy_str_gsub (RAWSTR_pl_rust_cargo_update_registry, "@sed@", sed_cmd);
update_cmd = xy_str_gsub (update_cmd, "@f@", cargo_config_file);
update_cmd = xy_str_gsub (update_cmd, "@url@", source.url);
chsrc_run (update_cmd, RunOpt_Default);
pl_write_rust_config (cargo_config_file, source.url);
goto finish;
}
else

char *mirror_url = xy_str_take_until_newline (raw_content + result_mirror.end + 1);
mirror_url = xy_str_gsub (mirror_url, " ", "");
if (!xy_str_find (mirror_url, "registry").found)
{
chsrc_append_to_file ("\n", cargo_config_file);
chsrc_append_to_file (content, cargo_config_file);
pl_write_rust_config (cargo_config_file, source.url);
goto finish;
}
mirror_url = xy_str_delete_prefix (mirror_url, "registry=\"");
mirror_url = xy_str_delete_suffix (mirror_url, "\"");

char *final_content = xy_str_gsub (raw_content, mirror_url, xy_2strcat ("sparse+", source.url));
chsrc_overwrite_file (final_content, cargo_config_file);
free (final_content);
goto finish;
}
else
{
chsrc_append_to_file (content, cargo_config_file);
}

pl_write_rust_config (cargo_config_file, source.url);
goto finish;

finish:
chsrc_determine_chgtype (ChgType_Auto);
chsrc_conclude (&source);
}
Expand Down
20 changes: 10 additions & 10 deletions src/recipe/lang/Rust/rustup.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ pl_rust_rustup_prelude (void)
chef_allow_user_define(this);

def_sources_begin()
{&UpstreamProvider, "https://static.rust-lang.org", FeedByPrelude},
{&MirrorZ, "https://mirrors.cernet.edu.cn/rustup", FeedByPrelude},
{&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/rustup", FeedByPrelude},
{&Ustc, "https://mirrors.ustc.edu.cn/rust-static", FeedByPrelude},
{&Nju, "https://mirror.nju.edu.cn/rustup", FeedByPrelude},
{&Sjtug_Zhiyuan, "https://mirror.sjtu.edu.cn/rust-static", FeedByPrelude},
{&Zju, "https://mirrors.zju.edu.cn/rustup", FeedByPrelude},
{&Iscas, "https://mirror.iscas.ac.cn/rustup", FeedByPrelude},
{&Ali, "https://mirrors.aliyun.com/rustup", FeedByPrelude},
{&RsProxyCN, "https://rsproxy.cn", FeedByPrelude}
{&UpstreamProvider, "https://static.rust-lang.org", FeedByPrelude},
{&MirrorZ, "https://mirrors.cernet.edu.cn/rustup", FeedByPrelude},
{&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/rustup", FeedByPrelude},
{&Ustc, "https://mirrors.ustc.edu.cn/rust-static", FeedByPrelude},
{&Nju, "https://mirror.nju.edu.cn/rustup", FeedByPrelude},
{&Sjtug_Zhiyuan, "https://mirror.sjtu.edu.cn/rust-static", FeedByPrelude},
{&Zju, "https://mirrors.zju.edu.cn/rustup", FeedByPrelude},
{&Iscas, "https://mirror.iscas.ac.cn/rustup", FeedByPrelude},
{&Ali, "https://mirrors.aliyun.com/rustup", FeedByPrelude},
{&RsProxyCN, "https://rsproxy.cn", FeedByPrelude}
def_sources_end()

// 20MB大小
Expand Down
8 changes: 1 addition & 7 deletions src/recipe/lang/rawstr4c.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,7 @@ char RAWSTR_pl_java_maven_config[] = "\074\155\151\162\162\157\162\076\012\040\0

char RAWSTR_pl_java_build_gradle[] = "\141\154\154\160\162\157\152\145\143\164\163\040\173\012\040\040\040\040\162\145\160\157\163\151\164\157\162\151\145\163\040\173\012\040\040\040\040\040\040\040\040\155\141\166\145\156\040\173\040\165\162\154\040\047\100\165\162\154\100\047\040\175\012\040\040\040\040\040\040\040\040\155\141\166\145\156\114\157\143\141\154\050\051\012\040\040\040\040\040\040\040\040\155\141\166\145\156\103\145\156\164\162\141\154\050\051\012\040\040\040\040\175\012\175";

char RAWSTR_pl_rust_cargo_config[] = "\133\163\157\165\162\143\145\056\143\162\141\164\145\163\055\151\157\135\012\162\145\160\154\141\143\145\055\167\151\164\150\040\075\040\047\155\151\162\162\157\162\047\012\012\133\163\157\165\162\143\145\056\155\151\162\162\157\162\135\012\162\145\147\151\163\164\162\171\040\075\040\042\163\160\141\162\163\145\053\100\165\162\154\100\042";

char RAWSTR_pl_rust_cargo_check_config[] = "\147\162\145\160\040\055\161\040\047\136\134\133\163\157\165\162\143\145\134\056\143\162\141\164\145\163\055\151\157\134\135\047\040\100\146\100";

char RAWSTR_pl_rust_cargo_update_replace_with[] = "\100\163\145\144\100\040\047\057\136\134\133\163\157\165\162\143\145\134\056\143\162\141\164\145\163\055\151\157\134\135\044\057\054\057\136\134\133\057\173\163\057\136\162\145\160\154\141\143\145\055\167\151\164\150\040\075\040\056\052\057\162\145\160\154\141\143\145\055\167\151\164\150\040\075\040\042\155\151\162\162\157\162\042\057\175\047\040\100\146\100";

char RAWSTR_pl_rust_cargo_update_registry[] = "\100\163\145\144\100\040\047\057\136\134\133\163\157\165\162\143\145\134\056\155\151\162\162\157\162\134\135\044\057\054\057\136\134\133\057\173\163\174\136\162\145\147\151\163\164\162\171\040\075\040\056\052\174\162\145\147\151\163\164\162\171\040\075\040\042\163\160\141\162\163\145\053\100\165\162\154\100\042\174\175\047\040\100\146\100";
char RAWSTR_pl_rust_cargo_config[] = "\133\163\157\165\162\143\145\056\143\162\141\164\145\163\055\151\157\135\012\162\145\160\154\141\143\145\055\167\151\164\150\040\075\040\042\155\151\162\162\157\162\042\012\012\133\163\157\165\162\143\145\056\155\151\162\162\157\162\135\012\162\145\147\151\163\164\162\171\040\075\040\042\163\160\141\162\163\145\053\100\165\162\154\100\042\012";

char RAWSTR_pl_haskell_cabal_config[] = "\162\145\160\157\163\151\164\157\162\171\040\155\151\162\162\157\162\012\040\040\165\162\154\072\040\100\165\162\154\100\012\040\040\163\145\143\165\162\145\072\040\124\162\165\145";

Expand Down
25 changes: 1 addition & 24 deletions src/recipe/lang/rawstr4c.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,34 +75,11 @@ allprojects {

```toml
[source.crates-io]
replace-with = 'mirror'
replace-with = "mirror"

[source.mirror]
registry = "sparse+@url@"
```

### cargo check config

- name = `cargo_check_config`

```bash
grep -q '^\[source\.crates-io\]' @f@
```

### cargo update replace-with

- name = `cargo_update_replace_with`

```bash
@sed@ '/^\[source\.crates-io\]$/,/^\[/{s/^replace-with = .*/replace-with = "mirror"/}' @f@
```

### cargo update registry

- name = `cargo_update_registry`

```bash
@sed@ '/^\[source\.mirror\]$/,/^\[/{s|^registry = .*|registry = "sparse+@url@"|}' @f@
```

<br>
Expand Down