diff --git a/ktpack-cli/build.gradle.kts b/ktpack-cli/build.gradle.kts index 14ec2f3..93fd936 100644 --- a/ktpack-cli/build.gradle.kts +++ b/ktpack-cli/build.gradle.kts @@ -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) diff --git a/ktpack-cli/src/commonMain/kotlin/ktpack/CliContext.kt b/ktpack-cli/src/commonMain/kotlin/ktpack/CliContext.kt index 0b5832e..55e293f 100644 --- a/ktpack-cli/src/commonMain/kotlin/ktpack/CliContext.kt +++ b/ktpack-cli/src/commonMain/kotlin/ktpack/CliContext.kt @@ -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 @@ -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) } diff --git a/ktpack-cli/src/commonMain/kotlin/ktpack/commands/BuildCommand.kt b/ktpack-cli/src/commonMain/kotlin/ktpack/commands/BuildCommand.kt index b7962be..ea3c08e 100644 --- a/ktpack-cli/src/commonMain/kotlin/ktpack/commands/BuildCommand.kt +++ b/ktpack-cli/src/commonMain/kotlin/ktpack/commands/BuildCommand.kt @@ -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( @@ -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) } diff --git a/ktpack-cli/src/commonMain/kotlin/ktpack/commands/CheckCommand.kt b/ktpack-cli/src/commonMain/kotlin/ktpack/commands/CheckCommand.kt index 9f2f314..fbc536e 100644 --- a/ktpack-cli/src/commonMain/kotlin/ktpack/commands/CheckCommand.kt +++ b/ktpack-cli/src/commonMain/kotlin/ktpack/commands/CheckCommand.kt @@ -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() { @@ -18,7 +19,7 @@ class CheckCommand : CliktCommand() { private val context by requireObject() override fun run() = runBlocking { - val packageConf = context.loadManifestToml(filePath) + val packageConf = context.load(filePath) // TODO: Validate all package properties println(packageConf) } diff --git a/ktpack-cli/src/commonMain/kotlin/ktpack/commands/CleanCommand.kt b/ktpack-cli/src/commonMain/kotlin/ktpack/commands/CleanCommand.kt index a2f7b09..4583c64 100644 --- a/ktpack-cli/src/commonMain/kotlin/ktpack/commands/CleanCommand.kt +++ b/ktpack-cli/src/commonMain/kotlin/ktpack/commands/CleanCommand.kt @@ -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, diff --git a/ktpack-cli/src/commonMain/kotlin/ktpack/commands/DependenciesCommand.kt b/ktpack-cli/src/commonMain/kotlin/ktpack/commands/DependenciesCommand.kt index 06265f5..7b486be 100644 --- a/ktpack-cli/src/commonMain/kotlin/ktpack/commands/DependenciesCommand.kt +++ b/ktpack-cli/src/commonMain/kotlin/ktpack/commands/DependenciesCommand.kt @@ -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) diff --git a/ktpack-cli/src/commonMain/kotlin/ktpack/commands/DocCommand.kt b/ktpack-cli/src/commonMain/kotlin/ktpack/commands/DocCommand.kt index 7c5a8b0..8676840 100644 --- a/ktpack-cli/src/commonMain/kotlin/ktpack/commands/DocCommand.kt +++ b/ktpack-cli/src/commonMain/kotlin/ktpack/commands/DocCommand.kt @@ -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() diff --git a/ktpack-cli/src/commonMain/kotlin/ktpack/commands/InitCommand.kt b/ktpack-cli/src/commonMain/kotlin/ktpack/commands/InitCommand.kt index ef8f9cf..1248afc 100644 --- a/ktpack-cli/src/commonMain/kotlin/ktpack/commands/InitCommand.kt +++ b/ktpack-cli/src/commonMain/kotlin/ktpack/commands/InitCommand.kt @@ -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 diff --git a/ktpack-cli/src/commonMain/kotlin/ktpack/commands/KtpackCommand.kt b/ktpack-cli/src/commonMain/kotlin/ktpack/commands/KtpackCommand.kt index 9ae6dba..9662195 100644 --- a/ktpack-cli/src/commonMain/kotlin/ktpack/commands/KtpackCommand.kt +++ b/ktpack-cli/src/commonMain/kotlin/ktpack/commands/KtpackCommand.kt @@ -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.") @@ -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(path.readString()) - .resolveDependencyShorthand() - } - override val http: HttpClient by lazy { HttpClient { install(ContentNegotiation) { diff --git a/ktpack-cli/src/commonMain/kotlin/ktpack/commands/NewCommand.kt b/ktpack-cli/src/commonMain/kotlin/ktpack/commands/NewCommand.kt index 3602a9d..6428714 100644 --- a/ktpack-cli/src/commonMain/kotlin/ktpack/commands/NewCommand.kt +++ b/ktpack-cli/src/commonMain/kotlin/ktpack/commands/NewCommand.kt @@ -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 diff --git a/ktpack-cli/src/commonMain/kotlin/ktpack/commands/RunCommand.kt b/ktpack-cli/src/commonMain/kotlin/ktpack/commands/RunCommand.kt index 3ef1e80..03ec28a 100644 --- a/ktpack-cli/src/commonMain/kotlin/ktpack/commands/RunCommand.kt +++ b/ktpack-cli/src/commonMain/kotlin/ktpack/commands/RunCommand.kt @@ -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 diff --git a/ktpack-cli/src/commonMain/kotlin/ktpack/commands/TestCommand.kt b/ktpack-cli/src/commonMain/kotlin/ktpack/commands/TestCommand.kt index 09f12d6..cfc8c4d 100644 --- a/ktpack-cli/src/commonMain/kotlin/ktpack/commands/TestCommand.kt +++ b/ktpack-cli/src/commonMain/kotlin/ktpack/commands/TestCommand.kt @@ -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) diff --git a/ktpack-cli/src/commonTest/kotlin/ktpack/TestCliContext.kt b/ktpack-cli/src/commonTest/kotlin/ktpack/TestCliContext.kt index 3577b03..b64b263 100644 --- a/ktpack-cli/src/commonTest/kotlin/ktpack/TestCliContext.kt +++ b/ktpack-cli/src/commonTest/kotlin/ktpack/TestCliContext.kt @@ -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 @@ -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}'" diff --git a/ktpack-cli/src/commonMain/kotlin/ktpack/util/MeasureSeconds.kt b/ktpack-internal/base/src/commonMain/kotlin/ktpack/util/MeasureSeconds.kt similarity index 100% rename from ktpack-cli/src/commonMain/kotlin/ktpack/util/MeasureSeconds.kt rename to ktpack-internal/base/src/commonMain/kotlin/ktpack/util/MeasureSeconds.kt diff --git a/ktpack-internal/manifest-loader/build.gradle.kts b/ktpack-internal/manifest-loader/build.gradle.kts new file mode 100644 index 0000000..434db34 --- /dev/null +++ b/ktpack-internal/manifest-loader/build.gradle.kts @@ -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) + } + } + } +} diff --git a/ktpack-internal/manifest-loader/src/commonMain/kotlin/ktpack/manifest/DefaultManifestLoader.kt b/ktpack-internal/manifest-loader/src/commonMain/kotlin/ktpack/manifest/DefaultManifestLoader.kt new file mode 100644 index 0000000..45d8a31 --- /dev/null +++ b/ktpack-internal/manifest-loader/src/commonMain/kotlin/ktpack/manifest/DefaultManifestLoader.kt @@ -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(path.readString()) + .resolveDependencyShorthand() + } +} diff --git a/ktpack-internal/manifest-loader/src/commonMain/kotlin/ktpack/manifest/ManifestLoader.kt b/ktpack-internal/manifest-loader/src/commonMain/kotlin/ktpack/manifest/ManifestLoader.kt new file mode 100644 index 0000000..ae74d99 --- /dev/null +++ b/ktpack-internal/manifest-loader/src/commonMain/kotlin/ktpack/manifest/ManifestLoader.kt @@ -0,0 +1,10 @@ +package ktpack.manifest + +const val MANIFEST_FILENAME = "pack.toml" + +interface ManifestLoader { + + fun load( + filePath: String = MANIFEST_FILENAME + ): ManifestToml +} diff --git a/ktpack-internal/module-builder/build.gradle.kts b/ktpack-internal/module-builder/build.gradle.kts new file mode 100644 index 0000000..74abd9b --- /dev/null +++ b/ktpack-internal/module-builder/build.gradle.kts @@ -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) + } + } + } +} diff --git a/ktpack-cli/src/commonMain/kotlin/ktpack/compilation/ArtifactResult.kt b/ktpack-internal/module-builder/src/commonMain/kotlin/ktpack/compilation/ArtifactResult.kt similarity index 100% rename from ktpack-cli/src/commonMain/kotlin/ktpack/compilation/ArtifactResult.kt rename to ktpack-internal/module-builder/src/commonMain/kotlin/ktpack/compilation/ArtifactResult.kt diff --git a/ktpack-internal/module-builder/src/commonMain/kotlin/ktpack/compilation/BuildContext.kt b/ktpack-internal/module-builder/src/commonMain/kotlin/ktpack/compilation/BuildContext.kt new file mode 100644 index 0000000..a4aaa65 --- /dev/null +++ b/ktpack-internal/module-builder/src/commonMain/kotlin/ktpack/compilation/BuildContext.kt @@ -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 diff --git a/ktpack-cli/src/commonMain/kotlin/ktpack/compilation/ModuleBuilder.kt b/ktpack-internal/module-builder/src/commonMain/kotlin/ktpack/compilation/ModuleBuilder.kt similarity index 95% rename from ktpack-cli/src/commonMain/kotlin/ktpack/compilation/ModuleBuilder.kt rename to ktpack-internal/module-builder/src/commonMain/kotlin/ktpack/compilation/ModuleBuilder.kt index 43221ab..f438acb 100644 --- a/ktpack-cli/src/commonMain/kotlin/ktpack/compilation/ModuleBuilder.kt +++ b/ktpack-internal/module-builder/src/commonMain/kotlin/ktpack/compilation/ModuleBuilder.kt @@ -10,23 +10,20 @@ import kotlinx.io.files.SystemFileSystem import kotlinx.io.files.SystemTemporaryDirectory import ksubprocess.* import ktpack.* -import ktpack.compilation.dependencies.MavenDependencyResolver import ktpack.compilation.dependencies.models.DependencyNode import ktpack.compilation.dependencies.models.resolveAndFlatten import ktpack.configuration.* -import ktpack.manifest.DependencyToml -import ktpack.manifest.ManifestToml -import ktpack.manifest.OutputToml +import ktpack.manifest.* import ktpack.util.* /** - * This class uses a Ktpack [ModuleConf] generated by a pack.toml manifest + * This class uses a Ktpack [ModuleToml] generated by a pack.toml manifest * and a specific action selected by the CLI to produce kotlin compiler * arguments with the correct list of kotlin source files and libraries. * - * @param module The root module configuration to build. - * @param context The cli run context. + * @param manifest The root manifest configuration to build. * @param modulePath The root directory containing the pack.toml manifest. + * @param context The build context. */ // https://github.com/JetBrains/kotlin/blob/master/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2NativeCompilerArguments.kt // https://github.com/JetBrains/kotlin/blob/master/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JSCompilerArguments.kt @@ -35,8 +32,8 @@ import ktpack.util.* // https://kotlinlang.org/docs/compiler-reference.html class ModuleBuilder( private val manifest: ManifestToml, - private val context: CliContext, val modulePath: Path, + private val context: BuildContext ) { enum class BuildType { BIN, LIB, TEST } @@ -46,7 +43,6 @@ class ModuleBuilder( private val srcFolder = Path(modulePath, "src") private val module = manifest.module - private val resolver = MavenDependencyResolver(context.http) internal val sourceCollector = KtpackSourceCollector(srcFolder) init { @@ -283,7 +279,7 @@ class ModuleBuilder( val (mavenDependencies, mavenDuration) = measureSeconds { val deps = root.filter { it.dependencyConf is DependencyToml.Maven } .resolveAndFlatten() - resolver.resolveArtifacts(deps, releaseMode, target) + context.resolver.resolveArtifacts(deps, releaseMode, target) } logger.d { "Assembled maven dependencies in ${mavenDuration}s" } @@ -311,7 +307,11 @@ class ModuleBuilder( ): DependencyNode { val localManifest = checkNotNull(child.localManifest) val childPath = Path(modulePath, localManifest.module.name) - val childBuilder = ModuleBuilder(localManifest, context, childPath) + val childBuilder = ModuleBuilder( + manifest = localManifest, + modulePath = childPath, + context = context + ) val innerDepNodes = child.children .filter { it.dependencyConf is DependencyToml.Local } @@ -392,7 +392,7 @@ class ModuleBuilder( artifacts = emptyList(), ) // TODO: address default target selection - return resolver.resolve(node, false, targets.first()) + return context.resolver.resolve(node, false, targets.first()) } private suspend fun resolveLocalDependency( @@ -403,7 +403,7 @@ class ModuleBuilder( includeCommon: Boolean, ): DependencyNode { val packFile = Path(rootFolder, dependencyConf.path, MANIFEST_FILENAME) - val localModule = context.loadManifestToml(packFile.toString()) + val localModule = context.load(packFile.toString()) val children = resolveDependencyTree( localModule, Path(rootFolder, dependencyConf.path), @@ -433,7 +433,7 @@ class ModuleBuilder( check(output.isAbsolute) { "ModuleBuilder.startKotlinCompiler(...) requires `output` to be an absolute path: $output" } - val defaultJdk = checkNotNull(context.jdkInstalls.getDefaultJdk()) { + val defaultJdk = checkNotNull(context.jdk.getDefaultJdk()) { "Run `ktpack setup` before using your project" } val outputDir = checkNotNull(output.parent).mkdirs() @@ -466,12 +466,10 @@ class ModuleBuilder( addAll(sourceFiles) } - val kotlincPath = if (target.isJs) { - context.kotlinInstalls.findKotlincJs(kotlinVersion) - } else if (target.isNative) { - context.kotlinInstalls.findKotlincNative(kotlinVersion) - } else { - context.kotlinInstalls.findKotlincJvm(kotlinVersion) + val kotlincPath = when { + target.isJs -> context.kotlinc.findKotlincJs(kotlinVersion) + target.isNative -> context.kotlinc.findKotlincNative(kotlinVersion) + else -> context.kotlinc.findKotlincJvm(kotlinVersion) } logger.d { "Launching Kotlin Compiler: $kotlincPath ${argsList.joinToString(" ")}" } return withKotlinCompilerArgFile(argsList) { argsFile -> @@ -564,7 +562,7 @@ class ModuleBuilder( libs: List, includes: List, ) { - val kotlincPath = Path(context.kotlinInstalls.findKotlincJs(kotlinVersion)) + val kotlincPath = Path(context.kotlinc.findKotlincJs(kotlinVersion)) if (isBinary) { add("-main") diff --git a/ktpack-cli/src/commonMain/kotlin/ktpack/compilation/SourceCollector.kt b/ktpack-internal/module-builder/src/commonMain/kotlin/ktpack/compilation/SourceCollector.kt similarity index 100% rename from ktpack-cli/src/commonMain/kotlin/ktpack/compilation/SourceCollector.kt rename to ktpack-internal/module-builder/src/commonMain/kotlin/ktpack/compilation/SourceCollector.kt diff --git a/ktpack-cli/src/commonTest/kotlin/ktpack/compilation/ModuleBuilderTests.kt b/ktpack-internal/module-builder/src/commonTest/kotlin/ktpack/compilation/ModuleBuilderTests.kt similarity index 57% rename from ktpack-cli/src/commonTest/kotlin/ktpack/compilation/ModuleBuilderTests.kt rename to ktpack-internal/module-builder/src/commonTest/kotlin/ktpack/compilation/ModuleBuilderTests.kt index 9f87781..e084c0a 100644 --- a/ktpack-cli/src/commonTest/kotlin/ktpack/compilation/ModuleBuilderTests.kt +++ b/ktpack-internal/module-builder/src/commonTest/kotlin/ktpack/compilation/ModuleBuilderTests.kt @@ -1,17 +1,49 @@ package ktpack.compilation +import io.ktor.client.* import kotlinx.io.files.Path -import ktpack.TestCliContext +import ktpack.KtpackUserConfig import ktpack.compilation.ModuleBuilder.BuildType +import ktpack.compilation.dependencies.MavenDependencyResolver import ktpack.configuration.KotlinTarget +import ktpack.manifest.DefaultManifestLoader +import ktpack.manifest.MANIFEST_FILENAME import ktpack.manifest.ManifestToml import ktpack.manifest.ModuleToml import ktpack.sampleDir +import ktpack.toolchain.jdk.JdkInstalls +import ktpack.toolchain.kotlin.KotlincInstalls +import kotlin.test.AfterTest +import kotlin.test.BeforeTest import kotlin.test.Test class ModuleBuilderTests { - lateinit var builder: ModuleBuilder + private lateinit var http: HttpClient + private lateinit var manifest: ManifestToml + private lateinit var context: BuildContext + private lateinit var builder: ModuleBuilder + + @BeforeTest + fun setup() { + val sampleRoot = Path(sampleDir, "6-dependencies") + val packScript = Path(sampleRoot, MANIFEST_FILENAME) + http = HttpClient() + val config = KtpackUserConfig() + context = BuildContext( + manifestLoader = DefaultManifestLoader(), + resolver = MavenDependencyResolver(http), + jdk = JdkInstalls(config = config.jdk, http = http), + kotlinc = KotlincInstalls(config = config, http = http), + debug = true + ) + manifest = context.load(packScript.toString()) + } + + @AfterTest + fun cleanup() { + http.close() + } @Test fun testCollectSourceFiles_1_basic() { @@ -55,9 +87,9 @@ class ModuleBuilderTests { body: CollectedSource.() -> Unit, ) { builder = ModuleBuilder( - ManifestToml(ModuleToml("test", "0.0.0")), - TestCliContext(), + manifest = ManifestToml(ModuleToml("test", "0.0.0")), modulePath = Path(sampleDir, sample), + context = context, ) if (target == null) { diff --git a/ktpack-cli/src/commonTest/kotlin/ktpack/compilation/dependencies/MavenDependencyResolverTests.kt b/ktpack-internal/module-builder/src/commonTest/kotlin/ktpack/compilation/dependencies/MavenDependencyResolverTests.kt similarity index 80% rename from ktpack-cli/src/commonTest/kotlin/ktpack/compilation/dependencies/MavenDependencyResolverTests.kt rename to ktpack-internal/module-builder/src/commonTest/kotlin/ktpack/compilation/dependencies/MavenDependencyResolverTests.kt index 0168ad1..e159027 100644 --- a/ktpack-cli/src/commonTest/kotlin/ktpack/compilation/dependencies/MavenDependencyResolverTests.kt +++ b/ktpack-internal/module-builder/src/commonTest/kotlin/ktpack/compilation/dependencies/MavenDependencyResolverTests.kt @@ -1,15 +1,21 @@ package ktpack.compilation.dependencies +import io.ktor.client.* import kotlinx.coroutines.test.runTest import kotlinx.io.files.Path -import ktpack.MANIFEST_FILENAME -import ktpack.TestCliContext +import ktpack.KtpackUserConfig +import ktpack.compilation.BuildContext import ktpack.compilation.ModuleBuilder import ktpack.compilation.dependencies.models.resolveAndFlatten import ktpack.configuration.KotlinTarget +import ktpack.manifest.DefaultManifestLoader +import ktpack.manifest.MANIFEST_FILENAME import ktpack.manifest.ManifestToml import ktpack.sampleDir +import ktpack.toolchain.jdk.JdkInstalls +import ktpack.toolchain.kotlin.KotlincInstalls import okio.Path.Companion.DIRECTORY_SEPARATOR +import kotlin.test.AfterTest import kotlin.test.BeforeTest import kotlin.test.Test import kotlin.test.assertTrue @@ -19,23 +25,35 @@ private val timeout = 2.minutes class MavenDependencyResolverTests { - private lateinit var module: ManifestToml - private lateinit var context: TestCliContext + private lateinit var http: HttpClient + private lateinit var manifest: ManifestToml + private lateinit var context: BuildContext private lateinit var builder: ModuleBuilder - private lateinit var resolver: MavenDependencyResolver @BeforeTest - fun setup() = runTest { + fun setup() { val sampleRoot = Path(sampleDir, "6-dependencies") val packScript = Path(sampleRoot, MANIFEST_FILENAME) - context = TestCliContext() - module = context.loadManifestToml(packScript.toString()) + http = HttpClient() + val config = KtpackUserConfig() + context = BuildContext( + manifestLoader = DefaultManifestLoader(), + resolver = MavenDependencyResolver(http), + jdk = JdkInstalls(config = config.jdk, http = http), + kotlinc = KotlincInstalls(config = config, http = http), + debug = true, + ) + manifest = context.load(packScript.toString()) builder = ModuleBuilder( - module, - context, + manifest = manifest, modulePath = sampleRoot, + context = context, ) - resolver = MavenDependencyResolver(context.http) + } + + @AfterTest + fun cleanup() { + http.close() } @Test @@ -43,7 +61,8 @@ class MavenDependencyResolverTests { val depTree = builder.resolveRootDependencyTree(listOf(KotlinTarget.JS_BROWSER)) val flatTree = depTree.resolveAndFlatten() - val children = resolver.resolveArtifacts(flatTree, releaseMode = false, target = KotlinTarget.JS_BROWSER) + val children = + context.resolver.resolveArtifacts(flatTree, releaseMode = false, target = KotlinTarget.JS_BROWSER) val actual = children .flatMap { it.artifacts } .map { it.substringAfterLast(DIRECTORY_SEPARATOR) } @@ -82,7 +101,7 @@ class MavenDependencyResolverTests { val depTree = builder.resolveRootDependencyTree(listOf(KotlinTarget.JVM)) val flatTree = depTree.resolveAndFlatten() - val children = resolver.resolveArtifacts(flatTree, releaseMode = false, target = KotlinTarget.JVM) + val children = context.resolver.resolveArtifacts(flatTree, releaseMode = false, target = KotlinTarget.JVM) val actual = children .flatMap { it.artifacts } .map { it.substringAfterLast(DIRECTORY_SEPARATOR) } @@ -126,7 +145,7 @@ class MavenDependencyResolverTests { val depTree = builder.resolveRootDependencyTree(listOf(KotlinTarget.MINGW_X64)) val flatTree = depTree.resolveAndFlatten() - val children = resolver.resolveArtifacts(flatTree, releaseMode = false, target = KotlinTarget.MINGW_X64) + val children = context.resolver.resolveArtifacts(flatTree, releaseMode = false, target = KotlinTarget.MINGW_X64) val actual = children .flatMap { it.artifacts } .map { it.substringAfterLast(DIRECTORY_SEPARATOR) } diff --git a/settings.gradle.kts b/settings.gradle.kts index 324ae33..3c584b5 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -3,15 +3,17 @@ rootProject.name = "ktpack" include( ":ktpack-cli", ":ktpack-cli-tests", - ":ktpack-internal:compression", ":ktpack-internal:base", + ":ktpack-internal:compression", ":ktpack-internal:dependency-resolver", ":ktpack-internal:dokka", ":ktpack-internal:git", ":ktpack-internal:github", ":ktpack-internal:gradle", + ":ktpack-internal:manifest-loader", ":ktpack-internal:maven", ":ktpack-internal:models", + ":ktpack-internal:module-builder", ":ktpack-internal:platform", ":ktpack-internal:test-utils", ":ktpack-internal:toolchains", @@ -20,6 +22,7 @@ include( ) dependencyResolutionManagement { + @Suppress("UnstableApiUsage") repositories { mavenCentral() }