Skip to content

Commit 55865d6

Browse files
committed
使用 xy.
1 parent 28d913c commit 55865d6

File tree

11 files changed

+41
-47
lines changed

11 files changed

+41
-47
lines changed

lib/xy.h

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,6 @@
5858
#endif
5959

6060

61-
bool xy_on_windows = false;
62-
bool xy_on_linux = false;
63-
bool xy_on_macos = false;
64-
bool xy_on_bsd = false;
65-
bool xy_on_android = false;
66-
6761

6862
/* 全局变量 与 全局状态 */
6963
struct
@@ -892,7 +886,7 @@ xy_run_capture (const char *cmd, char **output)
892886
static char *
893887
_xy_os_family ()
894888
{
895-
if (xy_on_windows)
889+
if (xy.on_windows)
896890
return "windows";
897891
else
898892
return "unix";
@@ -905,7 +899,7 @@ _xy_os_family ()
905899
static const char *
906900
xy_os_depend_str (const char *str_for_win, const char *str_for_unix)
907901
{
908-
if (xy_on_windows)
902+
if (xy.on_windows)
909903
return str_for_win;
910904
else
911905
return str_for_unix;
@@ -923,7 +917,7 @@ static char *
923917
_xy_os_home ()
924918
{
925919
char *home = NULL;
926-
if (xy_on_windows)
920+
if (xy.on_windows)
927921
home = getenv ("USERPROFILE");
928922
else
929923
home = getenv ("HOME");
@@ -965,7 +959,7 @@ _xy_win_documents ()
965959
static char *
966960
_xy_win_powershell_profile ()
967961
{
968-
if (xy_on_windows)
962+
if (xy.on_windows)
969963
{
970964
char *documents_dir = _xy_win_documents ();
971965
char *profile_path = xy_2strcat (documents_dir, "\\PowerShell\\Microsoft.PowerShell_profile.ps1");
@@ -985,7 +979,7 @@ _xy_win_powershell_profile ()
985979
static char *
986980
_xy_win_powershellv5_profile ()
987981
{
988-
if (xy_on_windows)
982+
if (xy.on_windows)
989983
{
990984
char *documents_dir = _xy_win_documents ();
991985
char *profile_path = xy_2strcat (documents_dir, "\\WindowsPowerShell\\Microsoft.PowerShell_profile.ps1");
@@ -1023,15 +1017,15 @@ static bool
10231017
xy_dir_exist (const char *path)
10241018
{
10251019
const char *dir = path;
1026-
if (xy_on_windows)
1020+
if (xy.on_windows)
10271021
{
10281022
if (xy_str_start_with (path, "~"))
10291023
{
10301024
dir = xy_2strcat (xy_os_home, path + 1);
10311025
}
10321026
}
10331027

1034-
if (xy_on_windows)
1028+
if (xy.on_windows)
10351029
{
10361030
#ifdef XY_Build_On_Windows
10371031
// 也可以用 opendir() #include <dirent.h>
@@ -1085,7 +1079,7 @@ xy_normalize_path (const char *path)
10851079
new = xy_2strcat (xy_os_home, xy_str_delete_prefix (new, "~"));
10861080
}
10871081

1088-
if (xy_on_windows)
1082+
if (xy.on_windows)
10891083
return xy_str_gsub (new, "/", "\\");
10901084
else
10911085
return new;
@@ -1121,7 +1115,7 @@ xy_parent_dir (const char *path)
11211115
*last = '\0';
11221116

11231117
/* Windows上重新使用 \ 作为路径分隔符 */
1124-
if (xy_on_windows)
1118+
if (xy.on_windows)
11251119
return xy_str_gsub (dir, "/", "\\");
11261120
else
11271121
return dir;
@@ -1141,7 +1135,7 @@ xy_detect_os ()
11411135
DIR *d = opendir (path);
11421136
if (d)
11431137
{
1144-
xy_on_windows = true;
1138+
xy.on_windows = true;
11451139
closedir (d);
11461140
return;
11471141
}
@@ -1155,12 +1149,12 @@ xy_detect_os ()
11551149
fclose (fp);
11561150
if (strstr (buf, "Android"))
11571151
{
1158-
xy_on_android = true;
1152+
xy.on_android = true;
11591153
return;
11601154
}
11611155
else if (strstr (buf, "Linux"))
11621156
{
1163-
xy_on_linux = true;
1157+
xy.on_linux = true;
11641158
return;
11651159
}
11661160
}
@@ -1173,7 +1167,7 @@ xy_detect_os ()
11731167
d = opendir ("/Library/Apple");
11741168
if (d)
11751169
{
1176-
xy_on_macos = true;
1170+
xy.on_macos = true;
11771171
closedir (d);
11781172
}
11791173
}
@@ -1185,7 +1179,7 @@ xy_detect_os ()
11851179
if (opendir ("/etc/rc.d"))
11861180
{
11871181
closedir (d);
1188-
xy_on_bsd = true;
1182+
xy.on_bsd = true;
11891183
return;
11901184
}
11911185
}
@@ -1195,10 +1189,10 @@ xy_detect_os ()
11951189
fgets (buf, sizeof (buf), fp);
11961190
pclose (fp);
11971191
if (strstr (buf, "BSD") != NULL)
1198-
xy_on_bsd = true;
1192+
xy.on_bsd = true;
11991193
}
12001194

1201-
if (!(xy_on_windows || xy_on_linux || xy_on_android || xy_on_macos || xy_on_bsd))
1195+
if (!(xy.on_windows || xy.on_linux || xy.on_android || xy.on_macos || xy.on_bsd))
12021196
xy_panic ("Unknown operating system");
12031197
}
12041198

@@ -1220,7 +1214,7 @@ xy_init ()
12201214
{
12211215
xy_detect_os ();
12221216

1223-
if (xy_on_windows)
1217+
if (xy.on_windows)
12241218
xy.os_devnull = "nul";
12251219
else
12261220
xy.os_devnull = "/dev/null";

src/framework/core.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ query_program_exist (char *check_cmd, char *prog_name, int mode)
381381
static char *
382382
cmd_to_check_program (char *prog_name)
383383
{
384-
char *check_tool = xy_on_windows ? "where " : "command -v ";
384+
char *check_tool = xy.on_windows ? "where " : "command -v ";
385385

386386
char *quiet_cmd = xy_str_to_quietcmd (xy_2strcat (check_tool, prog_name));
387387

@@ -906,7 +906,7 @@ auto_select_mirror (Source_t *sources, size_t size, const char *target_name)
906906
exit (Exit_UserCause);
907907
}
908908

909-
if (xy_on_windows)
909+
if (xy.on_windows)
910910
{
911911
char *curl_version = xy_run ("curl --version", 1);
912912
/**
@@ -1399,7 +1399,7 @@ chsrc_make_tmpfile (char *filename, char *postfix, bool loud, char **tmpfilename
13991399
* 这样的话,其实是是无法删除该文件的,但是生成在 /tmp 目录下我们恰好可以不用清理
14001400
* 但是在 Windows 上,就没有办法了,所以我们禁止在 Windows 上不指定返回出的临时文件名
14011401
*/
1402-
if (xy_on_windows && !tmpfilename)
1402+
if (xy.on_windows && !tmpfilename)
14031403
{
14041404
chsrc_error2 ("在 Windows 上,创建临时文件时必须指定返回的临时文件名");
14051405
xy_unreached();
@@ -1562,7 +1562,7 @@ chsrc_view_file (const char *path)
15621562
{
15631563
char *cmd = NULL;
15641564
path = xy_normalize_path (path);
1565-
if (xy_on_windows)
1565+
if (xy.on_windows)
15661566
{
15671567
cmd = xy_2strcat ("type ", path);
15681568
}
@@ -1586,7 +1586,7 @@ chsrc_ensure_dir (const char *dir)
15861586

15871587
// 不存在就生成
15881588
char *mkdir_cmd = NULL;
1589-
if (xy_on_windows)
1589+
if (xy.on_windows)
15901590
{
15911591
mkdir_cmd = "md "; // 已存在时返回 errorlevel = 1
15921592
}
@@ -1644,7 +1644,7 @@ chsrc_append_to_file (const char *str, const char *filename)
16441644

16451645
/*
16461646
char *cmd = NULL;
1647-
if (xy_on_windows)
1647+
if (xy.on_windows)
16481648
{
16491649
cmd = xy_strcat (4, "echo ", str, " >> ", file);
16501650
}
@@ -1669,7 +1669,7 @@ chsrc_prepend_to_file (const char *str, const char *filename)
16691669
chsrc_ensure_dir (dir);
16701670

16711671
char *cmd = NULL;
1672-
if (xy_on_windows)
1672+
if (xy.on_windows)
16731673
{
16741674
xy_unimplemented();
16751675
}
@@ -1697,7 +1697,7 @@ chsrc_overwrite_file (const char *str, const char *filename)
16971697
chsrc_ensure_dir (dir);
16981698

16991699
char *cmd = NULL;
1700-
if (xy_on_windows)
1700+
if (xy.on_windows)
17011701
{
17021702
cmd = xy_strcat (4, "echo ", str, " > ", file);
17031703
}
@@ -1730,12 +1730,12 @@ chsrc_backup (const char *path)
17301730
return;
17311731
}
17321732

1733-
if (xy_on_bsd || xy_on_macos)
1733+
if (xy.on_bsd || xy.on_macos)
17341734
{
17351735
/* BSD 和 macOS 的 cp 不支持 --backup 选项 */
17361736
cmd = xy_strcat (5, "cp -f ", path, " ", path, ".bak");
17371737
}
1738-
else if (xy_on_windows)
1738+
else if (xy.on_windows)
17391739
{
17401740
/**
17411741
* @note /Y 表示覆盖

src/recipe/lang/Dart/Flutter.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pl_dart_flutter_setsrc (char *option)
5555

5656
char *w = NULL;
5757
char *cmd = NULL;
58-
if (xy_on_windows)
58+
if (xy.on_windows)
5959
{
6060
cmd = xy_strcat (3, "setx FLUTTER_STORAGE_BASE_URL \"", source.url, "\"");
6161
chsrc_run (cmd, RunOpt_No_Last_New_Line);

src/recipe/lang/Dart/Pub.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pl_dart_setsrc (char *option)
5151

5252
char *w = NULL;
5353
char *cmd = NULL;
54-
if (xy_on_windows)
54+
if (xy.on_windows)
5555
{
5656
cmd = xy_strcat (3, "setx PUB_HOSTED_URL \"", source.url, "\"");
5757
chsrc_run (cmd, RunOpt_No_Last_New_Line);

src/recipe/lang/Haskell.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pl_haskell_setsrc (char *option)
4444
char *content = xy_str_gsub (RAWSTR_pl_haskell_cabal_config, "@url@", source.url);
4545

4646
char *config = NULL;
47-
if (xy_on_windows)
47+
if (xy.on_windows)
4848
{
4949
config = xy_normalize_path ("~/AppData/Roaming/cabal/config");
5050
}

src/recipe/lang/Python/uv.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pl_python_find_uv_config (bool mkdir)
4949
}
5050
else
5151
{
52-
if (xy_on_windows)
52+
if (xy.on_windows)
5353
{
5454
/* config path on Windows */
5555
char *appdata = getenv ("APPDATA");

src/recipe/lang/R.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pl_r_getsrc (char *option)
4646
* options()$repos
4747
* options()$BioC_mirror
4848
*/
49-
if (xy_on_windows)
49+
if (xy.on_windows)
5050
{
5151
chsrc_view_file (PL_R_Config_Windows);
5252
}
@@ -74,7 +74,7 @@ pl_r_setsrc (char *option)
7474

7575
// 或者我们调用 r.exe --slave -e 上面的内容
7676

77-
char *config = xy_on_windows ? PL_R_Config_Windows : PL_R_Config_POSIX;
77+
char *config = xy.on_windows ? PL_R_Config_Windows : PL_R_Config_POSIX;
7878

7979
chsrc_append_to_file (w, config);
8080

src/recipe/ware/Anaconda/Anaconda.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ wr_anaconda_setsrc (char *option)
5959
/* Windows 也是在这里 */
6060
char *configfile = xy_2strcat (xy_os_home, "/.condarc");
6161

62-
if (xy_on_windows)
62+
if (xy.on_windows)
6363
{
6464
if (xy_file_exist (configfile))
6565
{

src/recipe/ware/Docker/Docker.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ wr_docker_prelude ()
5050
void
5151
wr_docker_getsrc (char *option)
5252
{
53-
if (xy_on_linux || xy_on_bsd)
53+
if (xy.on_linux || xy.on_bsd)
5454
{
5555
chsrc_view_file (WR_Docker_ConfigFile);
5656
}
@@ -73,7 +73,7 @@ wr_docker_setsrc (char *option)
7373

7474
chsrc_use_this_source (wr_docker);
7575

76-
if (xy_on_linux || xy_on_bsd)
76+
if (xy.on_linux || xy.on_bsd)
7777
{
7878
char *to_add = xy_str_gsub (RAWSTR_wr_docker_insert_content, "@1@", source.url);
7979

@@ -119,7 +119,7 @@ wr_docker_setsrc (char *option)
119119
chsrc_append_to_file (to_add, WR_Docker_ConfigFile);
120120
}
121121

122-
if (xy_on_linux)
122+
if (xy.on_linux)
123123
{
124124
/* 由于 systemctl restart docker 会导致所有容器停止,所以不自动重启 */
125125
chsrc_alert2 ("请自行运行: sudo systemctl restart docker");

test/fw.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ main (int argc, char const *argv[])
5656
chsrc_ensure_dir ("test");
5757

5858
chsrc_append_to_file ("append", bkup);
59-
if (!xy_on_windows)
59+
if (!xy.on_windows)
6060
{
6161
chsrc_prepend_to_file ("prepend", bkup);
6262
}
@@ -67,7 +67,7 @@ main (int argc, char const *argv[])
6767
print ("chsrc: CPU cores = ");
6868
println (chsrc_get_cpucore ());
6969

70-
if (xy_on_windows)
70+
if (xy.on_windows)
7171
{
7272
chsrc_run ("del " bkup, RunOpt_No_Last_New_Line);
7373
}
@@ -83,7 +83,7 @@ main (int argc, char const *argv[])
8383

8484
// chsrc_run_in_inline_pwsh_shell ("Write-Host \"Hello from inline PowerShell\"");
8585

86-
if (xy_on_windows)
86+
if (xy.on_windows)
8787
{
8888
chsrc_run_as_pwsh_file ("Write-Host \"Hello from PowerShell file\"");
8989
}

0 commit comments

Comments
 (0)