Skip to content

Commit

Permalink
🎨 format
Browse files Browse the repository at this point in the history
  • Loading branch information
ryandens committed Jan 8, 2025
1 parent efcc5ef commit c35d8e3
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ import javax.inject.Inject
@CacheableTask
abstract class MergeServiceFiles
@Inject
constructor(private val fileSystemOperations: FileSystemOperations) :
DefaultTask() {
constructor(
private val fileSystemOperations: FileSystemOperations,
) : DefaultTask() {
@get:InputFiles
@get:PathSensitive(PathSensitivity.RELATIVE)
abstract val inputFiles: ConfigurableFileCollection
Expand All @@ -41,15 +42,17 @@ abstract class MergeServiceFiles
@TaskAction
fun execute() {
val serviceFiles: Set<File> =
inputFiles.asFileTree.matching { f -> f.include("META-INF/services/**").include("inst/META-INF/services/**") }
inputFiles.asFileTree
.matching { f -> f.include("META-INF/services/**").include("inst/META-INF/services/**") }
.files
val outputDir: File = outputDirectory.get().dir("META-INF/services").asFile
fileSystemOperations.delete {
it.delete(outputDir)
}
outputDir.mkdirs()
val perService: Map<String, List<File>> =
serviceFiles.stream()
serviceFiles
.stream()
.collect(Collectors.groupingBy(File::getName))
for ((serviceType, files) in perService) {
val mergedServiceFile = File(outputDir, serviceType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ class JavaagentJibExtensionFunctionalTest {
assertTrue(File(functionalTestDir, JIB_IMAGE).exists())

FileInputStream(File(functionalTestDir, JIB_IMAGE)).use { fis ->
ArchiveStreamFactory().createArchiveInputStream<ArchiveInputStream<TarArchiveEntry>>(ArchiveStreamFactory.TAR, fis).use {
ais ->
ArchiveStreamFactory().createArchiveInputStream<ArchiveInputStream<TarArchiveEntry>>(ArchiveStreamFactory.TAR, fis).use { ais ->
var entry = ais.nextEntry
while (entry != null) {
if ("config.json" == entry.name) {
Expand All @@ -76,8 +75,7 @@ class JavaagentJibExtensionFunctionalTest {
// verify the agent was added to entrypoint
assertTrue(File(functionalTestDir, JIB_IMAGE).exists())
FileInputStream(File(functionalTestDir, JIB_IMAGE)).use { fis ->
ArchiveStreamFactory().createArchiveInputStream<ArchiveInputStream<TarArchiveEntry>>(ArchiveStreamFactory.TAR, fis).use {
ais ->
ArchiveStreamFactory().createArchiveInputStream<ArchiveInputStream<TarArchiveEntry>>(ArchiveStreamFactory.TAR, fis).use { ais ->
var entry = ais.nextEntry
while (entry != null) {
if ("config.json" == entry.name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,23 @@ import org.gradle.api.tasks.application.CreateStartScripts
* the [ApplicationPlugin] to include javaagents and configure the [ApplicationPlugin.TASK_START_SCRIPTS_NAME] to
* automatically include the `-javaagent` flag when running the distribution.
*/
class JavaagentApplicationDistributionPlugin : Plugin<Project>, JavaagentPlugin {
class JavaagentApplicationDistributionPlugin :
Plugin<Project>,
JavaagentPlugin {
/**
* Destination directory for dependencies as specified by the [ApplicationPlugin]
*/
private val destinationDirectory = "agent-libs"

override fun dependentProjectPlugins(): Collection<Class<out Plugin<Project>>> {
return setOf(ApplicationPlugin::class.java)
}
override fun dependentProjectPlugins(): Collection<Class<out Plugin<Project>>> = setOf(ApplicationPlugin::class.java)

override fun applyAfterJavaagentSetup(
project: Project,
javaagentConfiguration: NamedDomainObjectProvider<Configuration>,
) {
project.extensions.getByType(DistributionContainer::class.java).named(DistributionPlugin.MAIN_DISTRIBUTION_NAME)
project.extensions
.getByType(DistributionContainer::class.java)
.named(DistributionPlugin.MAIN_DISTRIBUTION_NAME)
.configure { distribution ->
distribution.contents { copy ->
copy.from(javaagentConfiguration) {
Expand All @@ -52,8 +54,7 @@ class JavaagentApplicationDistributionPlugin : Plugin<Project>, JavaagentPlugin
// custom start script generator that replaces the placeholder
it.unixStartScriptGenerator =
JavaagentAwareStartScriptGenerator(
javaagentConfiguration.map {
configuration ->
javaagentConfiguration.map { configuration ->
configuration.files
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import org.gradle.api.tasks.JavaExec
/**
* Configures a project that leverages the [ApplicationPlugin] to easily integrate a javaagent into its tasks and outputs.
*/
class JavaagentApplicationRunPlugin : Plugin<Project>, JavaagentPlugin {
override fun dependentProjectPlugins(): Collection<Class<out Plugin<Project>>> {
return setOf(ApplicationPlugin::class.java)
}
class JavaagentApplicationRunPlugin :
Plugin<Project>,
JavaagentPlugin {
override fun dependentProjectPlugins(): Collection<Class<out Plugin<Project>>> = setOf(ApplicationPlugin::class.java)

override fun applyAfterJavaagentSetup(
project: Project,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ class JavaagentAwareStartScriptGenerator(
inner.generateScript(details, Fake(destination, javaagentConfiguration))
}

private class FakeTransformer(private val inner: StartScriptTemplateBindingFactory) :
Transformer<MutableMap<String, String>, JavaAppStartScriptGenerationDetails> by inner {
private class FakeTransformer(
private val inner: StartScriptTemplateBindingFactory,
) : Transformer<MutableMap<String, String>, JavaAppStartScriptGenerationDetails> by inner {
override fun transform(`in`: JavaAppStartScriptGenerationDetails): MutableMap<String, String> {
val result = inner.transform(`in`)
val jvmOpts = result["defaultJvmOpts"] ?: ""
Expand All @@ -42,7 +43,10 @@ class JavaagentAwareStartScriptGenerator(
}
}

private class Fake(private val inner: Writer, private val javaagentFiles: Provider<Set<File>>) : Writer() {
private class Fake(
private val inner: Writer,
private val javaagentFiles: Provider<Set<File>>,
) : Writer() {
override fun close() {
inner.close()
}
Expand All @@ -64,10 +68,11 @@ class JavaagentAwareStartScriptGenerator(
val replace =
if (files.isEmpty()) {
// handles case gracefully where there is a trailing space that needs to be removed if ogther default jvm opts are supplied
str.replace(
"-javaagent:COM_RYANDENS_JAVAAGENTS_PLACEHOLDER.jar ",
"",
).replace("-javaagent:COM_RYANDENS_JAVAAGENTS_PLACEHOLDER.jar", "")
str
.replace(
"-javaagent:COM_RYANDENS_JAVAAGENTS_PLACEHOLDER.jar ",
"",
).replace("-javaagent:COM_RYANDENS_JAVAAGENTS_PLACEHOLDER.jar", "")
} else {
str.replace(
"-javaagent:COM_RYANDENS_JAVAAGENTS_PLACEHOLDER.jar",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ import java.util.Optional
* with the javaagent in it and a modified [ContainerBuildPlan.entrypoint] that specifies the `javaagent` flag.
*/
@ExperimentalStdlibApi
class JavaagentJibExtension : JibGradlePluginExtension<Void>, JavaagentPlugin {
override fun getExtraConfigType(): Optional<Class<Void>> {
return Optional.empty()
}
class JavaagentJibExtension :
JibGradlePluginExtension<Void>,
JavaagentPlugin {
override fun getExtraConfigType(): Optional<Class<Void>> = Optional.empty()

override fun extendContainerBuildPlan(
buildPlan: ContainerBuildPlan?,
Expand All @@ -41,9 +41,13 @@ class JavaagentJibExtension : JibGradlePluginExtension<Void>, JavaagentPlugin {

val localAgentPaths =
checkNotNull(
gradleData?.project?.plugins?.getPlugin(
JavaagentJibExtension::class.java,
)?.javaagentPathProvider?.invoke(),
gradleData
?.project
?.plugins
?.getPlugin(
JavaagentJibExtension::class.java,
)?.javaagentPathProvider
?.invoke(),
)

val planBuilder = buildPlan.toBuilder()
Expand All @@ -61,7 +65,12 @@ class JavaagentJibExtension : JibGradlePluginExtension<Void>, JavaagentPlugin {
FileEntriesLayer.DEFAULT_MODIFICATION_TIME,
)
}
val javaagentLayer = FileEntriesLayer.builder().setName("javaagent").setEntries(javaagentFileEntries).build()
val javaagentLayer =
FileEntriesLayer
.builder()
.setName("javaagent")
.setEntries(javaagentFileEntries)
.build()
val layers =
buildList<LayerObject> {
add(javaagentLayer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ interface JavaagentPlugin : Plugin<Project> {
* Plugins that the implementing class expects to be applied before [applyAfterJavaagentSetup] runs. By default,
* this returns an empty [Collection]
*/
fun dependentProjectPlugins(): Collection<Class<out Plugin<Project>>> {
return emptySet()
}
fun dependentProjectPlugins(): Collection<Class<out Plugin<Project>>> = emptySet()

/**
* Initial setup for any plugin that wants to configure a javaagent for a project, followed by delegating to the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import org.gradle.api.plugins.JavaPlugin
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.testing.Test

class JavaagentTestPlugin : Plugin<Project>, JavaagentPlugin {
class JavaagentTestPlugin :
Plugin<Project>,
JavaagentPlugin {
companion object {
const val CONFIGURATION_NAME = "testJavaagent"
}

override fun dependentProjectPlugins(): Collection<Class<out Plugin<Project>>> {
return setOf(JavaPlugin::class.java)
}
override fun dependentProjectPlugins(): Collection<Class<out Plugin<Project>>> = setOf(JavaPlugin::class.java)

override fun applyAfterJavaagentSetup(
project: Project,
Expand All @@ -38,8 +38,7 @@ class JavaagentTestPlugin : Plugin<Project>, JavaagentPlugin {
if (enabled.get()) {
JavaForkOptionsConfigurer.configureJavaForkOptions(
it,
javaagentTestConfiguration.map {
configuration ->
javaagentTestConfiguration.map { configuration ->
configuration.files
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,26 @@ class JavaagentTestPluginTest {
}

@Test fun `plugin adds jvm argument provider for javaagent`() {
assertEquals(1, project.tasks.named("test", org.gradle.api.tasks.testing.Test::class.java).get().jvmArgumentProviders.size)
assertEquals(
1,
project.tasks
.named("test", org.gradle.api.tasks.testing.Test::class.java)
.get()
.jvmArgumentProviders.size,
)
}

@Test fun `plugin javaagent test configuration can be disabled`() {
project.extensions.configure(JavaagentTestExtension::class.java) {
it.enabled.set(false)
}

assertEquals(0, project.tasks.named("test", org.gradle.api.tasks.testing.Test::class.java).get().jvmArgumentProviders.size)
assertEquals(
0,
project.tasks
.named("test", org.gradle.api.tasks.testing.Test::class.java)
.get()
.jvmArgumentProviders.size,
)
}
}

0 comments on commit c35d8e3

Please sign in to comment.