@@ -87,28 +87,31 @@ abstract class CallSiteInstrumentationPlugin : Plugin<Project>{
87
87
// create a new source set for the csi files
88
88
val targetFolder = newBuildFolder(project, extension.targetFolder.get().asFile.toString())
89
89
val sourceSets = getSourceSets(project)
90
- val csiSourceSet = sourceSets.create(" csi" )
91
90
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()
102
104
}
103
105
104
106
// 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
+ }
108
111
project.dependencies.add(" testImplementation" , csiSourceSet.output)
109
112
110
113
// include classes in final JAR
111
- project.tasks.named(" jar" , Jar ::class .java).configure {
114
+ project.tasks.named(" jar" , Jar ::class .java) {
112
115
from(csiSourceSet.output.classesDirs)
113
116
}
114
117
}
@@ -137,16 +140,16 @@ abstract class CallSiteInstrumentationPlugin : Plugin<Project>{
137
140
}
138
141
139
142
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" )
143
144
val targetFolder = extension.targetFolder.get().asFile
144
145
project.tasks.withType(AbstractCompile ::class .java).matching {
145
146
task -> task.name.startsWith(" compileTest" )
146
147
}.configureEach {
148
+ inputs.dir(extension.targetFolder)
147
149
classpath + = project.files(targetFolder)
148
150
}
149
151
project.tasks.withType(Test ::class .java).configureEach {
152
+ inputs.dir(extension.targetFolder)
150
153
classpath + = project.files(targetFolder)
151
154
}
152
155
}
@@ -157,11 +160,10 @@ abstract class CallSiteInstrumentationPlugin : Plugin<Project>{
157
160
})
158
161
}
159
162
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" )
165
167
val rootFolder = extension.rootFolder.getOrElse(project.rootDir)
166
168
val pluginJarFile = Paths .get(
167
169
rootFolder.toString(),
@@ -171,49 +173,56 @@ abstract class CallSiteInstrumentationPlugin : Plugin<Project>{
171
173
" libs" ,
172
174
" call-site-instrumentation-plugin-all.jar"
173
175
).toFile()
174
- val programClassPath = getProgramClasspath( project).map { it.toString() }
176
+ val compileTask = project.tasks.named(compileTaskName, AbstractCompile :: class .java)
175
177
val callSiteGeneratorTask = project.tasks.register(taskName, JavaExec ::class .java) {
176
178
// Task description
177
179
group = " call site instrumentation"
178
- description = " Generates call sites from ${compileTask.name } "
180
+ description = " Generates call sites from ${compileTaskName } "
179
181
// Task input & output
180
182
val output = extension.targetFolder
181
- inputs.dir(input)
183
+ val inputProvider = compileTask.map { it.destinationDirectory.get() }
184
+ inputs.dir(inputProvider)
182
185
outputs.dir(output)
183
186
// JavaExec configuration
184
187
if (extension.javaVersion.isPresent) {
185
188
configureLanguage(this , extension.javaVersion.get())
186
189
}
187
- jvmArgs( extension.jvmArgs.get())
190
+ jvmArgumentProviders.add({ extension.jvmArgs.get() } )
188
191
classpath(pluginJarFile)
189
192
mainClass.set(CALL_SITE_INSTRUMENTER_MAIN_CLASS )
190
193
// Write the call site instrumenter arguments into a temporary file
191
194
doFirst {
192
- val argumentFile = newTempFile(temporaryDir, " call-site-arguments " )
195
+ val programClassPath = getProgramClasspath(project).map { it.toString() }
193
196
val arguments = listOf (
194
197
extension.srcFolder.get().asFile.toString(),
195
- input .get().asFile.toString(),
198
+ inputProvider .get().asFile.toString(),
196
199
output.get().asFile.toString(),
197
200
extension.suffix.get(),
198
201
extension.reporters.get().joinToString(" ," )
199
202
) + programClassPath
203
+
204
+ val argumentFile = newTempFile(temporaryDir, " call-site-arguments" )
200
205
Files .write(argumentFile.toPath(), arguments)
201
206
args(argumentFile.toString())
202
207
}
203
- }.get()
204
208
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
207
214
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
+ }
211
219
}
212
220
213
221
// 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
+ }
217
226
}
218
227
}
219
228
0 commit comments