gradle 脚本在构建之前转义单引号

发布于 2025-01-10 22:54:37 字数 2887 浏览 1 评论 0原文

我有带有翻译的 i18n 消息文件。问题在于某些语言(例如意大利语)在某些单词中使用单引号。我想运行一些脚本(可能作为 gradle.build 任务)来用双引号替换单引号。我将以下代码粘贴到我的 build.gradle.kts 文件中

task("replaceSingleQuotes") {
    doLast {
        ant.ReplaceRegExp(match:'\'', replace:'\'\'', flags:'g', byline:false) {
            fileset(dir: 'src/main/resources/i18n/', includes: '*')
        }
    }
}

第一个问题是现在我的项目无法配置以下错误:

e: build.gradle.kts:139:32: Expecting ')'e: D:\Sources\delivery-backend\build.gradle.kts:139:33: Unexpected tokens (use ';' to separate expressions on the same line)
e: build.gradle.kts:140:15: Expecting ')'
e: build.gradle.kts:140:17: Unexpected tokens (use ';' to separate expressions on the same line)
e: build.gradle.kts:154:13: Expecting an element
e: build.gradle.kts:155:28: Expecting ')'
e: build.gradle.kts:155:29: Unexpected tokens (use ';' to separate expressions on the same line)
e: build.gradle.kts:156:20: Expecting ')'
e: build.gradle.kts:156:22: Unexpected tokens (use ';' to separate expressions on the same line)
e: build.gradle.kts:139:13: Unresolved reference: ReplaceRegExp
e: build.gradle.kts:139:27: Unresolved reference: match
e: build.gradle.kts:140:4: Unresolved reference: fileset
e: build.gradle.kts:140:12: Function invocation 'dir(...)' expected
e: build.gradle.kts:140:12: Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: 
public inline fun SourceSetOutput.dir(dir: Any, vararg options: Pair<String, Any?>): Unit defined in org.gradle.kotlin.dsl
e: build.gradle.kts:154:1: Function invocation 'task(...)' expected
e: build.gradle.kts:154:1: None of the following functions can be called with the arguments supplied: 
public abstract fun task(p0: String!): Task! defined in org.gradle.api.Project
public abstract fun task(p0: String!, p1: Closure<(raw) Any!>!): Task! defined in org.gradle.api.Project
public abstract fun task(p0: String!, p1: Action<in Task!>!): Task! defined in org.gradle.api.Project
public abstract fun task(p0: (Mutable)Map<String!, *>!, p1: String!): Task! defined in org.gradle.api.Project
public abstract fun task(p0: (Mutable)Map<String!, *>!, p1: String!, p2: Closure<(raw) Any!>!): Task! defined in org.gradle.api.Project
e: build.gradle.kts:155:9: Unresolved reference: replaceregexp
e: build.gradle.kts:155:23: Unresolved reference: match
e: build.gradle.kts:156:9: Unresolved reference: fileset
e: build.gradle.kts:156:17: Function invocation 'dir(...)' expected
e: build.gradle.kts:156:17: Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: 
public inline fun SourceSetOutput.dir(dir: Any, vararg options: Pair<String, Any?>): Unit defined in org.gradle.kotlin.dsl

我猜第二个问题是每次我的任务执行时它会添加越来越多的双引号。 因此,这里有两个问题:如何使我的项目配置此任务以及如何更改正则表达式以仅匹配单引号?

I've got i18n messages files with translations. The problem is in that some languages (Italian for example) use single quotes in some words. I want to run some script (may be as a gradle.build task) to replace single quotes with double quotes. I paste following code into my build.gradle.kts file

task("replaceSingleQuotes") {
    doLast {
        ant.ReplaceRegExp(match:'\'', replace:'\'\'', flags:'g', byline:false) {
            fileset(dir: 'src/main/resources/i18n/', includes: '*')
        }
    }
}

The first problem is in that now my project failed to configure with following errors:

e: build.gradle.kts:139:32: Expecting ')'e: D:\Sources\delivery-backend\build.gradle.kts:139:33: Unexpected tokens (use ';' to separate expressions on the same line)
e: build.gradle.kts:140:15: Expecting ')'
e: build.gradle.kts:140:17: Unexpected tokens (use ';' to separate expressions on the same line)
e: build.gradle.kts:154:13: Expecting an element
e: build.gradle.kts:155:28: Expecting ')'
e: build.gradle.kts:155:29: Unexpected tokens (use ';' to separate expressions on the same line)
e: build.gradle.kts:156:20: Expecting ')'
e: build.gradle.kts:156:22: Unexpected tokens (use ';' to separate expressions on the same line)
e: build.gradle.kts:139:13: Unresolved reference: ReplaceRegExp
e: build.gradle.kts:139:27: Unresolved reference: match
e: build.gradle.kts:140:4: Unresolved reference: fileset
e: build.gradle.kts:140:12: Function invocation 'dir(...)' expected
e: build.gradle.kts:140:12: Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: 
public inline fun SourceSetOutput.dir(dir: Any, vararg options: Pair<String, Any?>): Unit defined in org.gradle.kotlin.dsl
e: build.gradle.kts:154:1: Function invocation 'task(...)' expected
e: build.gradle.kts:154:1: None of the following functions can be called with the arguments supplied: 
public abstract fun task(p0: String!): Task! defined in org.gradle.api.Project
public abstract fun task(p0: String!, p1: Closure<(raw) Any!>!): Task! defined in org.gradle.api.Project
public abstract fun task(p0: String!, p1: Action<in Task!>!): Task! defined in org.gradle.api.Project
public abstract fun task(p0: (Mutable)Map<String!, *>!, p1: String!): Task! defined in org.gradle.api.Project
public abstract fun task(p0: (Mutable)Map<String!, *>!, p1: String!, p2: Closure<(raw) Any!>!): Task! defined in org.gradle.api.Project
e: build.gradle.kts:155:9: Unresolved reference: replaceregexp
e: build.gradle.kts:155:23: Unresolved reference: match
e: build.gradle.kts:156:9: Unresolved reference: fileset
e: build.gradle.kts:156:17: Function invocation 'dir(...)' expected
e: build.gradle.kts:156:17: Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: 
public inline fun SourceSetOutput.dir(dir: Any, vararg options: Pair<String, Any?>): Unit defined in org.gradle.kotlin.dsl

The second problem, I guess, is in that every time my task executed it'll add more and more double quotes.
Thus, two questions here: how to make my project configure with this task and how to change regex to match only single quote?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不气馁 2025-01-17 22:54:37

使用与“([^\\])'”匹配并替换“\1\\\\'”应该

按照此处的建议工作 https:// /stackoverflow.com/a/60127123

using match with "([^\\])'" and replace "\1\\\\'" should work

as suggested here https://stackoverflow.com/a/60127123

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文