Repository: apple/pkl
File: pkl-cli/pkl-cli.gradle.kts (Kotlin)

Code before:
    .files(sourceSets.main.map { it.output })
    .withPropertyName("mainSourceSets")
    .withPathSensitivity(PathSensitivity.RELATIVE)
  inputs
    .files(configurations.runtimeClasspath)
    .withPropertyName("runtimeClasspath")
    .withNormalizer(ClasspathNormalizer::class)
  val nativeImageCommandName = if (buildInfo.os.isWindows) "native-image.cmd" else "native-image"
  inputs
    .files(file(graalVm.baseDir).resolve("bin/$nativeImageCommandName"))
    .withPropertyName("graalVmNativeImage")
    .withPathSensitivity(PathSensitivity.ABSOLUTE)
  outputs.file(outputFile)
  outputs.cacheIf { true }

  workingDir(outputFile.map { it.asFile.parentFile })
  executable = "${graalVm.baseDir}/bin/$nativeImageCommandName"

  // For any system properties starting with `pkl.native`, strip off that prefix and pass the rest
  // through as arguments to native-image.
  //
  // Allow setting args using flags like
  // (-Dpkl.native-Dpolyglot.engine.userResourceCache=/my/cache/dir) when building through Gradle.
  val extraArgsFromProperties =
    System.getProperties().mapNotNull { (key, value) ->
      if (!key.toString().startsWith("pkl.native")) null
      else "$key=$value".substring("pkl.native".length)
    }

  // JARs to exclude from the class path for the native-image build.
  val exclusions = listOf(libs.graalSdk).map { it.get().module.name }
  // https://www.graalvm.org/22.0/reference-manual/native-image/Options/
  argumentProviders.add(
    CommandLineArgumentProvider {
      buildList {
        // must be emitted before any experimental options are used
        add("-H:+UnlockExperimentalVMOptions")
        // currently gives a deprecation warning, but we've been told
        // that the "initialize everything at build time" *CLI* option is likely here to stay
        add("--initialize-at-build-time=")
        // needed for messagepack-java (see https://github.com/msgpack/msgpack-java/issues/600)
        add("--initialize-at-run-time=org.msgpack.core.buffer.DirectBufferAccess")
        add("--no-fallback")
        add("-H:IncludeResources=org/pkl/core/stdlib/.*\\.pkl")
        add("-H:IncludeResources=org/jline/utils/.*")
        add("-H:IncludeResourceBundles=org.pkl.core.errorMessages")
        add("-H:IncludeResources=org/pkl/commons/cli/PklCARoots.pem")
        add("-H:Class=org.pkl.cli.Main")
        add("-o")
        add(outputFile.get().asFile.name)
        // the actual limit (currently) used by native-image is this number + 1400 (idea is to

Review comment (suggestion):
I don't like the negation here 🫣
Could we use filter with just map instead?
I guess it reads a bit better
`filter { key.startsWith(pkl.native) }`.
Or does it have to be filterNot? 🤔 😂 

Code after:
    .files(sourceSets.main.map { it.output })
    .withPropertyName("mainSourceSets")
    .withPathSensitivity(PathSensitivity.RELATIVE)
  inputs
    .files(configurations.runtimeClasspath)
    .withPropertyName("runtimeClasspath")
    .withNormalizer(ClasspathNormalizer::class)
  val nativeImageCommandName = if (buildInfo.os.isWindows) "native-image.cmd" else "native-image"
  inputs
    .files(file(graalVm.baseDir).resolve("bin/$nativeImageCommandName"))
    .withPropertyName("graalVmNativeImage")
    .withPathSensitivity(PathSensitivity.ABSOLUTE)
  outputs.file(outputFile)
  outputs.cacheIf { true }

  workingDir(outputFile.map { it.asFile.parentFile })
  executable = "${graalVm.baseDir}/bin/$nativeImageCommandName"

  // For any system properties starting with `pkl.native`, strip off that prefix and pass the rest
  // through as arguments to native-image.
  //
  // Allow setting args using flags like
  // (-Dpkl.native-Dpolyglot.engine.userResourceCache=/my/cache/dir) when building through Gradle.
  val extraArgsFromProperties =
    System.getProperties()
      .filter { it.key.toString().startsWith("pkl.native") }
      .map { "${it.key}=${it.value}".substring("pkl.native".length) }

  // JARs to exclude from the class path for the native-image build.
  val exclusions = listOf(libs.graalSdk).map { it.get().module.name }
  // https://www.graalvm.org/22.0/reference-manual/native-image/Options/
  argumentProviders.add(
    CommandLineArgumentProvider {
      buildList {
        // must be emitted before any experimental options are used
        add("-H:+UnlockExperimentalVMOptions")
        // currently gives a deprecation warning, but we've been told
        // that the "initialize everything at build time" *CLI* option is likely here to stay
        add("--initialize-at-build-time=")
        // needed for messagepack-java (see https://github.com/msgpack/msgpack-java/issues/600)
        add("--initialize-at-run-time=org.msgpack.core.buffer.DirectBufferAccess")
        add("--no-fallback")
        add("-H:IncludeResources=org/pkl/core/stdlib/.*\\.pkl")
        add("-H:IncludeResources=org/jline/utils/.*")
        add("-H:IncludeResourceBundles=org.pkl.core.errorMessages")
        add("-H:IncludeResources=org/pkl/commons/cli/PklCARoots.pem")
        add("-H:Class=org.pkl.cli.Main")
        add("-o")
        add(outputFile.get().asFile.name)
        // the actual limit (currently) used by native-image is this number + 1400 (idea is to
        // compensate for Truffle's own nodes)