@@ -87,28 +87,31 @@ abstract class CallSiteInstrumentationPlugin : Plugin<Project>{
8787 // create a new source set for the csi files
8888 val targetFolder = newBuildFolder(project, extension.targetFolder.get().asFile.toString())
8989 val sourceSets = getSourceSets(project)
90- val csiSourceSet = sourceSets.create(" csi" )
9190 val mainSourceSet = sourceSets.named(SourceSet .MAIN_SOURCE_SET_NAME ).get()
92- val csiConfiguration = project.configurations.named(csiSourceSet.compileClasspathConfigurationName).get()
93- val mainConfiguration = project.configurations.named(mainSourceSet.compileClasspathConfigurationName).get()
94- csiConfiguration.extendsFrom(mainConfiguration)
95- csiSourceSet.compileClasspath + = mainSourceSet.output // mainly needed for the plugin tests
96- csiSourceSet.annotationProcessorPath + = mainSourceSet.annotationProcessorPath
97- csiSourceSet.java.srcDir(targetFolder)
98- project.getTasksByName(csiSourceSet.getCompileTaskName(" java" ), false ).forEach { task ->
99- val compile = task as AbstractCompile
100- compile.sourceCompatibility = JavaVersion .VERSION_1_8 .toString()
101- compile.targetCompatibility = JavaVersion .VERSION_1_8 .toString()
91+ val csiSourceSet = sourceSets.create(" csi" ) {
92+ compileClasspath + = mainSourceSet.output // mainly needed for the plugin tests
93+ annotationProcessorPath + = mainSourceSet.annotationProcessorPath
94+ java.srcDir(targetFolder)
95+
96+ }
97+ project.configurations.named(csiSourceSet.compileClasspathConfigurationName) {
98+ extendsFrom(project.configurations.named(mainSourceSet.compileClasspathConfigurationName).get())
99+ }
100+
101+ project.tasks.named(csiSourceSet.getCompileTaskName(" java" ), AbstractCompile ::class .java).configure {
102+ sourceCompatibility = JavaVersion .VERSION_1_8 .toString()
103+ targetCompatibility = JavaVersion .VERSION_1_8 .toString()
102104 }
103105
104106 // add csi classes to test classpath
105- val testSourceSet = sourceSets.named(SourceSet .TEST_SOURCE_SET_NAME ).get()
106- testSourceSet.compileClasspath + = csiSourceSet.output.classesDirs
107- testSourceSet.runtimeClasspath + = csiSourceSet.output.classesDirs
107+ sourceSets.named(SourceSet .TEST_SOURCE_SET_NAME ) {
108+ compileClasspath + = csiSourceSet.output.classesDirs
109+ runtimeClasspath + = csiSourceSet.output.classesDirs
110+ }
108111 project.dependencies.add(" testImplementation" , csiSourceSet.output)
109112
110113 // include classes in final JAR
111- project.tasks.named(" jar" , Jar ::class .java).configure {
114+ project.tasks.named(" jar" , Jar ::class .java) {
112115 from(csiSourceSet.output.classesDirs)
113116 }
114117 }
@@ -137,16 +140,16 @@ abstract class CallSiteInstrumentationPlugin : Plugin<Project>{
137140 }
138141
139142 private fun createTasks (project : Project , extension : CallSiteInstrumentationExtension ) {
140- val compileTask: AbstractCompile = project.tasks.named(" compileJava" , AbstractCompile ::class .java).get()
141- val input = compileTask.destinationDirectory
142- createGenerateCallSiteTask(project, extension, compileTask, input)
143+ registerGenerateCallSiteTask(project, extension, " compileJava" )
143144 val targetFolder = extension.targetFolder.get().asFile
144145 project.tasks.withType(AbstractCompile ::class .java).matching {
145146 task -> task.name.startsWith(" compileTest" )
146147 }.configureEach {
148+ inputs.dir(extension.targetFolder)
147149 classpath + = project.files(targetFolder)
148150 }
149151 project.tasks.withType(Test ::class .java).configureEach {
152+ inputs.dir(extension.targetFolder)
150153 classpath + = project.files(targetFolder)
151154 }
152155 }
@@ -157,11 +160,10 @@ abstract class CallSiteInstrumentationPlugin : Plugin<Project>{
157160 })
158161 }
159162
160- private fun createGenerateCallSiteTask (project : Project ,
161- extension : CallSiteInstrumentationExtension ,
162- compileTask : AbstractCompile ,
163- input : DirectoryProperty ) {
164- val taskName = compileTask.name.replace(" compile" , " generateCallSite" )
163+ private fun registerGenerateCallSiteTask (project : Project ,
164+ extension : CallSiteInstrumentationExtension ,
165+ compileTaskName : String ) {
166+ val taskName = compileTaskName.replace(" compile" , " generateCallSite" )
165167 val rootFolder = extension.rootFolder.getOrElse(project.rootDir)
166168 val pluginJarFile = Paths .get(
167169 rootFolder.toString(),
@@ -171,49 +173,56 @@ abstract class CallSiteInstrumentationPlugin : Plugin<Project>{
171173 " libs" ,
172174 " call-site-instrumentation-plugin-all.jar"
173175 ).toFile()
174- val programClassPath = getProgramClasspath( project).map { it.toString() }
176+ val compileTask = project.tasks.named(compileTaskName, AbstractCompile :: class .java)
175177 val callSiteGeneratorTask = project.tasks.register(taskName, JavaExec ::class .java) {
176178 // Task description
177179 group = " call site instrumentation"
178- description = " Generates call sites from ${compileTask.name } "
180+ description = " Generates call sites from ${compileTaskName } "
179181 // Task input & output
180182 val output = extension.targetFolder
181- inputs.dir(input)
183+ val inputProvider = compileTask.map { it.destinationDirectory.get() }
184+ inputs.dir(inputProvider)
182185 outputs.dir(output)
183186 // JavaExec configuration
184187 if (extension.javaVersion.isPresent) {
185188 configureLanguage(this , extension.javaVersion.get())
186189 }
187- jvmArgs( extension.jvmArgs.get())
190+ jvmArgumentProviders.add({ extension.jvmArgs.get() } )
188191 classpath(pluginJarFile)
189192 mainClass.set(CALL_SITE_INSTRUMENTER_MAIN_CLASS )
190193 // Write the call site instrumenter arguments into a temporary file
191194 doFirst {
192- val argumentFile = newTempFile(temporaryDir, " call-site-arguments " )
195+ val programClassPath = getProgramClasspath(project).map { it.toString() }
193196 val arguments = listOf (
194197 extension.srcFolder.get().asFile.toString(),
195- input .get().asFile.toString(),
198+ inputProvider .get().asFile.toString(),
196199 output.get().asFile.toString(),
197200 extension.suffix.get(),
198201 extension.reporters.get().joinToString(" ," )
199202 ) + programClassPath
203+
204+ val argumentFile = newTempFile(temporaryDir, " call-site-arguments" )
200205 Files .write(argumentFile.toPath(), arguments)
201206 args(argumentFile.toString())
202207 }
203- }.get()
204208
205- // insert task after compile
206- callSiteGeneratorTask.dependsOn(compileTask)
209+ // make task depends on compile
210+ dependsOn(compileTask)
211+ }
212+
213+ // make all sourcesets' class tasks depend on call site generator
207214 val sourceSets = getSourceSets(project)
208- val mainSourceSet = sourceSets.named(SourceSet .MAIN_SOURCE_SET_NAME ).get()
209- project.tasks.named(mainSourceSet.classesTaskName).configure {
210- dependsOn(callSiteGeneratorTask)
215+ sourceSets.named(SourceSet .MAIN_SOURCE_SET_NAME ) {
216+ project.tasks.named(classesTaskName) {
217+ dependsOn(callSiteGeneratorTask)
218+ }
211219 }
212220
213221 // compile generated sources
214- val csiSourceSet = sourceSets.named(" csi" ).get()
215- project.tasks.named(csiSourceSet.compileJavaTaskName).configure {
216- callSiteGeneratorTask.finalizedBy(this )
222+ sourceSets.named(" csi" ) {
223+ project.tasks.named(compileJavaTaskName) {
224+ dependsOn(callSiteGeneratorTask)
225+ }
217226 }
218227 }
219228
0 commit comments