aboutsummaryrefslogtreecommitdiff
path: root/tools/java/com/google/devtools/kotlin/SourceJarZipper.kt
blob: c2d34d8726535f14e6c2eae4414bfc0565f5e6ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
 * * Copyright 2022 Google LLC. All rights reserved.
 *
 * 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 com.google.devtools.kotlin

import java.io.BufferedInputStream
import java.io.BufferedOutputStream
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.nio.file.StandardCopyOption
import java.util.zip.ZipEntry
import java.util.zip.ZipInputStream
import java.util.zip.ZipOutputStream
import kotlin.system.exitProcess
import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Model.CommandSpec
import picocli.CommandLine.Option
import picocli.CommandLine.ParameterException
import picocli.CommandLine.Parameters
import picocli.CommandLine.Spec

@Command(
  name = "source-jar-zipper",
  subcommands = [Unzip::class, Zip::class, ZipResources::class],
  description = ["A tool to pack and unpack srcjar files, and to zip resource files"],
)
class SourceJarZipper : Runnable {
  @Spec private lateinit var spec: CommandSpec
  override fun run() {
    throw ParameterException(spec.commandLine(), "Specify a command: zip, zip_resources or unzip")
  }
}

fun main(args: Array<String>) {
  val exitCode = CommandLine(SourceJarZipper()).execute(*args)
  exitProcess(exitCode)
}

/**
 * Checks for duplicates and adds an entry into [errors] if one is found, otherwise adds a pair of
 * [zipPath] and [sourcePath] to the receiver
 *
 * @param[zipPath] relative path inside the jar, built either from package name (e.g. package
 *   com.google.foo -> com/google/foo/FileName.kt) or by resolving the file name relative to the
 *   directory it came from (e.g. foo/bar/1/2.txt came from foo/bar -> 1/2.txt)
 * @param[sourcePath] full path of file into its file system
 * @param[errors] list of strings describing catched errors
 * @receiver a mutable map of path to path, where keys are relative paths of files inside the
 *   resulting .jar, and values are full paths of files
 */
fun MutableMap<Path, Path>.checkForDuplicatesAndSetFilePathToPathInsideJar(
  zipPath: Path,
  sourcePath: Path,
  errors: MutableList<String>,
) {
  val duplicatedSourcePath: Path? = this[zipPath]
  if (duplicatedSourcePath == null) {
    this[zipPath] = sourcePath
  } else {
    errors.add(
      "${sourcePath} has the same path inside .jar as ${duplicatedSourcePath}! " +
        "If it is intended behavior rename one or both of them."
    )
  }
}

private fun clearSingletonEmptyPath(list: MutableList<Path>) {
  if (list.size == 1 && list[0].toString() == "") {
    list.clear()
  }
}

fun MutableMap<Path, Path>.writeToStream(
  zipper: ZipOutputStream,
  prefix: String = "",
) {
  for ((zipPath, sourcePath) in this) {
    BufferedInputStream(Files.newInputStream(sourcePath)).use { inputStream ->
      val entry = ZipEntry(Paths.get(prefix).resolve(zipPath).toString())
      entry.time = 0
      zipper.putNextEntry(entry)
      inputStream.copyTo(zipper, bufferSize = 1024)
    }
  }
}

@Command(name = "zip", description = ["Zip source files into a source jar file"])
class Zip : Runnable {

  @Parameters(index = "0", paramLabel = "outputJar", description = ["Output jar"])
  lateinit var outputJar: Path

  @Option(
    names = ["-i", "--ignore_not_allowed_files"],
    description = ["Ignore not .kt, .java or invalid file paths without raising an exception"],
  )
  var ignoreNotAllowedFiles = false

  @Option(
    names = ["--kotlin_srcs"],
    split = ",",
    description = ["Kotlin source files"],
  )
  val kotlinSrcs = mutableListOf<Path>()

  @Option(
    names = ["--common_srcs"],
    split = ",",
    description = ["Common source files"],
  )
  val commonSrcs = mutableListOf<Path>()

  companion object {
    const val PACKAGE_SPACE = "package "
    val PACKAGE_NAME_REGEX = "[a-zA-Z][a-zA-Z0-9_]*(\\.[a-zA-Z0-9_]+)*".toRegex()
  }

  override fun run() {
    clearSingletonEmptyPath(kotlinSrcs)
    clearSingletonEmptyPath(commonSrcs)

    // Validating files and getting paths for resulting .jar in one cycle
    // for each _srcs list
    val ktZipPathToSourcePath = mutableMapOf<Path, Path>()
    val commonZipPathToSourcePath = mutableMapOf<Path, Path>()
    val errors = mutableListOf<String>()

    fun Path.getPackagePath(): Path {
      this.toFile().bufferedReader().use { stream ->
        while (true) {
          val line = stream.readLine() ?: return this.fileName

          if (line.startsWith(PACKAGE_SPACE)) {
            // Kotlin allows usage of reserved words in package names framing them
            // with backquote symbol "`"
            val packageName =
              line.substring(PACKAGE_SPACE.length).trim().replace(";", "").replace("`", "")
            if (!PACKAGE_NAME_REGEX.matches(packageName)) {
              errors.add("${this} contains an invalid package name")
              return this.fileName
            }
            return Paths.get(packageName.replace(".", "/")).resolve(this.fileName)
          }
        }
      }
    }

    fun Path.validateFile(): Boolean {
      when {
        !Files.isRegularFile(this) -> {
          if (!ignoreNotAllowedFiles) errors.add("${this} is not a file")
          return false
        }
        !this.toString().endsWith(".kt") && !this.toString().endsWith(".java") -> {
          if (!ignoreNotAllowedFiles) errors.add("${this} is not a Kotlin file")
          return false
        }
        else -> return true
      }
    }

    for (sourcePath in kotlinSrcs) {
      if (sourcePath.validateFile()) {
        ktZipPathToSourcePath.checkForDuplicatesAndSetFilePathToPathInsideJar(
          sourcePath.getPackagePath(),
          sourcePath,
          errors,
        )
      }
    }

    for (sourcePath in commonSrcs) {
      if (sourcePath.validateFile()) {
        commonZipPathToSourcePath.checkForDuplicatesAndSetFilePathToPathInsideJar(
          sourcePath.getPackagePath(),
          sourcePath,
          errors,
        )
      }
    }

    check(errors.isEmpty()) { errors.joinToString("\n") }

    ZipOutputStream(BufferedOutputStream(Files.newOutputStream(outputJar))).use { zipper ->
      commonZipPathToSourcePath.writeToStream(zipper, "common-srcs")
      ktZipPathToSourcePath.writeToStream(zipper)
    }
  }
}

@Command(name = "unzip", description = ["Unzip a jar archive into a specified directory"])
class Unzip : Runnable {

  @Parameters(index = "0", paramLabel = "inputJar", description = ["Jar archive to unzip"])
  lateinit var inputJar: Path

  @Parameters(index = "1", paramLabel = "outputDir", description = ["Output directory"])
  lateinit var outputDir: Path

  override fun run() {
    ZipInputStream(Files.newInputStream(inputJar)).use { unzipper ->
      while (true) {
        val zipEntry: ZipEntry? = unzipper.nextEntry
        if (zipEntry == null) return

        val entryName = zipEntry.name
        check(!entryName.contains("./")) { "Cannot unpack srcjar with relative path ${entryName}" }

        if (!entryName.endsWith(".kt") && !entryName.endsWith(".java")) continue

        val entryPath = outputDir.resolve(entryName)
        if (!Files.exists(entryPath.parent)) Files.createDirectories(entryPath.parent)
        Files.copy(unzipper, entryPath, StandardCopyOption.REPLACE_EXISTING)
      }
    }
  }
}

@Command(name = "zip_resources", description = ["Zip resources"])
class ZipResources : Runnable {

  @Parameters(index = "0", paramLabel = "outputJar", description = ["Output jar"])
  lateinit var outputJar: Path

  @Option(
    names = ["--input_dirs"],
    split = ",",
    description = ["Input files directories"],
    required = true,
  )
  val inputDirs = mutableListOf<Path>()

  override fun run() {
    clearSingletonEmptyPath(inputDirs)

    val filePathToOutputPath = mutableMapOf<Path, Path>()
    val errors = mutableListOf<String>()

    // inputDirs has filter checking if the dir exists, because some empty dirs generated by blaze
    // may not exist from Kotlin compiler's side. It turned out to be safer to apply a filter then
    // to rely that generated directories are always directories, not just path names
    for (dirPath in inputDirs.filter { curDirPath -> Files.exists(curDirPath) }) {
      if (!Files.isDirectory(dirPath)) {
        errors.add("${dirPath} is not a directory")
      } else {
        Files.walk(dirPath)
          .filter { fileOrDir -> !Files.isDirectory(fileOrDir) }
          .forEach { filePath ->
            filePathToOutputPath.checkForDuplicatesAndSetFilePathToPathInsideJar(
              dirPath.relativize(filePath),
              filePath,
              errors
            )
          }
      }
    }

    check(errors.isEmpty()) { errors.joinToString("\n") }

    ZipOutputStream(BufferedOutputStream(Files.newOutputStream(outputJar))).use { zipper ->
      filePathToOutputPath.writeToStream(zipper)
    }
  }
}