Use .zip files on Windows in DependencyDownloader

This commit is contained in:
Svyatoslav Scherbina
2017-05-27 10:17:47 +03:00
committed by SvyatoslavScherbina
parent d7adbfd221
commit 9c35fbf91a
2 changed files with 70 additions and 4 deletions
@@ -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
+51
View File
@@ -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<String, Any?>(), 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)
}
}
}