mirror of
https://github.com/apple/pkl.git
synced 2026-08-26 21:54:03 +02:00
Add pkl project package --install to local cache (#1795)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
* Copyright © 2024-2026 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -31,6 +31,7 @@ class CliProjectPackager(
|
||||
private val testOptions: CliTestOptions,
|
||||
private val outputPath: String,
|
||||
private val skipPublishCheck: Boolean,
|
||||
private val install: Boolean,
|
||||
private val consoleWriter: Writer = System.out.writer(),
|
||||
private val errWriter: Writer = System.err.writer(),
|
||||
) : CliProjectCommand(baseOptions, projectDirs) {
|
||||
@@ -85,6 +86,8 @@ class CliProjectPackager(
|
||||
securityManager,
|
||||
httpClient,
|
||||
skipPublishCheck,
|
||||
install,
|
||||
moduleCacheDir,
|
||||
consoleWriter,
|
||||
)
|
||||
.createPackages()
|
||||
|
||||
@@ -21,6 +21,7 @@ import com.github.ajalt.clikt.core.subcommands
|
||||
import com.github.ajalt.clikt.parameters.arguments.argument
|
||||
import com.github.ajalt.clikt.parameters.arguments.multiple
|
||||
import com.github.ajalt.clikt.parameters.groups.provideDelegate
|
||||
import com.github.ajalt.clikt.parameters.options.check
|
||||
import com.github.ajalt.clikt.parameters.options.default
|
||||
import com.github.ajalt.clikt.parameters.options.flag
|
||||
import com.github.ajalt.clikt.parameters.options.option
|
||||
@@ -128,6 +129,15 @@ class PackageCommand : BaseCommand(name = "package", helpLink = helpLink) {
|
||||
.single()
|
||||
.flag()
|
||||
|
||||
private val install: Boolean by
|
||||
option(
|
||||
names = arrayOf("--install"),
|
||||
help = "Install the built package into the module cache dir",
|
||||
)
|
||||
.single()
|
||||
.flag()
|
||||
.check("not compatible with --no-cache") { !(it && baseOptions.noCache) }
|
||||
|
||||
override fun run() {
|
||||
CliProjectPackager(
|
||||
baseOptions.baseOptions(emptyList()),
|
||||
@@ -135,6 +145,7 @@ class PackageCommand : BaseCommand(name = "package", helpLink = helpLink) {
|
||||
testOptions.cliTestOptions,
|
||||
outputPath,
|
||||
skipPublishCheck,
|
||||
install,
|
||||
)
|
||||
.run()
|
||||
}
|
||||
|
||||
@@ -60,6 +60,7 @@ class CliProjectPackagerTest {
|
||||
CliTestOptions(),
|
||||
".out/%{name}@%{version}",
|
||||
skipPublishCheck = true,
|
||||
install = false,
|
||||
)
|
||||
val err = assertThrows<CliException> { packager.run() }
|
||||
assertThat(err).hasMessageStartingWith("No project visible to the working directory.")
|
||||
@@ -74,6 +75,7 @@ class CliProjectPackagerTest {
|
||||
CliTestOptions(),
|
||||
".out/%{name}@%{version}",
|
||||
skipPublishCheck = true,
|
||||
install = false,
|
||||
)
|
||||
val err = assertThrows<CliException> { packager.run() }
|
||||
assertThat(err).hasMessageStartingWith("Directory $tempDir does not contain a PklProject file.")
|
||||
@@ -96,6 +98,7 @@ class CliProjectPackagerTest {
|
||||
CliTestOptions(),
|
||||
".out/%{name}@%{version}",
|
||||
skipPublishCheck = true,
|
||||
install = false,
|
||||
)
|
||||
val err = assertThrows<CliException> { packager.run() }
|
||||
assertThat(err)
|
||||
@@ -140,6 +143,7 @@ class CliProjectPackagerTest {
|
||||
CliTestOptions(),
|
||||
".out/%{name}@%{version}",
|
||||
skipPublishCheck = true,
|
||||
install = false,
|
||||
consoleWriter = buffer,
|
||||
)
|
||||
val err = assertThrows<CliException> { packager.run() }
|
||||
@@ -187,6 +191,7 @@ class CliProjectPackagerTest {
|
||||
CliTestOptions(),
|
||||
".out/%{name}@%{version}",
|
||||
skipPublishCheck = true,
|
||||
install = false,
|
||||
consoleWriter = buffer,
|
||||
)
|
||||
packager.run()
|
||||
@@ -286,6 +291,7 @@ class CliProjectPackagerTest {
|
||||
CliTestOptions(),
|
||||
".out",
|
||||
skipPublishCheck = true,
|
||||
install = false,
|
||||
consoleWriter = buffer,
|
||||
)
|
||||
packager.run()
|
||||
@@ -337,6 +343,7 @@ class CliProjectPackagerTest {
|
||||
CliTestOptions(),
|
||||
".out/%{name}@%{version}",
|
||||
skipPublishCheck = true,
|
||||
install = false,
|
||||
consoleWriter = StringWriter(),
|
||||
)
|
||||
packager.run()
|
||||
@@ -419,6 +426,7 @@ class CliProjectPackagerTest {
|
||||
CliTestOptions(),
|
||||
".out/%{name}@%{version}",
|
||||
skipPublishCheck = true,
|
||||
install = false,
|
||||
consoleWriter = StringWriter(),
|
||||
)
|
||||
.run()
|
||||
@@ -517,6 +525,7 @@ class CliProjectPackagerTest {
|
||||
CliTestOptions(),
|
||||
".out/%{name}@%{version}",
|
||||
skipPublishCheck = true,
|
||||
install = false,
|
||||
consoleWriter = StringWriter(),
|
||||
)
|
||||
.run()
|
||||
@@ -655,6 +664,7 @@ class CliProjectPackagerTest {
|
||||
CliTestOptions(),
|
||||
".out/%{name}@%{version}",
|
||||
skipPublishCheck = true,
|
||||
install = false,
|
||||
consoleWriter = StringWriter(),
|
||||
)
|
||||
.run()
|
||||
@@ -695,6 +705,7 @@ class CliProjectPackagerTest {
|
||||
CliTestOptions(),
|
||||
".out/%{name}@%{version}",
|
||||
skipPublishCheck = true,
|
||||
install = false,
|
||||
consoleWriter = StringWriter(),
|
||||
)
|
||||
.run()
|
||||
@@ -749,6 +760,7 @@ class CliProjectPackagerTest {
|
||||
CliTestOptions(),
|
||||
".out/%{name}@%{version}",
|
||||
skipPublishCheck = true,
|
||||
install = false,
|
||||
consoleWriter = StringWriter(),
|
||||
)
|
||||
.run()
|
||||
@@ -795,6 +807,7 @@ class CliProjectPackagerTest {
|
||||
CliTestOptions(),
|
||||
".out/%{name}@%{version}",
|
||||
skipPublishCheck = true,
|
||||
install = false,
|
||||
consoleWriter = StringWriter(),
|
||||
)
|
||||
.run()
|
||||
@@ -832,6 +845,7 @@ class CliProjectPackagerTest {
|
||||
CliTestOptions(),
|
||||
".out/%{name}@%{version}",
|
||||
skipPublishCheck = true,
|
||||
install = false,
|
||||
consoleWriter = StringWriter(),
|
||||
)
|
||||
.run()
|
||||
@@ -875,6 +889,7 @@ class CliProjectPackagerTest {
|
||||
CliTestOptions(),
|
||||
".out/%{name}@%{version}",
|
||||
skipPublishCheck = true,
|
||||
install = false,
|
||||
consoleWriter = StringWriter(),
|
||||
)
|
||||
.run()
|
||||
@@ -919,6 +934,7 @@ class CliProjectPackagerTest {
|
||||
CliTestOptions(),
|
||||
".out/%{name}@%{version}",
|
||||
skipPublishCheck = true,
|
||||
install = false,
|
||||
consoleWriter = out,
|
||||
)
|
||||
.run()
|
||||
@@ -974,6 +990,7 @@ class CliProjectPackagerTest {
|
||||
CliTestOptions(),
|
||||
".out/%{name}@%{version}",
|
||||
skipPublishCheck = false,
|
||||
install = false,
|
||||
consoleWriter = StringWriter(),
|
||||
)
|
||||
.run()
|
||||
@@ -1018,6 +1035,7 @@ class CliProjectPackagerTest {
|
||||
CliTestOptions(),
|
||||
".out/%{name}@%{version}",
|
||||
skipPublishCheck = false,
|
||||
install = false,
|
||||
consoleWriter = out,
|
||||
)
|
||||
.run()
|
||||
@@ -1062,6 +1080,7 @@ class CliProjectPackagerTest {
|
||||
CliTestOptions(),
|
||||
".out/%{name}@%{version}",
|
||||
skipPublishCheck = true,
|
||||
install = false,
|
||||
consoleWriter = StringWriter(),
|
||||
)
|
||||
packager.run()
|
||||
@@ -1121,6 +1140,73 @@ class CliProjectPackagerTest {
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `install package to local cache`(@TempDir tempDir: Path, @TempDir cacheDir: Path) {
|
||||
tempDir
|
||||
.resolve("PklProject")
|
||||
.writeString(
|
||||
"""
|
||||
amends "pkl:Project"
|
||||
|
||||
package {
|
||||
name = "mypackage"
|
||||
version = "1.0.0"
|
||||
baseUri = "package://example.com/mypackage"
|
||||
packageZipUrl = "https://foo.com"
|
||||
}
|
||||
"""
|
||||
.trimIndent()
|
||||
)
|
||||
val packager =
|
||||
CliProjectPackager(
|
||||
CliBaseOptions(workingDir = tempDir, moduleCacheDir = cacheDir),
|
||||
listOf(tempDir),
|
||||
CliTestOptions(),
|
||||
".out/%{name}@%{version}",
|
||||
skipPublishCheck = true,
|
||||
install = true,
|
||||
consoleWriter = StringWriter(),
|
||||
)
|
||||
packager.run()
|
||||
|
||||
assertThat(cacheDir.resolve("package-2/example.com/mypackage@1.0.0/mypackage@1.0.0.json"))
|
||||
.exists()
|
||||
assertThat(cacheDir.resolve("package-2/example.com/mypackage@1.0.0/mypackage@1.0.0.zip"))
|
||||
.exists()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `cannot install with no cache`(@TempDir tempDir: Path) {
|
||||
tempDir
|
||||
.resolve("PklProject")
|
||||
.writeString(
|
||||
"""
|
||||
amends "pkl:Project"
|
||||
|
||||
package {
|
||||
name = "mypackage"
|
||||
version = "1.0.0"
|
||||
baseUri = "package://example.com/mypackage"
|
||||
packageZipUrl = "https://foo.com"
|
||||
}
|
||||
"""
|
||||
.trimIndent()
|
||||
)
|
||||
val packager =
|
||||
CliProjectPackager(
|
||||
CliBaseOptions(workingDir = tempDir, noCache = true),
|
||||
listOf(tempDir),
|
||||
CliTestOptions(),
|
||||
".out/%{name}@%{version}",
|
||||
skipPublishCheck = true,
|
||||
install = true,
|
||||
consoleWriter = StringWriter(),
|
||||
)
|
||||
val exc = assertThrows<CliException> { packager.run() }
|
||||
assertThat(exc.message)
|
||||
.isEqualTo("Cannot install package to module cache dir when module cache is disabled.")
|
||||
}
|
||||
|
||||
private fun Path.zipFilePaths(): List<String> {
|
||||
return FileSystems.newFileSystem(URI("jar:${toUri()}"), emptyMap<String, String>()).use { fs ->
|
||||
Files.walk(fs.getPath("/")).map(IoUtils::toNormalizedPathString).collect(Collectors.toList())
|
||||
|
||||
Reference in New Issue
Block a user