-
Notifications
You must be signed in to change notification settings - Fork 333
Add gradle debug plugin #8879
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add gradle debug plugin #8879
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
108 changes: 108 additions & 0 deletions
108
buildSrc/src/main/kotlin/datadog.gradle-debug.gradle.kts
This file contains hidden or 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,108 @@ | ||
| /* | ||
| * Gradle debugging plugin for dd-trace-java builds. | ||
| */ | ||
|
|
||
| val ddGradleDebugEnabled = project.hasProperty("ddGradleDebug") | ||
| val logPath = rootProject.layout.buildDirectory.file("datadog.gradle-debug.log") | ||
|
|
||
| fun inferJdkFromJavaHome(javaHome: String?): String { | ||
| val effectiveJavaHome = javaHome ?: System.getenv("JAVA_HOME") | ||
| if (effectiveJavaHome == null) { | ||
| throw IllegalStateException("JAVA_HOME is not set") | ||
| } | ||
| val javaExecutable = File(effectiveJavaHome, "bin/java").absolutePath | ||
| return try { | ||
| val process = ProcessBuilder(javaExecutable, "-version") | ||
| .redirectErrorStream(true) | ||
| .start() | ||
| val output = process.inputStream.bufferedReader().readText() | ||
| val versionLine = output.lines().firstOrNull() ?: "" | ||
| val versionMatch = Regex("version\\s+\"([0-9._]+)\"").find(versionLine) | ||
| versionMatch?.let { | ||
| val version = it.groupValues[1] | ||
| when { | ||
| version.startsWith("1.") -> version.substring(2, 3) | ||
| else -> version.split('.').first() | ||
| } | ||
| } ?: "unknown" | ||
| } catch (e: Exception) { | ||
| "error: ${e.message}" | ||
| } | ||
| } | ||
|
|
||
| fun getJdkFromCompilerOptions(co: CompileOptions): String? { | ||
| if (co.isFork) { | ||
| val fo = co.forkOptions | ||
| val javaHome = fo.javaHome | ||
| if (javaHome != null) { | ||
| return inferJdkFromJavaHome(javaHome.toString()) | ||
| } | ||
| } | ||
| return null | ||
| } | ||
|
|
||
| fun printJdkForProjectTasks(project: Project, logFile: File) { | ||
| project.tasks.forEach { task -> | ||
| val data = mutableMapOf<String, String>() | ||
| data["task"] = task.path.toString() | ||
| if (task is JavaExec) { | ||
| val launcher = task.javaLauncher.get() | ||
| data["jdk"] = launcher.metadata.languageVersion.toString() | ||
| } else if (task is Javadoc) { | ||
| val tool = task.javadocTool.get() | ||
| data["jdk"] = tool.metadata.languageVersion.toString() | ||
| } else if (task is Test) { | ||
| val launcher = task.javaLauncher.get() | ||
| data["jdk"] = launcher.metadata.languageVersion.toString() | ||
| } else if (task is Exec) { | ||
| val java_home = task.environment.get("JAVA_HOME")?.toString() | ||
| data["jdk"] = inferJdkFromJavaHome(java_home) | ||
| } else if (task is JavaCompile) { | ||
| val compiler = task.javaCompiler.get() | ||
| data["jdk"] = compiler.metadata.languageVersion.toString() | ||
| val jdkFromJavaHome = getJdkFromCompilerOptions(task.options) | ||
| if (jdkFromJavaHome != null && jdkFromJavaHome != data["jdk"]) { | ||
| data["java_home"] = jdkFromJavaHome | ||
| } | ||
| } else if (task is GroovyCompile) { | ||
| val launcher = task.javaLauncher.get() | ||
| data["jdk"] = launcher.metadata.languageVersion.toString() | ||
| val jdkFromJavaHome = getJdkFromCompilerOptions(task.options) | ||
| if (jdkFromJavaHome != null && jdkFromJavaHome != data["jdk"]) { | ||
| data["java_home"] = jdkFromJavaHome | ||
| } | ||
| } else if (task is ScalaCompile) { | ||
| val launcher = task.javaLauncher.get() | ||
| data["jdk"] = launcher.metadata.languageVersion.toString() | ||
| val jdkFromJavaHome = getJdkFromCompilerOptions(task.options) | ||
| if (jdkFromJavaHome != null && jdkFromJavaHome != data["jdk"]) { | ||
| data["java_home"] = jdkFromJavaHome | ||
| } | ||
| } else { | ||
| data["jdk"] = "unknown" | ||
| } | ||
| val json = data.entries.joinToString(prefix = "{", postfix = "}") { (k, v) -> "\"$k\":\"$v\"" } | ||
| logFile.appendText("$json\n") | ||
| } | ||
| } | ||
|
|
||
| class DebugBuildListener : org.gradle.BuildListener { | ||
| override fun settingsEvaluated(settings: Settings) = Unit | ||
|
|
||
| override fun projectsLoaded(gradle: Gradle) = Unit | ||
|
|
||
| override fun buildFinished(result: BuildResult) = Unit | ||
|
|
||
| override fun projectsEvaluated(gradle: Gradle) { | ||
| val logFile = logPath.get().asFile | ||
| logFile.writeText("") | ||
| gradle.rootProject.allprojects.forEach { project -> | ||
| printJdkForProjectTasks(project, logFile) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (ddGradleDebugEnabled) { | ||
| logger.lifecycle("datadog.gradle-debug plugin is enabled") | ||
| gradle.addListener(DebugBuildListener()) | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.