From 9c35fbf91a7124fe5776035d8152445e84032580 Mon Sep 17 00:00:00 2001 From: Svyatoslav Scherbina Date: Sat, 27 May 2017 10:17:47 +0300 Subject: [PATCH] Use .zip files on Windows in DependencyDownloader --- .../src/main/kotlin/DependencyDownloader.kt | 23 +++++++-- tools/helpers/src/main/kotlin/Unzip.kt | 51 +++++++++++++++++++ 2 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 tools/helpers/src/main/kotlin/Unzip.kt diff --git a/tools/helpers/src/main/kotlin/DependencyDownloader.kt b/tools/helpers/src/main/kotlin/DependencyDownloader.kt index 97252563ce9..a4bc0f84123 100644 --- a/tools/helpers/src/main/kotlin/DependencyDownloader.kt +++ b/tools/helpers/src/main/kotlin/DependencyDownloader.kt @@ -20,7 +20,6 @@ import java.io.* import java.net.URL import java.nio.file.Files import java.nio.file.StandardCopyOption -import java.util.Properties import kotlin.concurrent.thread // TODO: Try to use some dependency management system (Ivy?) @@ -57,6 +56,14 @@ class DependencyDownloader(dependenciesRoot: File, val dependenciesUrl: String, } } + private val useZip = System.getProperty("os.name").startsWith("Windows") + + private val archiveExtension = if (useZip) { + "zip" + } else { + "tar.gz" + } + private fun processDependency(dependency: String) { val depDir = File(dependenciesDirectory, dependency) val depName = depDir.name @@ -74,7 +81,7 @@ class DependencyDownloader(dependenciesRoot: File, val dependenciesUrl: String, isInfoShown = true } - val archive = File(cacheDirectory.canonicalPath, "$depName.tar.gz") + val archive = File(cacheDirectory.canonicalPath, "$depName.$archiveExtension") if (!archive.exists()) { download(depName, archive) } @@ -82,7 +89,15 @@ class DependencyDownloader(dependenciesRoot: File, val dependenciesUrl: String, extractedDependencies.addWithSave(depName) } - private fun extract(tarGz: File, target: File) { + private fun extract(archive: File, target: File) { + if (useZip) { + archive.toPath().unzipTo(target.toPath()) + } else { + extractTarGz(archive, target) + } + } + + private fun extractTarGz(tarGz: File, target: File) { println("Extract dependency: ${tarGz.canonicalPath} in ${target.canonicalPath}") val tarProcess = ProcessBuilder().apply { command("tar", "-xzf", "${tarGz.canonicalPath}") @@ -113,7 +128,7 @@ class DependencyDownloader(dependenciesRoot: File, val dependenciesUrl: String, private fun download(dependencyName: String, outputFile: File) { val tmpFile = File("${outputFile.canonicalPath}.part") - val url = URL("$dependenciesUrl/$dependencyName.tar.gz") + val url = URL("$dependenciesUrl/$dependencyName.$archiveExtension") val connection = url.openConnection() val totalBytes = connection.contentLengthLong diff --git a/tools/helpers/src/main/kotlin/Unzip.kt b/tools/helpers/src/main/kotlin/Unzip.kt new file mode 100644 index 00000000000..b0d85f6a92c --- /dev/null +++ b/tools/helpers/src/main/kotlin/Unzip.kt @@ -0,0 +1,51 @@ +/* + * Copyright 2010-2017 JetBrains s.r.o. + * + * 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 org.jetbrains.kotlin.konan + +import java.net.URI +import java.nio.file.FileSystems +import java.nio.file.Files +import java.nio.file.Path +import java.nio.file.StandardCopyOption + +fun Path.unzipTo(directory: Path) { + val zipUri = URI.create("jar:" + this.toUri()) + FileSystems.newFileSystem(zipUri, emptyMap(), null).use { zipfs -> + val zipPath = zipfs.getPath("/") + zipPath.recursiveCopyTo(directory) + } +} + +fun Path.recursiveCopyTo(destPath: Path) { + val sourcePath = this + Files.walk(sourcePath).forEach { oldPath -> + + val relative = sourcePath.relativize(oldPath) + + // We are copying files between file systems, + // so pass the relative path through the Sting. + val newPath = destPath.resolve(relative.toString()) + + if (Files.isDirectory(oldPath)) { + Files.createDirectories(newPath) + // TODO: consider copying attributes. + } else { + Files.copy(oldPath, newPath, + StandardCopyOption.REPLACE_EXISTING) + } + } +} \ No newline at end of file