OnJava8-Examples/build.gradle

196 lines
5.8 KiB
Groovy
Raw Normal View History

2016-07-08 15:39:40 -06:00
import org.apache.tools.ant.util.TeeOutputStream
2016-07-19 12:46:32 -06:00
// TODO: test for tags that span multiple lines
2016-07-14 18:43:34 -06:00
boolean debug = false
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
2016-07-14 18:18:02 -06:00
Boolean ignoreOutput = false // This tag isn't used in the build...
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
2016-07-18 14:46:40 -06:00
String outputLine = null
2016-07-13 16:34:14 -06:00
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
2016-07-27 17:31:28 -06:00
javaCmd = extract('java')
if(javaCmd) {
2016-07-14 13:29:44 -06:00
mainClass = javaCmd
2016-07-27 17:31:28 -06:00
println("fileRoot: " + fileRoot)
println("mainClass: " + mainClass)
}
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:')
2016-07-18 14:46:40 -06:00
lines.each {
if(it =~ /\\/* Output:/) {
outputLine = it.trim()
}
}
2016-07-13 16:34:14 -06:00
}
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-27 17:31:28 -06:00
ltrim = ltrim.replaceAll("//", " ")
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
2016-07-14 18:18:02 -06:00
"""
mainMethod
compileTimeError
throwsException
errorOutputExpected
validateByHand
ignoreOutput
fileRoot
mainClass
javaCmd
args
jVMArgs
exec
runFirst
""".split().each { str ->
if(this[str])
result += str + ": " + this[str] + "\n"
}
result
2016-07-13 16:34:14 -06:00
}
}
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 18:43:34 -06:00
if(debug && tags.hasTags()) println tags
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) {
2016-07-19 17:06:26 -06:00
// 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')
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-19 15:42:38 -06:00
doFirst {
if(outFile.exists())
outFile.delete()
if(tags.outputLine)
outFile << tags.outputLine + "\n"
standardOutput = new TeeOutputStream(new FileOutputStream(outFile, true), System.out)
errorOutput = new TeeOutputStream(new FileOutputStream(errFile), System.err)
}
doLast {
if(outFile.size() == 0)
outFile.delete()
else if(!outFile.text.contains("/* Output:"))
outFile.delete()
if(errFile.size() == 0) errFile.delete()
2016-07-19 15:42:38 -06:00
}
2016-07-07 12:43:10 -06:00
}
}
2016-07-19 17:06:26 -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')
}
}
2016-07-22 17:26:49 -06:00
task verify(type:Exec) {
println("execute 'gradlew run' first")
2016-07-26 10:28:19 -06:00
commandLine 'python', 'verify_output.py'
2016-07-22 17:26:49 -06:00
}