@@ -19,7 +19,7 @@ pl_rust_cargo_prelude (void)
1919
2020 chef_allow_local_mode (this , PartiallyCan , "可以基于本项目换源吗?请帮助确认" , "Can it change sources based on this project? Please help confirm" );
2121 chef_forbid_english (this );
22- chef_allow_user_define (this );
22+ chef_allow_user_define (this );
2323
2424
2525 // 以下都支持稀疏索引,我们换源时都将默认添加 `sparse+`。链接末尾的 `/` 不能缺少
@@ -48,55 +48,80 @@ pl_rust_cargo_prelude (void)
4848}
4949
5050
51+ void
52+ rust_cargo_note_get_src_default ()
53+ {
54+ if (ENGLISH )
55+ chsrc_note2 ("No source configured in Cargo, showing default upstream source:" );
56+ else
57+ chsrc_note2 ("Cargo 中未自定义源,显示默认源:" );
58+
59+ Source_t default_source = chsrc_yield_source (& pl_rust_cargo_target , "upstream" );
60+ say (default_source .url );
61+ }
62+
63+ void
64+ rust_cargo_note_get_src_mirror (char * url , bool sparse )
65+ {
66+ if (ENGLISH )
67+ {
68+ chsrc_note2 ("Custom source found:" );
69+ }
70+ else
71+ {
72+ chsrc_note2 ("已找到自定义源:" );
73+ }
74+ say (xy_2strcat (url , sparse ? " (sparse)" : "" ));
75+ }
76+
5177void
5278pl_rust_cargo_getsrc (char * option )
5379{
5480 char * cargo_config_file = xy_normalize_path ("~/.cargo/config.toml" );
55-
56- if (xy_file_exist (cargo_config_file ))
81+
82+ char * raw_content = xy_file_to_str (cargo_config_file );
83+ char * formatted_content = xy_str_gsub (raw_content , " " , "" );
84+ formatted_content = xy_str_gsub (raw_content , "\"" , "" );
85+ free (raw_content );
86+
87+ XyStrFindResult_t result_has_mirror = xy_str_find (formatted_content , "replace-with" );
88+ if (result_has_mirror .found )
5789 {
58- // 尝试提取 [source.mirror] 下的 registry URL
59- char * grep_cmd = xy_str_gsub ("grep -A1 '\\[source\\.mirror\\]' '@f@' | grep 'registry' | sed 's/[^\"]*\"\\([^\"]*\\)\".*/\\1/'" , "@f@" , cargo_config_file );
60- chsrc_ensure_program ("grep" );
61- chsrc_ensure_program ("sed" );
62-
63- char * mirror_url ;
64- int status = xy_run_get_stdout (grep_cmd , & mirror_url );
65- char * stripped_url = (mirror_url ) ? xy_str_strip (mirror_url ) : "" ;
66-
67- if (0 == status && stripped_url && strstr (stripped_url , "http" ))
90+ char * mirror_name = xy_str_take_until_newline (formatted_content + result_has_mirror .end + 1 );
91+ mirror_name = xy_str_delete_prefix (mirror_name , "=\"" );
92+ mirror_name = xy_str_delete_suffix (mirror_name , "\"" );
93+
94+ XyStrFindResult_t result_mirror = xy_str_find (formatted_content , xy_strcat (3 , "[source." , mirror_name , "]" ));
95+ if (!result_mirror .found )
6896 {
69- // 找到配置的镜像源,如果存在 sparse+ 前缀则去除
70- char * clean_url = (strstr (stripped_url , "sparse+" )) ?
71- stripped_url + 7 : stripped_url ;
72- say (clean_url );
97+ rust_cargo_note_get_src_default ();
98+ return ;
7399 }
74- else
100+ char * mirror_url = xy_str_take_until_newline (formatted_content + result_mirror .end + 1 );
101+ mirror_url = xy_str_delete_prefix (mirror_url , "registry=\"" );
102+ mirror_url = xy_str_delete_suffix (mirror_url , "\"" );
103+ if (xy_str_find (mirror_url , "sparse+" ).found )
75104 {
76- // 配置文件存在但没有找到镜像源配置,显示默认上游源
77- if (ENGLISH )
78- chsrc_note2 ("Config file exists but no mirror source found, showing default upstream source:" );
79- else
80- chsrc_note2 ("配置文件存在但未找到镜像源配置,显示默认上游源:" );
81-
82- Source_t default_source = chsrc_yield_source (& pl_rust_cargo_target , "upstream" );
83- say (default_source .url );
105+ rust_cargo_note_get_src_mirror (xy_str_delete_prefix (mirror_url , "sparse+" ), true);
84106 }
85107 }
86108 else
87109 {
88- // 配置文件不存在,显示默认上游源
89- if (ENGLISH )
90- chsrc_note2 ("No source configured in Cargo, showing default upstream source:" );
91- else
92- chsrc_note2 ("Cargo 中未配置源,显示默认上游源:" );
93-
94- Source_t default_source = chsrc_yield_source (& pl_rust_cargo_target , "upstream" );
95- say (default_source .url );
110+ rust_cargo_note_get_src_default ();
96111 }
97112}
98113
99114
115+ void
116+ write_rust_config (const char * path , const char * url )
117+ {
118+ remove (path );
119+ char * content = RAWSTR_pl_rust_cargo_config ;
120+ content = xy_str_gsub (content , "@url@" , url );
121+ chsrc_overwrite_file (content , path );
122+ free (content );
123+ }
124+
100125/**
101126 * @consult https://mirrors.tuna.tsinghua.edu.cn/help/crates.io-index/
102127 * @consult https://help.mirrors.cernet.edu.cn/crates.io-index
@@ -105,62 +130,65 @@ void
105130pl_rust_cargo_setsrc (char * option )
106131{
107132 chsrc_ensure_program ("cargo" );
108-
133+
109134 chsrc_use_this_source (pl_rust_cargo );
110135
136+ char * default_content = RAWSTR_pl_rust_cargo_config ;
111137 char * cargo_config_dir = "~/.cargo/" ;
112138 char * cargo_config_file = xy_2strcat (cargo_config_dir , "config.toml" );
113-
139+
114140 chsrc_ensure_dir (cargo_config_dir );
115-
141+
116142 cargo_config_file = xy_normalize_path (cargo_config_file );
117-
143+
118144 if (xy_file_exist (cargo_config_file ))
119145 {
120146 chsrc_backup (cargo_config_file );
121- }
122147
123- char * content = RAWSTR_pl_rust_cargo_config ;
124- content = xy_str_gsub (content , "@url@" , source .url );
148+ char * raw_content = xy_file_to_str (cargo_config_file );
149+ char * formatted_content = xy_str_gsub (raw_content , " " , "" );
150+ formatted_content = xy_str_gsub (raw_content , "\"" , "" );
125151
126- if (xy_file_exist (cargo_config_file ))
127- {
128- char * check_cmd = xy_str_gsub (RAWSTR_pl_rust_cargo_check_config , "@f@" , cargo_config_file );
129- chsrc_ensure_program ("grep" );
130- int status = chsrc_run_directly (check_cmd );
131-
132- if (0 == status )
152+ XyStrFindResult_t result_has_mirror = xy_str_find (raw_content , "replace-with" );
153+ if (!result_has_mirror .found )
133154 {
134- char * sed_cmd ;
135- if (xy .on_bsd || xy .on_macos )
136- {
137- sed_cmd = "sed -i '' " ;
138- }
139- else
140- {
141- sed_cmd = "sed -i " ;
142- }
143-
144- char * update_cmd = xy_str_gsub (RAWSTR_pl_rust_cargo_update_replace_with , "@sed@" , sed_cmd );
145- update_cmd = xy_str_gsub (update_cmd , "@f@" , cargo_config_file );
146- chsrc_run (update_cmd , RunOpt_Default );
147-
148- update_cmd = xy_str_gsub (RAWSTR_pl_rust_cargo_update_registry , "@sed@" , sed_cmd );
149- update_cmd = xy_str_gsub (update_cmd , "@f@" , cargo_config_file );
150- update_cmd = xy_str_gsub (update_cmd , "@url@" , source .url );
151- chsrc_run (update_cmd , RunOpt_Default );
155+ write_rust_config (cargo_config_file , source .url );
156+ goto finish ;
152157 }
153- else
158+
159+ char * mirror_name = xy_str_take_until_newline (raw_content + result_has_mirror .end + 1 );
160+ mirror_name = xy_str_gsub (mirror_name , " " , "" );
161+ mirror_name = xy_str_gsub (mirror_name , "'" , "\"" );
162+ mirror_name = xy_str_delete_prefix (mirror_name , "=\"" );
163+ mirror_name = xy_str_delete_suffix (mirror_name , "\"" );
164+
165+ XyStrFindResult_t result_mirror = xy_str_find (raw_content , xy_strcat (3 , "[source." , mirror_name , "]" ));
166+ if (!result_mirror .found )
154167 {
155- chsrc_append_to_file ( "\n" , cargo_config_file );
156- chsrc_append_to_file ( content , cargo_config_file ) ;
168+ write_rust_config ( cargo_config_file , source . url );
169+ goto finish ;
157170 }
171+
172+ char * mirror_url = xy_str_take_until_newline (raw_content + result_mirror .end + 1 );
173+ mirror_url = xy_str_gsub (mirror_url , " " , "" );
174+ if (!xy_str_find (mirror_url , "registry" ).found )
175+ {
176+ write_rust_config (cargo_config_file , source .url );
177+ goto finish ;
178+ }
179+ mirror_url = xy_str_delete_prefix (mirror_url , "registry=\"" );
180+ mirror_url = xy_str_delete_suffix (mirror_url , "\"" );
181+
182+ char * final_content = xy_str_gsub (raw_content , mirror_url , xy_2strcat ("sparse+" , source .url ));
183+ chsrc_overwrite_file (final_content , cargo_config_file );
184+ free (final_content );
185+ goto finish ;
158186 }
159- else
160- {
161- chsrc_append_to_file (content , cargo_config_file );
162- }
163187
188+ write_rust_config (cargo_config_file , source .url );
189+ goto finish ;
190+
191+ finish :
164192 chsrc_determine_chgtype (ChgType_Auto );
165193 chsrc_conclude (& source );
166194}
0 commit comments