Skip to content

Commit 6418b15

Browse files
committed
优先使用 pwsh,不存在时回落使用 powershell
1 parent a41ff32 commit 6418b15

2 files changed

Lines changed: 51 additions & 10 deletions

File tree

src/framework/core.c

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
* SPDX-License-Identifier: GPL-3.0-or-later
33
* -------------------------------------------------------------
44
* File Name : core.c
5-
* File Authors : 曾奥然 <ccmywish@qq.com>
6-
* | 郭恒 <2085471348@qq.com>
7-
* Contributors : Peng Gao <gn3po4g@outlook.com>
8-
* | Happy Game <happygame10124@gmail.com>
9-
* | Yangmoooo <yangmoooo@outlook.com>
10-
* | BingChunMoLi <bingchunmoli@bingchunmoli.com>
11-
* | Mikachu2333 <mikachu.23333@zohomail.com>
5+
* File Authors : @ccmywish
6+
* | @G_I_Y
7+
* Contributors : @Gn3po4g
8+
* | @happy-game
9+
* | @Yangmoooo
10+
* | @BingChunMoLi
11+
* | @Mikachu2333
1212
* |
1313
* Created On : <2023-08-29>
14-
* Last Modified : <2025-10-30>
14+
* Last Modified : <2025-12-29>
1515
*
1616
* chsrc framework
1717
* ------------------------------------------------------------*/
@@ -1557,7 +1557,7 @@ chsrc_run_as_pwsh_file (const char *script_content)
15571557
FILE *f = chsrc_make_tmpfile ("pwsh_script", ".ps1", false, &tmpfile);
15581558
fwrite (script_content, strlen (script_content), 1, f);
15591559
fclose (f);
1560-
char *msg = CHINESE ? "即将执行 PowerShell 脚本内容:" : "The PowerShell script content will be executed:";
1560+
char *msg = CHINESE ? "即将执行 PowerShell (v7以上) 脚本内容:" : "The PowerShell script content will be executed:";
15611561
chsrc_note2 (msg);
15621562
println (faint(script_content));
15631563
char *cmd = xy_2strcat ("pwsh ", tmpfile);
@@ -1567,6 +1567,47 @@ chsrc_run_as_pwsh_file (const char *script_content)
15671567
}
15681568

15691569

1570+
/**
1571+
* 以 powershell file.ps1 的形式运行脚本内容
1572+
*/
1573+
void
1574+
chsrc_run_as_powershellv5_file (const char *script_content)
1575+
{
1576+
char *tmpfile = NULL;
1577+
FILE *f = chsrc_make_tmpfile ("psv5_script", ".ps1", false, &tmpfile);
1578+
fwrite (script_content, strlen (script_content), 1, f);
1579+
fclose (f);
1580+
char *msg = CHINESE ? "即将执行 PowerShell v5 脚本内容:" : "The PowerShell v5 script content will be executed:";
1581+
chsrc_note2 (msg);
1582+
println (faint(script_content));
1583+
// -ExecutionPolicy Bypass
1584+
char *cmd = xy_2strcat ("powershell -File ", tmpfile);
1585+
chsrc_run (cmd, RunOpt_Dont_Abort_On_Failure);
1586+
remove (tmpfile);
1587+
free (tmpfile);
1588+
}
1589+
1590+
1591+
/**
1592+
* 使用 pwsh 或 旧的 powershell (v5) 运行脚本内容,优先使用 pwsh
1593+
*/
1594+
void
1595+
chsrc_run_as_powershell_file (const char *script_content)
1596+
{
1597+
// if (chsrc_check_program_quietly_when_exist ("pwsh"))
1598+
if (chsrc_check_program_quietly ("pwsh"))
1599+
{
1600+
chsrc_run_as_pwsh_file (script_content);
1601+
}
1602+
else
1603+
{
1604+
chsrc_alert2 (CHINESE ? "未检测到 PowerShell 7 及以上版本,默认使用 PowerShell v5"
1605+
: "PowerShell 7 or above not detected, switch to PowerShell v5");
1606+
chsrc_run_as_powershellv5_file (script_content);
1607+
}
1608+
}
1609+
1610+
15701611
/**
15711612
* @param cmdline 需要自己负责转义
15721613
*

src/recipe/lang/Python/uv.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ pl_python_uv_getsrc (char *option)
9595
{
9696
/* 在 Windows 上使用 PowerShell 替代 grep */
9797
char *script = xy_str_gsub (RAWSTR_pl_python_get_uv_config_on_windows, "@f@", uv_config);
98-
chsrc_run_as_pwsh_file (script);
98+
chsrc_run_as_powershell_file (script);
9999
}
100100
else
101101
{

0 commit comments

Comments
 (0)