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 #48

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
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, windows-latest]
runs-on: ${{ matrix.platform }}
steps:
- uses: actions/[email protected]
- uses: actions/[email protected]
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ org.gradle.jvmargs=--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAME
--add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
org.gradle.configuration-cache=true
org.gradle.caching=true
version="0.5.1"
group="com.ryandens"
version=0.5.1
group=com.ryandens
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 All @@ -25,7 +26,7 @@ class JavaagentPluginFunctionalTest {

@BeforeTest
fun beforeEach() {
functionalTestDir = File("build/functionalTest")
functionalTestDir = File("build", "functionalTest")
functionalTestDir.mkdirs()
helloWorldDir = File(functionalTestDir, "hello-world")
}
Expand Down Expand Up @@ -120,14 +121,16 @@ class JavaagentPluginFunctionalTest {
val result = runBuild(listOf("--configuration-cache", "build", "installDist", "execStartScript"))

// verify the distribution was created properly
val applicationDistribution = File(functionalTestDir, "hello-world/build/distributions/hello-world.tar")
val applicationDistribution =
File(functionalTestDir, "hello-world${File.separator}build${File.separator}distributions${File.separator}hello-world.tar")
assertTrue(applicationDistribution.exists())

// verify the expected text was injected into the start script
val expectedDefaultJavaOpts = """
DEFAULT_JVM_OPTS="-javaagent:${"$"}APP_HOME/agent-libs/simple-agent.jar -Xmx256m"
"""
val applicationDistributionScript = File(functionalTestDir, "hello-world/build/scripts/hello-world")
val applicationDistributionScript =
File(functionalTestDir, "hello-world${File.separator}build${File.separator}scripts${File.separator}hello-world")
assertTrue(applicationDistributionScript.readText().contains(expectedDefaultJavaOpts))

/*
Expand All @@ -140,7 +143,12 @@ DEFAULT_JVM_OPTS="-javaagent:${"$"}APP_HOME/lib/simple-agent.jar -Xmx256m"
*/

// verify the agent was added to the /lib/ dir of the distribution
assertTrue(File(functionalTestDir, "hello-world/build/install/hello-world/agent-libs/simple-agent.jar").exists())
assertTrue(
File(
functionalTestDir,
"hello-world/build/install/hello-world/agent-libs/simple-agent.jar".replace("/", File.separator),
).exists(),
)

// Verify the result
assertTrue(result.output.contains("Hello World!"))
Expand Down Expand Up @@ -180,21 +188,29 @@ DEFAULT_JVM_OPTS="-javaagent:${"$"}APP_HOME/lib/simple-agent.jar -Xmx256m"
val result = runBuild(listOf("build", "installDist", "execStartScript"))

// verify the distribution was created properly
val applicationDistribution = File(functionalTestDir, "hello-world/build/distributions/hello-world.tar")
val applicationDistribution =
File(functionalTestDir, "hello-world${File.separator}build${File.separator}distributions${File.separator}hello-world.tar")
assertTrue(applicationDistribution.exists())

val applicationDistributionScript = File(functionalTestDir, "hello-world/build/scripts/hello-world")
val applicationDistributionScript =
File(functionalTestDir, "hello-world${File.separator}build${File.separator}scripts${File.separator}hello-world")
assertTrue(applicationDistributionScript.readText().contains("""DEFAULT_JVM_OPTS="-Xmx256m"""))

assertFalse(File(functionalTestDir, "hello-world/build/install/hello-world/agent-libs/").exists())
assertFalse(
File(
functionalTestDir,
"hello-world${File.separator}build${File.separator}install${File.separator}hello-world${File.separator}agent-libs/",
).exists(),
)
assertTrue(result.output.contains("Hello World!"))
}

private fun createJavaagentProject(dependencies: String) {
File("src/functionalTest/resources/hello-world-project/").copyRecursively(helloWorldDir)
val helloWorldDir = File(functionalTestDir, "hello-world")
Paths.get("src", "functionalTest", "resources", "hello-world-project").toFile().copyRecursively(helloWorldDir)
val simpleAgentTestDir = File(functionalTestDir, "simple-agent")
val simpleAgentBuildScript = simpleAgentTestDir.resolve("build.gradle.kts")
File("../simple-agent/").copyRecursively(simpleAgentTestDir)
Paths.get("..", "simple-agent").toFile().copyRecursively(simpleAgentTestDir)
simpleAgentBuildScript.writeText(
simpleAgentBuildScript.readText().replace(
"id(\"com.ryandens.java-conventions\")\n",
Expand Down Expand Up @@ -232,11 +248,10 @@ DEFAULT_JVM_OPTS="-javaagent:${"$"}APP_HOME/lib/simple-agent.jar -Xmx256m"
}

task execStartScript(type: Exec) {
inputs.files(fileTree('${helloWorldDir.canonicalPath}/build/install/') {
builtBy tasks.named('installDist')
})
workingDir '${helloWorldDir.canonicalPath}/build/install/hello-world/bin/'
commandLine './hello-world'
dependsOn('installDist')
inputs.files(layout.buildDirectory.dir('install'))
workingDir(layout.buildDirectory.dir('install').map { it.dir('hello-world').dir('bin') })
commandLine '.${File.separator}hello-world'
environment JAVA_HOME: "${Jvm.current().getJavaHome()}"
}

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 @@ -50,15 +49,9 @@ class JavaagentApplicationDistributionPlugin : Plugin<Project>, JavaagentPlugin
.plus(it.defaultJvmOpts ?: listOf())
it.inputs.files(javaagentConfiguration)
// 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,18 +11,19 @@ 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 @@ -42,7 +42,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 Down Expand Up @@ -71,7 +75,9 @@ 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
16 changes: 16 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,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),
}
Loading