Skip to content

Commit 08b213a

Browse files
committed
feat(python/uv): 支持Windows平台的uv配置文件处理
通过PowerShell命令实现Windows平台上的文件操作, 替代原有的sed和grep命令,确保uv配置文件在Windows 环境下能够正确设置源地址。
1 parent 3816263 commit 08b213a

File tree

1 file changed

+33
-16
lines changed
  • src/recipe/lang/Python

1 file changed

+33
-16
lines changed

src/recipe/lang/Python/uv.c

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -119,19 +119,6 @@ pl_python_uv_setsrc (char *option)
119119

120120
const char *source_content = xy_str_gsub (RAWSTR_pl_python_uv_config_source_content, "@url@", source.url);
121121

122-
#if defined(XY_Build_On_macOS) || defined(XY_Build_On_BSD)
123-
char *sed_cmd = "sed -i '' ";
124-
#else
125-
char *sed_cmd = "sed -i ";
126-
#endif
127-
128-
/**
129-
* 将 [[index]] 到 default = true 之间的 url = ".*" 替换为 url = "source.url"
130-
*/
131-
char *update_config_cmd = xy_str_gsub (RAWSTR_pl_python_set_uv_config, "@sed@", sed_cmd);
132-
update_config_cmd = xy_str_gsub (update_config_cmd, "@f@", uv_config);
133-
update_config_cmd = xy_str_gsub (update_config_cmd, "@url@", source.url);
134-
135122
if (!xy_file_exist (uv_config))
136123
{
137124
/* 当 uv_config 不存在,直接写入文件 */
@@ -140,12 +127,42 @@ pl_python_uv_setsrc (char *option)
140127
else
141128
{
142129
/* 当 uv_config 存在,如果存在 [[index]] 则更新,否则追加到文件末尾 */
143-
char *cmd = xy_str_gsub (RAWSTR_pl_python_test_uv_if_set_source, "@f@", uv_config);
144-
chsrc_ensure_program ("grep");
130+
char *cmd = NULL;
131+
if (xy.on_windows)
132+
{
133+
/* 在 Windows 上使用 PowerShell 替代 grep */
134+
cmd = xy_str_gsub("powershell -Command \"if (Get-Content @f@ | Select-String '^\\[\\[index\\]\\]$') { exit 0 } else { exit 1 }\"", "@f@", uv_config);
135+
}
136+
else
137+
{
138+
cmd = xy_str_gsub (RAWSTR_pl_python_test_uv_if_set_source, "@f@", uv_config);
139+
}
140+
145141
int status = xy_run_get_status (cmd);
146142
if (0==status)
147143
{
148-
chsrc_run (update_config_cmd, RunOpt_Default);
144+
if (xy.on_windows)
145+
{
146+
/* 在 Windows 上使用 PowerShell 替代 sed */
147+
char *powershell_cmd_template = "powershell -Command \"$content = Get-Content '@f@'; $content = $content -replace '^url = \\\".*\\\"$', 'url = \\\"@url@\\\"'; $content | Set-Content '@f@'\"";
148+
char *powershell_cmd_with_file = xy_str_gsub(powershell_cmd_template, "@f@", uv_config);
149+
char *powershell_cmd = xy_str_gsub(powershell_cmd_with_file, "@url@", source.url);
150+
chsrc_run (powershell_cmd, RunOpt_Default);
151+
}
152+
else
153+
{
154+
/* 非 Windows 系统使用 sed */
155+
char *sed_cmd = NULL;
156+
#if defined(XY_Build_On_macOS) || defined(XY_Build_On_BSD)
157+
sed_cmd = "sed -i '' ";
158+
#else
159+
sed_cmd = "sed -i ";
160+
#endif
161+
char *update_config_cmd = xy_str_gsub (RAWSTR_pl_python_set_uv_config, "@sed@", sed_cmd);
162+
update_config_cmd = xy_str_gsub (update_config_cmd, "@f@", uv_config);
163+
update_config_cmd = xy_str_gsub (update_config_cmd, "@url@", source.url);
164+
chsrc_run (update_config_cmd, RunOpt_Default);
165+
}
149166
}
150167
else
151168
{

0 commit comments

Comments
 (0)