Skip to content
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

Add support for Windows #40

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text eol=lf
5 changes: 4 additions & 1 deletion .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ jobs:

build:
name: "Javaagent Gradle Plugin Validation"
runs-on: ubuntu-latest
strategy:
matrix:
platform: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.platform }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-java@v3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.TaskOutcome
import java.io.File
import java.nio.file.Paths
import kotlin.test.AfterTest
import kotlin.test.BeforeTest
import kotlin.test.Test
Expand Down Expand Up @@ -163,8 +164,8 @@ DEFAULT_JVM_OPTS="-javaagent:${"$"}APP_HOME/lib/simple-agent.jar -Xmx256m"

private fun createJavaagentProject(dependencies: String) {
val helloWorldDir = File(functionalTestDir, "hello-world")
File("src/functionalTest/resources/hello-world-project/").copyRecursively(helloWorldDir)
File("../simple-agent/").copyRecursively(File(functionalTestDir, "simple-agent"))
Paths.get("src", "functionalTest", "resources", "hello-world-project").toFile().copyRecursively(helloWorldDir)
Paths.get("..", "simple-agent").toFile().copyRecursively(File(functionalTestDir, "simple-agent"))

functionalTestDir.resolve("settings.gradle").writeText(
"""
Expand Down
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),
}