Skip to content

Instantly share code, notes, and snippets.

@drewbrokke
Forked from gamerson/pluginInfo-task.gradle
Last active August 11, 2023 23:28
Show Gist options
  • Save drewbrokke/6ce2762f9ca3c71690cc98003bbcded5 to your computer and use it in GitHub Desktop.
Save drewbrokke/6ce2762f9ca3c71690cc98003bbcded5 to your computer and use it in GitHub Desktop.
Script to print info for all plugins applied to a given project
import java.nio.file.FileSystem
import java.nio.file.FileSystems
import java.nio.file.FileVisitResult
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.nio.file.SimpleFileVisitor
import java.nio.file.attribute.BasicFileAttributes
allprojects {
task pluginInfo
pluginInfo {
doLast {
def appliedClasses = []
def classesIds = [:]
Set classpath = []
plugins.each {
appliedClasses << it.class.name
it.class.classLoader.getURLs().each { classpath.add(it)}
def jarUrl = it.class.protectionDomain.codeSource.location
if (jarUrl == null) {
println "WARNING: no codeSource location found for class: " + it.class.name
return
}
def pluginPath = Paths.get(jarUrl.toURI())
def pluginUri = URI.create("jar:" + pluginPath.toUri())
def jarFs
try {
jarFs = FileSystems.getFileSystem(pluginUri)
}
catch (Throwable t) {
}
if (jarFs == null) {
jarFs = FileSystems.newFileSystem(pluginUri, Collections.emptyMap())
}
def gradlePlugins = jarFs.getPath("/META-INF/gradle-plugins")
Files.walkFileTree(gradlePlugins, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
if (path.toString().endsWith(".properties")) {
Properties properties = new Properties()
properties.load(jarFs.provider.newInputStream(path))
String implClass = properties.get("implementation-class")
if (implClass != null) {
classesIds.put(implClass, path.fileName.toString().replaceAll(".properties\$",""))
}
}
return FileVisitResult.CONTINUE;
}
});
}
println "Plugin classpath..."
classpath.each { println it }
println "Applied ids..."
def appliedIds = classesIds.findAll { appliedClasses.contains(it.key) }
appliedIds.each { println "$it.value = $it.key" }
println "\nTotal plugins applied = ${appliedIds.size()}"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment