Skip to content

Commit

Permalink
Wire up the ZiplineDumpTask to the Zipline Gradle plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
squarejesse committed Jun 20, 2023
1 parent 78bf3b3 commit 45a1ae1
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 3 deletions.
1 change: 1 addition & 0 deletions zipline-gradle-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ dependencies {
implementation(kotlin("gradle-plugin-api"))
implementation(projects.zipline)
implementation(projects.ziplineBytecode)
implementation(projects.ziplineKotlinPlugin)
implementation(projects.ziplineLoader)
implementation(libs.http4k.core)
implementation(libs.http4k.server.jetty)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright (C) 2021 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package app.cash.zipline.gradle

import app.cash.zipline.api.compatibility.ActualApiHasProblems
import app.cash.zipline.api.compatibility.ExpectedApiIsUpToDate
import app.cash.zipline.api.compatibility.ExpectedApiRequiresUpdates
import app.cash.zipline.api.compatibility.makeApiCompatibilityDecision
import app.cash.zipline.api.fir.readFirZiplineApi
import app.cash.zipline.api.toml.TomlZiplineApi
import app.cash.zipline.api.toml.readZiplineApi
import app.cash.zipline.api.toml.writeZiplineApi
import javax.inject.Inject
import okio.buffer
import okio.sink
import okio.source
import org.gradle.api.DefaultTask
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.internal.file.FileCollectionFactory
import org.gradle.api.tasks.Classpath
import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction

@Suppress("unused") // Public API for Gradle plugin users.
abstract class ZiplineApiDumpTask @Inject constructor(
fileCollectionFactory: FileCollectionFactory,
) : DefaultTask() {

@get:OutputFile
abstract val ziplineApiFile: RegularFileProperty

@get:InputFiles
internal val sourcepath = fileCollectionFactory.configurableFiles("sourcepath")

@get:Classpath
internal val classpath = fileCollectionFactory.configurableFiles("classpath")

@TaskAction
fun task() {
val tomlFile = ziplineApiFile.get().asFile

val expectedZiplineApi = when {
tomlFile.exists() -> tomlFile.source().buffer().use { it.readZiplineApi() }
else -> TomlZiplineApi(listOf())
}

val actualZiplineApi = readFirZiplineApi(sourcepath.files, classpath.files)

when (val decision = makeApiCompatibilityDecision(expectedZiplineApi, actualZiplineApi)) {
is ActualApiHasProblems -> {
throw Exception(
"""
|Zipline API has compatibility problems:
| ${decision.messages.joinToString(separator = "\n").replace("\n", "\n ") }
""".trimMargin(),
)
}

is ExpectedApiRequiresUpdates -> {
tomlFile.sink().buffer().use { it.writeZiplineApi(decision.updatedApi) }
}

ExpectedApiIsUpToDate -> {
// Do nothing.
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import org.jetbrains.kotlin.gradle.plugin.SubpluginOption
import org.jetbrains.kotlin.gradle.targets.js.ir.JsIrBinary
import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrTarget
import org.jetbrains.kotlin.gradle.targets.js.webpack.KotlinWebpack
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.jetbrains.kotlin.gradle.tasks.KotlinCompileTool
import org.slf4j.LoggerFactory

@Suppress("unused") // Created reflectively by Gradle.
Expand Down Expand Up @@ -74,6 +76,12 @@ class ZiplinePlugin : KotlinCompilerPluginSupportPlugin {
it.dependsOn(kotlinWebpack)
}
}

target.tasks.withType(KotlinCompile::class.java) { kotlinCompile ->
if (kotlinCompile.name == "compileKotlinJvm") {
registerZiplineApiDumpTask(target, kotlinCompile)
}
}
}

private fun registerCompileZiplineTask(
Expand Down Expand Up @@ -105,6 +113,24 @@ class ZiplinePlugin : KotlinCompilerPluginSupportPlugin {
return ziplineCompileTask
}

private fun registerZiplineApiDumpTask(
project: Project,
kotlinCompileTool: KotlinCompileTool,
): TaskProvider<ZiplineApiDumpTask> {
val result = project.tasks.register(
"ziplineApiDump",
ZiplineApiDumpTask::class.java,
)

result.configure {
it.ziplineApiFile.set(project.file("api/zipline-api.toml"))
it.sourcepath.setFrom(kotlinCompileTool.sources)
it.classpath.setFrom(kotlinCompileTool.libraries)
}

return result
}

override fun applyToCompilation(
kotlinCompilation: KotlinCompilation<*>,
): Provider<List<SubpluginOption>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,36 @@ class ZiplinePluginTest {
)
}

@Test
fun ziplineApiDumpCreatesTomlFile() {
val projectDir = File("src/test/projects/basic")
val ziplineApiToml = projectDir.resolve("lib/api/zipline-api.toml")

try {
val taskName = ":lib:ziplineApiDump"
val result = createRunner(projectDir, "clean", taskName).build()
assertThat(SUCCESS_OUTCOMES)
.contains(result.task(taskName)!!.outcome)

assertThat(ziplineApiToml.readText()).isEqualTo(
"""
|[app.cash.zipline.tests.GreetService]
|
|functions = [
| # fun close(): kotlin.Unit
| "moYx+T3e",
|
| # fun greet(kotlin.String): kotlin.String
| "ipvircui",
|]
|
""".trimMargin(),
)
} finally {
ziplineApiToml.delete()
}
}

private fun createRunner(projectDir: File, vararg taskNames: String): GradleRunner {
val gradleRoot = projectDir.resolve("gradle").also { it.mkdir() }
File("../gradle/wrapper").copyRecursively(gradleRoot.resolve("wrapper"), true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@
*/
package app.cash.zipline.kotlin

import okio.ByteString.Companion.encodeUtf8
import java.security.MessageDigest
import java.util.Base64

fun String.signatureHash(): String =
encodeUtf8().sha256().substring(0, 6).base64() // In base64, 6 bytes takes 8 chars.
/** This would be more compact with Okio, but adding that dependency is tricky. */
fun String.signatureHash(): String {
val signatureUtf8 = encodeToByteArray()
val sha256 = MessageDigest.getInstance("SHA-256").digest(signatureUtf8)
val sha256Prefix = sha256.sliceArray(0 until 6)
return String(Base64.getEncoder().encode(sha256Prefix))
}

0 comments on commit 45a1ae1

Please sign in to comment.