1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-26 08:27:12 -07:00

Better uploading message.

This commit is contained in:
Cedric Beust 2015-10-14 23:35:26 -07:00
parent d4ea37bd81
commit 62f072d6de
7 changed files with 80 additions and 58 deletions

View file

@ -44,13 +44,13 @@ private class Main @Inject constructor(
data class RunInfo(val jc: JCommander, val args: Args) data class RunInfo(val jc: JCommander, val args: Args)
public fun run(jc: JCommander, args: Args) { public fun run(jc: JCommander, args: Args) : Int {
var result = 0
val latestVersionFuture = github.latestKobaltVersion val latestVersionFuture = github.latestKobaltVersion
benchmark("Build", { benchmark("Build", {
println(Banner.get() + Kobalt.version + "\n") println(Banner.get() + Kobalt.version + "\n")
// runTest() // runTest()
runWithArgs(jc, args) result = runWithArgs(jc, args)
executors.shutdown() executors.shutdown()
debug("All done") debug("All done")
}) })
@ -67,6 +67,7 @@ private class Main @Inject constructor(
log(1, it ) log(1, it )
} }
} }
return result
} }
public class Worker<T>(val runNodes: ArrayList<T>, val n: T) : IWorker<T> { public class Worker<T>(val runNodes: ArrayList<T>, val n: T) : IWorker<T> {
@ -94,7 +95,8 @@ private class Main @Inject constructor(
private val SCRIPT_JAR = "buildScript.jar" private val SCRIPT_JAR = "buildScript.jar"
private fun runWithArgs(jc: JCommander, args: Args) { private fun runWithArgs(jc: JCommander, args: Args) : Int {
var result = 0
val p = if (args.buildFile != null) File(args.buildFile) else findBuildFile() val p = if (args.buildFile != null) File(args.buildFile) else findBuildFile()
args.buildFile = p.absolutePath args.buildFile = p.absolutePath
val buildFile = BuildFile(Paths.get(p.absolutePath), p.name) val buildFile = BuildFile(Paths.get(p.absolutePath), p.name)
@ -154,11 +156,15 @@ private class Main @Inject constructor(
// //
// Launch the build // Launch the build
// //
taskManager.runTargets(args.targets, allProjects) val thisResult = taskManager.runTargets(args.targets, allProjects)
if (result == 0) {
result = thisResult
} }
} }
} }
} }
return result
}
private fun findBuildFile(): File { private fun findBuildFile(): File {
val files = arrayListOf("Build.kt", "build.kobalt", KFiles.src("build.kobalt"), val files = arrayListOf("Build.kt", "build.kobalt", KFiles.src("build.kobalt"),

View file

@ -39,7 +39,11 @@ public class DynamicGraphExecutor<T>(val graph: DynamicGraph<T>,
val executor = Executors.newFixedThreadPool(5, NamedThreadFactory("DynamicGraphExecutor")) val executor = Executors.newFixedThreadPool(5, NamedThreadFactory("DynamicGraphExecutor"))
val completion = ExecutorCompletionService<TaskResult2<T>>(executor) val completion = ExecutorCompletionService<TaskResult2<T>>(executor)
public fun run() { /**
* @return 0 if all went well, > 0 otherwise
*/
public fun run() : Int {
var lastResult : TaskResult? = null
while (graph.freeNodes.size() > 0) { while (graph.freeNodes.size() > 0) {
log(3, "Current count: ${graph.nodeCount}") log(3, "Current count: ${graph.nodeCount}")
synchronized(graph) { synchronized(graph) {
@ -55,6 +59,7 @@ public class DynamicGraphExecutor<T>(val graph: DynamicGraph<T>,
try { try {
val future = completion.take() val future = completion.take()
val taskResult = future.get(2, TimeUnit.SECONDS) val taskResult = future.get(2, TimeUnit.SECONDS)
lastResult = taskResult
log(3, "Received task result $taskResult") log(3, "Received task result $taskResult")
n-- n--
graph.setStatus(taskResult.value, graph.setStatus(taskResult.value,
@ -70,6 +75,7 @@ public class DynamicGraphExecutor<T>(val graph: DynamicGraph<T>,
} }
} }
executor.shutdown() executor.shutdown()
return if (lastResult?.success!!) 0 else 1
} }
} }

View file

@ -43,7 +43,8 @@ public class TaskManager @Inject constructor(val plugins: Plugins, val args: Arg
fun matches(projectName: String) = project == null || project == projectName fun matches(projectName: String) = project == null || project == projectName
} }
public fun runTargets(targets: List<String>, projects: List<Project>) { public fun runTargets(targets: List<String>, projects: List<Project>) : Int {
var result = 0
val tasksAlreadyRun = hashSetOf<String>() val tasksAlreadyRun = hashSetOf<String>()
projects.forEach { project -> projects.forEach { project ->
val projectName = project.name!! val projectName = project.name!!
@ -141,11 +142,15 @@ public class TaskManager @Inject constructor(val plugins: Plugins, val args: Arg
} }
val executor = DynamicGraphExecutor(graph, factory) val executor = DynamicGraphExecutor(graph, factory)
executor.run() val thisResult = executor.run()
if (result == 0) {
result = thisResult
} }
} }
} }
} }
return result
}
/** /**
* Find the free tasks of the graph. * Find the free tasks of the graph.

View file

@ -55,6 +55,7 @@ public class Http {
.put(RequestBody.create(MEDIA_TYPE_BINARY, file)) .put(RequestBody.create(MEDIA_TYPE_BINARY, file))
.build() .build()
log(2, "Uploading $file to $url")
val response = OkHttpClient().newCall(request).execute() val response = OkHttpClient().newCall(request).execute()
if (! response.isSuccessful) { if (! response.isSuccessful) {
error(response) error(response)

View file

@ -15,9 +15,9 @@ interface KobaltLogger {
Logger(false) Logger(false)
} }
fun log(level: Int, s: String) { fun log(level: Int, s: String, newLine: Boolean = true) {
if (level <= LOG_LEVEL) { if (level <= LOG_LEVEL) {
logger.log("Logger", s) logger.log("Logger", s, newLine)
} }
} }
@ -30,28 +30,28 @@ interface KobaltLogger {
} }
} }
final fun log(level: Int = 1, message: String) { // final fun log(level: Int = 1, message: String) {
if (level <= LOG_LEVEL) { // if (level <= LOG_LEVEL) {
logger.log("Logger", message) // logger.log("Logger", message)
} // }
} // }
//
final fun debug(message: String) { // final fun debug(message: String) {
logger.debug(message) // logger.debug(message)
} // }
//
final fun error(message: String, e: Throwable? = null) { // final fun error(message: String, e: Throwable? = null) {
logger.error("Logger", "***** $message", e) // logger.error("Logger", "***** $message", e)
} // }
//
final fun warn(message: String, e: Throwable? = null) { // final fun warn(message: String, e: Throwable? = null) {
logger.warn("Logger", message, e) // logger.warn("Logger", message, e)
} // }
} }
fun Any.log(level: Int, text: String) { fun Any.log(level: Int, text: String, newLine : Boolean = true) {
if (level <= KobaltLogger.LOG_LEVEL) { if (level <= KobaltLogger.LOG_LEVEL) {
KobaltLogger.logger.log(javaClass.simpleName, text) KobaltLogger.logger.log(javaClass.simpleName, text, newLine)
} }
} }
@ -88,6 +88,9 @@ class Logger(val dev: Boolean) {
final fun warn(tag: String, message: String, e: Throwable? = null) = final fun warn(tag: String, message: String, e: Throwable? = null) =
println(getPattern("W", "***** WARNING ", tag, message)) println(getPattern("W", "***** WARNING ", tag, message))
final fun log(tag: String, message: String) = final fun log(tag: String, message: String, newLine: Boolean) =
println(getPattern("L", "", tag, message)) with(getPattern("L", "", tag, message)) {
if (newLine) println(this)
else print("\r" + this)
}
} }

View file

@ -14,6 +14,7 @@ import org.jetbrains.annotations.Nullable
import java.io.ByteArrayInputStream import java.io.ByteArrayInputStream
import java.io.File import java.io.File
import java.nio.charset.Charset import java.nio.charset.Charset
import java.util.concurrent.Callable
import javax.inject.Inject import javax.inject.Inject
data class JCenterPackage(val jo: JsonObject) { data class JCenterPackage(val jo: JsonObject) {
@ -44,7 +45,7 @@ open public class UnauthenticatedJCenterApi @Inject constructor(open val http: H
public class JCenterApi @Inject constructor (@Nullable @Assisted("username") val username: String?, public class JCenterApi @Inject constructor (@Nullable @Assisted("username") val username: String?,
@Nullable @Assisted("password") val password: String?, @Nullable @Assisted("password") val password: String?,
override val http: Http, val gpg: Gpg) : UnauthenticatedJCenterApi(http) { override val http: Http, val gpg: Gpg, val executors: KobaltExecutors) : UnauthenticatedJCenterApi(http) {
interface IFactory { interface IFactory {
fun create(@Nullable @Assisted("username") username: String?, fun create(@Nullable @Assisted("username") username: String?,
@ -100,8 +101,6 @@ public class JCenterApi @Inject constructor (@Nullable @Assisted("username") val
private fun upload(files: List<File>, configuration : JCenterConfiguration?, fileToPath: (File) -> String, private fun upload(files: List<File>, configuration : JCenterConfiguration?, fileToPath: (File) -> String,
generateMd5: Boolean = false, generateAsc: Boolean) : TaskResult { generateMd5: Boolean = false, generateAsc: Boolean) : TaskResult {
val successes = arrayListOf<File>()
val failures = hashMapOf<File, String>()
val filesToUpload = arrayListOf<File>() val filesToUpload = arrayListOf<File>()
if (generateAsc) { if (generateAsc) {
@ -130,34 +129,36 @@ public class JCenterApi @Inject constructor (@Nullable @Assisted("username") val
// //
// TODO: These files should be uploaded from a thread pool instead of serially // TODO: These files should be uploaded from a thread pool instead of serially
// //
log(1, "Found ${filesToUpload.size()} artifacts to upload") val fileCount = filesToUpload.size()
filesToUpload.forEach { log(1, "Found $fileCount artifacts to upload")
var path = fileToPath(it) val callables = filesToUpload.map { FileUploadCallable(username, password, fileToPath(it), it) }
if (options.size() > 0) { var i = 1
path += "?" + options.join("&") val results = executors.completionService("FileUpload", 5, 60000000, callables, { tr: TaskResult ->
val last = i >= fileCount
val end = if (last) "\n" else ""
log(1, " Uploading " + (i++) + " / $fileCount$end", false)
})
val errorMessages = results.filter { ! it.success }.map { it.errorMessage!! }
if (errorMessages.isEmpty()) {
return TaskResult()
} else {
error("Errors while uploading:\n" + errorMessages.map { " $it" }.join("\n"))
return com.beust.kobalt.internal.TaskResult(false, errorMessages.join("\n"))
}
} }
log(1, " Uploading $it to $path") inner class FileUploadCallable(val username: String?, val password: String?, val url: String, val file: File)
http.uploadFile(username, password, path, it, : Callable<TaskResult> {
{ r: Response -> successes.add(it) }, override fun call(): TaskResult? {
var result: TaskResult? = null
http.uploadFile(username, password, url, file,
{ result = TaskResult() },
{ r: Response -> { r: Response ->
val jo = parseResponse(r.body().string()) val jo = parseResponse(r.body().string())
failures.put(it, jo.string("message") ?: "No message found") result = TaskResult(false, jo.string("message") ?: "No message found")
}) })
}
val result: TaskResult
if (successes.size() == filesToUpload.size()) {
log(1, "All artifacts successfully uploaded")
result = TaskResult(true)
} else {
result = TaskResult(false, failures.values().join(" "))
error("Failed to upload ${failures.size()} files:")
failures.forEach{ entry ->
error(" - ${entry.key} : ${entry.value}")
}
}
return result return result
} }
}
} }

View file

@ -34,7 +34,7 @@ public class DynamicGraphTest {
override val priority = 0 override val priority = 0
override fun call() : TaskResult2<T> { override fun call() : TaskResult2<T> {
log(2, "Running node $n") KobaltLogger.log(2, "Running node $n")
runNodes.add(n) runNodes.add(n)
return TaskResult2(errorFunction(n), n) return TaskResult2(errorFunction(n), n)
} }