Reformat K2JSDce.kt

This commit is contained in:
Anton Bannykh
2018-01-12 14:16:35 +03:00
parent ce5b1acfa7
commit 5d6d321fb2
@@ -38,8 +38,7 @@ class K2JSDce : CLITool<K2JSDceArguments>() {
val files = collectInputFiles(baseDir, arg, messageCollector) val files = collectInputFiles(baseDir, arg, messageCollector)
if (files != null) { if (files != null) {
files files
} } else {
else {
hasErrors = true hasErrors = true
emptyList() emptyList()
} }
@@ -56,8 +55,9 @@ class K2JSDce : CLITool<K2JSDceArguments>() {
for (file in files) { for (file in files) {
existingFiles[file.outputPath]?.let { existingFiles[file.outputPath]?.let {
messageCollector.report( messageCollector.report(
CompilerMessageSeverity.ERROR, CompilerMessageSeverity.ERROR,
"duplicate target file will be created for '${file.resource.name}' and '${it.resource.name}'") "duplicate target file will be created for '${file.resource.name}' and '${it.resource.name}'"
)
return ExitCode.COMPILATION_ERROR return ExitCode.COMPILATION_ERROR
} }
existingFiles[file.outputPath] = file existingFiles[file.outputPath] = file
@@ -69,8 +69,7 @@ class K2JSDce : CLITool<K2JSDceArguments>() {
return if (!arguments.devMode) { return if (!arguments.devMode) {
performDce(files, arguments, messageCollector) performDce(files, arguments, messageCollector)
} } else {
else {
copyFiles(files) copyFiles(files)
ExitCode.OK ExitCode.OK
} }
@@ -95,8 +94,10 @@ class K2JSDce : CLITool<K2JSDceArguments>() {
val reachabilitySeverity = if (arguments.printReachabilityInfo) CompilerMessageSeverity.INFO else CompilerMessageSeverity.LOGGING val reachabilitySeverity = if (arguments.printReachabilityInfo) CompilerMessageSeverity.INFO else CompilerMessageSeverity.LOGGING
messageCollector.report(reachabilitySeverity, "") messageCollector.report(reachabilitySeverity, "")
for (node in nodes.extractRoots()) { for (node in nodes.extractRoots()) {
printTree(node, { messageCollector.report(reachabilitySeverity, it) }, printTree(
printNestedMembers = false, showLocations = true) node, { messageCollector.report(reachabilitySeverity, it) },
printNestedMembers = false, showLocations = true
)
} }
return ExitCode.OK return ExitCode.OK
@@ -123,14 +124,13 @@ class K2JSDce : CLITool<K2JSDceArguments>() {
FileOutputStream(targetFile).use { output -> FileOutputStream(targetFile).use { output ->
input.copyTo(output) input.copyTo(output)
} }
} }
} }
private fun mapSourcePaths(inputFile: File, targetFile: File): Boolean { private fun mapSourcePaths(inputFile: File, targetFile: File): Boolean {
val json = try { val json = try {
InputStreamReader(FileInputStream(inputFile), "UTF-8").use { parseJson(it) } InputStreamReader(FileInputStream(inputFile), "UTF-8").use { parseJson(it) }
} } catch (e: JsonSyntaxException) {
catch (e: JsonSyntaxException) {
return false return false
} }
@@ -145,12 +145,10 @@ class K2JSDce : CLITool<K2JSDceArguments>() {
if (result != null) { if (result != null) {
if (File(targetFile.parentFile, result).exists()) { if (File(targetFile.parentFile, result).exists()) {
result result
} } else {
else {
it it
} }
} } else {
else {
it it
} }
} }
@@ -180,8 +178,10 @@ class K2JSDce : CLITool<K2JSDceArguments>() {
collectInputFilesFromZip(baseDir, fileName) collectInputFilesFromZip(baseDir, fileName)
} }
else -> { else -> {
messageCollector.report(CompilerMessageSeverity.ERROR, messageCollector.report(
"invalid file name '$fileName'; must end either with '.js', '.zip' or '.jar'") CompilerMessageSeverity.ERROR,
"invalid file name '$fileName'; must end either with '.js', '.zip' or '.jar'"
)
null null
} }
} }
@@ -197,41 +197,47 @@ class K2JSDce : CLITool<K2JSDceArguments>() {
val moduleName = getModuleNameFromPath(path) val moduleName = getModuleNameFromPath(path)
val pathToSourceMapCandidate = "$path.map" val pathToSourceMapCandidate = "$path.map"
val pathToSourceMap = if (File(pathToSourceMapCandidate).exists()) pathToSourceMapCandidate else null val pathToSourceMap = if (File(pathToSourceMapCandidate).exists()) pathToSourceMapCandidate else null
return InputFile(InputResource.file(path), pathToSourceMap?.let { InputResource.file(it) }, return InputFile(
File(baseDir, "$moduleName.js").absolutePath, moduleName) InputResource.file(path), pathToSourceMap?.let { InputResource.file(it) },
File(baseDir, "$moduleName.js").absolutePath, moduleName
)
} }
private fun collectInputFilesFromZip(baseDir: File, path: String): List<InputFile> { private fun collectInputFilesFromZip(baseDir: File, path: String): List<InputFile> {
return ZipFile(path).use { zipFile -> return ZipFile(path).use { zipFile ->
zipFile.entries().asSequence() zipFile.entries().asSequence()
.filter { !it.isDirectory } .filter { !it.isDirectory }
.filter { it.name.endsWith(".js") } .filter { it.name.endsWith(".js") }
.filter { zipFile.getEntry(it.name.metaJs()) != null } .filter { zipFile.getEntry(it.name.metaJs()) != null }
.distinctBy { it.name } .distinctBy { it.name }
.map { entry -> .map { entry ->
val moduleName = getModuleNameFromPath(entry.name) val moduleName = getModuleNameFromPath(entry.name)
val pathToSourceMapCandidate = "${entry.name}.map" val pathToSourceMapCandidate = "${entry.name}.map"
val pathToSourceMap = if (zipFile.getEntry(pathToSourceMapCandidate) != null) pathToSourceMapCandidate else null val pathToSourceMap = if (zipFile.getEntry(pathToSourceMapCandidate) != null) pathToSourceMapCandidate else null
InputFile(InputResource.zipFile(path, entry.name), pathToSourceMap?.let { InputResource.zipFile(path, it) }, InputFile(
File(baseDir, "$moduleName.js").absolutePath, moduleName) InputResource.zipFile(path, entry.name), pathToSourceMap?.let { InputResource.zipFile(path, it) },
} File(baseDir, "$moduleName.js").absolutePath, moduleName
.toList() )
}
.toList()
} }
} }
private fun collectInputFilesFromDirectory(baseDir: File, path: String): List<InputFile> { private fun collectInputFilesFromDirectory(baseDir: File, path: String): List<InputFile> {
return File(path).walkTopDown().asSequence() return File(path).walkTopDown().asSequence()
.filter { !it.isDirectory } .filter { !it.isDirectory }
.filter { it.name.endsWith(".js") } .filter { it.name.endsWith(".js") }
.filter { File(it.path.metaJs()).exists() } .filter { File(it.path.metaJs()).exists() }
.map { entry -> .map { entry ->
val moduleName = getModuleNameFromPath(entry.name) val moduleName = getModuleNameFromPath(entry.name)
val pathToSourceMapCandidate = "${entry.path}.map" val pathToSourceMapCandidate = "${entry.path}.map"
val pathToSourceMap = if (File(pathToSourceMapCandidate).exists()) pathToSourceMapCandidate else null val pathToSourceMap = if (File(pathToSourceMapCandidate).exists()) pathToSourceMapCandidate else null
InputFile(InputResource.file(entry.path), pathToSourceMap?.let { InputResource.file(it) }, InputFile(
File(baseDir, "$moduleName.js").absolutePath, moduleName) InputResource.file(entry.path), pathToSourceMap?.let { InputResource.file(it) },
} File(baseDir, "$moduleName.js").absolutePath, moduleName
.toList() )
}
.toList()
} }
private fun String.metaJs() = removeSuffix(".js") + ".meta.js" private fun String.metaJs() = removeSuffix(".js") + ".meta.js"