Skip to content

Conversation

@MingriLingran
Copy link
Contributor

@MingriLingran MingriLingran commented Dec 27, 2025

在Windows平台上使用PowerShell替代sed和grep命令来处理uv配置文件

问题描述

修复:#318
修复:#308
修复:使用get uv命令不可用的问题

方案与实现

  1. 替换 grep 命令检测 [[index]] 配置段
    powershell -Command "if (Get-Content @f@ | Select-String '^\[\[index\]\]$') { exit 0 } else { exit 1 }"

  2. 替换 sed 命令修改 url 配置段:
    powershell -Command "$content = Get-Content '@f@'; $content = $content -replace '^url = \".*\"$', 'url = \"@url@\"'; $content | Set-Content '@f@'"

  3. 换 grep 命令检测 [[index]] 配置段,使用pwsh直接输出文件内容
    powershell -Command "Get-Content '@f@'"


PS:(超大声的说)这应该是我在GitHub第1个正式的PR(超开心)

MingriLingran and others added 6 commits December 27, 2025 20:09
通过PowerShell命令实现Windows平台上的文件操作,
替代原有的sed和grep命令,确保uv配置文件在Windows
环境下能够正确设置源地址。
在Windows平台上使用PowerShell替代sed和grep命令来处理uv配置文件
@github-actions
Copy link

Hi @MingriLingran

❤️ 感谢你的贡献!我们将在最少半小时,最多5天内阅读此 PR 并回复你

@ccmywish ccmywish added the pl_target pl target label Dec 28, 2025
@ccmywish ccmywish added this to the v0.2.4 milestone Dec 28, 2025
This was linked to issues Dec 28, 2025
@ccmywish
Copy link
Contributor

这应该是我在GitHub第1个正式的PR

你可以大声自豪地说这一点!

这个 PR 我很喜欢,因为它质量很高,运用 PowerShell 和我们开发的 rawstr4c 巧妙且完美地替代了 grep,而这一点正是我特别想看到的!在多个场合我曾表达了使用 PowerShell 的倾向,例如: 和 @happy-game 关于 chsrcWindows 上的讨论: #201

这个 PR 直击痛点,把悬而未决两个多月的几个问题给解决了 👍👍👍


你提到这是你 第1个正式的PR,这也让我非常开心,因为这是建设 chsrc 想要达成的一个核心目标,正如README中所说:

这也许是你可以参与的第一个现实世界中有用的C语言项目... 欢迎以此为契机学习参与贡献

Windows 上使用 chsrcPython 换源的用户众多,这个 PR 至关重要!谢谢你!❤️ @MingriLingran

@ccmywish
Copy link
Contributor

ccmywish commented Dec 28, 2025

最后,你需要再提交一次,把自己的贡献记录进 chsrc

  1. https://github.com/RubyMetric/chsrc/blob/main/src/chsrc-main.c#L78

    chsrc-main.c 登记维护者信息(这个是按参与时间排序)

  2. https://github.com/RubyMetric/chsrc/blob/main/src/recipe/lang/Python/uv.c#L18

    uv.c 这里,把你放在第一个 saucier 里(这个是按贡献价值程度排序)

然后把时间给更新一下 chef_set_last_updated(),用 chsrc ls uv 来核查。

@MingriLingran
Copy link
Contributor Author

非常感谢您的认可,我已经更新了贡献值列表
并且注意到uv使用get获取配置时,依旧使用grep的情况顺便把这个地方做了更改@ccmywish

@ccmywish
Copy link
Contributor

@MingriLingran

🤝 get config 那里,最好也用 Select-String 去匹配一下打印出来

@MingriLingran
Copy link
Contributor Author

MingriLingran commented Dec 29, 2025

Q:为什么获取配置的命令这么复杂?
A:原方案是用的是Select-String匹配后-Context 0,3输出后三行,但我发现会有文件名和行号,以及用“>”指示匹配行

❯ powershell -Command "Get-Content C:\Users\xxxxx\AppData\Roaming\uv\uv.toml | Select-String '^\[\[index\]\]$' -Context 0,3"

> [[index]]
  url = "https://pypi.org/simple"
  default = true


我认为这些是不需要的也不美观,改为了匹配[[index]]字段获取后三行输出

❯ just bd && .\chsrc-debug.exe get uv
Starting: Build in DEBUG mode: 'gcc' -Iinclude -Ilib  -g -DXY_DEBUG -o chsrc-debug
Finished: Build in DEBUG mode
[chsrc 检查] ✓ 文件 C:\Users\30353\AppData\Roaming\uv\uv.toml 存在
[chsrc 运行] powershell -Command "$lines = Get-Content C:\Users\30353\AppData\Roaming\uv\uv.toml; for($index=0; $index -lt $lines.Count; $index++) { if($lines[$index] -match '^\[\[index\]\]$') { $lines[$index..($index+3)] } }"
[[index]]
url = "https://pypi.tuna.tsinghua.edu.cn/simple"
default = true
[chsrc 运行] ✓ 命令执行成功

@ccmywish ccmywish merged commit 79b9f8d into RubyMetric:dev Dec 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pl_target pl target

Projects

None yet

Development

Successfully merging this pull request may close these issues.

uv 源重复写入 Windows 上 uv 换源需要 grep

2 participants