英文:
Passing a list of filenames to SwiftLint using awk and xargs
问题
目标
我尝试逐渐引入SwiftLint到一个现有的项目中,并强制执行“你修改它,你修复它”的规则。所有对项目的更改都通过分支和PRs完成,意图是所有由PR创建或修改的文件在合并之前必须通过linting规则。
进展
目前我已经有了以下命令:
git diff --name-status "main" \
| grep '^[^D]' \
| grep '.swift$' \
| awk -F '\t' '{ printf("%sgit diff --name-status "main" \
| grep '^[^D]' \
| grep '.swift$' \
| awk -F '\t' '{ printf("%s\0", $NF) }' \
| xargs -0 swiftlint lint --strict --quiet --reporter xcode
", $NF) }' \
| xargs -0 swiftlint lint --strict --quiet --reporter xcode
这是:
- 相对于“main”的所有更改
- .. 不是删除操作
- .. 是
.swift
文件 - .. 提取最后一个以制表符分隔的字段(用于处理重命名),列出以
null
分隔符 - .. 并通过
xargs
传递给swiftlint
,期望null
分隔符
这在交互式shell中的效果与预期一致,但当作为Xcode的构建步骤调用时,它会对每个文件执行lint。如果我更改最后一行以包括echo
,然后检查Xcode的构建日志:
| xargs -0 echo swiftlint lint --strict --quiet --reporter Xcode
...我可以看到文件名被连接在一起,而不是单独列出。
预期:
swiftlint lint --strict --quiet --reporter Xcode path/to/first.swift path/to/second.swift
实际:
swiftlint lint --strict --quiet --reporter Xcode path/to/first.swiftpath/to/second.swift
swiftLint
将其视为一个文件,因为它不存在,然后退而进行linting所有文件。
无论Xcode使用/bin/sh
还是/bin/bash
,结果都相同。
我怀疑我的问题在于awk
中的\0
的引用或转义,但找不到解决方案。
英文:
Goal
I'm trying to introduce SwiftLint to an existing project, in a gradual way, enforcing a "you touched it, you fix it" rule. All changes to the project are done via branches and PRs and the intention is that all files created or modified by a PR must pass the linting rules before being merged.
Progress
Currently I have:
git diff --name-status "main" \
| grep '^[^D]' \
| grep '.swift$' \
| awk -F '\t' '{ printf("%sgit diff --name-status "main" \
| grep '^[^D]' \
| grep '.swift$' \
| awk -F '\t' '{ printf("%s\0", $NF) }' \
| xargs -0 swiftlint lint --strict --quiet --reporter xcode
", $NF) }' \
| xargs -0 swiftlint lint --strict --quiet --reporter xcode
Which is:
- All changes relative to "main"
- .. that aren't deletions
- .. that are to
.swift
files - .. extract the last tab-separated field (to handle renames), listed with
null
separators - .. and pass to
swiftlint
viaxargs
, expectingnull
separators
That works as intended in an interactive shell, but when invoked by Xcode as a build step it lints every file instead. If I change the last line to include echo
and then inspect Xcode's build log:
| xargs -0 echo swiftlint lint --strict --quiet --reporter Xcode
... I can see that the file names are concatenated together, rather than listed individually.
Expected:
swiftlint lint --strict --quiet --reporter Xcode path/to/first.swift path/to/second.swift
Actual:
swiftlint lint --strict --quiet --reporter Xcode path/to/first.swiftpath/to/second.swift
swiftLint
sees that as one file that doesn't exist and then falls back to linting everything instead.
The outcome is the same whether Xcode is using /bin/sh
or /bin/bash
.
I suspect my problem is around the quoting or escaping of the \0
for awk
, but can't find the solution.
答案1
得分: 1
确实,awk
中的 printf()
中的 \0
似乎不会产生你期望的效果(还可能取决于平台,即 Linux 或 macOS)。
我建议改用 %c
,如下所示:
| awk -F '\t' '{ printf("%s%c", $NF, 0) }' \
如果可以的话,感谢确认是否对你有效!谢谢!
英文:
Indeed it looks like that \0
in awk
's printf()
may not be doing what you want it to (while also likely depending on the platform i.e. Linux or macos)
I'd suggest using %c
instead as:
| awk -F '\t' '{ printf("%s%c", $NF, 0) }' \
Appreciate if you can confirm it worked for you, thanks!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论