diff --git a/plugin/src/main/kotlin/com/ryandens/javaagent/JavaagentApplicationDistributionPlugin.kt b/plugin/src/main/kotlin/com/ryandens/javaagent/JavaagentApplicationDistributionPlugin.kt index 965c776..173bbe8 100644 --- a/plugin/src/main/kotlin/com/ryandens/javaagent/JavaagentApplicationDistributionPlugin.kt +++ b/plugin/src/main/kotlin/com/ryandens/javaagent/JavaagentApplicationDistributionPlugin.kt @@ -49,9 +49,9 @@ class JavaagentApplicationDistributionPlugin : Plugin, 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) } } } diff --git a/plugin/src/main/kotlin/com/ryandens/javaagent/JavaagentAwareStartScriptGenerator.kt b/plugin/src/main/kotlin/com/ryandens/javaagent/JavaagentAwareStartScriptGenerator.kt index a5f3ea9..8d8fc33 100644 --- a/plugin/src/main/kotlin/com/ryandens/javaagent/JavaagentAwareStartScriptGenerator.kt +++ b/plugin/src/main/kotlin/com/ryandens/javaagent/JavaagentAwareStartScriptGenerator.kt @@ -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 @@ -12,15 +11,16 @@ import java.io.Writer class JavaagentAwareStartScriptGenerator( private val javaagentConfiguration: Provider>, + 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) : @@ -39,7 +39,11 @@ class JavaagentAwareStartScriptGenerator( } } - private class Fake(private val inner: Writer, private val javaagentFiles: Provider>) : Writer() { + private class Fake( + private val inner: Writer, + private val javaagentFiles: Provider>, + private val pathSeparator: String + ) : Writer() { override fun close() { inner.close() } @@ -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) diff --git a/plugin/src/main/kotlin/com/ryandens/javaagent/Platform.kt b/plugin/src/main/kotlin/com/ryandens/javaagent/Platform.kt new file mode 100644 index 0000000..80d789c --- /dev/null +++ b/plugin/src/main/kotlin/com/ryandens/javaagent/Platform.kt @@ -0,0 +1,16 @@ +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) +}