1+ import org.gradle.internal.jvm.Jvm
12import org.apache.tools.ant.util.TeeOutputStream
23// TODO: test for tags that span multiple lines
3- boolean debug = false
4+ boolean debug = false
45
56class Tags {
67 Boolean hasMainMethod = false
@@ -17,13 +18,12 @@ class Tags {
1718 String javap = null
1819 String runFirst = null
1920 String outputLine = null
20- private List<String > lines
2121 private String block
22- def Tags (List<String > lines ) {
23- this . lines = lines
24- block = lines. join(' ' )
22+ def Tags (File file ) {
23+ block = file. text
2524 hasMainMethod = block. contains(' main(String[] args)' )
26- fileRoot = lines[0 ]. split(" /" )[-1 ] - " .java"
25+ def firstLine = block. substring(0 , block. indexOf(" \n " ))
26+ fileRoot = firstLine. split(" /" )[-1 ] - " .java"
2727 mainClass = fileRoot
2828 javaCmd = extract(' java' )
2929 if (javaCmd) {
@@ -43,27 +43,30 @@ class Tags {
4343 ignoreOutput = hasTag(' IgnoreOutput' )
4444 javap = extract(' javap' ) // Includes only arguments to command
4545 runFirst = extract(' RunFirst:' )
46- lines. each {
47- if (it =~ / \\ / * Output :/ ) {
48- outputLine = it. trim()
49- }
50- }
46+ outputLine = extractOutputLine()
5147 }
5248 private def hasTag (String marker ) {
5349 return block. contains(" // {" + marker + " }" )
5450 }
51+
52+ def extractOutputLine () {
53+ if (! block. contains(" /* Output:" )) {
54+ return null
55+ } else {
56+ return " /* Output:"
57+ }
58+ }
59+
5560 private def extract (String marker ) {
56- if (! block. contains(" // {" + marker))
61+ // Assume some whitespace is after marker
62+ if (! block. contains(" // {${ marker} " ))
5763 return null
58- def re = " \\ /\\ / \\ {" + marker + / [^}]+}/
59- def tagline = block =~ re
60- if (tagline. getCount()) {
61- def rtrim = tagline[0 ]. reverse(). dropWhile{ it != ' }' }. reverse()[0 .. -2 ]
62- def ltrim = rtrim - (" // {" + marker)
63- ltrim = ltrim. replaceAll(" //" , " " )
64- return ltrim. trim()
64+ def matcher = (block =~ / \/\/ \{ ${marker}\s +([^}]+)/ )
65+ if (matcher) {
66+ def matched = matcher[0 ][1 ]. trim()
67+ return matched. replaceAll(" \n ?//" , " " )
6568 } else {
66- println " Searching for: " + re
69+ println " Searching for: " + matcher
6770 println block
6871 System . exit(1 )
6972 }
@@ -82,11 +85,10 @@ class Tags {
8285 }
8386 public String toString () {
8487 String result = " "
85- for ( ln in lines)
88+ block . eachLine{ ln ->
8689 if (ln. startsWith(" //" ) || ln. startsWith(" package " ))
8790 result + = ln + " \n "
88- else
89- break
91+ }
9092 """
9193 hasMainMethod
9294 compileTimeError
@@ -129,57 +131,72 @@ subprojects {
129131 }
130132 }
131133
132- List tasks = []
134+ List createdTasks = []
133135
134- projectDir. eachFileRecurse {
135- if (it . name. endsWith(' .java' )) {
136+ projectDir. eachFileRecurse { file ->
137+ if (file . name. endsWith(' .java' )) {
136138
137- Tags tags = new Tags (it . readLines() )
139+ Tags tags = new Tags (file )
138140 if (debug && tags. hasTags()) println tags
139141
140- // Add tasks for java sources with main methods
141- if ((tags. hasMainMethod || tags. javaCmd) && (! tags. compileTimeError)) {
142-
143- if (! tags. validateByHand) {
144- // Only add tasks that we know we can run successfully to the task list
145- tasks. add(tags. fileRoot)
142+ // Exclude java sources that will not compile
143+ if (tags. compileTimeError) {
144+ sourceSets. main. java. excludes. add(it. name)
145+ } else {
146+ JavaExec javaTask = null
147+ // Add tasks for java sources with main methods
148+ if (tags. hasMainMethod || tags. javaCmd) {
149+
150+ javaTask = tasks. create(name : tags. fileRoot, type : JavaExec , dependsOn : tags. runFirst) {
151+ main = tags. mainClass
152+ classpath = sourceSets. main. runtimeClasspath
153+ args = tags. args
154+ jvmArgs = tags. jVMArgs
155+ }
156+ } else if (tags. javap) {
157+ // Create task for running javap
158+ javaTask = tasks. create(name : " ${ tags.fileRoot} " , type : JavaExec , dependsOn : tags. runFirst) {
159+ main = " com.sun.tools.javap.Main"
160+ classpath = sourceSets. main. runtimeClasspath + files(Jvm . current(). toolsJar)
161+ // Assuming javap represents all the args and there's no need to jVMArgs
162+ args tags. javap. split()
163+ }
146164 }
147165
148- String basePath = it. absolutePath. replaceAll(' \\ .java' , ' ' )
149- File outFile = new File (basePath + ' .out' )
150- File errFile = new File (basePath + ' .err' )
151-
152- task " $tags . fileRoot " (type : JavaExec , dependsOn : tags. runFirst) {
153- main = tags. mainClass
154- classpath = sourceSets. main. runtimeClasspath
155- args = tags. args
156- jvmArgs = tags. jVMArgs
157- ignoreExitValue = tags. validateByHand || tags. throwsException
158- doFirst {
159- if (outFile. exists())
160- outFile. delete()
161- if (tags. outputLine)
162- outFile << tags. outputLine + " \n "
163-
164- standardOutput = new TeeOutputStream (new FileOutputStream (outFile, true ), System . out)
165- errorOutput = new TeeOutputStream (new FileOutputStream (errFile), System . err)
166+ if (javaTask) {
167+ def baseName = file. name. substring(0 , file. name. lastIndexOf(' .' ))
168+ File outFile = new File (file. parentFile, baseName + ' .out' )
169+ File errFile = new File (file. parentFile, baseName + ' .err' )
170+
171+ javaTask. configure {
172+ ignoreExitValue = tags. validateByHand || tags. throwsException
173+ doFirst {
174+ if (outFile. exists())
175+ outFile. delete()
176+ if (tags. outputLine)
177+ outFile << tags. outputLine + " \n "
178+
179+ standardOutput = new TeeOutputStream (new FileOutputStream (outFile, true ), System . out)
180+ errorOutput = new TeeOutputStream (new FileOutputStream (errFile), System . err)
181+ }
182+ doLast {
183+ if (outFile. size() == 0 )
184+ outFile. delete()
185+ else if (! outFile. text. contains(" /* Output:" ))
186+ outFile. delete()
187+ if (errFile. size() == 0 ) errFile. delete()
188+ }
166189 }
167- doLast {
168- if (outFile. size() == 0 )
169- outFile. delete()
170- else if (! outFile. text. contains(" /* Output:" ))
171- outFile. delete()
172- if (errFile. size() == 0 ) errFile. delete()
190+
191+ if (! tags. validateByHand) {
192+ // Only add tasks that we know we can run successfully to the task list
193+ createdTasks. add(javaTask)
173194 }
174195 }
175196 }
176- // Exclude java sources that will not compile
177- if (tags. compileTimeError) {
178- sourceSets. main. java. excludes. add(it. name)
179- }
180197 }
181198 }
182- task run(dependsOn : tasks )
199+ task run(dependsOn : createdTasks )
183200}
184201
185202configure(subprojects - project(' :onjava' )) {
0 commit comments