Last active
February 28, 2016 21:57
-
-
Save ysb33r/4034466ce638c92a3700 to your computer and use it in GitHub Desktop.
Using a Gradle Task as a data-driven file generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Uses a single Groovy template and multiple sets of text input files. | |
// It generates a set of Groovy source files using the content of the text input files to set tokens within the | |
// template file. | |
task generateGroovySources( type : Copy ) { | |
from 'examples', { | |
include '*.txt' | |
} | |
into "${buildDir}/generated-src" | |
eachFile { fcd -> | |
String sourceName = fcd.sourceName.replaceAll( /\.txt$/, '') | |
String content = fcd.file.text | |
fcd.filter {null} | |
fcd.filter ConcatFilter, append : project.file('src/templates/my.template') | |
fcd.filter ReplaceTokens, tokens : [ | |
token1 : content, | |
token2 : sourceName | |
] | |
} | |
rename ~/\.txt$/, '.groovy' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment