|
| 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 | +} |
0 commit comments