Skip to content

Commit

Permalink
Add support for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
punya committed Jul 19, 2023
1 parent 3fb6ce2 commit bef5470
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.distribution.DistributionContainer
import org.gradle.api.distribution.plugins.DistributionPlugin
import org.gradle.api.internal.plugins.WindowsStartScriptGenerator
import org.gradle.api.plugins.ApplicationPlugin
import org.gradle.api.tasks.application.CreateStartScripts

Expand Down Expand Up @@ -49,9 +48,9 @@ class JavaagentApplicationDistributionPlugin : Plugin<Project>, JavaagentPlugin
it.defaultJvmOpts = listOf("-javaagent:COM_RYANDENS_JAVAAGENTS_PLACEHOLDER.jar")
.plus(it.defaultJvmOpts ?: listOf())
// custom start script generator that replaces the placeholder
it.unixStartScriptGenerator = JavaagentAwareStartScriptGenerator(javaagentConfiguration.map { configuration -> configuration.files })
// TODO build support for windows
it.windowsStartScriptGenerator = WindowsStartScriptGenerator()
val agentFiles = javaagentConfiguration.map { configuration -> configuration.files }
it.unixStartScriptGenerator = JavaagentAwareStartScriptGenerator(agentFiles, Platform.UNIX)
it.windowsStartScriptGenerator = JavaagentAwareStartScriptGenerator(agentFiles, Platform.WINDOWS)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.ryandens.javaagent
import org.gradle.api.Transformer
import org.gradle.api.internal.plugins.DefaultTemplateBasedStartScriptGenerator
import org.gradle.api.internal.plugins.StartScriptTemplateBindingFactory
import org.gradle.api.internal.plugins.UnixStartScriptGenerator
import org.gradle.api.provider.Provider
import org.gradle.jvm.application.scripts.JavaAppStartScriptGenerationDetails
import org.gradle.jvm.application.scripts.ScriptGenerator
Expand All @@ -12,15 +11,16 @@ import java.io.Writer

class JavaagentAwareStartScriptGenerator(
private val javaagentConfiguration: Provider<Set<File>>,
private val platform: Platform,
private val inner: ScriptGenerator = DefaultTemplateBasedStartScriptGenerator(
"\n",
FakeTransformer(StartScriptTemplateBindingFactory.unix()),
UnixStartScriptGenerator().template,
platform.lineSeparator,
FakeTransformer(platform.templateBindingFactory),
platform.template,
),
) : ScriptGenerator {

override fun generateScript(details: JavaAppStartScriptGenerationDetails, destination: Writer) {
inner.generateScript(details, Fake(destination, javaagentConfiguration))
inner.generateScript(details, Fake(destination, javaagentConfiguration, platform.pathSeparator))
}

private class FakeTransformer(private val inner: StartScriptTemplateBindingFactory) :
Expand All @@ -39,7 +39,11 @@ 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>>,
private val pathSeparator: String,
) : Writer() {
override fun close() {
inner.close()
}
Expand All @@ -60,7 +64,7 @@ class JavaagentAwareStartScriptGenerator(
} else {
str.replace(
"-javaagent:COM_RYANDENS_JAVAAGENTS_PLACEHOLDER.jar",
javaagentFiles.get().joinToString(" ") { jar -> "-javaagent:\$APP_HOME/agent-libs/${jar.name}" },
javaagentFiles.get().joinToString(" ") { jar -> "-javaagent:\$APP_HOME${pathSeparator}agent-libs${pathSeparator}${jar.name}" },
)
}
super.write(replace)
Expand Down
17 changes: 17 additions & 0 deletions plugin/src/main/kotlin/com/ryandens/javaagent/Platform.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.ryandens.javaagent

import org.gradle.api.internal.plugins.StartScriptTemplateBindingFactory
import org.gradle.api.internal.plugins.UnixStartScriptGenerator
import org.gradle.api.internal.plugins.WindowsStartScriptGenerator
import org.gradle.api.resources.TextResource

enum class Platform(
val lineSeparator: String,
val pathSeparator: String,
val templateBindingFactory: StartScriptTemplateBindingFactory,
val template: TextResource,
) {

UNIX("\n", "/", StartScriptTemplateBindingFactory.unix(), UnixStartScriptGenerator().template),
WINDOWS("\r\n", "\\", StartScriptTemplateBindingFactory.windows(), WindowsStartScriptGenerator().template),
}

0 comments on commit bef5470

Please sign in to comment.