Groovy 多行字符串插值空格

Groovy multiline string interpolation whitespace

我正在尝试为 Jenkins 生成一些通用 Groovy 代码,但我似乎无法处理多行字符串和额外的白色 space。我已经尝试了通过谷歌搜索可以找到的所有内容,但我似乎无法正常工作。

我的问题与简单的多行字符串无关。对于简单的情况,我通过使用 stripIndent() 和 stripMargin() 方法设法 trim 白色 space。我的问题是由于我的字符串中有内插方法引起的。

Groovy 信息:Groovy Version: 3.0.2 JVM: 13.0.2 Vendor: Oracle Corporation OS: Mac OS X

String method2(String tier, String jobName) {
    return """
            Map downstreamJobs = [:]
            stage ("${jobName}-${tier}-${region}_${jobName}") {
                test
            }
        """.stripIndent().stripMargin()
}

static String simpleLog() {
    return """
            script {
               def user = env.BUILD_USER_ID
            }
          """.stripIndent().stripMargin()
}

static String method1() {
    return """\
            import jenkins.model.Jenkins
            currentBuild.displayName = "name"

            ${simpleLog()}
        """.stripIndent().stripMargin()
}

String generateFullDeploymentPipelineCode() {
    return """Text here
            ${method1()}
            ${method2("test1", "test2")}
            """.stripIndent().stripMargin()
}

println(generateFullDeploymentPipelineCode())

这是它打印(或写入磁盘)的内容:

Text here
                      import jenkins.model.Jenkins
          currentBuild.displayName = "name"

script {
   def user = env.BUILD_USER_ID
}



Map downstreamJobs = [:]
stage ("test2-test1-${region}_test2") {
    test
}

为什么要在导入行周围多出 space?我知道根据前导 space 的最少数量,缩进方法应该 trim 全白 space,所以这就是我们使用反斜杠的原因(这里的示例 )。

这适用于简单的字符串,但一旦开始使用插值,它就会崩溃。不适用于常规变量,只是在插入整个方法时。

作为变体 - 只使用 stripMargin() 并且在最终字符串上只使用一次

String method2(String tier, String jobName) {
    return """\
            |Map downstreamJobs = [:]
            |stage ("${jobName}-${tier}-${region}_${jobName}") {
            |    test
            |}
        """
}

static String simpleLog() {
    return """\
            |script {
            |   def user = env.BUILD_USER_ID
            |}
          """
}

static String method1() {
    return """\
            |import jenkins.model.Jenkins
            |currentBuild.displayName = "name"

            ${simpleLog()}
        """
}

String generateFullDeploymentPipelineCode() {
    return """\
            |Text here
            ${method1()}
            ${method2("test1", "test2")}
            """.stripIndent().stripMargin()
}

println(generateFullDeploymentPipelineCode())

结果:

Text here
import jenkins.model.Jenkins
currentBuild.displayName = "name"

script {
   def user = env.BUILD_USER_ID
}

Map downstreamJobs = [:]
stage ("test2-test1-${region}_test2") {
    test
}

另一个带有 trim() 和 stripIndent() 的变体

def method2(String tier, String jobName) {
    return """
            Map downstreamJobs = [:]
            stage ("${jobName}-${tier}-${region}_${jobName}") {
                test
            }
        """.trim()
}

def simpleLog() {
    return """
            script {
               def user = env.BUILD_USER_ID
            }
          """.trim()
}

def method1() {
    return """
            import jenkins.model.Jenkins
            currentBuild.displayName = "name"
            ${simpleLog()}
        """.trim()
}

def generateFullDeploymentPipelineCode() {
    return """\
            Text here
            ${method1()}
            ${method2("test1", "test2")}
            """.stripIndent()
}

println(generateFullDeploymentPipelineCode())

当你通过插值插入一个字符串时,你只缩进了它的第一行。插入字符串的以下行将以不同的方式缩进,这会把一切都搞砸。

使用 GString 的一些鲜为人知的成员(即 .strings[].values[]),我们可以对齐每个插值的所有行的缩进。

String method2(String tier, String jobName) {
    indented """
        Map downstreamJobs = [:]
        stage ("${jobName}-${tier}-${region}_${jobName}") {
            test
        }
    """
}

String simpleLog() {
    indented """\
        script {
           def user = env.BUILD_USER_ID
        }
    """
}

String method1() {
    indented """\
        import jenkins.model.Jenkins
        currentBuild.displayName = "name"

        ${simpleLog()}
    """
}

String generateFullDeploymentPipelineCode() {
    indented """\
        Text here
        ${method1()}
        ${method2("test1", "test2")}
    """
}

println generateFullDeploymentPipelineCode()

//---------- Move the following code into its own script ----------

// Function to adjust the indentation of interpolated values so that all lines
// of a value match the indentation of the first line.
// Finally stripIndent() will be called before returning the string.

String indented( GString templ ) {

    // Iterate over the interpolated values of the GString template.
    templ.values.eachWithIndex{ value, i ->

        // Get the string preceding the current value. Always defined, even
        // when the value is at the beginning of the template.
        def beforeValue = templ.strings[ i ]

        // RegEx to match any indent substring before the value.
        // Special case for the first string, which doesn't necessarily contain '\n'. 
        def regexIndent = i == 0
                          ? /(?:^|\n)([ \t]+)$/
                          : /\n([ \t]+)$/

        def matchIndent = ( beforeValue =~ regexIndent )
        if( matchIndent ) {
            def indent = matchIndent[ 0 ][ 1 ]
            def lines = value.readLines()
            def linesNew = [ lines.head() ]  // The 1st line is already indented.
            // Insert the indentation from the 1st line into all subsequent lines.
            linesNew += lines.tail().collect{ indent + it }
            // Finally replace the value with the reformatted lines.
            templ.values[ i ] = linesNew.join('\n')
        }
    }

    return templ.stripIndent()
}

// Fallback in case the input string is not a GString (when it doesn't contain expressions)
String indented( String templ ) {
    return templ.stripIndent()  
}

Live Demo at codingground

输出:

Text here
import jenkins.model.Jenkins
currentBuild.displayName = "name"

script {
   def user = env.BUILD_USER_ID
}

Map downstreamJobs = [:]
stage ("test2-test1-${region}_test2") {
    test
}

结论:

使用 indented 函数,实现了从 GString 模板生成代码的简洁 Groovy 语法。

这是一次很好的学习经历。我首先尝试使用 evaluate 函数来完全不同,结果证明它太复杂而且不那么灵活。然后我随机浏览了mrhaki blog (always a good read!) until I discovered "Groovy Goodness: Get to Know More About a GString"的一些帖子。这是实施此解决方案的关键。