Gradle task with 2 ant targets - trying to combine ant targets to speed up execution time

198 Views Asked by At

I currently have a Gradle Task that uses 2 ant targets (copy and move). These 2 ant tasks give me the right results. However, I was wondering if it is possible to join them into one ant target to speed up the execution time and to reduce the amount of code.

I have tried to do that with 2 mappers, but I can only use one mapper apparently. I have tried to use chainedmapper and compositemapper, but the results were not what I expected.

What I want

I have some files that I need to change all their extension (.icls -> .xml), and I have one file that I want to rename (ChroMATERIAL IntelliJ IDEA -> ChroMATERIAL)

Starting file name                    final file name
------------------                    ---------------
ChroMATERIAL - Darker.icls        ->  ChroMATERIAL - Darker.xml
ChroMATERIAL - Darcula.icls       ->  ChroMATERIAL - Darcula.xml
ChroMATERIAL IntelliJ IDEA.icls   ->  ChroMATERIAL.xml

The code below is what I was trying to go for originally before I got an error message. The code at the very end is my workaround that I currently use.

EDIT: I do prefer an ant-based grade task. I have looked at pure ant based files that appear to allow for multiple mappers and that is what I think I need, but I have not been able to get something similar working.

What I Tried

task syncFiles {

    doLast {

        // Sync files from IntelliJ's color scheme and rename extension
        ant.copy(todir: sourceDir) {
            ant.fileset(dir: intelliJColorSchemeDir)

            // NOTE: This is an error message. Can only have one mapper!
            ant.mapper(type: "glob", from: "*.icls", to: "*.xml")
            ant.mapper(type: "glob", from: "ChroMATERIAL IntelliJ IDEA.xml", to: "ChroMATERIAL.xml")    
        }
    }
}

My Current Implementation

This is what I want to turn into one ant task with 2 mappers. This I believe can be turned into slimmer code that only calls ant once. I want this code to execute a little faster, too.

task syncFiles {

    doLast {

        // Sync files from IntelliJ's color scheme and rename extension
        ant.copy(todir: sourceDir) {
            ant.fileset(dir: intelliJColorSchemeDir)
            ant.mapper(type: "glob", from: "*.icls", to: "*.xml")
        }

        // Rename one specific file. I want this mapper to be joined with the above mapper
        ant.move(todir: sourceDir) {
            ant.fileset(dir: sourceDir)
            ant.mapper(type: "glob", from: "ChroMATERIAL IntelliJ IDEA.xml", to: "ChroMATERIAL.xml")
        }
    }
}
2

There are 2 best solutions below

1
On BEST ANSWER

With staying with the original question and providing an Ant-based solution, the following code seems to be the cleanest. There might be other types of composite mappers that perform better, but this is what I could come up with.

    ant.copy(todir: sourceDir) {
        ant.fileset(dir: intelliJColorSchemeDir)
        ant.firstmatchmapper {
            ant.globmapper(from: "ChroMATERIAL IntelliJ IDEA.icls", to: "ChroMATERIAL.xml")
            ant.globmapper(from: "*.icls", to: "*.xml")
        }
    }

Things I like about it. The code is very clean and does not use strange syntax such as regex. It also combines the mappers into one block. Lastly, it uses Ant like I originally wanted.

Performing some quick tests seem to show very little change in execution time, but I have reported some of the quickest execution times for this task with the above code. Don't know if that is a fluke or if it is slightly faster.

Edit:

Here is the clear and concise version of using Gradle only...

into sourceDir
from intelliJColorSchemeDir, {
    rename 'ChroMATERIAL IntelliJ IDEA.icls','ChroMATERIAL.xml'  // Edit: moved to top and changed file extension.
    rename '(.*)\\.icls$', '$1.xml'

}

This code is actually faster on every run it appears.

6
On
task syncFiles(type: Copy) {
    into sourceDir
    with copySpec {
       from intelliJColorSchemeDir
       include '**/ChroMATERIAL IntelliJ IDEA.icls'
       rename '.*', 'ChroMATERIAL.xml'
    }
    with copySpec {
       from intelliJColorSchemeDir
       exclude '**/ChroMATERIAL IntelliJ IDEA.icls'
       rename '(.*)\\.icls$', '$1.xls'
    }
}