Custom progress reporting for dependency downloader
This commit is contained in:
@@ -8,10 +8,18 @@ import java.nio.file.Files
|
|||||||
import java.nio.file.StandardCopyOption
|
import java.nio.file.StandardCopyOption
|
||||||
import java.util.concurrent.*
|
import java.util.concurrent.*
|
||||||
|
|
||||||
|
typealias ProgressCallback = (url: String, currentBytes: Long, totalBytes: Long) -> Unit
|
||||||
|
|
||||||
class DependencyDownloader(
|
class DependencyDownloader(
|
||||||
var maxAttempts: Int = DEFAULT_MAX_ATTEMPTS,
|
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<Unit>(Executors.newSingleThreadExecutor(object : ThreadFactory {
|
val executor = ExecutorCompletionService<Unit>(Executors.newSingleThreadExecutor(object : ThreadFactory {
|
||||||
override fun newThread(r: Runnable?): Thread {
|
override fun newThread(r: Runnable?): Thread {
|
||||||
val thread = Thread(r)
|
val thread = Thread(r)
|
||||||
@@ -81,10 +89,10 @@ class DependencyDownloader(
|
|||||||
|
|
||||||
var result: Future<Unit>?
|
var result: Future<Unit>?
|
||||||
do {
|
do {
|
||||||
updateProgressMsg(originalUrl.toString(), progress.currentBytes, totalBytes)
|
progressCallback(originalUrl.toString(), progress.currentBytes, totalBytes)
|
||||||
result = executor.poll(1, TimeUnit.SECONDS)
|
result = executor.poll(1, TimeUnit.SECONDS)
|
||||||
} while(result == null)
|
} while(result == null)
|
||||||
updateProgressMsg(originalUrl.toString(), progress.currentBytes, totalBytes)
|
progressCallback(originalUrl.toString(), progress.currentBytes, totalBytes)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
result.get()
|
result.get()
|
||||||
@@ -188,10 +196,6 @@ class DependencyDownloader(
|
|||||||
return "%.1f %siB".format(this / Math.pow(1024.0, exp.toDouble()), prefix)
|
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 {
|
companion object {
|
||||||
const val DEFAULT_MAX_ATTEMPTS = 10
|
const val DEFAULT_MAX_ATTEMPTS = 10
|
||||||
const val DEFAULT_ATTEMPT_INTERVAL_MS = 3000L
|
const val DEFAULT_ATTEMPT_INTERVAL_MS = 3000L
|
||||||
|
|||||||
@@ -64,7 +64,8 @@ class DependencyProcessor(dependenciesRoot: File,
|
|||||||
homeDependencyCache: String = DEFAULT_HOME_DEPENDENCY_CACHE,
|
homeDependencyCache: String = DEFAULT_HOME_DEPENDENCY_CACHE,
|
||||||
val airplaneMode: Boolean = false,
|
val airplaneMode: Boolean = false,
|
||||||
maxAttempts: Int = DependencyDownloader.DEFAULT_MAX_ATTEMPTS,
|
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 dependenciesDirectory = dependenciesRoot.apply { mkdirs() }
|
||||||
val cacheDirectory = System.getProperty("user.home")?.let {
|
val cacheDirectory = System.getProperty("user.home")?.let {
|
||||||
@@ -77,7 +78,7 @@ class DependencyProcessor(dependenciesRoot: File,
|
|||||||
private var isInfoShown = false
|
private var isInfoShown = false
|
||||||
|
|
||||||
// TOOO: Rename pause -> interval
|
// TOOO: Rename pause -> interval
|
||||||
private val downloader = DependencyDownloader(maxAttempts, attemptIntervalMs)
|
private val downloader = DependencyDownloader(maxAttempts, attemptIntervalMs, customProgressCallback)
|
||||||
private val extractor = DependencyExtractor()
|
private val extractor = DependencyExtractor()
|
||||||
|
|
||||||
private val archiveExtension get() = extractor.archiveExtension
|
private val archiveExtension get() = extractor.archiveExtension
|
||||||
|
|||||||
Reference in New Issue
Block a user