OnJava8-Examples/build.gradle

176 lines
5.7 KiB
Groovy
Raw Normal View History

2016-07-08 15:39:40 -06:00
import org.apache.tools.ant.util.TeeOutputStream
2016-07-07 16:45:20 -06:00
2016-07-13 16:34:14 -06:00
class Tags {
Boolean mainMethod = false
Boolean compileTimeError = false
Boolean throwsException = false
Boolean errorOutputExpected = false
Boolean validateByHand = false
Boolean ignoreOutput = false // probably don't need this tag
2016-07-14 13:29:44 -06:00
String fileRoot
String mainClass
2016-07-13 16:34:14 -06:00
String javaCmd = null
2016-07-14 12:23:33 -06:00
List<String> args = []
2016-07-14 13:43:54 -06:00
List<String> jVMArgs = []
2016-07-13 16:34:14 -06:00
String exec = null
String runFirst = null
private List<String> lines
private String block
def Tags(List<String> lines) {
this.lines = lines
block = lines.join('')
mainMethod = block.contains('main(String[] args)')
2016-07-14 13:29:44 -06:00
fileRoot = lines[0].split("/")[-1] - ".java"
mainClass = fileRoot
javaCmd = extract('main:')
if(javaCmd)
mainClass = javaCmd
2016-07-13 16:34:14 -06:00
compileTimeError = testFor('CompileTimeError')
throwsException = testFor('ThrowsException')
errorOutputExpected = testFor('ErrorOutputExpected')
validateByHand = testFor('ValidateByHand')
ignoreOutput = testFor('IgnoreOutput')
2016-07-14 12:23:33 -06:00
def argString = extract('Args:')
2016-07-14 13:29:44 -06:00
if(argString)
args = argString.split(' ')
2016-07-14 13:43:54 -06:00
def jvmArgString = extract('JVMArgs:')
if(jvmArgString)
jVMArgs = jvmArgString.split(' ')
2016-07-13 16:34:14 -06:00
exec = extract('Exec:')
runFirst = extract('RunFirst:')
}
private def testFor(String marker) {
return block.contains("// {" + marker + "}")
}
private def extract(String marker) {
2016-07-14 05:59:30 -06:00
if(!block.contains("// {" + marker))
2016-07-13 16:34:14 -06:00
return null
def re = "\\/\\/ \\{" + marker + /[^}]+}/
def tagline = block =~ re
if(tagline.getCount()) {
2016-07-14 12:23:33 -06:00
def rtrim = tagline[0].reverse().dropWhile{ it != '}'}.reverse()[0..-2]
2016-07-13 16:34:14 -06:00
def ltrim = rtrim - ("// {" + marker)
2016-07-14 05:59:30 -06:00
return ltrim.trim()
2016-07-13 16:34:14 -06:00
} else {
println "Searching for: " + re
println block
System.exit(1)
}
}
2016-07-14 05:59:30 -06:00
public boolean hasTags() {
return compileTimeError ||
throwsException ||
errorOutputExpected ||
validateByHand ||
ignoreOutput ||
javaCmd ||
args ||
jVMArgs ||
exec ||
runFirst
}
2016-07-13 16:34:14 -06:00
public String toString() {
String result = ""
for(ln in lines)
2016-07-14 05:59:30 -06:00
if(ln.startsWith("//") || ln.startsWith("package "))
result += ln + "\n"
else
break
if(mainMethod)
result += "mainMethod: " + mainMethod + "\n"
if(compileTimeError)
result += "compileTimeError: " + compileTimeError + "\n"
if(throwsException)
result += "throwsException: " + throwsException + "\n"
if(errorOutputExpected)
result += "errorOutputExpected: " + errorOutputExpected + "\n"
if(validateByHand)
result += "validateByHand: " + validateByHand + "\n"
if(ignoreOutput)
result += "ignoreOutput: " + ignoreOutput + "\n"
if(javaCmd)
result += "javaCmd: " + javaCmd + "\n"
if(args)
result += "args: " + args + "\n"
if(jVMArgs)
result += "jVMArgs: " + jVMArgs + "\n"
if(exec)
result += "exec: " + exec + "\n"
if(runFirst)
result += "runFirst: " + runFirst + "\n"
2016-07-13 16:34:14 -06:00
return result
}
}
2016-07-07 12:43:10 -06:00
subprojects {
apply plugin: 'java'
2015-12-15 15:35:04 -07:00
2016-07-07 12:43:10 -06:00
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
2015-12-15 15:35:04 -07:00
2016-07-07 12:43:10 -06:00
repositories {
mavenLocal()
mavenCentral()
}
2015-12-15 15:35:04 -07:00
2016-07-07 12:43:10 -06:00
sourceSets {
main {
java {
srcDir projectDir
}
2015-12-15 15:35:04 -07:00
}
}
2016-07-07 12:43:10 -06:00
List tasks = []
2015-12-15 15:35:04 -07:00
2016-07-07 12:43:10 -06:00
projectDir.eachFileRecurse {
if (it.name.endsWith('.java')) {
2015-12-15 15:35:04 -07:00
2016-07-14 13:56:55 -06:00
Tags tags = new Tags(it.readLines())
2016-07-14 12:23:33 -06:00
// if(tags.hasTags()) println tags // Trace output for debugging
2015-12-15 15:35:04 -07:00
2016-07-14 05:59:30 -06:00
// Add tasks for java sources with main methods
2016-07-14 12:23:33 -06:00
if ((tags.mainMethod || tags.javaCmd) && (!tags.compileTimeError)) {
2015-12-15 15:35:04 -07:00
2016-07-14 12:23:33 -06:00
if (!tags.validateByHand) {
// only add tasks that we know we can run successfully to the task list
2016-07-14 13:29:44 -06:00
tasks.add(tags.fileRoot)
2016-07-07 12:43:10 -06:00
}
2016-07-14 12:23:33 -06:00
String basePath = it.absolutePath.replaceAll('\\.java', '')
File outFile = new File(basePath + '.out')
File errFile = new File(basePath + '.err')
OutputStream runStandardOutput = new TeeOutputStream(new FileOutputStream(outFile), System.out)
OutputStream runErrorOutput = new TeeOutputStream(new FileOutputStream(errFile), System.err)
2016-07-14 13:52:04 -06:00
task "$tags.fileRoot"(type: JavaExec, dependsOn: tags.runFirst) {
2016-07-14 13:29:44 -06:00
main = tags.mainClass
2016-07-14 12:23:33 -06:00
classpath = sourceSets.main.runtimeClasspath
args = tags.args
2016-07-14 13:43:54 -06:00
jvmArgs = tags.jVMArgs
2016-07-14 13:56:55 -06:00
ignoreExitValue = tags.validateByHand || tags.throwsException
2016-07-14 12:23:33 -06:00
standardOutput = runStandardOutput
errorOutput = runErrorOutput
} << {
runStandardOutput.close()
runErrorOutput.close()
if (outFile.size() == 0) outFile.delete()
if (errFile.size() == 0) errFile.delete()
2016-07-07 12:43:10 -06:00
}
}
// exclude java sources that will not compile
2016-07-14 12:23:33 -06:00
if (tags.compileTimeError) {
2016-07-07 12:43:10 -06:00
sourceSets.main.java.excludes.add(it.name)
}
}
}
task run(dependsOn: tasks)
2015-12-15 15:35:04 -07:00
}
2016-07-07 12:43:10 -06:00
configure(subprojects - project(':onjava')) {
dependencies {
compile project(':onjava')
}
}