Code-Apprentice
Code-Apprentice

Reputation: 83537

Creating a closure in ext

I am implementing the texturePacker task given in LibGDX's TexturePacker with gradle.

project.ext {
    // ...
    texturePacker = ["assets", "../android/assets", "texture"]
}

import com.badlogic.gdx.tools.texturepacker.TexturePacker
task texturePacker << {
    if (project.ext.has('texturePacker')) {
        logger.info "Calling TexturePacker: "+ texturePacker
        TexturePacker.process(texturePacker[0], texturePacker[1], texturePacker[2])
    }
}

I got it working with the suggested modifications for the classpath and added extension variable. Now I want to modify the textPacker extension variable to be a closure (Is that the right terminology?) with descriptive member names rather than an array. I tried doing this:

project.ext {
    // ...
    texturePacker {
        inputDir = "assets"
        outputDir = "../android/assets"
        packFileName = "texture"
    }
}

This gives the following error:

Error:Could not find method texturePacker() for arguments [build_4dusyb6n0t7j9dfuws8cc2jlu$_run_closure1$_closure7@6305684e] on project ':desktop' of type org.gradle.api.Project.

I am very new to gradle and groovy, so I have no idea what this error means. More importantly, what is the correct way to do what I want?

Upvotes: 1

Views: 891

Answers (2)

stdout
stdout

Reputation: 2651

Or, I think you can also go for implementing your own task with those properties. You can use DefaultTask which is a standard implementation of a regular task (and I'm sure it'd be enough for you);

class TexturePacker extends DefaultTask {
     String inputDir; // a property - not a field!
     String outputDir; // a property - not a field!
     ...

     @TaskAction
     void doSth(){
        // do sth with properties above - that will be called automatically by gradle as a task-execution
     }
}

task packer (type:TexturePacker) {
     inputDir '<your-input-dir>'
     outputDir '<your-output-dir>'
}

Syntax might not be super correct, but I think you get the idea.

Upvotes: 1

Stanislav
Stanislav

Reputation: 28106

I suppose, closure is not the thing you need, since it's used not to store variables, but to store some executable code. By the way, if need to store it, you have to add = as follows:

project.ext {
    texturePacker = {
        inputDir = "assets"
        outputDir = "../android/assets"
        packFileName = "texture"
    }
}

Anyway, if need to store variables within texturePacker variable, you rather have to use a Map type, then a Closure. This could be done like this:

project.ext {
    texturePacker = [
        inputDir : "assets",
        outputDir : "../android/assets",
        packFileName : "texture"
    ]
}

And then you can access this variable just by names, as:

println texturePacker.inputDir

Upvotes: 2

Related Questions