@@ -22,20 +22,15 @@ import com.diffplug.gradle.JRE
22
22
import java.io.BufferedInputStream
23
23
import java.io.BufferedOutputStream
24
24
import java.io.File
25
- import java.io.IOException
26
25
import java.nio.charset.StandardCharsets
27
26
import java.nio.file.Files
28
- import java.util.*
29
- import java.util.function.Consumer
27
+ import java.util.SortedMap
30
28
import java.util.jar.Attributes
31
29
import java.util.jar.Manifest
32
- import java.util.stream.Collectors
33
30
import javax.inject.Inject
34
31
import org.gradle.api.DefaultTask
35
- import org.gradle.api.Task
36
32
import org.gradle.api.file.ConfigurableFileCollection
37
33
import org.gradle.api.file.FileCollection
38
- import org.gradle.api.model.ObjectFactory
39
34
import org.gradle.api.plugins.JavaPluginExtension
40
35
import org.gradle.api.provider.Property
41
36
import org.gradle.api.tasks.Classpath
@@ -57,8 +52,6 @@ abstract class PlugGenerateTask : DefaultTask() {
57
52
58
53
@get:Inject abstract val workerExecutor: WorkerExecutor
59
54
60
- @get:Inject abstract val fS: ObjectFactory ?
61
-
62
55
@get:InputFiles @get:Classpath abstract val jarsToLinkAgainst: ConfigurableFileCollection
63
56
64
57
@get:Internal var resourcesFolder: File ? = null
@@ -70,9 +63,9 @@ abstract class PlugGenerateTask : DefaultTask() {
70
63
@InputFiles var classesFolders: FileCollection ? = null
71
64
72
65
init {
73
- this .outputs.upToDateWhen { unused : Task ? ->
66
+ this .outputs.upToDateWhen {
74
67
val manifest = loadManifest()
75
- val componentsCmd = atplugComponents()
68
+ val componentsCmd = atplugComponents(atplugInfFolder )
76
69
val componentsActual = manifest.mainAttributes.getValue(PlugPlugin .SERVICE_COMPONENT )
77
70
componentsActual == componentsCmd
78
71
}
@@ -98,7 +91,7 @@ abstract class PlugGenerateTask : DefaultTask() {
98
91
99
92
// clean out the ATPLUG-INF folder, and put the map's content into the folder
100
93
FileMisc .cleanDir(atplugInfFolder)
101
- for ((key, value) in result!! ) {
94
+ for ((key, value) in result) {
102
95
val serviceFile = File (atplugInfFolder, key + PlugPlugin .DOT_JSON )
103
96
Files .write(serviceFile.toPath(), value.toByteArray(StandardCharsets .UTF_8 ))
104
97
}
@@ -107,7 +100,7 @@ abstract class PlugGenerateTask : DefaultTask() {
107
100
// for tests to work
108
101
// so we'll get a manifest (empty if necessary, but preferably we'll load what already exists)
109
102
val manifest = loadManifest()
110
- val componentsCmd = atplugComponents()
103
+ val componentsCmd = atplugComponents(atplugInfFolder )
111
104
val componentsActual = manifest.mainAttributes.getValue(PlugPlugin .SERVICE_COMPONENT )
112
105
if (componentsActual == componentsCmd) {
113
106
return
@@ -131,29 +124,21 @@ abstract class PlugGenerateTask : DefaultTask() {
131
124
private fun loadManifest (): Manifest {
132
125
val manifest = Manifest ()
133
126
if (manifestFile().isFile) {
134
- try {
135
- BufferedInputStream (Files .newInputStream(manifestFile().toPath())).use { input ->
136
- manifest.read(input)
137
- }
138
- } catch (e: IOException ) {
139
- throw RuntimeException (e)
127
+ BufferedInputStream (Files .newInputStream(manifestFile().toPath())).use { input ->
128
+ manifest.read(input)
140
129
}
141
130
}
142
131
return manifest
143
132
}
144
133
145
134
private fun saveManifest (manifest : Manifest ) {
146
135
FileMisc .mkdirs(manifestFile().parentFile)
147
- try {
148
- BufferedOutputStream (Files .newOutputStream(manifestFile().toPath())).use { output ->
149
- manifest.write(output)
150
- }
151
- } catch (e: IOException ) {
152
- throw RuntimeException (e)
136
+ BufferedOutputStream (Files .newOutputStream(manifestFile().toPath())).use { output ->
137
+ manifest.write(output)
153
138
}
154
139
}
155
140
156
- private fun generate (): SortedMap <String , String >? {
141
+ private fun generate (): SortedMap <String , String > {
157
142
val input =
158
143
PlugGeneratorJavaExecable (ArrayList (classesFolders!! .files), jarsToLinkAgainst.files)
159
144
return if (launcher.isPresent) {
@@ -164,53 +149,44 @@ abstract class PlugGenerateTask : DefaultTask() {
164
149
options.setExecutable(launcher.get().executablePath)
165
150
}
166
151
}
167
- exec(workQueue, input).atplugInf
152
+ exec(workQueue, input).atplugInf!!
168
153
} else {
169
154
input.run ()
170
- input.atplugInf
155
+ input.atplugInf!!
171
156
}
172
157
}
173
158
174
- private fun atplugComponents (): String? {
175
- return atplugComponents(atplugInfFolder)
176
- }
177
-
178
159
companion object {
179
160
fun atplugComponents (atplugInf : File ): String? {
180
- return if (! atplugInf.isDirectory) {
181
- null
182
- } else {
161
+ return if (! atplugInf.isDirectory) null
162
+ else {
183
163
val serviceComponents: MutableList <String > = ArrayList ()
184
164
for (file in FileMisc .list(atplugInf)) {
185
165
if (file.name.endsWith(PlugPlugin .DOT_JSON )) {
186
166
serviceComponents.add(PlugPlugin .ATPLUG_INF + file.name)
187
167
}
188
168
}
189
- Collections .sort(serviceComponents )
190
- serviceComponents.stream().collect( Collectors .joining( " ," ) )
169
+ serviceComponents .sort()
170
+ serviceComponents.joinToString( " ," )
191
171
}
192
172
}
193
173
194
174
fun fromLocalClassloader (): Set <File > {
195
- val files: MutableSet <File > = LinkedHashSet ()
196
- val addPeerClasses = Consumer { clazz: Class <* > ->
197
- try {
198
- for (url in JRE .getClasspath(clazz.classLoader)) {
199
- val name = url.file
200
- if (name != null ) {
201
- files.add(File (name))
202
- }
175
+ val files = mutableSetOf<File >()
176
+ val addPeerClasses = { clazz: Class <* > ->
177
+ for (url in JRE .getClasspath(clazz.classLoader)) {
178
+ val name = url.file
179
+ if (name != null ) {
180
+ files.add(File (name))
203
181
}
204
- } catch (e: Exception ) {
205
- throw RuntimeException (e)
206
182
}
207
183
}
208
184
// add the classes that we need
209
- addPeerClasses.accept (PlugGeneratorJavaExecable ::class .java)
185
+ addPeerClasses(PlugGeneratorJavaExecable ::class .java)
210
186
// add the gradle API
211
- addPeerClasses.accept (JavaExec ::class .java)
187
+ addPeerClasses(JavaExec ::class .java)
212
188
// Needed because of Gradle API classloader hierarchy changes with 2c5adc8 in Gradle 6.7+
213
- addPeerClasses.accept (FileCollection ::class .java)
189
+ addPeerClasses(FileCollection ::class .java)
214
190
return files
215
191
}
216
192
}
0 commit comments