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
102 changes: 96 additions & 6 deletions src/recipe/lang/Rust/Cargo.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ 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-07-22");
chef_set_last_updated (this, "2025-09-18");
chef_set_sources_last_updated (this, "2025-06-18");

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

chef_allow_local_mode (this, PartiallyCan, "可以基于本项目换源吗?请帮助确认", "Can it change sources based on this project? Please help confirm");
Expand Down Expand Up @@ -51,7 +51,49 @@ pl_rust_cargo_prelude (void)
void
pl_rust_cargo_getsrc (char *option)
{
chsrc_view_file ("~/.cargo/config.toml");
char *cargo_config_file = xy_normalize_path ("~/.cargo/config.toml");

if (xy_file_exist (cargo_config_file))
{
// 尝试提取 [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"))
{
// 找到配置的镜像源,如果存在 sparse+ 前缀则去除
char *clean_url = (strstr(stripped_url, "sparse+")) ?
stripped_url + 7 : stripped_url;
say (clean_url);
}
else
{
// 配置文件存在但没有找到镜像源配置,显示默认上游源
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);
}
}
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);
}
}


Expand All @@ -62,14 +104,62 @@ pl_rust_cargo_getsrc (char *option)
void
pl_rust_cargo_setsrc (char *option)
{
chsrc_ensure_program ("cargo");

chsrc_use_this_source (pl_rust_cargo);

char *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);

chsrc_note2 (xy_strcat (3, "请手动写入以下内容到 ", xy_normalize_path ("~/.cargo/config.toml"), " 文件中:"));
println (content);
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)
{
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);
}
else
{
chsrc_append_to_file ("\n", cargo_config_file);
chsrc_append_to_file (content, cargo_config_file);
}
}
else
{
chsrc_append_to_file (content, cargo_config_file);
}

chsrc_determine_chgtype (ChgType_Auto);
chsrc_conclude (&source);
Expand Down
6 changes: 6 additions & 0 deletions src/recipe/lang/rawstr4c.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ char RAWSTR_pl_java_build_gradle[] = "\141\154\154\160\162\157\152\145\143\164\1

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_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";

char RAWSTR_pl_haskell_stackage_yaml[] = "\160\141\143\153\141\147\145\055\151\156\144\145\170\072\012\040\040\144\157\167\156\154\157\141\144\055\160\162\145\146\151\170\072\040\100\165\162\154\100\012\040\040\150\141\143\153\141\147\145\055\163\145\143\165\162\151\164\171\072\012\040\040\040\040\153\145\171\151\144\163\072\012\040\040\040\040\040\040\055\040\060\141\065\143\067\145\141\064\067\143\144\061\142\061\065\146\060\061\146\065\146\065\061\141\063\063\141\144\144\141\067\145\066\065\065\142\143\060\146\060\142\060\066\061\065\142\141\141\070\145\062\067\061\146\064\143\063\063\065\061\145\062\061\144\012\040\040\040\040\040\040\055\040\061\145\141\071\142\141\063\062\143\065\062\066\144\061\143\143\071\061\141\142\065\145\065\142\144\063\066\064\145\143\065\145\071\145\070\143\142\066\067\061\067\071\141\064\067\061\070\067\062\146\066\145\062\066\146\060\141\145\067\067\063\144\064\062\012\040\040\040\040\040\040\055\040\062\070\060\142\061\060\061\065\063\141\065\062\062\066\070\061\061\066\063\066\065\070\143\142\064\071\146\066\063\062\143\144\145\063\146\063\070\144\067\066\070\142\067\063\066\144\144\142\143\071\060\061\144\071\071\141\061\141\067\067\062\070\063\063\012\040\040\040\040\040\040\055\040\062\141\071\066\142\061\070\070\071\144\143\062\062\061\143\061\067\062\071\066\146\143\143\062\142\142\063\064\142\071\060\070\143\141\071\067\063\064\063\067\066\146\060\146\063\066\061\066\066\060\062\060\060\071\063\065\071\061\066\145\146\062\060\061\012\040\040\040\040\040\040\055\040\062\143\066\143\063\066\062\067\142\144\066\143\071\070\062\071\071\060\062\063\071\064\070\067\146\061\141\142\144\060\062\145\060\070\141\060\062\145\066\143\146\061\066\145\144\142\061\060\065\141\070\060\061\062\144\064\064\064\144\070\067\060\143\063\012\040\040\040\040\040\040\055\040\065\061\146\060\061\066\061\142\071\060\066\060\061\061\142\065\062\143\066\066\061\063\063\067\066\142\061\141\145\071\063\067\066\067\060\144\141\066\071\063\062\062\061\061\063\141\062\064\066\141\060\071\146\070\060\067\143\066\062\146\066\071\062\061\012\040\040\040\040\040\040\055\040\067\067\062\145\071\146\064\143\067\144\142\063\063\144\062\065\061\144\065\143\066\145\063\065\067\061\071\071\143\070\061\071\145\065\066\071\144\061\063\060\070\065\067\144\143\062\062\065\065\064\071\142\064\060\070\064\065\146\146\060\070\071\060\144\012\040\040\040\040\040\040\055\040\141\141\063\061\065\062\070\066\145\066\141\144\062\070\061\141\144\066\061\061\070\062\062\063\065\065\063\063\143\064\061\145\070\060\066\145\065\141\067\070\067\145\060\142\066\144\061\145\067\145\145\146\063\146\060\071\144\061\063\067\144\062\145\071\012\040\040\040\040\040\040\055\040\146\145\063\063\061\065\060\062\066\060\066\070\060\062\146\145\141\143\061\065\145\065\061\064\144\071\142\071\145\141\070\063\146\145\145\070\142\066\146\146\145\146\067\061\063\063\065\064\067\071\141\062\145\066\070\144\070\064\141\144\143\066\142\060\012\040\040\040\040\153\145\171\055\164\150\162\145\163\150\157\154\144\072\040\063\040\043\040\156\165\155\142\145\162\040\157\146\040\153\145\171\163\040\162\145\161\165\151\162\145\144\012\040\040\040\040\043\040\151\147\156\157\162\145\040\145\170\160\151\162\141\164\151\157\156\040\144\141\164\145\054\040\163\145\145\040\150\164\164\160\163\072\057\057\147\151\164\150\165\142\056\143\157\155\057\143\157\155\155\145\162\143\151\141\154\150\141\163\153\145\154\154\057\163\164\141\143\153\057\160\165\154\154\057\064\066\061\064\012\040\040\040\040\151\147\156\157\162\145\055\145\170\160\151\162\171\072\040\156\157";
Expand Down
28 changes: 26 additions & 2 deletions src/recipe/lang/rawstr4c.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
! -------------------------------------------------------------
! Config Type : rawstr4c (Markdown)
! Config Authors: Aoran Zeng <ccmywish@qq.com>
! Contributors : Nil Null <nil@null.org>
! Contributors : Happy Game <happygame1024@gmail.com>
! Created On : <2025-07-14>
! Last Modified : <2025-08-08>
! Last Modified : <2025-09-13>
! ---------------------------------------------------------- -->

# rawstr4c input for PL
Expand Down Expand Up @@ -81,6 +81,30 @@ replace-with = '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