From 7a96ca1dfbbf3d66e4341c66808985552f75ad81 Mon Sep 17 00:00:00 2001 From: Alexey Tsvetkov Date: Fri, 6 Mar 2015 15:41:27 +0300 Subject: [PATCH] JS: LibraryUtils refactoring --- .../jetbrains/kotlin/utils/LibraryUtils.kt | 60 +++++++++++-------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/compiler/util/src/org/jetbrains/kotlin/utils/LibraryUtils.kt b/compiler/util/src/org/jetbrains/kotlin/utils/LibraryUtils.kt index 00b9202a1be..ee70e0a7501 100644 --- a/compiler/util/src/org/jetbrains/kotlin/utils/LibraryUtils.kt +++ b/compiler/util/src/org/jetbrains/kotlin/utils/LibraryUtils.kt @@ -107,41 +107,52 @@ public object LibraryUtils { } } - private fun copyJsFilesFromDirectory(dir: File, outputLibraryJsPath: String) { + private fun processDirectory(dir: File, action: (File, String) -> Unit) { FileUtil.processFilesRecursively(dir, object : Processor { override fun process(file: File): Boolean { - var relativePath = FileUtil.getRelativePath(dir, file) - assert(relativePath != null) { "relativePath should not be null " + dir + " " + file } - if (file.isFile() && relativePath!!.endsWith(JS_EXT)) { - relativePath = getSuggestedPath(relativePath) - if (relativePath == null) return true - - try { - val copyFile = File(outputLibraryJsPath, relativePath) - FileUtil.copy(file, copyFile) - } - catch (ex: IOException) { - LOG.error("Could not copy " + relativePath + " from " + dir + ": " + ex.getMessage()) - } + val relativePath = FileUtil.getRelativePath(dir, file) ?: throw IllegalArgumentException("relativePath should not be null " + dir + " " + file) + if (file.isFile() && relativePath.endsWith(JS_EXT)) { + val suggestedRelativePath = getSuggestedPath(relativePath) + if (suggestedRelativePath == null) return true + action(file, suggestedRelativePath) } return true } }) } - private fun copyJsFilesFromZip(file: File, outputLibraryJsPath: String) { - try { - traverseArchive(file, outputLibraryJsPath) + private fun copyJsFilesFromDirectory(dir: File, outputLibraryJsPath: String) { + traverseDirectoryWithReportingIOException(dir) { + file, relativePath -> FileUtil.copy(file, File(outputLibraryJsPath, relativePath)) } - catch (ex: IOException) { - LOG.error("Could not extract javascript files from " + file.getName() + ": " + ex.getMessage()) - } - } - throws(javaClass()) - private fun traverseArchive(file: File, outputLibraryJsPath: String) { + private fun traverseDirectoryWithReportingIOException(dir: File, action: (File, String) -> Unit) { + try { + processDirectory(dir, action) + } + catch (ex: IOException) { + LOG.error("Could not read files from directory ${dir.getName()}: ${ex.getMessage()}") + } + } + + private fun copyJsFilesFromZip(file: File, outputLibraryJsPath: String) { + traverseArchiveWithReportingIOException(file) { content, relativePath -> + FileUtil.writeToFile(File(outputLibraryJsPath, relativePath), content) + } + } + + private fun traverseArchiveWithReportingIOException(file: File, action: (String, String) -> Unit) { + try { + traverseArchive(file, action) + } + catch (ex: IOException) { + LOG.error("Could not extract files from archive ${file.getName()}: ${ex.getMessage()}") + } + } + + private fun traverseArchive(file: File, action: (String, String) -> Unit) { val zipFile = ZipFile(file.getPath()) try { val zipEntries = zipFile.entries() @@ -154,8 +165,7 @@ public object LibraryUtils { val stream = zipFile.getInputStream(entry) val content = FileUtil.loadTextAndClose(stream) - val outputFile = File(outputLibraryJsPath, relativePath) - FileUtil.writeToFile(outputFile, content) + action(content, relativePath) } } }