-
Notifications
You must be signed in to change notification settings - Fork 834
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
Create OtelVersion class at build time. #5365
Merged
jack-berg
merged 10 commits into
open-telemetry:main
from
breedx-splk:add_resource_class
Apr 15, 2023
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
800c143
fix warning
breedx-splk b274538
create plugin for version class
breedx-splk 804022f
wire up version class to Resource
breedx-splk 4abd4ea
checkstyle
breedx-splk 1905d57
jApiCmp
breedx-splk bb81bb3
move to internal package
breedx-splk 8e01e0d
jApiCmp
breedx-splk 3a09de5
Update buildSrc/src/main/kotlin/io/opentelemetry/gradle/OtelVersionCl…
breedx-splk e622d7f
Update buildSrc/src/main/kotlin/io/opentelemetry/gradle/OtelVersionCl…
breedx-splk 840df35
inline methods
breedx-splk 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
70 changes: 70 additions & 0 deletions
70
buildSrc/src/main/kotlin/io/opentelemetry/gradle/OtelVersionClassPlugin.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,70 @@ | ||
package io.opentelemetry.gradle | ||
|
||
import org.gradle.api.Plugin | ||
import org.gradle.api.Project | ||
import org.gradle.api.plugins.JavaPlugin | ||
import org.gradle.api.plugins.JavaPluginExtension | ||
import org.gradle.kotlin.dsl.the | ||
import java.io.File | ||
|
||
/** | ||
* This gradle plugin will define a new task called generateOtelVersionClass. | ||
* This task generates a Java source file that contains the project version | ||
* as a string constant. The "compileJava" task is updated to depend on | ||
* generateOtelVersionClass, and the project source set is updated to | ||
* include the new file. | ||
*/ | ||
class OtelVersionClassPlugin : Plugin<Project> { | ||
override fun apply(project: Project) { | ||
project.plugins.apply(JavaPlugin::class.java) | ||
|
||
project.task("generateOtelVersionClass") { | ||
doLast { | ||
writeFile(project) | ||
} | ||
} | ||
// Add dependency on this task | ||
project.tasks.getByName("compileJava") { | ||
dependsOn("generateOtelVersionClass") | ||
} | ||
// Add new source dir to the "main" source set | ||
val outDir = buildOutDir(project) | ||
val java = project.the<JavaPluginExtension>() | ||
java.sourceSets.getByName("main").java { | ||
srcDir(outDir) | ||
} | ||
} | ||
|
||
private fun writeFile(project: Project) { | ||
val group = "${project.group}".replace('.', '/') | ||
val projectName = project.name.replace('-', '/') | ||
val outDir = buildOutDir(project) | ||
val filename = "$group/$projectName/internal/OtelVersion.java" | ||
val outFile = File(outDir, filename) | ||
val packageName = "${project.group}.${project.name.replace('-', '.')}.internal" | ||
val classBody = getClassBody("${project.version}", packageName) | ||
|
||
outFile.parentFile.mkdirs() | ||
outFile.writeText(classBody) | ||
} | ||
|
||
private fun getClassBody(version: String, packageName: String): String { | ||
return """ | ||
package $packageName; | ||
|
||
import javax.annotation.Generated; | ||
|
||
/** Autogenerated class do not edit. */ | ||
@Generated("io.opentelemetry.gradle.OtelVersionClassPlugin") | ||
public final class OtelVersion { | ||
public static final String VERSION = "$version"; | ||
|
||
private OtelVersion() {} | ||
} | ||
""".trimIndent() | ||
} | ||
|
||
private fun buildOutDir(project: Project): File { | ||
return File(project.buildDir, "generated/sources/version/java/main") | ||
} | ||
} |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we do this for
:sdk:common
, should we do it everywhere?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I only wanted to do this in the one place where it's used. If there are other places that ever need to reference the class, then those modules can apply the plugin. I don't think we need it floating around every module. Feels bloaty.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That seems reasonable. Here are some other places we read the version:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And those will still continue work, I didn't remove the properties behavior. Are you suggesting that we should tackle those all in this same PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we have this pattern, it offers a pretty clean way of accessing the version. Adding a plugin to the gradle build is cleaner than copy / pasting the code to read from the resource.
Doesn't have to be in this PR, but if we like this pattern I think we should standardize on it.