Skip to content

Commit

Permalink
TS-35591 Rewrite logback system property names
Browse files Browse the repository at this point in the history
  • Loading branch information
DreierF committed Sep 22, 2023
1 parent db32f07 commit 5ee0805
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 6 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ jobs:
distribution: 'temurin'
java-version: 11
- name: Build with Gradle
# Workaround for a bug in the Gradle shadow plugin that attempt to transform artifacts too early
run: ./gradlew jar && ./gradlew build
run: ./gradlew build
- name: Upload Release Assets
if: startsWith(github.ref, 'refs/tags/v')
uses: svenstaro/upload-release-action@v2
Expand Down Expand Up @@ -83,6 +82,6 @@ jobs:
uses: docker/build-push-action@v5
with:
file: 'agent/src/docker/Dockerfile'
push: ${{ startsWith(github.ref, 'refs/tags/v') }}
push: ${{ startsWith(github.ref, 'refs/tags/') }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
3 changes: 1 addition & 2 deletions agent/src/docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
FROM openjdk:8 as build
ADD . /src
WORKDIR /src
# Workaround for a bug in the Gradle shadow plugin that attempt to transform artifacts too early
RUN ./gradlew :teamscale-client:jar :report-generator:jar && ./gradlew :agent:assemble
RUN ./gradlew :agent:assemble

# --- production image ---

Expand Down
41 changes: 40 additions & 1 deletion buildSrc/src/main/kotlin/JulManagerFreeTransform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ abstract class JulManagerFreeTransform : TransformAction<TransformParameters.Non
if (jarEntry.name.endsWith(".class")) {
val classReader = ClassReader(input)
val classWriter = ClassWriter(classReader, 0)
val classVisitor = IdentifyStaticGetLoggerCalls(classWriter)
val stringReplacer = StringValueReplacer(classWriter) {
if (it.startsWith("logback.")) "shadow.$it" else it
}
val classVisitor = IdentifyStaticGetLoggerCalls(stringReplacer)
classReader.accept(classVisitor, ClassReader.EXPAND_FRAMES)
val rewrittenClass = classWriter.toByteArray()

Expand Down Expand Up @@ -132,3 +135,39 @@ private class StaticGetLoggerReplacer(
)
}
}

/** Replaces string constants within a class file using the given replace function. */
private class StringValueReplacer(
classVisitor: ClassVisitor?,
private val stringReplacer: (string: String) -> String,
) : ClassVisitor(Opcodes.ASM9, classVisitor) {

private fun transformConstant(value: Any?): Any? {
return if (value is String) stringReplacer(value) else value
}

override fun visitField(
access: Int,
name: String?,
descriptor: String?,
signature: String?,
value: Any?
): FieldVisitor {
return super.visitField(access, name, descriptor, signature, transformConstant(value))
}

override fun visitMethod(
access: Int,
name: String,
descriptor: String,
signature: String?,
exceptions: Array<String>?
): MethodVisitor {
val mv = super.visitMethod(access, name, descriptor, signature, exceptions)
return object : MethodVisitor(Opcodes.ASM9, mv) {
override fun visitLdcInsn(cst: Any?) {
super.visitLdcInsn(transformConstant(cst))
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import com.github.jengelman.gradle.plugins.shadow.tasks.ConfigureShadowRelocatio
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar

plugins {
java
id("com.github.johnrengelman.shadow")
}

Expand All @@ -27,3 +28,18 @@ tasks.named<ShadowJar>("shadowJar") {
attributes["Multi-Release"] = "true"
}
}

// Defer the resolution of 'runtimeClasspath'. This is an issue in the shadow
// plugin that it automatically accesses the files in 'runtimeClasspath' while
// Gradle is building the task graph. The lines below work around that.
// https://github.com/johnrengelman/shadow/issues/882
tasks.withType<ShadowJar> {
inputs.files(project.configurations.runtimeClasspath)
configurations = emptyList()
doFirst { configurations = listOf(project.configurations.runtimeClasspath.get()) }
}

tasks.withType<ConfigureShadowRelocation> {
inputs.files(project.configurations.runtimeClasspath)
doFirst { target.configurations = listOf(project.configurations.runtimeClasspath.get()) }
}

0 comments on commit 5ee0805

Please sign in to comment.