Encode filepaths to be safe on Windows

This changes the file paths to use characters that are safe for Windows.

Channges the output of the following:
* Package cache directory
* Generated pkl-doc files
* Kotlin generated code

Unsafe characters are encoded as (<hex>).
For example, the colon character `:` is encoded as `(3a)`.

Additionally, this changes the cache directory prefix (package-1 to
package-2).

Follows the design of https://github.com/apple/pkl-evolution/pull/3
This commit is contained in:
Dan Chao
2024-04-26 07:34:31 -07:00
committed by Daniel Chao
parent 110dc89e86
commit a5c13e325a
36 changed files with 185 additions and 106 deletions

View File

@@ -22,6 +22,7 @@ import java.net.URI
import java.util.*
import org.pkl.core.*
import org.pkl.core.util.CodeGeneratorUtils
import org.pkl.core.util.IoUtils
data class KotlinCodegenOptions(
/** The characters to use for indenting generated Kotlin code. */
@@ -89,7 +90,7 @@ class KotlinCodeGenerator(
private val propertyFileName: String
get() =
"resources/META-INF/org/pkl/config/java/mapper/classes/${moduleSchema.moduleName}.properties"
"resources/META-INF/org/pkl/config/java/mapper/classes/${IoUtils.encodePath(moduleSchema.moduleName)}.properties"
private val propertiesFile: String
get() {
@@ -195,7 +196,7 @@ class KotlinCodeGenerator(
}
private fun relativeOutputPathFor(moduleName: String): String {
val nameParts = moduleName.split(".")
val nameParts = moduleName.split(".").map(IoUtils::encodePath)
val dirPath = nameParts.dropLast(1).joinToString("/")
val fileName = nameParts.last().replaceFirstChar { it.titlecaseChar() }
return if (dirPath.isEmpty()) {

View File

@@ -1501,6 +1501,24 @@ class KotlinCodeGeneratorTest {
confirmSerDe(bigStruct)
}
@Test
fun `encoded file paths`(@TempDir path: Path) {
val kotlinCode =
generateKotlinFiles(
path,
PklModule(
"FooBar.pkl",
"""
module `Foo*Bar`
someProp: String
"""
.trimIndent()
)
)
assertThat(kotlinCode).containsKey("kotlin/Foo(2a)Bar.kt")
}
private fun generateFiles(tempDir: Path, vararg pklModules: PklModule): Map<String, String> {
val pklFiles = pklModules.map { it.writeToDisk(tempDir.resolve("pkl/${it.name}.pkl")) }
val evaluator = Evaluator.preconfigured()