Use xy_str_to_quietcmd

This commit is contained in:
Aoran Zeng 2023-09-04 15:10:18 +08:00
parent dad186a071
commit d089646058
2 changed files with 27 additions and 42 deletions

59
chsrc.c
View File

@ -181,13 +181,10 @@ void
pl_ruby_setsrc (char* option) pl_ruby_setsrc (char* option)
{ {
int index = 0; int index = 0;
char* check_cmd = xy_str_to_quietcmd("gem -v");
char* check_cmd = NULL;
if (xy_on_windows) check_cmd = "gem -v >nul 2>nul";
else check_cmd = "gem -v 1>/dev/null 2>&1";
bool exist_b = does_the_program_exist (check_cmd, "gem"); bool exist_b = does_the_program_exist (check_cmd, "gem");
if (!exist_b) { if (!exist_b) {
xy_error ("chsrc: 未找到 gem 相关命令,请检查是否存在"); xy_error ("chsrc: 未找到 gem 命令,请检查是否存在");
return; return;
} }
@ -198,30 +195,27 @@ pl_ruby_setsrc (char* option)
} }
source_info source = pl_ruby_sources[index]; source_info source = pl_ruby_sources[index];
const char* source_name = source.mirror->name;
const char* source_url = source.url;
say_for_setsrc (&source); say_for_setsrc (&source);
xy_info("chsrc: 为 gem 命令换源"); xy_info("chsrc: 为 gem 命令换源");
system("gem source -r https://rubygems.org/"); system("gem source -r https://rubygems.org/");
char* cmd = xy_2strjoin("gem source -a ", source_url); char* cmd = xy_2strjoin("gem source -a ", source.url);
system(cmd); system(cmd);
if (xy_on_windows) check_cmd = "bundle -v >nul 2>nul"; check_cmd = xy_str_to_quietcmd("bundle -v");
else check_cmd = "bundle -v 1>/dev/null 2>&1";
exist_b = does_the_program_exist (check_cmd, "bundle"); exist_b = does_the_program_exist (check_cmd, "bundle");
if (!exist_b) { if (!exist_b) {
xy_error ("chsrc: 未找到 bundle 相关命令,请检查是否存在"); xy_error ("chsrc: 未找到 bundle 命令,请检查是否存在");
return; return;
} }
cmd = xy_2strjoin("bundle config 'mirror.https://rubygems.org' ", source_url); cmd = xy_2strjoin("bundle config 'mirror.https://rubygems.org' ", source.url);
xy_info("chsrc: 为 bundler 命令换源"); xy_info("chsrc: 为 bundler 命令换源");
system(cmd); system(cmd);
xy_success(xy_2strjoin("chsrc: 感谢镜像提供方:", source_name)); xy_success(xy_2strjoin("chsrc: 感谢镜像提供方:", source.mirror->name));
} }
@ -232,17 +226,13 @@ pl_ruby_setsrc (char* option)
void void
_pl_python_check_cmd (char** prog) _pl_python_check_cmd (char** prog)
{ {
char* check_cmd = NULL; *prog = NULL; *prog = NULL;
// 不要调用 python 自己,而是使用 python --version,避免Windows弹出Microsoft Store // 不要调用 python 自己,而是使用 python --version,避免Windows弹出Microsoft Store
if (xy_on_windows) check_cmd = "python --version >nul 2>nul"; char* check_cmd = xy_str_to_quietcmd("python --version");
else check_cmd = "python --version 1>/dev/null 2>&1";
bool exist_b = does_the_program_exist (check_cmd, "python"); bool exist_b = does_the_program_exist (check_cmd, "python");
if (!exist_b) { if (!exist_b) {
if (xy_on_windows) check_cmd = "python3 --version >nul 2>nul"; check_cmd = xy_str_to_quietcmd("python3 --version");
else check_cmd = "python3 --version 1>/dev/null 2>&1";
exist_b = does_the_program_exist (check_cmd, "python3"); exist_b = does_the_program_exist (check_cmd, "python3");
if (exist_b) *prog = "python3"; if (exist_b) *prog = "python3";
} }
@ -297,10 +287,7 @@ pl_python_setsrc (char* option)
void void
_pl_nodejs_check_cmd () _pl_nodejs_check_cmd ()
{ {
char* check_cmd = NULL; char* check_cmd = xy_str_to_quietcmd("npm -v");
if (xy_on_windows) check_cmd = "npm -v >nul 2>nul";
else check_cmd = "npm -v 1>/dev/null 2>&1";
bool exist_b = does_the_program_exist (check_cmd, "npm"); bool exist_b = does_the_program_exist (check_cmd, "npm");
if (!exist_b) { if (!exist_b) {
xy_error ("chsrc: 未找到 npm 命令,请检查是否存在"); xy_error ("chsrc: 未找到 npm 命令,请检查是否存在");
@ -347,10 +334,7 @@ pl_nodejs_setsrc (char* option)
void void
_pl_perl_check_cmd () _pl_perl_check_cmd ()
{ {
char* check_cmd = NULL; char* check_cmd = xy_str_to_quietcmd("perl --version");
if (xy_on_windows) check_cmd = "perl --version >nul 2>nul";
else check_cmd = "perl --version 1>/dev/null 2>&1";
bool exist_b = does_the_program_exist (check_cmd, "perl"); bool exist_b = does_the_program_exist (check_cmd, "perl");
if (!exist_b) { if (!exist_b) {
@ -401,10 +385,7 @@ pl_perl_setsrc (char* option)
void void
_pl_php_check_cmd() _pl_php_check_cmd()
{ {
char* check_cmd = NULL; char* check_cmd = xy_str_to_quietcmd("composer --version");
if (xy_on_windows) check_cmd = "composer --version >nul 2>nul";
else check_cmd = "composer --version 1>/dev/null 2>&1";
bool exist_b = does_the_program_exist (check_cmd, "composer"); bool exist_b = does_the_program_exist (check_cmd, "composer");
if (!exist_b) { if (!exist_b) {
@ -456,10 +437,7 @@ pl_php_setsrc (char* option)
void void
_pl_go_check_cmd () _pl_go_check_cmd ()
{ {
char* check_cmd = NULL; char* check_cmd = xy_str_to_quietcmd("go version");
if (xy_on_windows) check_cmd = "go version >nul 2>nul";
else check_cmd = "go version 1>/dev/null 2>&1";
bool exist_b = does_the_program_exist (check_cmd, "go"); bool exist_b = does_the_program_exist (check_cmd, "go");
if (!exist_b) { if (!exist_b) {
@ -472,7 +450,6 @@ void
pl_go_getsrc (char* option) pl_go_getsrc (char* option)
{ {
_pl_go_check_cmd (); _pl_go_check_cmd ();
char* cmd = "go env GOPROXY"; char* cmd = "go env GOPROXY";
system(cmd); system(cmd);
} }
@ -587,14 +564,12 @@ pl_java_getsrc (char* option)
void void
pl_java_setsrc (char* option) pl_java_setsrc (char* option)
{ {
int index = 0; char* check_cmd = NULL; int index = 0;
if (xy_on_windows) check_cmd = "mvn --version >nul 2>nul"; char* check_cmd = xy_str_to_quietcmd("mvn --version");
else check_cmd = "mvn --version 1>/dev/null 2>&1";
bool mvn_exist_b = does_the_program_exist (check_cmd, "mvn"); bool mvn_exist_b = does_the_program_exist (check_cmd, "mvn");
if (xy_on_windows) check_cmd = "gradle --version >nul 2>nul"; check_cmd = xy_str_to_quietcmd("gradle --version");
else check_cmd = "gradle --version 1>/dev/null 2>&1";
bool gradle_exist_b = does_the_program_exist (check_cmd, "gradle"); bool gradle_exist_b = does_the_program_exist (check_cmd, "gradle");
if (!mvn_exist_b && !gradle_exist_b) { if (!mvn_exist_b && !gradle_exist_b) {

10
xy.h
View File

@ -252,6 +252,15 @@ xy_strjoin (unsigned int count, ...)
} }
char*
xy_strdup(const char* str)
{
size_t len = strlen(str);
char* new = xy_malloc0(len+1);
strcpy(new, str);
}
bool bool
xy_streql(const char* str1, const char* str2) { xy_streql(const char* str1, const char* str2) {
return strcmp(str1, str2) == 0 ? true : false; return strcmp(str1, str2) == 0 ? true : false;
@ -267,6 +276,7 @@ xy_str_to_quietcmd (const char* cmd)
#else #else
ret = xy_2strjoin (cmd, " 1>/dev/null 2>&1"); ret = xy_2strjoin (cmd, " 1>/dev/null 2>&1");
#endif #endif
return ret;
} }
#endif #endif