Müller
Müller

Reputation: 1023

How to capture a variable in groovy append function?

I am writing a Gradle task to define a Global Artifact Marker for my executable being generated. This task will create a C file which will later be packaged into the executable containing other sources.

task genArtifactMarker{
        project.ext.HASH = "git rev-parse --short HEAD".execute().text
        project.ext.ARTIFACT_VERSION = "$PROJECT-$VERSION-$HASH"
        def version_file = new File('version.c')
        version_file.append('\n/* Automatically generated file for marker */')
        version_file.append('\nconst volatile char marker[] __attribute__ ((section ("MARKER"))) = "${ARTIFACT_VERSION}";')
}

How do I capture the ARTIFACT_VERSION variable within the append function? I want something like MY_EXE-V2.1.2-47add78in the place of ARTIFACT_VERSION in my generated version.c file.

EDIT:

Adding code to help request for debugging.

task genArtifactMarker{
    doLast{
        project.ext.HASH = "git rev-parse --short HEAD".execute().text
        def ARTIFACT_VERSION = "$PROJECT-$VERSION-$HASH"
        def version_file = new File('version.c')
        version_file.append('\n/* Automatically generated file for marker */')
        version_file << """\nconst volatile char marker[] __attribute__ ((section ("MARKER"))) = "${ARTIFACT_VERSION}";"""
    }
}   

My output is as follows:

/* Automatically generated file for marker */ 
const volatile char marker[] __attribute__ ((section ("MARKER"))) = "RPI-V2.1.2_PO3-e75ee8d 
";

I want my output as below:

/* Automatically generated file for marker */ 
const volatile char marker[] __attribute__ ((section ("MARKER"))) = "RPI-V2.1.2_PO3-e75ee8d";

Upvotes: 2

Views: 351

Answers (2)

Matias Bjarland
Matias Bjarland

Reputation: 4482

Coalescing the above answers and adding the << append operator for files and triple double quoted strings in groovy we get:

ext { 
  version_file = file('text.txt')
  ARTIFACT_VERSION ="1.0"
}

task genArtifactMarker {
  doLast { 
    version_file << """\nconst volatile char marker[] __attribute__ ((section ("MARKER"))) = "${ARTIFACT_VERSION}";"""
  }
}

or if you need to control the indentation level of a multi line string:

task genArtifactMarker {
  doLast { 
    version_file << """
     |const volatile char marker[] __attribute__ ((section ("MARKER"))) = "${ARTIFACT_VERSION}";
     |
     |more text needing correct indentation
     |""".stripMargin()
  }
}

As we are using the doLast block, we are now executing the code in the execution phase in gradle and since we are using triple quotes, we don't need to escape the quotes with backslash characters inside the string.

The groovy stripMargin() method can be used to get a multi-line string nicely aligned in your code without affecting the formatting of the resulting string.

edit: in response to comment, here is the output of the last version of the code:

$ ls -l
total 4
-rw-r--r-- 1 mbjarland mbjarland 628 Feb  7 15:08 build.gradle

$ gradle genArtifactMarker
:genArtifactMarker

BUILD SUCCESSFUL

Total time: 1.102 secs

$ ls -l
total 8
-rw-r--r-- 1 mbjarland mbjarland 628 Feb  7 15:08 build.gradle
-rw-r--r-- 1 mbjarland mbjarland 115 Feb  8 15:26 text.txt

$ cat text.txt 

const volatile char marker[] __attribute__ ((section ("MARKER"))) = "1.0";

more text needing correct indentation

Upvotes: 1

tim_yates
tim_yates

Reputation: 171074

You need to use double quotes to get templating:

version_file.append("\nconst volatile char marker[] __attribute__ ((section (\"MARKER\"))) = \"${ARTIFACT_VERSION}\";")

Upvotes: 2

Related Questions