Why can't groovy deal with curly braces? - groovy

In an Android Studio (and presumably ANY) gradle file, the following code works:
task build {
}
And one minor change causes a complete meltdown:
task build
{
}
This has come up in other threads before, but in the context of fixing the build files. My question is why can't gradle/groovy be made to deal with either bracing style? Many other languages cope just fine with it, so what's the big deal here?

It's actually all right there in the error message:
build file '.../build.gradle': 80: Ambiguous expression could be a parameterless closure expression, an isolated open code block, or it may continue a previous statement;
solution: Add an explicit parameter list, e.g. {it -> ...}, or force it to be treated as an open block by giving it a label, e.g. L:{...}, and also either remove the previous newline, or add an explicit semicolon ';' # line 80, column 1.
Because of a Groovy syntax sugar to make methods with a lambda as the last parameter look line language constructs, the following code blocks:
task build {}
task build2(type: Copy) {}
are equal to their more regular form:
task build({})
task build(type: Copy, {})
Now, you do not really want those curly braces there to delimit a regular code block, but a Groovy lambda, which should be passed as a parameter to the build method.
Yet from the looks of it, Groovy can't really decide if it really is a lambda being passed as a parameter to the method in the previous line or an unrelated code block when you put a newline in between. And there you go, an ambiguousness as described in the error message, right there.
Following the advice in the error message, you can also use the following syntax instead of the one where you are escaping the new-line character:
task build
{ ->
}
Finally, the task keyword used to invoke the dynamic method (named build in your example) is not Groovy specific, but a Gradle DSL feature.

In case anyone reading this is wondering, the work-around is simple enough.
task build \
{
}
I was just wondering as to the "why"...

Related

What $() syntax means for Groovy language?

I found this in Groovy Syntax documentation at 4.6.1. Special cases:
As slashy strings were mostly designed to make regexp easier so a few
things that are errors in GStrings like $() or $5 will work with
slashy strings.
What $() syntax means? give some usage examples please
I also found it at Define the Contract Locally in the Repository of the Fraud Detection Service:
body([ // (4)
"client.id": $(regex('[0-9]{10}')),
loanAmount : 99999
])
but I don't understand what $() means when used with regex('[0-9]{10}').
It means nothing (or what you make of it). There are two places, you
are addressing, but they have nothing to do with each other.
The docs just mention this as "you can use slashy strings to write
things, that would give you an error with a GString" - the same is true
for just using '-Strings.
E.g.
"hello $()"
Gives this error:
unknown recognition error type: groovyjarjarantlr4.v4.runtime.LexerNoViableAltException
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
/tmp/x.groovy: 1: token recognition error at: '(' # line 1, column 9.
"hello $()"
The parser either wants a { or any char, that is a valid first char
for a variable (neither ( nor 5 is).
The other place you encountered $() (in Spring cloud contract), this
is just a function with the name $.
Form the docs 8. Contract DSL:
You can set the properties inside the body either with the value method or, if you use the Groovy map notation, with $()
So this is just a function, with a very short name.
E.g. you can try this yourself:
void $(x) { println x }
$("Hello")

Groovy method naming convention or magic?

I try to create a small DSL, but i'm struggling with even simple stuff.
The following script gives me an error.
def DEMON(String input) {
['a': input]
}
DEMON 'Hello thingy' a
For some reasons, the parentheses around the parameters are not optional and i get an error.
This script runs fine:
def dEMON(String input) {
['a': input]
}
dEMON 'Hello thingy' a
Note: the only difference is the lowercase first char.
So what is going on here? Why are the scripts interpreted (compiled?) different? Is there some kind of method/class naming schemes i have to follow?
Update: The error message. I guess a Syntax error:
unexpected token: Hello thingy # line 4, column 7.
The groovy syntax is sometime complex, and the compiler use some rules to choose what it must do. One of this rule is simple : If a word starts with an uppercase, it's probably a class.
for example, f String is a syntax valid in groovy, and the compiler converts it to f(String.class).
you can use parenthesis to help groovy understand your DEMON is not a class but a method, DEMON('Hello thingy', a)

Using variables in Gradle build script

I am using Gradle in my project. I have a task for doing some extra configuration with my war. I need to build a string to use in my task like, lets say I have:
task extraStuff{
doStuff 'org.springframework:spring-web:3.0.6.RELEASE#war'
}
This works fine. What I need to do is define version (actually already defined in properties file) and use this in the task like:
springVersion=3.0.6.RELEASE
task extraStuff{
doStuff 'org.springframework:spring-web:${springVersion}#war'
}
My problem is spring version is not recognised as variable. So how can I pass it inside the string?
If you're developing an Android application using Gradle, you can declare a variable (i.e holding a dependency version) thanks to the keyword def like below:
def version = '1.2'
dependencies {
compile "groupId:artifactId:${version}"
}
I think the problem may lay on string literal delimiters:
The string literals are defined exactly as in groovy so enclose it in single or double quotes (e.g. "3.0.6.RELEASE");
Gstrings are not parsed in single quotes strings (both single '...' or triple '''...''' ones) if i recall correctly;
So the code will be:
springVersion = '3.0.6.RELEASE' //or with double quotes "..."
task extraStuff{
doStuff "org.springframework:spring-web:${springVersion}#war"
}
On android there are actually 2 possibilities how to achieve this. It really depends which suits your needs. Those two possibilities have their pros and cons. You can use def variable or ext{} block. Variable def is awesome because it lets you click on the variable and points exactly where it is defined in the file compared to ext{} block which does NOT points to that exact variable. On the other hand ext{} has one good advantage and that is you can refer variables from project_name/build.gradle to project_name/app/build.gradle which in some cases is very useful BUT as I said if you click on that variable lets say only inside only one file it wont points out to the definition of that variable which is very bad because it takes you more search time if your dependency list grows.
1) def option which is propably best and saves you search time.
def lifecycle = '2.0.0'
dependencies {
implementation 'androidx.lifecycle:lifecycle-extensions:$lifecycle'
}
2) second ext{} block. Its kinda ok if dependency list is not huge.
ext {
lifecycle = '1.1.1'
}
dependencies {
implementation 'androidx.lifecycle:lifecycle-extensions:$lifecycle'
}
3) In some cases if you want to share variables between project_name/build.gradle and project_name/app/build.gradle use ext{}
in project_name/build.gradle you define kotlin_shared_variable:
buildscript {
ext.kotlin_shared_variable = '1.3.41'
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_shared_variable"
}
}
which you can use in project_name/app/build.gradle
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_shared_variable"
}
and of course you can combine them.
see here.
Double-quoted strings are plain java.lang.String if there’s no interpolated expression, but are groovy.lang.GString instances if interpolation is present.
Gradle uses Groovy as a DSL. Here "${springVersion}" is a placeholder, what you want is to interpolate, so you should use the double quote, only the double quote in GString has the capability to interpolate.
You can also define variables in the gradle.properties file at the root of your project. You don't have to use double quotes in that file. You would need to add the following line:
lifecycle=2.0.0

