OnJava8-Examples/build.gradle

207 lines
7.4 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
String javaCmd = null
String args = null
String jVMArgs = null
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)')
compileTimeError = testFor('CompileTimeError')
throwsException = testFor('ThrowsException')
errorOutputExpected = testFor('ErrorOutputExpected')
validateByHand = testFor('ValidateByHand')
ignoreOutput = testFor('IgnoreOutput')
javaCmd = extract('main:')
args = extract('Args:')
jVMArgs = extract('JVMArgs:')
exec = extract('Exec:')
runFirst = extract('RunFirst:')
}
private def testFor(String marker) {
return block.contains("// {" + marker + "}")
}
private def extract(String marker) {
if(!block.contains("// {" + marker))
return null
def re = "\\/\\/ \\{" + marker + /[^}]+}/
def tagline = block =~ re
if(tagline.getCount()) {
println lines[0]
def rtrim = tagline[0] - "}"
def ltrim = rtrim - ("// {" + marker)
println ">" + ltrim
return ltrim
} else {
println "Searching for: " + re
println block
System.exit(1)
}
}
public String toString() {
String result = ""
for(ln in lines)
result += ln + "\n"
result += "mainMethod: " + mainMethod + "\n"
result += "compileTimeError: " + compileTimeError + "\n"
result += "throwsException: " + throwsException + "\n"
result += "errorOutputExpected: " + errorOutputExpected + "\n"
result += "validateByHand: " + validateByHand + "\n"
result += "ignoreOutput: " + ignoreOutput + "\n"
result += "javaCmd: " + javaCmd + "\n"
result += "args: " + args + "\n"
result += "jVMArgs: " + jVMArgs + "\n"
result += "exec: " + exec + "\n"
result += "runFirst: " + runFirst + "\n"
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-07 12:43:10 -06:00
List lines = it.readLines()
2015-12-15 15:35:04 -07:00
2016-07-13 16:34:14 -06:00
Tags tags = new Tags(lines) // Not used yet; just for demo
println tags
2016-07-07 12:43:10 -06:00
Boolean hasMainMethod = lines.join('').contains('main(String[] args)')
Boolean hasMainParam = lines.find { it.startsWith('// {main: ')}
2016-07-07 14:15:50 -06:00
Boolean willNotCompile = lines.find { it.startsWith('// {CompileTimeError') }
Boolean validateByHand = lines.find { it.startsWith('// {ValidateByHand}') }
Boolean throwsException = lines.find { it.startsWith('// {ThrowsException}') }
2015-12-15 15:35:04 -07:00
2016-07-07 12:43:10 -06:00
// add tasks for java sources with main methods
2016-07-07 14:15:50 -06:00
if ((hasMainMethod || hasMainParam) && (!willNotCompile)) {
2015-12-15 15:35:04 -07:00
2016-07-07 12:43:10 -06:00
String maybeArgsLine = lines.find { it.startsWith('// {Args: ')}
List maybeArgs = []
if (maybeArgsLine != null) {
maybeArgs = maybeArgsLine.trim().replaceAll('\\/\\/ \\{Args: ', '').reverse().replaceFirst('}', '').reverse().split(' ')
}
String mainClass = it.name.replaceAll('.java', '')
String taskName = mainClass
if (!validateByHand) {
// only add tasks that we know we can run successfully to the task list
tasks.add(taskName)
}
2016-07-07 12:43:10 -06:00
// some java sources with a main method specify a different main class
String maybeMainLine = lines.find { it.startsWith('// {main: ')}
if (maybeMainLine != null) {
mainClass = maybeMainLine.trim().replaceAll('\\/\\/ \\{main: ', '').replaceAll('}', '')
}
// some java sources with JVM args
List maybeJvmArgs = []
String maybeJvmArgsLine = lines.find { it.startsWith('// {JVMArgs: ')}
if (maybeJvmArgsLine != null) {
maybeJvmArgs = maybeJvmArgsLine.trim().replaceAll('\\/\\/ \\{JVMArgs: ', '').replaceAll('}', '').split(' ')
}
2015-12-15 15:35:04 -07:00
2016-07-07 12:43:10 -06:00
// some java sources depend on others to be run first
String maybeRunFirst = null
String maybeRunFirstLine = lines.find { it.startsWith('// {RunFirst: ')}
if (maybeRunFirstLine != null) {
maybeRunFirst = maybeRunFirstLine.trim().replaceAll('\\/\\/ \\{RunFirst: ', '').replaceAll('}', '')
}
2016-07-07 14:15:50 -06:00
// some java apps intentionally throw an exception
Boolean maybeIgnoreExitValue = (validateByHand || throwsException)
2016-07-07 12:43:10 -06:00
2016-07-07 14:15:50 -06:00
// some java apps need a timeout
Boolean timeOutDuringTesting = false
if (lines.find { it.startsWith('// {TimeOutDuringTesting}') } != null) {
timeOutDuringTesting = true
}
// create run and test tasks
2016-07-07 14:15:50 -06:00
if (timeOutDuringTesting) {
// todo: apps with timeOutDuringTesting need special handling
task "$taskName" << {
println "not implemented"
}
2016-07-07 12:43:10 -06:00
}
2016-07-07 14:15:50 -06:00
else {
2016-07-09 14:03:58 -06:00
String basePath = it.absolutePath.replaceAll('\\.java', '')
File outFile = new File(basePath + '.out')
File errFile = new File(basePath + '.err')
2016-07-07 14:15:50 -06:00
2016-07-08 15:39:40 -06:00
OutputStream runStandardOutput = new TeeOutputStream(new FileOutputStream(outFile), System.out)
2016-07-07 14:15:50 -06:00
2016-07-08 15:39:40 -06:00
OutputStream runErrorOutput = new TeeOutputStream(new FileOutputStream(errFile), System.err)
2016-07-07 14:15:50 -06:00
2016-07-08 15:39:40 -06:00
task "$taskName"(type: JavaExec, dependsOn: maybeRunFirst) {
2016-07-07 14:15:50 -06:00
main = mainClass
classpath = sourceSets.main.runtimeClasspath
args = maybeArgs
jvmArgs = maybeJvmArgs
ignoreExitValue = maybeIgnoreExitValue
2016-07-08 15:39:40 -06:00
standardOutput = runStandardOutput
errorOutput = runErrorOutput
addShutdownHook {
runStandardOutput.close()
runErrorOutput.close()
if (outFile.size() == 0) outFile.delete()
if (errFile.size() == 0) errFile.delete()
2016-07-07 14:15:50 -06:00
}
2016-07-07 12:43:10 -06:00
}
}
}
// exclude java sources that will not compile
2016-07-07 14:15:50 -06:00
if (willNotCompile) {
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')
}
}