mirror of
https://github.com/RubyMetric/chsrc
synced 2025-12-30 06:39:30 +08:00
feat(python/uv): 支持Windows平台的uv配置文件处理
通过PowerShell命令实现Windows平台上的文件操作, 替代原有的sed和grep命令,确保uv配置文件在Windows 环境下能够正确设置源地址。
This commit is contained in:
@@ -119,19 +119,6 @@ pl_python_uv_setsrc (char *option)
|
|||||||
|
|
||||||
const char *source_content = xy_str_gsub (RAWSTR_pl_python_uv_config_source_content, "@url@", source.url);
|
const char *source_content = xy_str_gsub (RAWSTR_pl_python_uv_config_source_content, "@url@", source.url);
|
||||||
|
|
||||||
#if defined(XY_Build_On_macOS) || defined(XY_Build_On_BSD)
|
|
||||||
char *sed_cmd = "sed -i '' ";
|
|
||||||
#else
|
|
||||||
char *sed_cmd = "sed -i ";
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 将 [[index]] 到 default = true 之间的 url = ".*" 替换为 url = "source.url"
|
|
||||||
*/
|
|
||||||
char *update_config_cmd = xy_str_gsub (RAWSTR_pl_python_set_uv_config, "@sed@", sed_cmd);
|
|
||||||
update_config_cmd = xy_str_gsub (update_config_cmd, "@f@", uv_config);
|
|
||||||
update_config_cmd = xy_str_gsub (update_config_cmd, "@url@", source.url);
|
|
||||||
|
|
||||||
if (!xy_file_exist (uv_config))
|
if (!xy_file_exist (uv_config))
|
||||||
{
|
{
|
||||||
/* 当 uv_config 不存在,直接写入文件 */
|
/* 当 uv_config 不存在,直接写入文件 */
|
||||||
@@ -140,13 +127,43 @@ pl_python_uv_setsrc (char *option)
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
/* 当 uv_config 存在,如果存在 [[index]] 则更新,否则追加到文件末尾 */
|
/* 当 uv_config 存在,如果存在 [[index]] 则更新,否则追加到文件末尾 */
|
||||||
char *cmd = xy_str_gsub (RAWSTR_pl_python_test_uv_if_set_source, "@f@", uv_config);
|
char *cmd = NULL;
|
||||||
chsrc_ensure_program ("grep");
|
if (xy.on_windows)
|
||||||
|
{
|
||||||
|
/* 在 Windows 上使用 PowerShell 替代 grep */
|
||||||
|
cmd = xy_str_gsub("powershell -Command \"if (Get-Content @f@ | Select-String '^\\[\\[index\\]\\]$') { exit 0 } else { exit 1 }\"", "@f@", uv_config);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
cmd = xy_str_gsub (RAWSTR_pl_python_test_uv_if_set_source, "@f@", uv_config);
|
||||||
|
}
|
||||||
|
|
||||||
int status = xy_run_get_status (cmd);
|
int status = xy_run_get_status (cmd);
|
||||||
if (0==status)
|
if (0==status)
|
||||||
{
|
{
|
||||||
|
if (xy.on_windows)
|
||||||
|
{
|
||||||
|
/* 在 Windows 上使用 PowerShell 替代 sed */
|
||||||
|
char *powershell_cmd_template = "powershell -Command \"$content = Get-Content '@f@'; $content = $content -replace '^url = \\\".*\\\"$', 'url = \\\"@url@\\\"'; $content | Set-Content '@f@'\"";
|
||||||
|
char *powershell_cmd_with_file = xy_str_gsub(powershell_cmd_template, "@f@", uv_config);
|
||||||
|
char *powershell_cmd = xy_str_gsub(powershell_cmd_with_file, "@url@", source.url);
|
||||||
|
chsrc_run (powershell_cmd, RunOpt_Default);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* 非 Windows 系统使用 sed */
|
||||||
|
char *sed_cmd = NULL;
|
||||||
|
#if defined(XY_Build_On_macOS) || defined(XY_Build_On_BSD)
|
||||||
|
sed_cmd = "sed -i '' ";
|
||||||
|
#else
|
||||||
|
sed_cmd = "sed -i ";
|
||||||
|
#endif
|
||||||
|
char *update_config_cmd = xy_str_gsub (RAWSTR_pl_python_set_uv_config, "@sed@", sed_cmd);
|
||||||
|
update_config_cmd = xy_str_gsub (update_config_cmd, "@f@", uv_config);
|
||||||
|
update_config_cmd = xy_str_gsub (update_config_cmd, "@url@", source.url);
|
||||||
chsrc_run (update_config_cmd, RunOpt_Default);
|
chsrc_run (update_config_cmd, RunOpt_Default);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
chsrc_append_to_file (source_content, uv_config);
|
chsrc_append_to_file (source_content, uv_config);
|
||||||
|
|||||||
Reference in New Issue
Block a user