Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy: How to define a manipulated string as a variable?

I'm writing a Jenkins pipeline as a code for an Android application.

I have the following strings:

BUILDFLAV = "Staging"
BUILDTYPE = "Debug"

I want to define another variable called artifact_name which will look like:

product_name-BUILDFLAV(in lowercase)-BUILDTYPE(in lowercase).apk

for example:

App01-staging-debug.apk

Since I'm new to groovy i'm not sure that I'm doing it correctly.

I've defined 2 variables:

aa = BUILDFLAV[0].toLowerCase()+BUILDFLAV.substring(1)
bb = BUILDTYPE[0].toLowerCase()+BUILDTYPE.substring(1)

This allows me to change the first char of each string from uppercase to lowercase.

Then, I want the artifact_name variable to look like so:

App01-$(aa}-${bb}.apk

So I've tried something like that:

artifact_name = "App01-${BUILDFLAV[0]}.toLowerCase()+${BUILDFLAV}.substring(1)+${BUILDTYPE[0]}.toLowerCase()+${BUILDTYPE}.substring(1).apk"

But it throws me an error.

Doing this, however, worked but seems like there's a more elegant way to do that:

BUILDFLAV = "Staging"
BUILDTYPE = "Debug"
aa = BUILDFLAV[0].toLowerCase()+BUILDFLAV.substring(1)
bb = BUILDTYPE[0].toLowerCase()+BUILDTYPE.substring(1)
ARTIFACT_NAME = "App01-${aa}-${bb}.apk"

If you ask yourself why I'm not setting the variables in lowercase from the beginning is because the gradle task which is run in the build is using the first letter uppercase convention and what I'm trying to achieve is related to a more advanced step of the build.

Is it possible to achieve the same result by doing what I did in vars "aa" and "bb" in just one line? like the above example with artifact_name...

like image 235
Itai Ganot Avatar asked Nov 06 '16 14:11

Itai Ganot


People also ask

How do I declare a variable in Groovy?

Variables in Groovy can be defined in two ways − using the native syntax for the data type or the next is by using the def keyword. For variable definitions it is mandatory to either provide a type name explicitly or to use "def" in replacement. This is required by the Groovy parser.

What is ?: In Groovy?

Yes, the "?:" operator will return the value to the left, if it is not null. Else, return the value to the right. "Yes, the "?:" operator will return the value to the left, if it is not null." - That is incorrect.

How can a Groovy String be expressed?

Strings in Groovy can be enclosed in single quotes ('), double quotes (“), or triple quotes (“””). Further, a Groovy String enclosed by triple quotes may span multiple lines.

How do I replace a character in a String in Groovy?

Groovy - replaceAll() Replaces all occurrences of a captured group by the result of a closure on that text.


2 Answers

If you want to use templates in strings in groovy, you can put it inside ${..} like you already did. However you need to put everything that needs to be executed inside it.

So use

ARTIFACT_NAME = "App01-${BUILDFLAV[0].toLowerCase()}${BUILDFLAV.substring(1).toLowerCase()-${BUILDTYPE[0].toLowerCase()}${BUILDTYPE.substring(1).toLowerCase()}.apk"`

But please note that it is unnecessary to cast the first char to lowercase and then appending the rest. The next will give the same result

`ARTIFACT_NAME = "App01-${BUILDFLAV.toLowerCase()}-${BUILDTYPE.toLowerCase()}.apk"`

If you for whatever reason want to do it like that anyway, you can use BUILDTYPE[1..-1] instead of BUILDTYPE.substring(1)

like image 105
Rik Avatar answered Oct 23 '22 02:10

Rik


Dumbed down/different way of explaining Rik's answer for anyone that wants to know how to do this on a general-level:

  1. Acquire a plugin that allows you to inject variables via Groovy scripts
  2. In the Evaluated Groovy script field, replace variables wrapped with % like %Var% with their actual value with this following block of text:

def map = [%New_Var%: %Old_Var%.toUpperCase()]
return map

Then you can verify it's working for Linux as: echo $New_Var
And Windows as: echo %New_Var%

As an example,

Let's say you have a parameter named Label, that you've set to Hello

def map = [ucase_Label: Label.toUpperCase()]
return map

echo $ucase_Label

HELLO

And if you need the value appended/prepended, you can do stuff like,
def map = [newString: "some_String"+Label.toUpperCase()]

like image 39
kayleeFrye_onDeck Avatar answered Oct 23 '22 02:10

kayleeFrye_onDeck