mirror of
https://github.com/RubyMetric/chsrc
synced 2025-09-08 07:34:42 +08:00
使用 xy.
This commit is contained in:
parent
28d913c58b
commit
55865d6a7b
40
lib/xy.h
40
lib/xy.h
@ -58,12 +58,6 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
bool xy_on_windows = false;
|
|
||||||
bool xy_on_linux = false;
|
|
||||||
bool xy_on_macos = false;
|
|
||||||
bool xy_on_bsd = false;
|
|
||||||
bool xy_on_android = false;
|
|
||||||
|
|
||||||
|
|
||||||
/* 全局变量 与 全局状态 */
|
/* 全局变量 与 全局状态 */
|
||||||
struct
|
struct
|
||||||
@ -892,7 +886,7 @@ xy_run_capture (const char *cmd, char **output)
|
|||||||
static char *
|
static char *
|
||||||
_xy_os_family ()
|
_xy_os_family ()
|
||||||
{
|
{
|
||||||
if (xy_on_windows)
|
if (xy.on_windows)
|
||||||
return "windows";
|
return "windows";
|
||||||
else
|
else
|
||||||
return "unix";
|
return "unix";
|
||||||
@ -905,7 +899,7 @@ _xy_os_family ()
|
|||||||
static const char *
|
static const char *
|
||||||
xy_os_depend_str (const char *str_for_win, const char *str_for_unix)
|
xy_os_depend_str (const char *str_for_win, const char *str_for_unix)
|
||||||
{
|
{
|
||||||
if (xy_on_windows)
|
if (xy.on_windows)
|
||||||
return str_for_win;
|
return str_for_win;
|
||||||
else
|
else
|
||||||
return str_for_unix;
|
return str_for_unix;
|
||||||
@ -923,7 +917,7 @@ static char *
|
|||||||
_xy_os_home ()
|
_xy_os_home ()
|
||||||
{
|
{
|
||||||
char *home = NULL;
|
char *home = NULL;
|
||||||
if (xy_on_windows)
|
if (xy.on_windows)
|
||||||
home = getenv ("USERPROFILE");
|
home = getenv ("USERPROFILE");
|
||||||
else
|
else
|
||||||
home = getenv ("HOME");
|
home = getenv ("HOME");
|
||||||
@ -965,7 +959,7 @@ _xy_win_documents ()
|
|||||||
static char *
|
static char *
|
||||||
_xy_win_powershell_profile ()
|
_xy_win_powershell_profile ()
|
||||||
{
|
{
|
||||||
if (xy_on_windows)
|
if (xy.on_windows)
|
||||||
{
|
{
|
||||||
char *documents_dir = _xy_win_documents ();
|
char *documents_dir = _xy_win_documents ();
|
||||||
char *profile_path = xy_2strcat (documents_dir, "\\PowerShell\\Microsoft.PowerShell_profile.ps1");
|
char *profile_path = xy_2strcat (documents_dir, "\\PowerShell\\Microsoft.PowerShell_profile.ps1");
|
||||||
@ -985,7 +979,7 @@ _xy_win_powershell_profile ()
|
|||||||
static char *
|
static char *
|
||||||
_xy_win_powershellv5_profile ()
|
_xy_win_powershellv5_profile ()
|
||||||
{
|
{
|
||||||
if (xy_on_windows)
|
if (xy.on_windows)
|
||||||
{
|
{
|
||||||
char *documents_dir = _xy_win_documents ();
|
char *documents_dir = _xy_win_documents ();
|
||||||
char *profile_path = xy_2strcat (documents_dir, "\\WindowsPowerShell\\Microsoft.PowerShell_profile.ps1");
|
char *profile_path = xy_2strcat (documents_dir, "\\WindowsPowerShell\\Microsoft.PowerShell_profile.ps1");
|
||||||
@ -1023,7 +1017,7 @@ static bool
|
|||||||
xy_dir_exist (const char *path)
|
xy_dir_exist (const char *path)
|
||||||
{
|
{
|
||||||
const char *dir = path;
|
const char *dir = path;
|
||||||
if (xy_on_windows)
|
if (xy.on_windows)
|
||||||
{
|
{
|
||||||
if (xy_str_start_with (path, "~"))
|
if (xy_str_start_with (path, "~"))
|
||||||
{
|
{
|
||||||
@ -1031,7 +1025,7 @@ xy_dir_exist (const char *path)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (xy_on_windows)
|
if (xy.on_windows)
|
||||||
{
|
{
|
||||||
#ifdef XY_Build_On_Windows
|
#ifdef XY_Build_On_Windows
|
||||||
// 也可以用 opendir() #include <dirent.h>
|
// 也可以用 opendir() #include <dirent.h>
|
||||||
@ -1085,7 +1079,7 @@ xy_normalize_path (const char *path)
|
|||||||
new = xy_2strcat (xy_os_home, xy_str_delete_prefix (new, "~"));
|
new = xy_2strcat (xy_os_home, xy_str_delete_prefix (new, "~"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (xy_on_windows)
|
if (xy.on_windows)
|
||||||
return xy_str_gsub (new, "/", "\\");
|
return xy_str_gsub (new, "/", "\\");
|
||||||
else
|
else
|
||||||
return new;
|
return new;
|
||||||
@ -1121,7 +1115,7 @@ xy_parent_dir (const char *path)
|
|||||||
*last = '\0';
|
*last = '\0';
|
||||||
|
|
||||||
/* Windows上重新使用 \ 作为路径分隔符 */
|
/* Windows上重新使用 \ 作为路径分隔符 */
|
||||||
if (xy_on_windows)
|
if (xy.on_windows)
|
||||||
return xy_str_gsub (dir, "/", "\\");
|
return xy_str_gsub (dir, "/", "\\");
|
||||||
else
|
else
|
||||||
return dir;
|
return dir;
|
||||||
@ -1141,7 +1135,7 @@ xy_detect_os ()
|
|||||||
DIR *d = opendir (path);
|
DIR *d = opendir (path);
|
||||||
if (d)
|
if (d)
|
||||||
{
|
{
|
||||||
xy_on_windows = true;
|
xy.on_windows = true;
|
||||||
closedir (d);
|
closedir (d);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -1155,12 +1149,12 @@ xy_detect_os ()
|
|||||||
fclose (fp);
|
fclose (fp);
|
||||||
if (strstr (buf, "Android"))
|
if (strstr (buf, "Android"))
|
||||||
{
|
{
|
||||||
xy_on_android = true;
|
xy.on_android = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else if (strstr (buf, "Linux"))
|
else if (strstr (buf, "Linux"))
|
||||||
{
|
{
|
||||||
xy_on_linux = true;
|
xy.on_linux = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1173,7 +1167,7 @@ xy_detect_os ()
|
|||||||
d = opendir ("/Library/Apple");
|
d = opendir ("/Library/Apple");
|
||||||
if (d)
|
if (d)
|
||||||
{
|
{
|
||||||
xy_on_macos = true;
|
xy.on_macos = true;
|
||||||
closedir (d);
|
closedir (d);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1185,7 +1179,7 @@ xy_detect_os ()
|
|||||||
if (opendir ("/etc/rc.d"))
|
if (opendir ("/etc/rc.d"))
|
||||||
{
|
{
|
||||||
closedir (d);
|
closedir (d);
|
||||||
xy_on_bsd = true;
|
xy.on_bsd = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1195,10 +1189,10 @@ xy_detect_os ()
|
|||||||
fgets (buf, sizeof (buf), fp);
|
fgets (buf, sizeof (buf), fp);
|
||||||
pclose (fp);
|
pclose (fp);
|
||||||
if (strstr (buf, "BSD") != NULL)
|
if (strstr (buf, "BSD") != NULL)
|
||||||
xy_on_bsd = true;
|
xy.on_bsd = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(xy_on_windows || xy_on_linux || xy_on_android || xy_on_macos || xy_on_bsd))
|
if (!(xy.on_windows || xy.on_linux || xy.on_android || xy.on_macos || xy.on_bsd))
|
||||||
xy_panic ("Unknown operating system");
|
xy_panic ("Unknown operating system");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1220,7 +1214,7 @@ xy_init ()
|
|||||||
{
|
{
|
||||||
xy_detect_os ();
|
xy_detect_os ();
|
||||||
|
|
||||||
if (xy_on_windows)
|
if (xy.on_windows)
|
||||||
xy.os_devnull = "nul";
|
xy.os_devnull = "nul";
|
||||||
else
|
else
|
||||||
xy.os_devnull = "/dev/null";
|
xy.os_devnull = "/dev/null";
|
||||||
|
@ -381,7 +381,7 @@ query_program_exist (char *check_cmd, char *prog_name, int mode)
|
|||||||
static char *
|
static char *
|
||||||
cmd_to_check_program (char *prog_name)
|
cmd_to_check_program (char *prog_name)
|
||||||
{
|
{
|
||||||
char *check_tool = xy_on_windows ? "where " : "command -v ";
|
char *check_tool = xy.on_windows ? "where " : "command -v ";
|
||||||
|
|
||||||
char *quiet_cmd = xy_str_to_quietcmd (xy_2strcat (check_tool, prog_name));
|
char *quiet_cmd = xy_str_to_quietcmd (xy_2strcat (check_tool, prog_name));
|
||||||
|
|
||||||
@ -906,7 +906,7 @@ auto_select_mirror (Source_t *sources, size_t size, const char *target_name)
|
|||||||
exit (Exit_UserCause);
|
exit (Exit_UserCause);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (xy_on_windows)
|
if (xy.on_windows)
|
||||||
{
|
{
|
||||||
char *curl_version = xy_run ("curl --version", 1);
|
char *curl_version = xy_run ("curl --version", 1);
|
||||||
/**
|
/**
|
||||||
@ -1399,7 +1399,7 @@ chsrc_make_tmpfile (char *filename, char *postfix, bool loud, char **tmpfilename
|
|||||||
* 这样的话,其实是是无法删除该文件的,但是生成在 /tmp 目录下我们恰好可以不用清理
|
* 这样的话,其实是是无法删除该文件的,但是生成在 /tmp 目录下我们恰好可以不用清理
|
||||||
* 但是在 Windows 上,就没有办法了,所以我们禁止在 Windows 上不指定返回出的临时文件名
|
* 但是在 Windows 上,就没有办法了,所以我们禁止在 Windows 上不指定返回出的临时文件名
|
||||||
*/
|
*/
|
||||||
if (xy_on_windows && !tmpfilename)
|
if (xy.on_windows && !tmpfilename)
|
||||||
{
|
{
|
||||||
chsrc_error2 ("在 Windows 上,创建临时文件时必须指定返回的临时文件名");
|
chsrc_error2 ("在 Windows 上,创建临时文件时必须指定返回的临时文件名");
|
||||||
xy_unreached();
|
xy_unreached();
|
||||||
@ -1562,7 +1562,7 @@ chsrc_view_file (const char *path)
|
|||||||
{
|
{
|
||||||
char *cmd = NULL;
|
char *cmd = NULL;
|
||||||
path = xy_normalize_path (path);
|
path = xy_normalize_path (path);
|
||||||
if (xy_on_windows)
|
if (xy.on_windows)
|
||||||
{
|
{
|
||||||
cmd = xy_2strcat ("type ", path);
|
cmd = xy_2strcat ("type ", path);
|
||||||
}
|
}
|
||||||
@ -1586,7 +1586,7 @@ chsrc_ensure_dir (const char *dir)
|
|||||||
|
|
||||||
// 不存在就生成
|
// 不存在就生成
|
||||||
char *mkdir_cmd = NULL;
|
char *mkdir_cmd = NULL;
|
||||||
if (xy_on_windows)
|
if (xy.on_windows)
|
||||||
{
|
{
|
||||||
mkdir_cmd = "md "; // 已存在时返回 errorlevel = 1
|
mkdir_cmd = "md "; // 已存在时返回 errorlevel = 1
|
||||||
}
|
}
|
||||||
@ -1644,7 +1644,7 @@ log_anyway:
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
char *cmd = NULL;
|
char *cmd = NULL;
|
||||||
if (xy_on_windows)
|
if (xy.on_windows)
|
||||||
{
|
{
|
||||||
cmd = xy_strcat (4, "echo ", str, " >> ", file);
|
cmd = xy_strcat (4, "echo ", str, " >> ", file);
|
||||||
}
|
}
|
||||||
@ -1669,7 +1669,7 @@ chsrc_prepend_to_file (const char *str, const char *filename)
|
|||||||
chsrc_ensure_dir (dir);
|
chsrc_ensure_dir (dir);
|
||||||
|
|
||||||
char *cmd = NULL;
|
char *cmd = NULL;
|
||||||
if (xy_on_windows)
|
if (xy.on_windows)
|
||||||
{
|
{
|
||||||
xy_unimplemented();
|
xy_unimplemented();
|
||||||
}
|
}
|
||||||
@ -1697,7 +1697,7 @@ chsrc_overwrite_file (const char *str, const char *filename)
|
|||||||
chsrc_ensure_dir (dir);
|
chsrc_ensure_dir (dir);
|
||||||
|
|
||||||
char *cmd = NULL;
|
char *cmd = NULL;
|
||||||
if (xy_on_windows)
|
if (xy.on_windows)
|
||||||
{
|
{
|
||||||
cmd = xy_strcat (4, "echo ", str, " > ", file);
|
cmd = xy_strcat (4, "echo ", str, " > ", file);
|
||||||
}
|
}
|
||||||
@ -1730,12 +1730,12 @@ chsrc_backup (const char *path)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (xy_on_bsd || xy_on_macos)
|
if (xy.on_bsd || xy.on_macos)
|
||||||
{
|
{
|
||||||
/* BSD 和 macOS 的 cp 不支持 --backup 选项 */
|
/* BSD 和 macOS 的 cp 不支持 --backup 选项 */
|
||||||
cmd = xy_strcat (5, "cp -f ", path, " ", path, ".bak");
|
cmd = xy_strcat (5, "cp -f ", path, " ", path, ".bak");
|
||||||
}
|
}
|
||||||
else if (xy_on_windows)
|
else if (xy.on_windows)
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @note /Y 表示覆盖
|
* @note /Y 表示覆盖
|
||||||
|
@ -55,7 +55,7 @@ pl_dart_flutter_setsrc (char *option)
|
|||||||
|
|
||||||
char *w = NULL;
|
char *w = NULL;
|
||||||
char *cmd = NULL;
|
char *cmd = NULL;
|
||||||
if (xy_on_windows)
|
if (xy.on_windows)
|
||||||
{
|
{
|
||||||
cmd = xy_strcat (3, "setx FLUTTER_STORAGE_BASE_URL \"", source.url, "\"");
|
cmd = xy_strcat (3, "setx FLUTTER_STORAGE_BASE_URL \"", source.url, "\"");
|
||||||
chsrc_run (cmd, RunOpt_No_Last_New_Line);
|
chsrc_run (cmd, RunOpt_No_Last_New_Line);
|
||||||
|
@ -51,7 +51,7 @@ pl_dart_setsrc (char *option)
|
|||||||
|
|
||||||
char *w = NULL;
|
char *w = NULL;
|
||||||
char *cmd = NULL;
|
char *cmd = NULL;
|
||||||
if (xy_on_windows)
|
if (xy.on_windows)
|
||||||
{
|
{
|
||||||
cmd = xy_strcat (3, "setx PUB_HOSTED_URL \"", source.url, "\"");
|
cmd = xy_strcat (3, "setx PUB_HOSTED_URL \"", source.url, "\"");
|
||||||
chsrc_run (cmd, RunOpt_No_Last_New_Line);
|
chsrc_run (cmd, RunOpt_No_Last_New_Line);
|
||||||
|
@ -44,7 +44,7 @@ pl_haskell_setsrc (char *option)
|
|||||||
char *content = xy_str_gsub (RAWSTR_pl_haskell_cabal_config, "@url@", source.url);
|
char *content = xy_str_gsub (RAWSTR_pl_haskell_cabal_config, "@url@", source.url);
|
||||||
|
|
||||||
char *config = NULL;
|
char *config = NULL;
|
||||||
if (xy_on_windows)
|
if (xy.on_windows)
|
||||||
{
|
{
|
||||||
config = xy_normalize_path ("~/AppData/Roaming/cabal/config");
|
config = xy_normalize_path ("~/AppData/Roaming/cabal/config");
|
||||||
}
|
}
|
||||||
|
@ -49,7 +49,7 @@ pl_python_find_uv_config (bool mkdir)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (xy_on_windows)
|
if (xy.on_windows)
|
||||||
{
|
{
|
||||||
/* config path on Windows */
|
/* config path on Windows */
|
||||||
char *appdata = getenv ("APPDATA");
|
char *appdata = getenv ("APPDATA");
|
||||||
|
@ -46,7 +46,7 @@ pl_r_getsrc (char *option)
|
|||||||
* options()$repos
|
* options()$repos
|
||||||
* options()$BioC_mirror
|
* options()$BioC_mirror
|
||||||
*/
|
*/
|
||||||
if (xy_on_windows)
|
if (xy.on_windows)
|
||||||
{
|
{
|
||||||
chsrc_view_file (PL_R_Config_Windows);
|
chsrc_view_file (PL_R_Config_Windows);
|
||||||
}
|
}
|
||||||
@ -74,7 +74,7 @@ pl_r_setsrc (char *option)
|
|||||||
|
|
||||||
// 或者我们调用 r.exe --slave -e 上面的内容
|
// 或者我们调用 r.exe --slave -e 上面的内容
|
||||||
|
|
||||||
char *config = xy_on_windows ? PL_R_Config_Windows : PL_R_Config_POSIX;
|
char *config = xy.on_windows ? PL_R_Config_Windows : PL_R_Config_POSIX;
|
||||||
|
|
||||||
chsrc_append_to_file (w, config);
|
chsrc_append_to_file (w, config);
|
||||||
|
|
||||||
|
@ -59,7 +59,7 @@ wr_anaconda_setsrc (char *option)
|
|||||||
/* Windows 也是在这里 */
|
/* Windows 也是在这里 */
|
||||||
char *configfile = xy_2strcat (xy_os_home, "/.condarc");
|
char *configfile = xy_2strcat (xy_os_home, "/.condarc");
|
||||||
|
|
||||||
if (xy_on_windows)
|
if (xy.on_windows)
|
||||||
{
|
{
|
||||||
if (xy_file_exist (configfile))
|
if (xy_file_exist (configfile))
|
||||||
{
|
{
|
||||||
|
@ -50,7 +50,7 @@ wr_docker_prelude ()
|
|||||||
void
|
void
|
||||||
wr_docker_getsrc (char *option)
|
wr_docker_getsrc (char *option)
|
||||||
{
|
{
|
||||||
if (xy_on_linux || xy_on_bsd)
|
if (xy.on_linux || xy.on_bsd)
|
||||||
{
|
{
|
||||||
chsrc_view_file (WR_Docker_ConfigFile);
|
chsrc_view_file (WR_Docker_ConfigFile);
|
||||||
}
|
}
|
||||||
@ -73,7 +73,7 @@ wr_docker_setsrc (char *option)
|
|||||||
|
|
||||||
chsrc_use_this_source (wr_docker);
|
chsrc_use_this_source (wr_docker);
|
||||||
|
|
||||||
if (xy_on_linux || xy_on_bsd)
|
if (xy.on_linux || xy.on_bsd)
|
||||||
{
|
{
|
||||||
char *to_add = xy_str_gsub (RAWSTR_wr_docker_insert_content, "@1@", source.url);
|
char *to_add = xy_str_gsub (RAWSTR_wr_docker_insert_content, "@1@", source.url);
|
||||||
|
|
||||||
@ -119,7 +119,7 @@ wr_docker_setsrc (char *option)
|
|||||||
chsrc_append_to_file (to_add, WR_Docker_ConfigFile);
|
chsrc_append_to_file (to_add, WR_Docker_ConfigFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (xy_on_linux)
|
if (xy.on_linux)
|
||||||
{
|
{
|
||||||
/* 由于 systemctl restart docker 会导致所有容器停止,所以不自动重启 */
|
/* 由于 systemctl restart docker 会导致所有容器停止,所以不自动重启 */
|
||||||
chsrc_alert2 ("请自行运行: sudo systemctl restart docker");
|
chsrc_alert2 ("请自行运行: sudo systemctl restart docker");
|
||||||
|
@ -56,7 +56,7 @@ main (int argc, char const *argv[])
|
|||||||
chsrc_ensure_dir ("test");
|
chsrc_ensure_dir ("test");
|
||||||
|
|
||||||
chsrc_append_to_file ("append", bkup);
|
chsrc_append_to_file ("append", bkup);
|
||||||
if (!xy_on_windows)
|
if (!xy.on_windows)
|
||||||
{
|
{
|
||||||
chsrc_prepend_to_file ("prepend", bkup);
|
chsrc_prepend_to_file ("prepend", bkup);
|
||||||
}
|
}
|
||||||
@ -67,7 +67,7 @@ main (int argc, char const *argv[])
|
|||||||
print ("chsrc: CPU cores = ");
|
print ("chsrc: CPU cores = ");
|
||||||
println (chsrc_get_cpucore ());
|
println (chsrc_get_cpucore ());
|
||||||
|
|
||||||
if (xy_on_windows)
|
if (xy.on_windows)
|
||||||
{
|
{
|
||||||
chsrc_run ("del " bkup, RunOpt_No_Last_New_Line);
|
chsrc_run ("del " bkup, RunOpt_No_Last_New_Line);
|
||||||
}
|
}
|
||||||
@ -83,7 +83,7 @@ main (int argc, char const *argv[])
|
|||||||
|
|
||||||
// chsrc_run_in_inline_pwsh_shell ("Write-Host \"Hello from inline PowerShell\"");
|
// chsrc_run_in_inline_pwsh_shell ("Write-Host \"Hello from inline PowerShell\"");
|
||||||
|
|
||||||
if (xy_on_windows)
|
if (xy.on_windows)
|
||||||
{
|
{
|
||||||
chsrc_run_as_pwsh_file ("Write-Host \"Hello from PowerShell file\"");
|
chsrc_run_as_pwsh_file ("Write-Host \"Hello from PowerShell file\"");
|
||||||
}
|
}
|
||||||
|
@ -110,7 +110,7 @@ main (int argc, char const *argv[])
|
|||||||
|
|
||||||
assert (xy_file_exist ("./doc/image/chsrc.png"));
|
assert (xy_file_exist ("./doc/image/chsrc.png"));
|
||||||
assert (xy_dir_exist ("~"));
|
assert (xy_dir_exist ("~"));
|
||||||
if (xy_on_windows)
|
if (xy.on_windows)
|
||||||
{
|
{
|
||||||
say (xy_normalize_path ("~"));
|
say (xy_normalize_path ("~"));
|
||||||
say (xy_normalize_path ("~/"));
|
say (xy_normalize_path ("~/"));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user