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

Extract DependenciesData logic.

In preparation for generating IDEA files.
This commit is contained in:
Cedric Beust 2016-02-22 23:20:18 +04:00
parent 7d0adc616c
commit 61ad74f6ee
4 changed files with 81 additions and 73 deletions

View file

@ -14,7 +14,7 @@ import javax.inject.Inject
/**
* Update Kobalt to the latest version.
*/
public class UpdateKobalt @Inject constructor(val github: GithubApi, val wrapperProperties: KobaltWrapperProperties) {
class UpdateKobalt @Inject constructor(val github: GithubApi, val wrapperProperties: KobaltWrapperProperties) {
fun updateKobalt() {
val newVersion = github.latestKobaltVersion
wrapperProperties.create(newVersion.get())

View file

@ -0,0 +1,73 @@
package com.beust.kobalt.app.remote
import com.beust.kobalt.Args
import com.beust.kobalt.api.IClasspathDependency
import com.beust.kobalt.api.ProjectDescription
import com.beust.kobalt.app.BuildFileCompiler
import com.beust.kobalt.internal.JvmCompilerPlugin
import com.beust.kobalt.internal.PluginInfo
import com.beust.kobalt.internal.build.BuildFile
import com.beust.kobalt.maven.DependencyManager
import com.beust.kobalt.maven.dependency.FileDependency
import com.beust.kobalt.maven.dependency.MavenDependency
import com.beust.kobalt.misc.KobaltExecutors
import com.beust.kobalt.misc.log
import com.google.inject.Inject
import java.io.File
import java.nio.file.Paths
class DependencyData @Inject constructor(val executors: KobaltExecutors, val dependencyManager: DependencyManager,
val buildFileCompilerFactory: BuildFileCompiler.IFactory, val pluginInfo: PluginInfo) {
fun dependenciesDataFor(buildFilePath: String, args: Args) : GetDependenciesData {
val projectDatas = arrayListOf<ProjectData>()
val executor = executors.miscExecutor
fun toDependencyData(d: IClasspathDependency, scope: String): DependencyData {
val dep = MavenDependency.create(d.id, executor = executor)
return DependencyData(d.id, scope, dep.jarFile.get().absolutePath)
}
fun allDeps(l: List<IClasspathDependency>) = dependencyManager.transitiveClosure(l)
val buildFile = BuildFile(Paths.get(buildFilePath), "GetDependenciesCommand")
val buildFileCompiler = buildFileCompilerFactory.create(listOf(buildFile), pluginInfo)
val projects = buildFileCompiler.compileBuildFiles(args)
val pluginUrls = buildFileCompiler.parsedBuildFiles.flatMap { it.pluginUrls }
val pluginDependencies = pluginUrls.map { File(it.toURI()) }.map { FileDependency(it.absolutePath) }
projects.forEach { project ->
val compileDependencies = pluginDependencies.map { toDependencyData(it, "compile") } +
allDeps(project.compileDependencies).map { toDependencyData(it, "compile") } +
allDeps(project.compileProvidedDependencies).map { toDependencyData(it, "compile") }
val testDependencies = allDeps(project.testDependencies).map { toDependencyData(it, "testCompile") }
@Suppress("UNCHECKED_CAST")
val pd = (project.projectProperties.get(JvmCompilerPlugin.DEPENDENT_PROJECTS)
as List<ProjectDescription>)
val dependentProjects = pd.filter { it.project.name == project.name }.flatMap {
it.dependsOn.map {
it
.name
}
}
projectDatas.add(ProjectData(project.name, project.directory, dependentProjects,
compileDependencies, testDependencies,
project.sourceDirectories, project.sourceDirectoriesTest))
}
log(1, "Returning BuildScriptInfo")
return GetDependenciesData(projectDatas)
}
/////
// The JSON payloads that this command uses
//
class DependencyData(val id: String, val scope: String, val path: String)
class ProjectData(val name: String, val directory: String,
val dependentProjects: List<String>,
val compileDependencies: List<DependencyData>,
val testDependencies: List<DependencyData>, val sourceDirs: Set<String>, val testDirs: Set<String>)
class GetDependenciesData(val projects: List<ProjectData>)
}

View file

@ -1,26 +1,10 @@
package com.beust.kobalt.app.remote
import com.beust.kobalt.Args
import com.beust.kobalt.api.IClasspathDependency
import com.beust.kobalt.api.Project
import com.beust.kobalt.api.ProjectDescription
import com.beust.kobalt.app.BuildFileCompiler
import com.beust.kobalt.internal.JvmCompilerPlugin
import com.beust.kobalt.internal.PluginInfo
import com.beust.kobalt.internal.build.BuildFile
import com.beust.kobalt.internal.remote.CommandData
import com.beust.kobalt.internal.remote.ICommand
import com.beust.kobalt.internal.remote.ICommandSender
import com.beust.kobalt.maven.DependencyManager
import com.beust.kobalt.maven.dependency.FileDependency
import com.beust.kobalt.maven.dependency.MavenDependency
import com.beust.kobalt.misc.KobaltExecutors
import com.beust.kobalt.misc.log
import com.google.gson.Gson
import com.google.gson.JsonObject
import java.io.File
import java.net.URL
import java.nio.file.Paths
import javax.inject.Inject
/**
@ -29,62 +13,13 @@ import javax.inject.Inject
* { "name" : "getDependencies", "buildFile": "/Users/beust/kotlin/kobalt/kobalt/src/Build.kt" }
* The response is a GetDependenciesData.
*/
class GetDependenciesCommand @Inject constructor(val executors: KobaltExecutors,
val buildFileCompilerFactory: BuildFileCompiler.IFactory, val args: Args,
val dependencyManager: DependencyManager, val pluginInfo: PluginInfo) : ICommand {
class GetDependenciesCommand @Inject constructor(val args: Args, val dependencyData: DependencyData) : ICommand {
override val name = "getDependencies"
override fun run(sender: ICommandSender, received: JsonObject) {
val buildFile = BuildFile(Paths.get(received.get("buildFile").asString), "GetDependenciesCommand")
val buildFileCompiler = buildFileCompilerFactory.create(listOf(buildFile), pluginInfo)
val projects = buildFileCompiler.compileBuildFiles(args)
sender.sendData(toData(projects, buildFileCompiler.parsedBuildFiles.flatMap { it.pluginUrls }))
val buildFile = received.get("buildFile").asString
val data = toCommandData(Gson().toJson(dependencyData.dependenciesDataFor(buildFile, args)))
sender.sendData(data)
}
private fun toData(projects: List<Project>, pluginUrls: List<URL>) : CommandData {
val projectDatas = arrayListOf<ProjectData>()
val executor = executors.miscExecutor
fun toDependencyData(d: IClasspathDependency, scope: String) : DependencyData {
val dep = MavenDependency.create(d.id, executor = executor)
return DependencyData(d.id, scope, dep.jarFile.get().absolutePath)
}
fun allDeps(l: List<IClasspathDependency>) = dependencyManager.transitiveClosure(l)
val pluginDependencies = pluginUrls.map { File(it.toURI()) }.map { FileDependency(it.absolutePath) }
projects.forEach { project ->
val compileDependencies = pluginDependencies.map { toDependencyData(it, "compile")} +
allDeps(project.compileDependencies).map { toDependencyData(it, "compile") } +
allDeps(project.compileProvidedDependencies).map { toDependencyData(it, "compile") }
val testDependencies = allDeps(project.testDependencies).map { toDependencyData(it, "testCompile") }
@Suppress("UNCHECKED_CAST")
val pd = (project.projectProperties.get(JvmCompilerPlugin.DEPENDENT_PROJECTS)
as List<ProjectDescription>)
val dependentProjects = pd.filter { it.project.name == project.name }.flatMap {
it.dependsOn.map { it
.name
}}
projectDatas.add(ProjectData(project.name, project.directory, dependentProjects,
compileDependencies, testDependencies,
project.sourceDirectories, project.sourceDirectoriesTest))
}
log(1, "Returning BuildScriptInfo")
val result = toCommandData(Gson().toJson(GetDependenciesData(projectDatas)))
log(2, " $result")
return result
}
/////
// The JSON payloads that this command uses
//
class DependencyData(val id: String, val scope: String, val path: String)
class ProjectData(val name: String, val directory: String,
val dependentProjects: List<String>,
val compileDependencies: List<DependencyData>,
val testDependencies: List<DependencyData>, val sourceDirs: Set<String>, val testDirs: Set<String>)
class GetDependenciesData(val projects: List<ProjectData>)
}

View file

@ -40,7 +40,7 @@ public class KobaltClient @Inject constructor() : Runnable {
done = true
} else {
val data = jo.get("data").asString
val dd = Gson().fromJson(data, GetDependenciesCommand.GetDependenciesData::class.java)
val dd = Gson().fromJson(data, DependencyData.GetDependenciesData::class.java)
println("Read GetDependencyData, project count: ${dd.projects.size}")
line = ins.readLine()
}