@@ -2,7 +2,6 @@ package com.diffplug.atplug.tooling.gradle
2
2
3
3
import com.diffplug.atplug.tooling.PlugParser
4
4
import java.io.File
5
- import java.nio.charset.StandardCharsets
6
5
import org.gradle.api.DefaultTask
7
6
import org.gradle.api.file.ConfigurableFileCollection
8
7
import org.gradle.api.file.DirectoryProperty
@@ -20,7 +19,7 @@ abstract class FindPlugsTask : DefaultTask() {
20
19
@get:InputFiles
21
20
abstract val classesFolders: ConfigurableFileCollection
22
21
23
- /* * Directory where we will store discovered plugs in .txt files, etc . */
22
+ /* * Directory where we will store discovered plugs. */
24
23
@get:OutputDirectory abstract val discoveredPlugsDir: DirectoryProperty
25
24
26
25
@TaskAction
@@ -34,6 +33,7 @@ abstract class FindPlugsTask : DefaultTask() {
34
33
discoveredPlugsDir.get().asFile.mkdirs()
35
34
36
35
// For each changed file in classesFolders, determine if it has @Plug
36
+ val parser = PlugParser ()
37
37
for (change in inputChanges.getFileChanges(classesFolders)) {
38
38
if (! change.file.name.endsWith(" .class" )) {
39
39
continue
@@ -45,21 +45,26 @@ abstract class FindPlugsTask : DefaultTask() {
45
45
}
46
46
ChangeType .ADDED ,
47
47
ChangeType .MODIFIED -> {
48
- parseAndWriteMetadata(change.file)
48
+ parseAndWriteMetadata(parser, change.file)
49
49
}
50
50
}
51
51
}
52
52
}
53
53
54
- private fun parseAndWriteMetadata (classFile : File ) {
55
- val parser = PlugParser ()
56
- parser.parse(classFile)
57
- if (parser.hasPlug()) {
54
+ private fun parseAndWriteMetadata (parser : PlugParser , classFile : File ) {
55
+ val plugToSocket = parser.parse(classFile)
56
+ if (plugToSocket != null ) {
58
57
// For example: write a single line containing the discovered plug FQN
59
- val discoveredFile = discoveredPlugsDir.file(parser.plugClassName + " .txt" ).get().asFile
60
- discoveredFile.parentFile.mkdirs()
61
- discoveredFile.writeText(
62
- parser.plugClassName!! + " |" + parser.socketClassName!! , StandardCharsets .UTF_8 )
58
+ val discoveredFile = discoveredPlugsDir.file(classFile.nameWithoutExtension).get().asFile
59
+ if (discoveredFile.exists()) {
60
+ val existing = discoveredFile.readText().split(" |" )
61
+ check(existing[0 ] == plugToSocket.first) {
62
+ " You need to rename one of these plugs because they have the same classfile name: ${existing[0 ]} and $plugToSocket "
63
+ }
64
+ } else {
65
+ discoveredFile.parentFile.mkdirs()
66
+ }
67
+ discoveredFile.writeText(plugToSocket.let { " ${it.first} |${it.second} " })
63
68
} else {
64
69
// If previously discovered, remove it
65
70
removeOldMetadata(classFile)
@@ -68,7 +73,7 @@ abstract class FindPlugsTask : DefaultTask() {
68
73
69
74
private fun removeOldMetadata (classFile : File ) {
70
75
// Remove any discovered file for the old .class
71
- val possibleName = classFile.nameWithoutExtension + " .txt "
76
+ val possibleName = classFile.nameWithoutExtension
72
77
val discoveredFile = discoveredPlugsDir.file(possibleName).get().asFile
73
78
if (discoveredFile.exists()) {
74
79
discoveredFile.delete()
0 commit comments