Skip to content

Commit cece09e

Browse files
committed
Add compile subcommand to zipline-cli
Update Gradle plugin to invoke CLI to perform compilation. This allows compilation to not be subject to the version of Kotlin on the Gradle buildscript classpath.
1 parent d06cbb8 commit cece09e

File tree

28 files changed

+188
-64
lines changed

28 files changed

+188
-64
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
* New: "compile" subcommand in 'zipline-cli' compiles `.js` files to `.zipline` files.
6+
57

68
## [1.17.0] - 2024-08-28
79
[1.17.0]: https://github.com/cashapp/zipline/releases/tag/1.17.0

zipline-cli/build.gradle.kts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,19 @@ artifacts {
3838
kotlin {
3939
sourceSets {
4040
all {
41+
languageSettings.optIn("app.cash.zipline.EngineApi")
4142
languageSettings.optIn("kotlinx.serialization.ExperimentalSerializationApi")
4243
}
4344
}
4445
}
4546

4647
dependencies {
47-
api(projects.ziplineApiValidator)
48-
api(projects.ziplineLoader)
48+
implementation(projects.ziplineApiValidator)
49+
implementation(projects.ziplineBytecode)
50+
implementation(projects.ziplineLoader)
4951
implementation(libs.clikt)
5052
implementation(libs.okHttp.core)
53+
implementation(libs.kotlinx.serialization.json)
5154

5255
testImplementation(projects.ziplineLoaderTesting)
5356
testImplementation(libs.assertk)
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright (C) 2024 Cash App
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package app.cash.zipline.cli
17+
18+
import app.cash.zipline.loader.ManifestSigner
19+
import app.cash.zipline.loader.SignatureAlgorithmId
20+
import com.github.ajalt.clikt.core.CliktCommand
21+
import com.github.ajalt.clikt.parameters.options.associate
22+
import com.github.ajalt.clikt.parameters.options.convert
23+
import com.github.ajalt.clikt.parameters.options.flag
24+
import com.github.ajalt.clikt.parameters.options.help
25+
import com.github.ajalt.clikt.parameters.options.multiple
26+
import com.github.ajalt.clikt.parameters.options.option
27+
import com.github.ajalt.clikt.parameters.options.required
28+
import com.github.ajalt.clikt.parameters.types.file
29+
import okio.ByteString.Companion.decodeHex
30+
31+
internal class Compile : CliktCommand(
32+
name = "compile",
33+
help = "Compile .js files to .zipline files",
34+
) {
35+
private val inputDir by option("--input").file().required()
36+
.help("Directory from which .js files will be loaded.")
37+
38+
private val outputDir by option("--output").file().required()
39+
.help("Directory into which .zipline files will be written.")
40+
41+
private val addedFiles by option("--added").file().multiple()
42+
.help("Input files added since the last compile. This will trigger incremental compilation.")
43+
private val removedFiles by option("--removed").file().multiple()
44+
.help("Input files removed since the last compile. This will trigger incremental compilation.")
45+
private val modifiedFiles by option("--modified").file().multiple()
46+
.help("Input files modified since the last compile. This will trigger incremental compilation.")
47+
48+
private val mainFunction by option()
49+
private val mainModuleId by option()
50+
private val version by option()
51+
private val stripLineNumbers by option().flag()
52+
53+
private val signers by option("--signer")
54+
.convert {
55+
val (idName, name, keyHex) = it.split(':', limit = 3)
56+
val id = SignatureAlgorithmId.valueOf(idName)
57+
val key = keyHex.decodeHex()
58+
Triple(id, name, key)
59+
}
60+
.multiple()
61+
62+
private val metadata by option().associate()
63+
64+
override fun run() {
65+
val manifestSigner = ManifestSigner.Builder()
66+
.apply {
67+
for ((id, name, key) in signers) {
68+
add(id, name, key)
69+
}
70+
}
71+
.build()
72+
73+
val compiler = ZiplineCompiler(
74+
outputDir = outputDir,
75+
mainFunction = mainFunction,
76+
mainModuleId = mainModuleId,
77+
manifestSigner = manifestSigner,
78+
version = version,
79+
metadata = metadata,
80+
stripLineNumbers = stripLineNumbers,
81+
)
82+
83+
if (addedFiles.size or removedFiles.size or modifiedFiles.size != 0) {
84+
compiler.incrementalCompile(
85+
modifiedFiles = modifiedFiles,
86+
addedFiles = addedFiles,
87+
removedFiles = removedFiles,
88+
)
89+
} else {
90+
compiler.compile(inputDir)
91+
}
92+
}
93+
}

zipline-cli/src/main/kotlin/app/cash/zipline/cli/Main.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ fun main(vararg args: String) {
3030

3131
NoOpCliktCommand()
3232
.subcommands(
33+
Compile(),
3334
Download(),
3435
GenerateKeyPair(),
3536
ValidateZiplineApi(NAME_CHECK),

zipline-gradle-plugin/src/main/kotlin/app/cash/zipline/gradle/ZiplineCompiler.kt renamed to zipline-cli/src/main/kotlin/app/cash/zipline/cli/ZiplineCompiler.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package app.cash.zipline.gradle
16+
package app.cash.zipline.cli
1717

1818
import app.cash.zipline.QuickJs
1919
import app.cash.zipline.ZiplineManifest

zipline-gradle-plugin/src/test/kotlin/app/cash/zipline/gradle/ZiplineCompilerTest.kt renamed to zipline-cli/src/test/kotlin/app/cash/zipline/cli/ZiplineCompilerTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package app.cash.zipline.gradle
17+
package app.cash.zipline.cli
1818

1919
import app.cash.zipline.QuickJs
2020
import app.cash.zipline.ZiplineManifest
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)