Groovy how to multi line GStrings for exception messages

What is the standard (or best practice) for Groovy error messages that that shouldn't span over a certain number of characters/line, e.g., 80 characters?
Consider the following (which is working fine)
throw new IOException("""\
A Jenkins configuration for the given version control
system (${vcs.name}) does not exist."""
.stripIndent()
.replaceAll('\n', ' '))
This will result in a one-line error message with no indention characters (what I want). But is there some other way ("the Groovy way of doing it") how to achieve this? If not, how could you add such a method to the GString class in a standalone Groovy application (if found hints regarding a Bootstrap.groovy file but it seems to be related to Grails)?
Example: """Consider a multi line string as shown above""".toSingleLine()
You could use the String continuation character then strip multiple spaces:
throw new IOException( "A Jenkins configuration for the given version control \
system (${vcs.name}) does not exist.".replaceAll( /( )\1+/, '$1' ) )
Or you could wrap this in a function and add it to the String.metaClass as I believe the answers you've seen point to.
You're right in thinking that Bootstrap.groovy is a Grails thing, but if you just set the metaClass early on in your applications lifecycle, you should get the same result...
String.metaClass.stripRepeatedWhitespace = { delegate.replaceAll( /( )\1+/, '$1' ) }
In saying all this however, I'd probably just keep the message on a single line

groovy: use brackets on method calls or not?

this is a fairly general question about whether people should be using brackets on method calls that take parameters or not.
i.e.
def someFunc(def p) {
...
}
then calling:
someFunc "abc"
vs...
someFunc("abc")
Is this just a question of consistency, or is there specific use cases for each?
It's primarily a question of consistency and readability, but note that Groovy won't always let you get away with omitting parentheses. For one, you can't omit parentheses in nested method calls:
def foo(n) { n }
println foo 1 // won't work
See the section entitled "Omitting parentheses" in the Style guide.
There's no specific case where you must remove them, you can always use them. It's just prettier to leave them out.
There are cases where you can't do that (where you could confuse a list/map parameter with a subscript operator for instance, nested calls, or when the statement is an assignment), but the general rule is that the outmost call can have no parenthesis if there is no ambiguity.
(deleted several lines, as I've just received notification that there is a post already with that info)
Groovy 1.8 will allow even more cases to omit parenthesis, you can check them out at
http://groovyconsole.appspot.com/script/355001
"an empty pair of parentheses is just useless syntactical noise!"
It seems to me that they are encouraging you to use parenthesis when they serve a purpose, but omit them when they are just "noise"

Resources