@@ -18,8 +18,7 @@ import org.gradle.jvm.toolchain.JavaLanguageVersion
1818
1919import java.nio.file.Paths
2020
21- import static groovy.io.FileType.FILES
22-
21+ @SuppressWarnings (' unused' )
2322@CompileStatic
2423class CallSiteInstrumentationPlugin implements Plugin<Project > {
2524
@@ -39,9 +38,9 @@ class CallSiteInstrumentationPlugin implements Plugin<Project> {
3938 final targetFolder = newBuildFolder(target, extension. targetFolder)
4039 final sourceSets = getSourceSets(target)
4140 final csiSourceSet = sourceSets. create(' csi' )
42- final mainSourceSet = sourceSets. getByName (SourceSet . MAIN_SOURCE_SET_NAME )
43- final csiConfiguration = target. configurations. getByName (csiSourceSet. compileClasspathConfigurationName)
44- final mainConfiguration = target. configurations. getByName (mainSourceSet. compileClasspathConfigurationName)
41+ final mainSourceSet = sourceSets. named (SourceSet . MAIN_SOURCE_SET_NAME ) . get( )
42+ final csiConfiguration = target. configurations. named (csiSourceSet. compileClasspathConfigurationName) . get( )
43+ final mainConfiguration = target. configurations. named (mainSourceSet. compileClasspathConfigurationName) . get( )
4544 csiConfiguration. extendsFrom(mainConfiguration)
4645 csiSourceSet. compileClasspath + = mainSourceSet. output // mainly needed for the plugin tests
4746 csiSourceSet. annotationProcessorPath + = mainSourceSet. annotationProcessorPath
@@ -53,7 +52,7 @@ class CallSiteInstrumentationPlugin implements Plugin<Project> {
5352 }
5453
5554 // add csi classes to test classpath
56- final testSourceSet = sourceSets. getByName (SourceSet . TEST_SOURCE_SET_NAME )
55+ final testSourceSet = sourceSets. named (SourceSet . TEST_SOURCE_SET_NAME ) . get( )
5756 testSourceSet. compileClasspath + = csiSourceSet. output. classesDirs
5857 testSourceSet. runtimeClasspath + = csiSourceSet. output. classesDirs
5958 target. dependencies. add(' testImplementation' , csiSourceSet. output)
@@ -63,31 +62,23 @@ class CallSiteInstrumentationPlugin implements Plugin<Project> {
6362 }
6463
6564 private static void createTasks (final Project target ) {
66- final compileTask = (AbstractCompile ) target. tasks. findByName (' compileJava' )
65+ final compileTask = (AbstractCompile ) target. tasks. named (' compileJava' ) . get( )
6766 final extension = target. extensions. getByType(CallSiteInstrumentationExtension )
6867 final input = compileTask. destinationDirectory
6968 final output = target. layout. buildDirectory. dir(extension. targetFolder)
7069 final targetFolder = output. get(). asFile
7170 createGenerateCallSiteTask(target, compileTask, input, output)
72- target. tasks. matching { Task task -> task. name. startsWith(' compileTest' ) }. all {
73- final compileTestTask = (AbstractCompile ) it
74- compileTestTask. classpath = compileTestTask. classpath + target. files(targetFolder)
71+ target. tasks. matching { Task task -> task. name. startsWith(' compileTest' ) }. configureEach {
72+ ((AbstractCompile ) it). classpath + = target. files(targetFolder)
7573 }
76- target. tasks. matching { Task task -> task instanceof Test }. all {
77- final testTask = (Test ) it
78- testTask. classpath = testTask. classpath + target. files(targetFolder)
74+ target. tasks. matching { Task task -> task instanceof Test }. configureEach {
75+ ((Test ) it). classpath + = target. files(targetFolder)
7976 }
8077 }
8178
8279 private static File newBuildFolder (final Project target , final String name ) {
83- final folder = new File (target. buildDir, name)
84- if (folder. exists()) {
85- folder. traverse(type : FILES ) {
86- if (! it. delete()) {
87- throw new GradleException (" Cannot delete stale file $it " )
88- }
89- }
90- } else {
80+ final folder = target. layout. buildDirectory. dir(name). get(). asFile
81+ if (! folder. exists()) {
9182 if (! folder. mkdirs()) {
9283 throw new GradleException (" Cannot create folder $folder " )
9384 }
@@ -97,12 +88,10 @@ class CallSiteInstrumentationPlugin implements Plugin<Project> {
9788
9889 private static File newTempFile (final File folder , final String name ) {
9990 final file = new File (folder, name)
100- file. deleteOnExit()
101- if (file. exists()) {
102- file. text = ' '
103- } else if (! file. createNewFile()) {
91+ if (! file. exists() && ! file. createNewFile()) {
10492 throw new GradleException (" Cannot create temporary file: $file " )
10593 }
94+ file. deleteOnExit()
10695 return file
10796 }
10897
@@ -112,7 +101,7 @@ class CallSiteInstrumentationPlugin implements Plugin<Project> {
112101 final Provider<Directory > output ) {
113102 final extension = target. extensions. getByType(CallSiteInstrumentationExtension )
114103 final taskName = compileTask. name. replace(' compile' , ' generateCallSite' )
115- final callSiteGeneratorTask = target. tasks. create (taskName, JavaExec )
104+ final callSiteGeneratorTask = target. tasks. register (taskName, JavaExec ) . get( )
116105 final stdout = new ByteArrayOutputStream ()
117106 final stderr = new ByteArrayOutputStream ()
118107 callSiteGeneratorTask. group = ' call site instrumentation'
@@ -128,13 +117,13 @@ class CallSiteInstrumentationPlugin implements Plugin<Project> {
128117
129118 final rootFolder = extension. rootFolder ?: target. rootDir
130119 final path = Paths . get(rootFolder. toString(),
131- ' buildSrc' , ' call-site-instrumentation-plugin' , ' build' , ' libs' , ' call-site-instrumentation-plugin.jar' )
120+ ' buildSrc' , ' call-site-instrumentation-plugin' , ' build' , ' libs' , ' call-site-instrumentation-plugin-all .jar' )
132121 callSiteGeneratorTask. jvmArgs(extension. jvmArgs)
133122 callSiteGeneratorTask. classpath(path. toFile())
134123 callSiteGeneratorTask. setIgnoreExitValue(true )
135124 // pass the arguments to the main via file to prevent issues with too long classpaths
136125 callSiteGeneratorTask. doFirst { JavaExec execTask ->
137- final argumentFile = newTempFile(execTask. getTemporaryDir(), " call-site-arguments" )
126+ final argumentFile = newTempFile(execTask. getTemporaryDir(), ' call-site-arguments' )
138127 argumentFile. withWriter {
139128 it. writeLine(target. getProjectDir(). toPath(). resolve(extension. srcFolder). toString())
140129 it. writeLine(input. get(). asFile. toString())
@@ -156,28 +145,28 @@ class CallSiteInstrumentationPlugin implements Plugin<Project> {
156145 // insert task after compile
157146 callSiteGeneratorTask. dependsOn(compileTask)
158147 final sourceSets = getSourceSets(target)
159- final mainSourceSet = sourceSets. getByName (SourceSet . MAIN_SOURCE_SET_NAME )
148+ final mainSourceSet = sourceSets. named (SourceSet . MAIN_SOURCE_SET_NAME ) . get( )
160149 target. tasks. named(mainSourceSet. classesTaskName). configure { it. dependsOn(callSiteGeneratorTask) }
161150
162151 // compile generated sources
163- final csiSourceSet = sourceSets. getByName (' csi' )
152+ final csiSourceSet = sourceSets. named (' csi' ) . get( )
164153 target. tasks. named(csiSourceSet. compileJavaTaskName). configure { callSiteGeneratorTask. finalizedBy(it) }
165154 }
166155
167156 private static List<File > getProgramClasspath (final Project project ) {
168157 final List<File > classpath = []
169158 // 1. Compilation outputs
170- project. tasks. matching { Task task -> task instanceof AbstractCompile }. all {
159+ project. tasks. matching { Task task -> task instanceof AbstractCompile }. configureEach {
171160 final compileTask = (AbstractCompile ) it
172161 classpath. add(compileTask. destinationDirectory. getAsFile(). get())
173162 }
174163 // 2. Compile time dependencies
175- project. tasks. matching { Task task -> task instanceof AbstractCompile }. all {
164+ project. tasks. matching { Task task -> task instanceof AbstractCompile }. configureEach {
176165 final compileTask = (AbstractCompile ) it
177166 compileTask. classpath. every { classpath. add(it) }
178167 }
179168 // 3. Test time dependencies
180- project. tasks. matching { Task task -> task instanceof Test }. all {
169+ project. tasks. matching { Task task -> task instanceof Test }. configureEach {
181170 final testTask = (Test ) it
182171 testTask. classpath. every { classpath. add(it) }
183172 }
0 commit comments