Add common functions for rest languages

This commit is contained in:
Aoran Zeng 2023-09-03 20:49:55 +08:00
parent 2d8c049684
commit 9f3b851565

430
chsrc.c
View File

@ -193,12 +193,6 @@ pl_ruby_setsrc (char* option)
{ {
int index = 0; int index = 0;
if (NULL!=option) {
index = does_the_mirror_exist (pl_ruby_sources, pl_ruby_sources_n, "ruby", option);
} else {
index = pl_ruby_cesu ("");
}
char* check_cmd = NULL; char* check_cmd = NULL;
if (xy_on_windows) check_cmd = "gem -v >nul 2>nul"; if (xy_on_windows) check_cmd = "gem -v >nul 2>nul";
else check_cmd = "gem -v 1>/dev/null 2>&1"; else check_cmd = "gem -v 1>/dev/null 2>&1";
@ -208,6 +202,12 @@ pl_ruby_setsrc (char* option)
return; return;
} }
if (NULL!=option) {
index = does_the_mirror_exist (pl_ruby_sources, pl_ruby_sources_n, "ruby", option);
} else {
index = pl_ruby_cesu ("");
}
source_info source = pl_ruby_sources[index]; source_info source = pl_ruby_sources[index];
const char* source_name = source.mirror->name; const char* source_name = source.mirror->name;
const char* source_url = source.url; const char* source_url = source.url;
@ -237,16 +237,6 @@ pl_ruby_setsrc (char* option)
/**
* TODO: Python
*/
int
pl_python_cesu (char* option)
{
return common_cesu (pl_python_sources, pl_python_sources_n,
"gems/nokogiri-1.15.0-java.gem");
}
/** /**
* @param[out] prog Python NULL * @param[out] prog Python NULL
*/ */
@ -277,6 +267,19 @@ _pl_python_check_cmd (char** prog)
} }
} }
/**
* TODO: Python
*/
int
pl_python_cesu (char* option)
{
return common_cesu (pl_python_sources, pl_python_sources_n,
"gems/nokogiri-1.15.0-java.gem");
}
void void
pl_python_getsrc (char* option) pl_python_getsrc (char* option)
{ {
@ -294,37 +297,26 @@ pl_python_getsrc (char* option)
void void
pl_python_setsrc (char* option) pl_python_setsrc (char* option)
{ {
int selected = 0; int index = 0;
char* prog = NULL; char* prog = NULL;
_pl_python_check_cmd (&prog); _pl_python_check_cmd (&prog);
selected = pl_python_cesu (""); if (NULL!=option) {
index = does_the_mirror_exist (pl_python_sources, pl_python_sources_n, "python", option);
} else {
index = pl_python_cesu ("");
}
const char* source_name = pl_python_sources[selected].mirror->name; source_info source = pl_python_sources[index];
const char* source_abbr = pl_python_sources[selected].mirror->abbr; say_for_setsrc(&source);
const char* source_url = pl_python_sources[selected].url;
xy_info (xy_2strjoin("chsrc: 选中镜像站:", source_abbr)); char* cmd = xy_2strjoin(prog, xy_2strjoin(" -m pip config set global.index-url ", source.url));
char* cmd = xy_2strjoin(prog, xy_2strjoin(" -m pip config set global.index-url ", source_url));
system(cmd); system(cmd);
free(cmd); xy_success(xy_2strjoin("chsrc: 感谢镜像提供方:", source.mirror->name));
xy_success(xy_2strjoin("chsrc: 感谢镜像提供方:", source_name));
} }
/**
* TODO:
*/
int
pl_nodejs_cesu (char* option)
{
return common_cesu (pl_nodejs_sources, pl_nodejs_sources_n,
"gems/nokogiri-1.15.0-java.gem");
}
void void
_pl_nodejs_check_cmd () _pl_nodejs_check_cmd ()
{ {
@ -339,6 +331,16 @@ _pl_nodejs_check_cmd ()
} }
} }
/**
* TODO:
*/
int
pl_nodejs_cesu (char* option)
{
return common_cesu (pl_nodejs_sources, pl_nodejs_sources_n,
"gems/nokogiri-1.15.0-java.gem");
}
void void
pl_nodejs_getsrc (char* option) pl_nodejs_getsrc (char* option)
{ {
@ -355,31 +357,24 @@ pl_nodejs_setsrc (char* option)
{ {
_pl_nodejs_check_cmd(); _pl_nodejs_check_cmd();
int selected = pl_nodejs_cesu (""); int index = 0;
const char* source_name = pl_nodejs_sources[selected].mirror->name; if (NULL!=option) {
const char* source_abbr = pl_nodejs_sources[selected].mirror->abbr; index = does_the_mirror_exist (pl_nodejs_sources, pl_nodejs_sources_n, "nodejs", option);
const char* source_url = pl_nodejs_sources[selected].url; } else {
index = pl_nodejs_cesu ("");
}
xy_info (xy_2strjoin("chsrc: 选中镜像站:", source_abbr)); source_info source = pl_nodejs_sources[index];
say_for_setsrc (&source);
char* cmd = xy_2strjoin("npm config set registry ", source_url); char* cmd = xy_2strjoin("npm config set registry ", source.url);
system(cmd); system(cmd);
free(cmd);
xy_success(xy_2strjoin("chsrc: 感谢镜像提供方:", source_name)); xy_success(xy_2strjoin("chsrc: 感谢镜像提供方:", source.mirror->name));
} }
/**
* TODO:
*/
int
pl_perl_cesu (char* option)
{
return common_cesu (pl_perl_sources, pl_perl_sources_n,
"gems/nokogiri-1.15.0-java.gem");
}
void void
_pl_perl_check_cmd () _pl_perl_check_cmd ()
@ -396,6 +391,16 @@ _pl_perl_check_cmd ()
} }
} }
/**
* TODO:
*/
int
pl_perl_cesu (char* option)
{
return common_cesu (pl_perl_sources, pl_perl_sources_n,
"gems/nokogiri-1.15.0-java.gem");
}
/* TODO: 暂未实现 */ /* TODO: 暂未实现 */
void void
pl_perl_getsrc (char* option) pl_perl_getsrc (char* option)
@ -412,39 +417,28 @@ pl_perl_getsrc (char* option)
void void
pl_perl_setsrc (char* option) pl_perl_setsrc (char* option)
{ {
int selected = 0; char* check_cmd, *prog = NULL; int index = 0;
selected = pl_perl_cesu (""); if (NULL!=option) {
index = does_the_mirror_exist (pl_perl_sources, pl_perl_sources_n, "perl", option);
} else {
index = pl_perl_cesu ("");
}
const char* source_name = pl_perl_sources[selected].mirror->name; source_info source = pl_perl_sources[index];
const char* source_abbr = pl_perl_sources[selected].mirror->abbr; say_for_setsrc (&source);
const char* source_url = pl_perl_sources[selected].url;
xy_info (xy_2strjoin("chsrc: 选中镜像站:", source_abbr));
char* cmd = xy_strjoin(3, char* cmd = xy_strjoin(3,
"perl -MCPAN -e 'CPAN::HandleConfig->edit(\"pushy_https\", 0); CPAN::HandleConfig->edit(\"urllist\", \"unshift\", \"", "perl -MCPAN -e 'CPAN::HandleConfig->edit(\"pushy_https\", 0); CPAN::HandleConfig->edit(\"urllist\", \"unshift\", \"",
source_url, source.url,
"\"); mkmyconfig'"); "\"); mkmyconfig'");
system(cmd); system(cmd);
free(cmd); xy_success(xy_2strjoin("chsrc: 感谢镜像提供方:", source.mirror->name));
xy_success(xy_2strjoin("chsrc: 感谢镜像提供方:", source_name));
} }
/**
* TODO:
*/
int
pl_php_cesu (char* option)
{
return common_cesu (pl_php_sources, pl_php_sources_n,
"gems/nokogiri-1.15.0-java.gem");
}
void void
_pl_php_check_cmd() _pl_php_check_cmd()
{ {
@ -460,6 +454,16 @@ _pl_php_check_cmd()
} }
} }
/**
* TODO:
*/
int
pl_php_cesu (char* option)
{
return common_cesu (pl_php_sources, pl_php_sources_n,
"gems/nokogiri-1.15.0-java.gem");
}
/* TODO: 待PHP用户确认 */ /* TODO: 待PHP用户确认 */
void void
pl_php_getsrc (char* option) pl_php_getsrc (char* option)
@ -477,33 +481,29 @@ pl_php_setsrc (char* option)
{ {
_pl_php_check_cmd(); _pl_php_check_cmd();
int selected = pl_php_cesu (""); int index = 0;
const char* source_name = pl_php_sources[selected].mirror->name; if (NULL!=option) {
const char* source_abbr = pl_php_sources[selected].mirror->abbr; index = does_the_mirror_exist (pl_php_sources, pl_php_sources_n, "php", option);
const char* source_url = pl_php_sources[selected].url; } else {
index = pl_php_cesu ("");
}
xy_info (xy_2strjoin("chsrc: 选中镜像站:", source_abbr)); source_info source = pl_php_sources[index];
say_for_setsrc (&source);
char* cmd = xy_2strjoin("composer config repo.packagist composer ", source_url); char* cmd = xy_2strjoin("composer config repo.packagist composer ", source.url);
system(cmd); system(cmd);
xy_success(xy_2strjoin("chsrc: 感谢镜像提供方:", source_name)); xy_success(xy_2strjoin("chsrc: 感谢镜像提供方:", source.mirror->name));
} }
/**
* Go换源
*
* https://goproxy.cn/
*/
void void
pl_go_setsrc (char* option) _pl_go_check_cmd ()
{ {
int selected = 0; char* check_cmd = NULL; char* check_cmd = NULL;
if (xy_on_windows) check_cmd = "go --version >nul 2>nul"; if (xy_on_windows) check_cmd = "go --version >nul 2>nul";
else check_cmd = "go --version 1>/dev/null 2>&1"; else check_cmd = "go --version 1>/dev/null 2>&1";
@ -511,28 +511,78 @@ pl_go_setsrc (char* option)
if (!exist_b) { if (!exist_b) {
xy_error ("chsrc: 未找到 go 相关命令,请检查是否存在"); xy_error ("chsrc: 未找到 go 相关命令,请检查是否存在");
return; exit(1);
}
} }
for (int i=0;i<sizeof(pl_go_sources);i++) {
// 循环测速 /**
* TODO:
*/
int
pl_go_cesu (char* option)
{
return common_cesu (pl_go_sources, pl_go_sources_n,
"gems/nokogiri-1.15.0-java.gem");
} }
const char* source_name = pl_go_sources[selected].mirror->name; /* TODO: 暂未实现 */
const char* source_abbr = pl_go_sources[selected].mirror->abbr; void
const char* source_url = pl_go_sources[selected].url; pl_go_getsrc (char* option)
{
_pl_go_check_cmd ();
// char* cmd = "npm config get registry";
// system(cmd);
}
/**
* Go换源https://goproxy.cn/
*/
void
pl_go_setsrc (char* option)
{
_pl_go_check_cmd();
int index = 0;
if (NULL!=option) {
index = does_the_mirror_exist (pl_go_sources, pl_go_sources_n, "go", option);
} else {
index = pl_go_cesu ("");
}
source_info source = pl_go_sources[index];
say_for_setsrc (&source);
xy_info (xy_2strjoin("chsrc: 选中镜像站:", source_abbr));
char* cmd = "go env -w GO111MODULE=on"; char* cmd = "go env -w GO111MODULE=on";
system(cmd); system(cmd);
cmd = xy_strjoin(3, "go env -w GOPROXY=", source_url, ",direct"); cmd = xy_strjoin(3, "go env -w GOPROXY=", source.url, ",direct");
system(cmd); system(cmd);
xy_success(xy_2strjoin("chsrc: 感谢镜像提供方:", source_name)); xy_success(xy_2strjoin("chsrc: 感谢镜像提供方:", source.mirror->name));
} }
/**
* TODO:
*/
int
pl_rust_cesu (char* option)
{
return common_cesu (pl_rust_sources, pl_rust_sources_n,
"gems/nokogiri-1.15.0-java.gem");
}
/* TODO: 暂未实现 */
void
pl_rust_getsrc (char* option)
{
// char* cmd = "npm config get registry";
// system(cmd);
}
/** /**
* Rust * Rust
* *
@ -541,25 +591,23 @@ pl_go_setsrc (char* option)
void void
pl_rust_setsrc (char* option) pl_rust_setsrc (char* option)
{ {
int selected = 0; char* check_cmd = NULL; int index = 0;
for (int i=0;i<sizeof(pl_rust_sources);i++) { if (NULL!=option) {
// 循环测速 index = does_the_mirror_exist (pl_rust_sources, pl_rust_sources_n, "rust", option);
} else {
index = pl_rust_cesu ("");
} }
const char* source_name = pl_rust_sources[selected].mirror->name; source_info source = pl_rust_sources[index];
const char* source_abbr = pl_rust_sources[selected].mirror->abbr; say_for_setsrc(&source);
const char* source_url = pl_rust_sources[selected].url;
xy_info (xy_2strjoin("chsrc: 选中镜像站:", source_abbr));
const char* file = xy_strjoin (3, const char* file = xy_strjoin (3,
"[source.crates-io]\n" "[source.crates-io]\n"
"replace-with = 'mirror'\n\n" "replace-with = 'mirror'\n\n"
"[source.mirror]\n" "[source.mirror]\n"
"registry = \"", source_url, "\""); "registry = \"", source.url, "\"");
char* cmd = NULL; char* cmd = NULL;
@ -569,20 +617,32 @@ pl_rust_setsrc (char* option)
cmd = xy_strjoin(3, "echo ", file, ">> $HOME/.cargo"); cmd = xy_strjoin(3, "echo ", file, ">> $HOME/.cargo");
system(cmd); system(cmd);
xy_success(xy_2strjoin("chsrc: 感谢镜像提供方:", source_name)); xy_success(xy_2strjoin("chsrc: 感谢镜像提供方:", source.mirror->name));
} }
void
pl_dotnet_cesu (char* option)
{
xy_error ("chsrc: 暂时无法为NuGet测速若有需求请您提交issue");
}
void
pl_dotnet_getsrc (char* option)
{
xy_error ("chsrc: 暂时无法查看NuGet源若有需求请您提交issue");
}
/** /**
* NuGet * NuGet
*
*/ */
void void
pl_dotnet_setsrc (char* option) pl_dotnet_setsrc (char* option)
{ {
int selected = 0; char* check_cmd = NULL; int index = 0; char* check_cmd = NULL;
xy_error ("chsrc: 暂时无法为NuGet换源若有需求请您提交issue"); xy_error ("chsrc: 暂时无法为NuGet换源若有需求请您提交issue");
} }
@ -590,6 +650,23 @@ pl_dotnet_setsrc (char* option)
/**
* TODO:
*/
int
pl_java_cesu (char* option)
{
return common_cesu (pl_java_sources, pl_java_sources_n,
"gems/nokogiri-1.15.0-java.gem");
}
/* TODO: 暂未实现 */
void
pl_java_getsrc (char* option)
{
// char* cmd = "npm config get registry";
// system(cmd);
}
/** /**
* Java * Java
@ -599,7 +676,7 @@ pl_dotnet_setsrc (char* option)
void void
pl_java_setsrc (char* option) pl_java_setsrc (char* option)
{ {
int selected = 0; char* check_cmd = NULL; int index = 0; char* check_cmd = NULL;
if (xy_on_windows) check_cmd = "mvn --version >nul 2>nul"; if (xy_on_windows) check_cmd = "mvn --version >nul 2>nul";
else check_cmd = "mvn --version 1>/dev/null 2>&1"; else check_cmd = "mvn --version 1>/dev/null 2>&1";
@ -614,15 +691,14 @@ pl_java_setsrc (char* option)
return; return;
} }
for (int i=0;i<sizeof(pl_java_sources);i++) { if (NULL!=option) {
// 循环测速 index = does_the_mirror_exist (pl_java_sources, pl_java_sources_n, "java", option);
} else {
index = pl_java_cesu ("");
} }
const char* source_name = pl_java_sources[selected].mirror->name; source_info source = pl_java_sources[index];
const char* source_abbr = pl_java_sources[selected].mirror->abbr; say_for_setsrc(&source);
const char* source_url = pl_java_sources[selected].url;
xy_info (xy_2strjoin("chsrc: 选中镜像站:", source_abbr));
if (mvn_exist_b) { if (mvn_exist_b) {
const char* file = xy_strjoin(3, const char* file = xy_strjoin(3,
@ -630,7 +706,7 @@ pl_java_setsrc (char* option)
" <id>aliyunmaven</id>\n" " <id>aliyunmaven</id>\n"
" <mirrorOf>*</mirrorOf>\n" " <mirrorOf>*</mirrorOf>\n"
" <name>阿里云公共仓库</name>\n" " <name>阿里云公共仓库</name>\n"
" <url>", source_url, "</url>\n" " <url>", source.url, "</url>\n"
"</mirror>"); "</mirror>");
xy_info ("chsrc: 请在您的 maven安装目录/conf/settings.xml 中添加:\n"); xy_info ("chsrc: 请在您的 maven安装目录/conf/settings.xml 中添加:\n");
@ -641,7 +717,7 @@ pl_java_setsrc (char* option)
const char* file = xy_strjoin(3, const char* file = xy_strjoin(3,
"allprojects {\n" "allprojects {\n"
" repositories {\n" " repositories {\n"
" maven { url '", source_url, "' }\n" " maven { url '", source.url, "' }\n"
" mavenLocal()\n" " mavenLocal()\n"
" mavenCentral()\n" " mavenCentral()\n"
" }\n" " }\n"
@ -651,34 +727,48 @@ pl_java_setsrc (char* option)
puts (file); puts (file);
} }
xy_success(xy_2strjoin("chsrc: 感谢镜像提供方:", source_name)); xy_success(xy_2strjoin("chsrc: 感谢镜像提供方:", source.mirror->name));
} }
/** /**
* R * TODO:
* */
* https://help.mirrors.cernet.edu.cn/CRAN/ int
pl_r_cesu (char* option)
{
return common_cesu (pl_r_sources, pl_r_sources_n,
"gems/nokogiri-1.15.0-java.gem");
}
/* TODO: 暂未实现 */
void
pl_r_getsrc (char* option)
{
// char* cmd = "npm config get registry";
// system(cmd);
}
/**
* R https://help.mirrors.cernet.edu.cn/CRAN/
*/ */
void void
pl_r_setsrc (char* option) pl_r_setsrc (char* option)
{ {
int selected = 0; char* check_cmd = NULL; int index = 0;
for (int i=0;i<sizeof(pl_r_sources);i++) { if (NULL!=option) {
// 循环测速 index = does_the_mirror_exist (pl_r_sources, pl_r_sources_n, "r", option);
} else {
index = pl_r_cesu ("");
} }
const char* source_name = pl_r_sources[selected].mirror->name; source_info source = pl_r_sources[index];
const char* source_abbr = pl_r_sources[selected].mirror->abbr; say_for_setsrc(&source);
const char* source_url = pl_r_sources[selected].url;
xy_info (xy_2strjoin("chsrc: 选中镜像站:", source_abbr));
const char* file = xy_strjoin (3, "options(\"repos\" = c(CRAN=\"", source_url, "\"))" ); const char* file = xy_strjoin (3, "options(\"repos\" = c(CRAN=\"", source.url, "\"))" );
char* cmd = NULL; char* cmd = NULL;
// TODO: 待确认Windows 下是否也是该文件 // TODO: 待确认Windows 下是否也是该文件
@ -688,34 +778,48 @@ pl_r_setsrc (char* option)
cmd = xy_strjoin(3, "echo ", file, " >> ~/.Rprofile"); cmd = xy_strjoin(3, "echo ", file, " >> ~/.Rprofile");
system(cmd); system(cmd);
xy_success(xy_2strjoin("chsrc: 感谢镜像提供方:", source.mirror->name));
xy_success(xy_2strjoin("chsrc: 感谢镜像提供方:", source_name));
} }
/** /**
* Julia * TODO:
* */
* https://help.mirrors.cernet.edu.cn/julia/ int
pl_julia_cesu (char* option)
{
return common_cesu (pl_r_sources, pl_r_sources_n,
"gems/nokogiri-1.15.0-java.gem");
}
/* TODO: 暂未实现 */
void
pl_julia_getsrc (char* option)
{
// char* cmd = "npm config get registry";
// system(cmd);
}
/**
* Julia https://help.mirrors.cernet.edu.cn/julia/
*/ */
void void
pl_julia_setsrc (char* option) pl_julia_setsrc (char* option)
{ {
int selected = 0; char* check_cmd = NULL; int index = 0;
for (int i=0;i<sizeof(pl_r_sources);i++) { if (NULL!=option) {
// 循环测速 index = does_the_mirror_exist (pl_julia_sources, pl_julia_sources_n, "julia", option);
} else {
index = pl_julia_cesu ("");
} }
const char* source_name = pl_r_sources[selected].mirror->name; source_info source = pl_julia_sources[index];
const char* source_abbr = pl_r_sources[selected].mirror->abbr; say_for_setsrc(&source);
const char* source_url = pl_r_sources[selected].url;
xy_info (xy_2strjoin("chsrc: 选中镜像站:", source_abbr)); const char* file = xy_strjoin (3, "ENV[\"JULIA_PKG_SERVER\"] = \"", source.url, "\"");
const char* file = xy_strjoin (3, "ENV[\"JULIA_PKG_SERVER\"] = \"", source_url, "\"");
char* cmd = NULL; char* cmd = NULL;
// TODO: $JULIA_DEPOT_PATH/config/startup.jl 是否要考虑环境变量 // TODO: $JULIA_DEPOT_PATH/config/startup.jl 是否要考虑环境变量
@ -725,7 +829,7 @@ pl_julia_setsrc (char* option)
cmd = xy_strjoin(3, "echo ", file, " >> ~/.julia/config/startup.jl"); cmd = xy_strjoin(3, "echo ", file, " >> ~/.julia/config/startup.jl");
system(cmd); system(cmd);
xy_success(xy_2strjoin("chsrc: 感谢镜像提供方:", source_name)); xy_success(xy_2strjoin("chsrc: 感谢镜像提供方:", source.mirror->name));
} }
@ -1022,17 +1126,17 @@ usage[] = {
"维护: https://gitee.com/RubyMetric/chsrc\n", "维护: https://gitee.com/RubyMetric/chsrc\n",
"使用chsrc <command> [target]", "使用chsrc <command> [target] [mirror]",
"help # 打印此帮助,或 h, -h, --help", "help 打印此帮助,或 h, -h, --help",
"list (或 ls, 或 l) # 查看可用镜像源,和可换源软件", "list (或 ls, 或 l) 查看可用镜像源,和可换源软件",
"list mirror(s) # 查看可用镜像源", "list mirror(s) 查看可用镜像源",
"list target(s) # 查看可换源软件", "list target(s) 查看可换源软件",
"list <target> # 查看该软件可以使用哪些源", "list <target> 查看该软件可以使用哪些源",
"cesu <target> # 对该软件所有源测速", "cesu <target> 对该软件所有源测速",
"get <target> # 查看当前软件的源使用情况", "get <target> 查看当前软件的源使用情况",
"set <target> # 换源,自动测速后挑选最快源", "set <target> 换源,自动测速后挑选最快源",
"set <target> def(ault) # 换源,默认挑选经维护者测速排序第一的源", "set <target> def(ault) 换源,默认挑选经维护者测速排序第一的源",
"set <target> <mirror> # 换源,指定使用某镜像站\n" "set <target> <mirror> 换源,指定使用某镜像站\n"
}; };