It’s a common usage to modify the code when pkgbuild the package.
Here are several ways.

sed

de facto way
Pros
  1. easy to get, so old that stable, one of GNU toolkit
  1. is turing complete, so in theory has high limitation
Cons
  1. very hard to use, learn and maintain. simple usage like sed -i 's/a/b/g' is already hard to read. If with regex, it’s hell

patch

de facto way in git based work flow
Pros
  1. easy to get
  1. easy to generate, just diff a b > patch
  1. easy to use, patch -Np1 < patch
Cons
  1. stick to the origin file, not possible to apply to many files.
  1. if many replaces, patch will be huge, even if they are the same rules.

ast-grep

Pros
  1. easy to use
  1. easy to generate
Cons
  1. not built-in
  1. only support some languages
  1. added by author: pre-compiled version doesn’t support glibc < 2.31, or you can compile it yourself.

Example

code base need to change a function name

there are so many function calls there
  1. sed, just replace, but will replace ALL, even in string. The problem of using part of text is that
hard to write correct rule.
  1. patch, patch may be huge, but is accepted
  1. ast-grep, replace the function call

Now consider the update of software update

consider a thing:
The upstream ignore a request, so maintainers have to keep sync and do the patch.
  • sed: need to change even if var names have changed.
  • patch: every time repatch.
  • ast-grep: just keep the same replace, if the api and code shape doesn’t change. Even if changed, still easy to write.

So may be I could give ast-grep a try, when doing packaging.
 
Loading...
Steven Lynn
Steven Lynn
喂马、劈柴、周游世界
最新发布
我与 Dify 的半年
2025-3-9
我的2022年终小结
2024-11-9
记录雅思考试经历与一点学习心得
2024-11-9
Hackergame 2024 思路小结
2024-11-9
黑客松、日本、入职:我的2024下半年的总结
2024-11-9
NotionNext:基于Notion和NextJS的开源博客
2024-11-9