Skip to content

Commit 3abf751

Browse files
committed
Simplify thanks to Kotlin.
1 parent 2d954ff commit 3abf751

File tree

1 file changed

+25
-49
lines changed

1 file changed

+25
-49
lines changed

atplug-plugin-gradle/src/main/java/com/diffplug/atplug/tooling/gradle/PlugGenerateTask.kt

Lines changed: 25 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,15 @@ import com.diffplug.gradle.JRE
2222
import java.io.BufferedInputStream
2323
import java.io.BufferedOutputStream
2424
import java.io.File
25-
import java.io.IOException
2625
import java.nio.charset.StandardCharsets
2726
import java.nio.file.Files
28-
import java.util.*
29-
import java.util.function.Consumer
27+
import java.util.SortedMap
3028
import java.util.jar.Attributes
3129
import java.util.jar.Manifest
32-
import java.util.stream.Collectors
3330
import javax.inject.Inject
3431
import org.gradle.api.DefaultTask
35-
import org.gradle.api.Task
3632
import org.gradle.api.file.ConfigurableFileCollection
3733
import org.gradle.api.file.FileCollection
38-
import org.gradle.api.model.ObjectFactory
3934
import org.gradle.api.plugins.JavaPluginExtension
4035
import org.gradle.api.provider.Property
4136
import org.gradle.api.tasks.Classpath
@@ -57,8 +52,6 @@ abstract class PlugGenerateTask : DefaultTask() {
5752

5853
@get:Inject abstract val workerExecutor: WorkerExecutor
5954

60-
@get:Inject abstract val fS: ObjectFactory?
61-
6255
@get:InputFiles @get:Classpath abstract val jarsToLinkAgainst: ConfigurableFileCollection
6356

6457
@get:Internal var resourcesFolder: File? = null
@@ -70,9 +63,9 @@ abstract class PlugGenerateTask : DefaultTask() {
7063
@InputFiles var classesFolders: FileCollection? = null
7164

7265
init {
73-
this.outputs.upToDateWhen { unused: Task? ->
66+
this.outputs.upToDateWhen {
7467
val manifest = loadManifest()
75-
val componentsCmd = atplugComponents()
68+
val componentsCmd = atplugComponents(atplugInfFolder)
7669
val componentsActual = manifest.mainAttributes.getValue(PlugPlugin.SERVICE_COMPONENT)
7770
componentsActual == componentsCmd
7871
}
@@ -98,7 +91,7 @@ abstract class PlugGenerateTask : DefaultTask() {
9891

9992
// clean out the ATPLUG-INF folder, and put the map's content into the folder
10093
FileMisc.cleanDir(atplugInfFolder)
101-
for ((key, value) in result!!) {
94+
for ((key, value) in result) {
10295
val serviceFile = File(atplugInfFolder, key + PlugPlugin.DOT_JSON)
10396
Files.write(serviceFile.toPath(), value.toByteArray(StandardCharsets.UTF_8))
10497
}
@@ -107,7 +100,7 @@ abstract class PlugGenerateTask : DefaultTask() {
107100
// for tests to work
108101
// so we'll get a manifest (empty if necessary, but preferably we'll load what already exists)
109102
val manifest = loadManifest()
110-
val componentsCmd = atplugComponents()
103+
val componentsCmd = atplugComponents(atplugInfFolder)
111104
val componentsActual = manifest.mainAttributes.getValue(PlugPlugin.SERVICE_COMPONENT)
112105
if (componentsActual == componentsCmd) {
113106
return
@@ -131,29 +124,21 @@ abstract class PlugGenerateTask : DefaultTask() {
131124
private fun loadManifest(): Manifest {
132125
val manifest = Manifest()
133126
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)
140129
}
141130
}
142131
return manifest
143132
}
144133

145134
private fun saveManifest(manifest: Manifest) {
146135
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)
153138
}
154139
}
155140

156-
private fun generate(): SortedMap<String, String>? {
141+
private fun generate(): SortedMap<String, String> {
157142
val input =
158143
PlugGeneratorJavaExecable(ArrayList(classesFolders!!.files), jarsToLinkAgainst.files)
159144
return if (launcher.isPresent) {
@@ -164,53 +149,44 @@ abstract class PlugGenerateTask : DefaultTask() {
164149
options.setExecutable(launcher.get().executablePath)
165150
}
166151
}
167-
exec(workQueue, input).atplugInf
152+
exec(workQueue, input).atplugInf!!
168153
} else {
169154
input.run()
170-
input.atplugInf
155+
input.atplugInf!!
171156
}
172157
}
173158

174-
private fun atplugComponents(): String? {
175-
return atplugComponents(atplugInfFolder)
176-
}
177-
178159
companion object {
179160
fun atplugComponents(atplugInf: File): String? {
180-
return if (!atplugInf.isDirectory) {
181-
null
182-
} else {
161+
return if (!atplugInf.isDirectory) null
162+
else {
183163
val serviceComponents: MutableList<String> = ArrayList()
184164
for (file in FileMisc.list(atplugInf)) {
185165
if (file.name.endsWith(PlugPlugin.DOT_JSON)) {
186166
serviceComponents.add(PlugPlugin.ATPLUG_INF + file.name)
187167
}
188168
}
189-
Collections.sort(serviceComponents)
190-
serviceComponents.stream().collect(Collectors.joining(","))
169+
serviceComponents.sort()
170+
serviceComponents.joinToString(",")
191171
}
192172
}
193173

194174
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))
203181
}
204-
} catch (e: Exception) {
205-
throw RuntimeException(e)
206182
}
207183
}
208184
// add the classes that we need
209-
addPeerClasses.accept(PlugGeneratorJavaExecable::class.java)
185+
addPeerClasses(PlugGeneratorJavaExecable::class.java)
210186
// add the gradle API
211-
addPeerClasses.accept(JavaExec::class.java)
187+
addPeerClasses(JavaExec::class.java)
212188
// 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)
214190
return files
215191
}
216192
}

0 commit comments

Comments
 (0)