Skip to content

Commit

Permalink
Restructure ModuleBuilder dependencies, move into new module
Browse files Browse the repository at this point in the history
  • Loading branch information
DrewCarlson committed Oct 27, 2024
1 parent 23a7cba commit b732d2e
Show file tree
Hide file tree
Showing 25 changed files with 247 additions and 76 deletions.
2 changes: 2 additions & 0 deletions ktpack-cli/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ kotlin {
implementation(project(":ktpack-internal:git"))
implementation(project(":ktpack-internal:github"))
implementation(project(":ktpack-internal:gradle"))
implementation(project(":ktpack-internal:manifest-loader"))
implementation(project(":ktpack-internal:maven"))
implementation(project(":ktpack-internal:webserver"))
implementation(project(":ktpack-internal:models"))
implementation(project(":ktpack-internal:module-builder"))
implementation(project(":ktpack-internal:platform"))
implementation(project(":ktpack-internal:toolchains"))
implementation(libs.ksubprocess)
Expand Down
20 changes: 14 additions & 6 deletions ktpack-cli/src/commonMain/kotlin/ktpack/CliContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ package ktpack

import com.github.ajalt.mordant.terminal.Terminal
import io.ktor.client.HttpClient
import ktpack.compilation.BuildContext
import ktpack.compilation.dependencies.MavenDependencyResolver
import ktpack.compilation.tools.DokkaCli
import ktpack.manifest.ManifestToml
import ktpack.manifest.ManifestLoader
import ktpack.toolchain.jdk.JdkInstalls
import ktpack.toolchain.kotlin.KotlincInstalls
import ktpack.toolchain.nodejs.NodejsInstalls
import ktpack.util.GitCli

const val MANIFEST_FILENAME = "pack.toml"

interface CliContext {
interface CliContext : ManifestLoader {

/**
* When true, print the full stacktrace in the case of an uncaught
Expand All @@ -33,7 +33,15 @@ interface CliContext {
val dokka: DokkaCli
val gitCli: GitCli

fun updateConfig(body: KtpackUserConfig.() -> KtpackUserConfig)
fun createBuildContext(): BuildContext {
return BuildContext(
manifestLoader = this,
resolver = MavenDependencyResolver(http),
jdk = jdkInstalls,
kotlinc = kotlinInstalls,
debug = debug,
)
}

fun loadManifestToml(filePath: String = MANIFEST_FILENAME): ManifestToml
fun updateConfig(body: KtpackUserConfig.() -> KtpackUserConfig)
}
10 changes: 7 additions & 3 deletions ktpack-cli/src/commonMain/kotlin/ktpack/commands/BuildCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,15 @@ class BuildCommand : CliktCommand() {
.flag()

override fun run() = runBlocking {
val manifest = context.loadManifestToml()
val manifest = context.load()
val module = manifest.module
val output =
manifest.module.output ?: KtpackSourceCollector(Path(workingDirectory, "src")).getDefaultOutput(userTarget)
val moduleBuilder = ModuleBuilder(manifest, context, workingDirectory)
val moduleBuilder = ModuleBuilder(
manifest = manifest,
modulePath = workingDirectory,
context = context.createBuildContext()
)

logger.i {
"{} {} v{} ({})".format(
Expand Down Expand Up @@ -110,7 +114,7 @@ class BuildCommand : CliktCommand() {
is ArtifactResult.ProcessError -> {
logger.i { "${failed("Failed")} failed to compile selected target(s)" }
if (!artifact.message.isNullOrBlank()) {
logger.i(artifact.message)
logger.i(artifact.message!!)
}
exitProcess(1)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.github.ajalt.clikt.core.*
import com.github.ajalt.clikt.parameters.arguments.*
import kotlinx.coroutines.runBlocking
import ktpack.*
import ktpack.manifest.MANIFEST_FILENAME

class CheckCommand : CliktCommand() {

Expand All @@ -18,7 +19,7 @@ class CheckCommand : CliktCommand() {
private val context by requireObject<CliContext>()

override fun run() = runBlocking {
val packageConf = context.loadManifestToml(filePath)
val packageConf = context.load(filePath)
// TODO: Validate all package properties
println(packageConf)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ class CleanCommand : CliktCommand() {

override fun run() = runBlocking {
val userTarget = userTarget
val manifestToml = context.loadManifestToml()
val moduleBuilder = ModuleBuilder(manifestToml, context, workingDirectory)
val manifest = context.load()
val moduleBuilder = ModuleBuilder(
manifest = manifest,
modulePath = workingDirectory,
context = context.createBuildContext()
)

val dependencyTree = moduleBuilder.resolveRootDependencyTree(
KotlinTarget.entries,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,12 @@ class DependenciesCommand : CliktCommand() {
.multiple(DependencyScope.entries)

override fun run(): Unit = runBlocking {
val packageConf = context.loadManifestToml()
val moduleBuilder = ModuleBuilder(packageConf, context, workingDirectory)
val manifest = context.load()
val moduleBuilder = ModuleBuilder(
manifest = manifest,
modulePath = workingDirectory,
context = context.createBuildContext()
)

val tree = moduleBuilder.resolveRootDependencyTree(listOfNotNull(target), scopes)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class DocCommand : CliktCommand(name = "doc") {
.default(9543)

override fun run(): Unit = runBlocking {
val manifest = context.loadManifestToml()
val manifest = context.load()
val jdk = checkNotNull(context.jdkInstalls.getDefaultJdk())
val dokkaVersion = manifest.docs.version ?: context.config.dokkaVersion
val docOutputDir = Path(workingDirectory, "out", "docs").toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import co.touchlab.kermit.Logger
import com.github.ajalt.clikt.core.*
import kotlinx.io.files.Path
import ktpack.CliContext
import ktpack.MANIFEST_FILENAME
import ktpack.manifest.MANIFEST_FILENAME
import ktpack.util.exists
import ktpack.util.failed
import ktpack.util.forClass
Expand Down
18 changes: 5 additions & 13 deletions ktpack-cli/src/commonMain/kotlin/ktpack/commands/KtpackCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,22 @@ import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.client.plugins.logging.*
import io.ktor.serialization.kotlinx.json.*
import kotlinx.io.files.Path
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import ktpack.*
import ktpack.compilation.dependencies.MavenDependencyResolver
import ktpack.compilation.tools.DokkaCli
import ktpack.manifest.DefaultManifestLoader
import ktpack.manifest.ManifestLoader
import ktpack.toolchain.kotlin.KotlincInstalls
import ktpack.manifest.ManifestToml
import ktpack.manifest.toml
import ktpack.toolchain.jdk.JdkInstalls
import ktpack.toolchain.nodejs.NodejsInstalls
import ktpack.util.*

class KtpackCommand(
override val term: Terminal,
) : CliktCommand(), CliContext {
) : CliktCommand(),
CliContext,
ManifestLoader by DefaultManifestLoader() {

override fun help(context: Context): String {
return context.theme.info("A simple tool for building and publishing Kotlin software.")
Expand Down Expand Up @@ -83,15 +84,6 @@ class KtpackCommand(
configPath.writeString(encodedConfig, ::logError)
}

override fun loadManifestToml(filePath: String): ManifestToml {
val path = Path(filePath).let { path ->
Path(workingDirectory, path.toString()).resolve()
}
check(path.exists()) { "No $MANIFEST_FILENAME file found in '${path.parent}'" }
return toml.decodeFromString<ManifestToml>(path.readString())
.resolveDependencyShorthand()
}

override val http: HttpClient by lazy {
HttpClient {
install(ContentNegotiation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import kotlinx.io.files.Path
import kotlinx.serialization.encodeToString
import ktpack.CliContext
import ktpack.Ktpack
import ktpack.MANIFEST_FILENAME
import ktpack.configuration.KotlinTarget
import ktpack.manifest.MANIFEST_FILENAME
import ktpack.manifest.ManifestToml
import ktpack.manifest.ModuleToml
import ktpack.manifest.toml
Expand Down
10 changes: 7 additions & 3 deletions ktpack-cli/src/commonMain/kotlin/ktpack/commands/RunCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,17 @@ class RunCommand : CliktCommand() {
.default(9543)

override fun run(): Unit = runBlocking {
val manifest = context.loadManifestToml()
val manifest = context.load()
val targetBin = targetBin ?: manifest.module.name
val output = manifest.module.output
?: OutputToml.BinCommon.Bin(
targets = PlatformUtils.getHostSupportedTargets(),
)
val moduleBuilder = ModuleBuilder(manifest, context, workingDirectory)
val targetBin = targetBin ?: manifest.module.name
val moduleBuilder = ModuleBuilder(
manifest = manifest,
modulePath = workingDirectory,
context = context.createBuildContext()
)

logger.i {
val name = manifest.module.name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@ class TestCommand : CliktCommand() {
.multiple(listOf(PlatformUtils.getHostTarget()))

override fun run(): Unit = runBlocking {
val manifest = context.loadManifestToml()
val moduleBuilder = ModuleBuilder(manifest, context, workingDirectory)
val manifest = context.load()
val moduleBuilder = ModuleBuilder(
manifest = manifest,
modulePath = workingDirectory,
context = context.createBuildContext()
)

userTargets.forEach { target ->
buildAndRunTests(manifest, moduleBuilder, target)
Expand Down
3 changes: 2 additions & 1 deletion ktpack-cli/src/commonTest/kotlin/ktpack/TestCliContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import kotlinx.io.files.Path
import kotlinx.serialization.decodeFromString
import ktpack.compilation.dependencies.MavenDependencyResolver
import ktpack.compilation.tools.DokkaCli
import ktpack.manifest.MANIFEST_FILENAME
import ktpack.toolchain.kotlin.KotlincInstalls
import ktpack.manifest.ManifestToml
import ktpack.manifest.toml
Expand Down Expand Up @@ -53,7 +54,7 @@ class TestCliContext : CliContext {
config = config.run(body)
}

override fun loadManifestToml(filePath: String): ManifestToml {
override fun load(filePath: String): ManifestToml {
val path = Path(filePath).resolve()
check(path.exists()) {
"No $MANIFEST_FILENAME file found in '${path.parent}'"
Expand Down
21 changes: 21 additions & 0 deletions ktpack-internal/manifest-loader/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
plugins {
id("internal-lib")
}

kotlin {
sourceSets {
commonMain {
dependencies {
implementation(project(":ktpack-internal:platform"))
implementation(project(":ktpack-internal:models"))
}
}

commonTest {
dependencies {
implementation(project(":ktpack-internal:test-utils"))
implementation(libs.coroutines.test)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ktpack.manifest

import kotlinx.io.files.Path
import kotlinx.serialization.decodeFromString
import ktpack.util.exists
import ktpack.util.readString
import ktpack.util.resolve
import ktpack.util.workingDirectory

class DefaultManifestLoader : ManifestLoader {

override fun load(filePath: String): ManifestToml {
val path = Path(filePath).let { path ->
if (path.isAbsolute) {
path
} else {
Path(workingDirectory, path.toString()).resolve()
}
}
check(path.exists()) { "No $MANIFEST_FILENAME file found in '${path.parent}'" }
return toml.decodeFromString<ManifestToml>(path.readString())
.resolveDependencyShorthand()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ktpack.manifest

const val MANIFEST_FILENAME = "pack.toml"

interface ManifestLoader {

fun load(
filePath: String = MANIFEST_FILENAME
): ManifestToml
}
26 changes: 26 additions & 0 deletions ktpack-internal/module-builder/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
plugins {
id("internal-lib")
}

kotlin {
sourceSets {
commonMain {
dependencies {
implementation(project(":ktpack-internal:platform"))
implementation(project(":ktpack-internal:models"))
implementation(project(":ktpack-internal:manifest-loader"))
implementation(project(":ktpack-internal:dependency-resolver"))
implementation(project(":ktpack-internal:toolchains"))
implementation(libs.ksubprocess)
implementation(libs.semver)
}
}

commonTest {
dependencies {
implementation(project(":ktpack-internal:test-utils"))
implementation(libs.coroutines.test)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ktpack.compilation

import ktpack.compilation.dependencies.MavenDependencyResolver
import ktpack.manifest.ManifestLoader
import ktpack.toolchain.jdk.JdkInstalls
import ktpack.toolchain.kotlin.KotlincInstalls

class BuildContext(
manifestLoader: ManifestLoader,
val resolver: MavenDependencyResolver,
val jdk: JdkInstalls,
val kotlinc: KotlincInstalls,
val debug: Boolean
) : ManifestLoader by manifestLoader
Loading

0 comments on commit b732d2e

Please sign in to comment.