From 424a330f7e9a1a5096889c7b21852f93d7ddf03f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 10 Oct 2017 15:23:02 +0300 Subject: [PATCH] Custom progress reporting for dependency downloader --- .../kotlin/konan/util/DependencyDownloader.kt | 18 +++++++++++------- .../kotlin/konan/util/DependencyProcessor.kt | 5 +++-- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/shared/src/main/kotlin/org/jetbrains/kotlin/konan/util/DependencyDownloader.kt b/shared/src/main/kotlin/org/jetbrains/kotlin/konan/util/DependencyDownloader.kt index 80015534f94..4062244209c 100644 --- a/shared/src/main/kotlin/org/jetbrains/kotlin/konan/util/DependencyDownloader.kt +++ b/shared/src/main/kotlin/org/jetbrains/kotlin/konan/util/DependencyDownloader.kt @@ -8,10 +8,18 @@ import java.nio.file.Files import java.nio.file.StandardCopyOption import java.util.concurrent.* +typealias ProgressCallback = (url: String, currentBytes: Long, totalBytes: Long) -> Unit + class DependencyDownloader( var maxAttempts: Int = DEFAULT_MAX_ATTEMPTS, - var attemptIntervalMs: Long = DEFAULT_ATTEMPT_INTERVAL_MS + var attemptIntervalMs: Long = DEFAULT_ATTEMPT_INTERVAL_MS, + customProgressCallback: ProgressCallback? = null ) { + + private val progressCallback = customProgressCallback ?: { url, currentBytes, totalBytes -> + print("\rDownloading dependency: $url (${currentBytes.humanReadable}/${totalBytes.humanReadable}). ") + } + val executor = ExecutorCompletionService(Executors.newSingleThreadExecutor(object : ThreadFactory { override fun newThread(r: Runnable?): Thread { val thread = Thread(r) @@ -81,10 +89,10 @@ class DependencyDownloader( var result: Future? do { - updateProgressMsg(originalUrl.toString(), progress.currentBytes, totalBytes) + progressCallback(originalUrl.toString(), progress.currentBytes, totalBytes) result = executor.poll(1, TimeUnit.SECONDS) } while(result == null) - updateProgressMsg(originalUrl.toString(), progress.currentBytes, totalBytes) + progressCallback(originalUrl.toString(), progress.currentBytes, totalBytes) try { result.get() @@ -188,10 +196,6 @@ class DependencyDownloader( return "%.1f %siB".format(this / Math.pow(1024.0, exp.toDouble()), prefix) } - private fun updateProgressMsg(url: String, currentBytes: Long, totalBytes: Long) { - print("\rDownloading dependency: $url (${currentBytes.humanReadable}/${totalBytes.humanReadable}). ") - } - companion object { const val DEFAULT_MAX_ATTEMPTS = 10 const val DEFAULT_ATTEMPT_INTERVAL_MS = 3000L diff --git a/shared/src/main/kotlin/org/jetbrains/kotlin/konan/util/DependencyProcessor.kt b/shared/src/main/kotlin/org/jetbrains/kotlin/konan/util/DependencyProcessor.kt index 0ae984a53da..f3c9d65ef96 100644 --- a/shared/src/main/kotlin/org/jetbrains/kotlin/konan/util/DependencyProcessor.kt +++ b/shared/src/main/kotlin/org/jetbrains/kotlin/konan/util/DependencyProcessor.kt @@ -64,7 +64,8 @@ class DependencyProcessor(dependenciesRoot: File, homeDependencyCache: String = DEFAULT_HOME_DEPENDENCY_CACHE, val airplaneMode: Boolean = false, maxAttempts: Int = DependencyDownloader.DEFAULT_MAX_ATTEMPTS, - attemptIntervalMs: Long = DependencyDownloader.DEFAULT_ATTEMPT_INTERVAL_MS) { + attemptIntervalMs: Long = DependencyDownloader.DEFAULT_ATTEMPT_INTERVAL_MS, + customProgressCallback: ProgressCallback? = null) { val dependenciesDirectory = dependenciesRoot.apply { mkdirs() } val cacheDirectory = System.getProperty("user.home")?.let { @@ -77,7 +78,7 @@ class DependencyProcessor(dependenciesRoot: File, private var isInfoShown = false // TOOO: Rename pause -> interval - private val downloader = DependencyDownloader(maxAttempts, attemptIntervalMs) + private val downloader = DependencyDownloader(maxAttempts, attemptIntervalMs, customProgressCallback) private val extractor = DependencyExtractor() private val archiveExtension get() = extractor.archiveExtension