Last active
December 10, 2015 10:09
-
-
Save mike-neck/4419050 to your computer and use it in GitHub Desktop.
gradleb - gradle builder for easy creating gradle build script
This file contains hidden or 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
import groovy.transform.* | |
@ToString (includeNames = true) | |
class BuildGradle { | |
final StringWriter w = new StringWriter() | |
def plugins = [] | |
void apply () { | |
plugins.each { | |
w << "apply plugin : '${it}'\n" | |
} | |
w << '\n' | |
} | |
String project | |
void structure () { | |
def root = (!project)?'src' : "${project}/src" | |
def ant = new AntBuilder () | |
def dirs = plugins.collect {plg -> | |
PLUGIN.values().collect {type -> | |
type.applicable(plg)? type.dir(root, group) : null | |
} | |
}.flatten().unique() - null | |
dirs.each { | |
ant.echo it | |
ant.mkdir dir : it | |
} | |
} | |
enum PLUGIN { | |
JAVA { | |
List<String> TYPE = [ | |
'java', 'groovy', 'scala', 'antlr', 'application', | |
'jetty', 'war', 'osgi', 'checkstyle', 'codenarc', | |
'findbugs', 'jdepend', 'pmd'] | |
List<String> dir (String grp) { | |
["main/java/${grp}", "test/java/${grp}"] | |
} | |
@Override | |
boolean applicable (String plugin) {TYPE.contains(plugin)} | |
}, GROOVY { | |
List<String> TYPE = ['groovy', 'codenarc'] | |
List<String> dir (String grp) { | |
["main/groovy/${grp}", "test/groovy/${grp}"] | |
} | |
@Override | |
boolean applicable (String plugin) {TYPE.contains(plugin)} | |
}, SCALA { | |
List<String> TYPE = ['scala'] | |
List<String> dir (String grp) { | |
["main/scala/${grp}", "test/scala/${grp}"] | |
} | |
@Override | |
boolean applicable (String plugin) {TYPE.contains(plugin)} | |
}, WEB { | |
List<String> TYPE = ['jetty', 'war'] | |
List<String> dir (String grp) { | |
["main/webapp/WEB-INF"] | |
} | |
@Override | |
boolean applicable (String plugin) {TYPE.contains(plugin)} | |
}, RESOURCES { | |
List<String> dir (String grp) { | |
["main/resources", "test/resources"] | |
} | |
@Override | |
boolean applicable (String plugin) {true} | |
}; | |
List<String> dir (String root, String grp) { | |
this.dir(grp.replace('.', '/')).collect {"${root}/${it}"} | |
} | |
boolean applicable (String plugin) {} | |
} | |
String version | |
void wVersion () { | |
w << "version = '${version}'\n" | |
} | |
String group | |
void wGroup () { | |
w << "group = '${group}'\n" | |
} | |
String compatibility | |
void wCompatibility () { | |
w << "sourceCompatibility = '${compatibility}'\n" | |
w << "targetCompatibility = '${compatibility}'\n" | |
w << '\n' | |
} | |
def repos = [] | |
void wRepos () { | |
w << 'repositories {\n' | |
if (repos.contains('mavenCentral')) { | |
w << ' mavenCentral ()\n' | |
} else { | |
w << '\n' | |
} | |
def otherRepos = (repos - 'mavenCentral').collect { | |
" url (${it})\n" | |
} | |
if (otherRepos.size() > 0) { | |
w << ' maven {\n' | |
otherRepos.each { | |
w << " ${it}\n" | |
} | |
w << ' }' | |
} | |
w << '}\n' | |
} | |
def dependencies = [] | |
void wDependencies () { | |
w << 'dependencies {\n' | |
dependencies.each { | |
def item = it.split('/') | |
def scp = item[0] | |
def dep = item[1] | |
w << " ${scp} '${dep}'\n" | |
} | |
w << '}\n' | |
} | |
void compile () { | |
apply() | |
wGroup() | |
wVersion() | |
wCompatibility() | |
wRepos() | |
wDependencies() | |
structure() | |
new File((!project)? '' : "${project}" + '/build.gradle').write(w.toString(),'UTF-8') | |
} | |
} | |
def gradleb (args) { | |
def cli = new CliBuilder (usage : 'gradleb -[hpajgvrd]') | |
cli.with { | |
h longOpt : 'help', 'Show usage information' | |
p longOpt : 'project-name', args : 1, argName : 'project', | |
'project name' | |
a longOpt : 'apply-plugins', args : 1, argName : 'plugin', | |
'Apply plugins to the project (you can specify multi plugins separated by commas. default : java)' | |
j longOpt : 'java-compatibility', args : 1, argName : 'compatibility', | |
'Java compatibility for source and target (default : 1.7)' | |
g longOpt : 'group-name', args : 1, argName : 'group', | |
'Group name for the project' | |
v longOpt : 'archive-version', args : 1, argName : 'version', | |
'a version of the project' | |
r longOpt : 'repositories', args : 1, argName : 'repositoryUrls', | |
'repositories to resolve dependencies. As a default maven central repository is included. You can specify more than one repositories separated by semi-colon(;)' | |
d longOpt : 'dependencies', args : 1, argName : 'dependencies', | |
'dependencies required. You should specify dependencies scope/group:artifact:version. You can specify multi dependencies separated by commas. (ex. groovy/org.codehaus.groovy:groovy-all:2.0.5,testCompile/junit:junit:4.11)' | |
} | |
if (args.size () == 0) { | |
cli.usage() | |
return null | |
} | |
def options = cli.parse(args) | |
if (options.h) { | |
cli.usage () | |
return null | |
} | |
def project = '' | |
if (options.p) { | |
project = options.p | |
} | |
def plugins = ['java'] | |
if (options.a) { | |
plugins += options.a.split(',').collect {it.trim()} | |
} | |
plugins.unique() | |
def compatibility = '1.7' | |
if (options.j) { | |
compatibility = options.j | |
} | |
def pattern = ~/^[a-zA-Z]([a-zA-Z0-9]*)(\.[a-zA-Z][a-zA-Z0-9]*)*(\.[a-zA-Z][a-zA-Z0-9]*)?$/ | |
def group | |
if (options.g) { | |
def grp = options.g | |
if (pattern.matcher(grp).matches()) group = grp | |
else new IllegalArgumentException("The package name [${grp}] is invalid group name.").printStackTrace() | |
} | |
def version = '1.0' | |
if (options.v) { | |
version = options.v | |
} | |
def repos = ['mavenCentral'] | |
if (options.r) { | |
def rps = options.r | |
repos += rps.split(';').collect {it.trim()} | |
} | |
def dependencies = [] | |
if (options.d) { | |
def dps = options.d | |
dependencies += dps.split(',').collect {it.trim()} | |
} | |
def buildGradle = new BuildGradle ( | |
project : project, | |
plugins : plugins, | |
version : version, | |
group : group, | |
compatibility : compatibility, | |
repos : repos, | |
dependencies : dependencies | |
) | |
} | |
def gradle = gradleb(args) | |
if (gradle) gradle.compile() | |
gradleb -hとやればヘルプが出ます。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
後はaliasに
gradleb = 'groovy /path/to/gradleb.groovy'
とやっておけばおk