-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Huge speedup by separating "find" from "generate" (#66)
- Loading branch information
Showing
8 changed files
with
170 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
atplug-plugin-gradle/src/main/java/com/diffplug/atplug/tooling/gradle/FindPlugsTask.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.diffplug.atplug.tooling.gradle | ||
|
||
import com.diffplug.atplug.tooling.PlugParser | ||
import java.io.File | ||
import org.gradle.api.DefaultTask | ||
import org.gradle.api.file.ConfigurableFileCollection | ||
import org.gradle.api.file.DirectoryProperty | ||
import org.gradle.api.tasks.* | ||
import org.gradle.work.* | ||
|
||
/** | ||
* Incrementally scans compiled classes for @Plug usage and writes discovered plug classes into an | ||
* output directory. | ||
*/ | ||
@CacheableTask | ||
abstract class FindPlugsTask : DefaultTask() { | ||
@get:CompileClasspath | ||
@get:Incremental | ||
@get:InputFiles | ||
abstract val classesFolders: ConfigurableFileCollection | ||
|
||
/** Directory where we will store discovered plugs. */ | ||
@get:OutputDirectory abstract val discoveredPlugsDir: DirectoryProperty | ||
|
||
@TaskAction | ||
fun findPlugs(inputChanges: InputChanges) { | ||
// If not incremental, clear everything and rescan | ||
if (!inputChanges.isIncremental) { | ||
discoveredPlugsDir.get().asFile.deleteRecursively() | ||
} | ||
|
||
// Make sure our output directory exists | ||
discoveredPlugsDir.get().asFile.mkdirs() | ||
|
||
// For each changed file in classesFolders, determine if it has @Plug | ||
val parser = PlugParser() | ||
for (change in inputChanges.getFileChanges(classesFolders)) { | ||
if (!change.file.name.endsWith(".class")) { | ||
continue | ||
} | ||
when (change.changeType) { | ||
ChangeType.REMOVED -> { | ||
// Remove old discovered data for this file | ||
removeOldMetadata(change.file) | ||
} | ||
ChangeType.ADDED, | ||
ChangeType.MODIFIED -> { | ||
parseAndWriteMetadata(parser, change.file) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun parseAndWriteMetadata(parser: PlugParser, classFile: File) { | ||
val plugToSocket = parser.parse(classFile) | ||
if (plugToSocket != null) { | ||
// For example: write a single line containing the discovered plug FQN | ||
val discoveredFile = discoveredPlugsDir.file(classFile.nameWithoutExtension).get().asFile | ||
if (discoveredFile.exists()) { | ||
val existing = discoveredFile.readText().split("|") | ||
check(existing[0] == plugToSocket.first) { | ||
"You need to rename one of these plugs because they have the same classfile name: ${existing[0]} and $plugToSocket" | ||
} | ||
} else { | ||
discoveredFile.parentFile.mkdirs() | ||
} | ||
discoveredFile.writeText(plugToSocket.let { "${it.first}|${it.second}" }) | ||
} else { | ||
// If previously discovered, remove it | ||
removeOldMetadata(classFile) | ||
} | ||
} | ||
|
||
private fun removeOldMetadata(classFile: File) { | ||
// Remove any discovered file for the old .class | ||
val possibleName = classFile.nameWithoutExtension | ||
val discoveredFile = discoveredPlugsDir.file(possibleName).get().asFile | ||
if (discoveredFile.exists()) { | ||
discoveredFile.delete() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.