JS: LibraryUtils refactoring
This commit is contained in:
@@ -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<File> {
|
FileUtil.processFilesRecursively(dir, object : Processor<File> {
|
||||||
override fun process(file: File): Boolean {
|
override fun process(file: File): Boolean {
|
||||||
var relativePath = FileUtil.getRelativePath(dir, file)
|
val relativePath = FileUtil.getRelativePath(dir, file) ?: throw IllegalArgumentException("relativePath should not be null " + dir + " " + file)
|
||||||
assert(relativePath != null) { "relativePath should not be null " + dir + " " + file }
|
if (file.isFile() && relativePath.endsWith(JS_EXT)) {
|
||||||
if (file.isFile() && relativePath!!.endsWith(JS_EXT)) {
|
val suggestedRelativePath = getSuggestedPath(relativePath)
|
||||||
relativePath = getSuggestedPath(relativePath)
|
if (suggestedRelativePath == null) return true
|
||||||
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())
|
|
||||||
}
|
|
||||||
|
|
||||||
|
action(file, suggestedRelativePath)
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun copyJsFilesFromZip(file: File, outputLibraryJsPath: String) {
|
private fun copyJsFilesFromDirectory(dir: File, outputLibraryJsPath: String) {
|
||||||
try {
|
traverseDirectoryWithReportingIOException(dir) {
|
||||||
traverseArchive(file, outputLibraryJsPath)
|
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<IOException>())
|
private fun traverseDirectoryWithReportingIOException(dir: File, action: (File, String) -> Unit) {
|
||||||
private fun traverseArchive(file: File, outputLibraryJsPath: String) {
|
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())
|
val zipFile = ZipFile(file.getPath())
|
||||||
try {
|
try {
|
||||||
val zipEntries = zipFile.entries()
|
val zipEntries = zipFile.entries()
|
||||||
@@ -154,8 +165,7 @@ public object LibraryUtils {
|
|||||||
|
|
||||||
val stream = zipFile.getInputStream(entry)
|
val stream = zipFile.getInputStream(entry)
|
||||||
val content = FileUtil.loadTextAndClose(stream)
|
val content = FileUtil.loadTextAndClose(stream)
|
||||||
val outputFile = File(outputLibraryJsPath, relativePath)
|
action(content, relativePath)
|
||||||
FileUtil.writeToFile(outputFile, content)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user