Skip to content

Commit 3e3e9ae

Browse files
committed
Add chsrc_run_as_x_file()
1 parent fbdeeb1 commit 3e3e9ae

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

src/framework/core.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,6 +1375,92 @@ chsrc_make_tmpfile (char *filename, char *postfix, bool loud, char **tmpfilename
13751375
}
13761376

13771377

1378+
/**
1379+
* 以 bash file.bash 的形式运行脚本内容
1380+
*/
1381+
void
1382+
chsrc_run_as_bash_file (const char *script_content)
1383+
{
1384+
char *tmpfile = NULL;
1385+
FILE *f = chsrc_make_tmpfile ("bash_script", ".bash", false, &tmpfile);
1386+
fwrite (script_content, strlen (script_content), 1, f);
1387+
fclose (f);
1388+
// chmod (tmpfile, 0700);
1389+
char *msg = CHINESE ? "即将执行 Bash 脚本内容:" : "The Bash script content will be executed:";
1390+
chsrc_note2 (msg);
1391+
println (script_content);
1392+
char *cmd = xy_2strjoin ("bash ", tmpfile);
1393+
chsrc_run (cmd, RunOpt_Dont_Abort_On_Failure);
1394+
remove (tmpfile);
1395+
}
1396+
1397+
1398+
/**
1399+
* 以 sh file.sh 的形式运行脚本内容
1400+
*/
1401+
void
1402+
chsrc_run_as_sh_file (const char *script_content)
1403+
{
1404+
char *tmpfile = NULL;
1405+
FILE *f = chsrc_make_tmpfile ("sh_script", ".sh", false, &tmpfile);
1406+
fwrite (script_content, strlen (script_content), 1, f);
1407+
fclose (f);
1408+
// chmod (tmpfile, 0700);
1409+
char *msg = CHINESE ? "即将执行 sh 脚本内容:" : "The sh script content will be executed:";
1410+
chsrc_note2 (msg);
1411+
println (script_content);
1412+
char *cmd = xy_2strjoin ("sh ", tmpfile);
1413+
chsrc_run (cmd, RunOpt_Dont_Abort_On_Failure);
1414+
remove (tmpfile);
1415+
}
1416+
1417+
1418+
/**
1419+
* 以 pwsh file.ps1 的形式运行脚本内容
1420+
*/
1421+
void
1422+
chsrc_run_as_pwsh_file (const char *script_content)
1423+
{
1424+
char *tmpfile = NULL;
1425+
FILE *f = chsrc_make_tmpfile ("pwsh_script", ".ps1", false, &tmpfile);
1426+
fwrite (script_content, strlen (script_content), 1, f);
1427+
fclose (f);
1428+
char *msg = CHINESE ? "即将执行 PowerShell 脚本内容:" : "The PowerShell script content will be executed:";
1429+
chsrc_note2 (msg);
1430+
println (script_content);
1431+
char *cmd = xy_2strjoin ("pwsh ", tmpfile);
1432+
chsrc_run (cmd, RunOpt_Dont_Abort_On_Failure);
1433+
remove (tmpfile);
1434+
}
1435+
1436+
1437+
/**
1438+
* @param cmdline 需要自己负责转义
1439+
*
1440+
* @danger 需要经过 Bash 的转义,很容易出错,不要用这个函数
1441+
*/
1442+
XY_Deprecate_This("Don't use this function")
1443+
void
1444+
chsrc_run_in_inline_bash_shell (const char *cmdline)
1445+
{
1446+
char *cmd = xy_strjoin (3, "bash -c '", cmdline, "'");
1447+
chsrc_run (cmd, RunOpt_Dont_Abort_On_Failure);
1448+
}
1449+
1450+
1451+
/**
1452+
* @param cmdline 需要自己负责转义
1453+
*
1454+
* @danger 需要经过 PowerShell 的转义,很容易出错,不要用这个函数
1455+
*/
1456+
XY_Deprecate_This("Don't use this function")
1457+
void
1458+
chsrc_run_in_inline_pwsh_shell (const char *cmdline)
1459+
{
1460+
char *cmd = xy_strjoin (3, "pwsh -Command '", cmdline, "'");
1461+
chsrc_run (cmd, RunOpt_Dont_Abort_On_Failure);
1462+
}
1463+
13781464

13791465
static void
13801466
chsrc_view_env (const char *var1, ...)

0 commit comments

Comments
 (0)