Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions pkl-gradle/src/main/java/org/pkl/gradle/ExternalReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright © 2025 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pkl.gradle;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

public record ExternalReader(String executable, List<String> arguments) implements Serializable {
public ExternalReader(String executable) {
this(executable, Collections.emptyList());
}
}
2 changes: 2 additions & 0 deletions pkl-gradle/src/main/java/org/pkl/gradle/PklPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,8 @@ private <T extends BasePklTask, S extends BasePklSpec> void configureBaseTask(T
task.getHttpProxy().set(spec.getHttpProxy());
task.getHttpNoProxy().set(spec.getHttpNoProxy());
task.getHttpRewrites().set(spec.getHttpRewrites());
task.getExternalModuleReaders().set(spec.getExternalModuleReaders());
task.getExternalResourceReaders().set(spec.getExternalResourceReaders());
}

private List<File> getTransitiveModules(AnalyzeImportsTask analyzeTask) {
Expand Down
5 changes: 5 additions & 0 deletions pkl-gradle/src/main/java/org/pkl/gradle/spec/BasePklSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.gradle.api.provider.ListProperty;
import org.gradle.api.provider.MapProperty;
import org.gradle.api.provider.Property;
import org.pkl.gradle.ExternalReader;

/** Configuration options shared between plugin features. Documented in user manual. */
public interface BasePklSpec {
Expand Down Expand Up @@ -59,4 +60,8 @@ public interface BasePklSpec {
ListProperty<String> getHttpNoProxy();

MapProperty<URI, URI> getHttpRewrites();

MapProperty<String, ExternalReader> getExternalModuleReaders();

MapProperty<String, ExternalReader> getExternalResourceReaders();
}
23 changes: 21 additions & 2 deletions pkl-gradle/src/main/java/org/pkl/gradle/task/BasePklTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@
import org.gradle.api.tasks.TaskAction;
import org.pkl.commons.cli.CliBaseOptions;
import org.pkl.core.evaluatorSettings.Color;
import org.pkl.core.evaluatorSettings.PklEvaluatorSettings;
import org.pkl.core.util.LateInit;
import org.pkl.core.util.Nullable;
import org.pkl.gradle.ExternalReader;
import org.pkl.gradle.utils.PluginUtils;

public abstract class BasePklTask extends DefaultTask {
Expand Down Expand Up @@ -146,6 +148,12 @@ public Provider<String> getEvalRootDirPath() {
@Optional
public abstract MapProperty<URI, URI> getHttpRewrites();

@Input
public abstract MapProperty<String, ExternalReader> getExternalModuleReaders();

@Input
public abstract MapProperty<String, ExternalReader> getExternalResourceReaders();

/**
* There are issues with using native libraries in Gradle plugins. As a workaround for now, make
* Truffle use an un-optimized runtime.
Expand Down Expand Up @@ -200,8 +208,8 @@ protected CliBaseOptions getCliBaseOptions() {
getHttpProxy().getOrNull(),
getHttpNoProxy().getOrElse(List.of()),
getHttpRewrites().getOrNull(),
Map.of(),
Map.of());
parseExternalReaders(getExternalModuleReaders()),
parseExternalReaders(getExternalResourceReaders()));
}
return cachedOptions;
}
Expand Down Expand Up @@ -236,4 +244,15 @@ protected List<Pattern> patternsFromStrings(List<String> patterns) {
@Nullable T value = provider.getOrNull();
return value == null ? null : f.apply(value);
}

protected Map<String, PklEvaluatorSettings.ExternalReader> parseExternalReaders(
Provider<Map<String, ExternalReader>> provider) {
return provider.getOrElse(Map.of()).entrySet().stream()
.collect(
Collectors.toMap(
Map.Entry::getKey,
(it) ->
new PklEvaluatorSettings.ExternalReader(
it.getValue().executable(), it.getValue().arguments())));
}
}
4 changes: 2 additions & 2 deletions pkl-gradle/src/main/java/org/pkl/gradle/task/ModulesTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ protected CliBaseOptions getCliBaseOptions() {
null,
List.of(),
getHttpRewrites().getOrNull(),
Map.of(),
Map.of());
parseExternalReaders(getExternalModuleReaders()),
parseExternalReaders(getExternalResourceReaders()));
}
return cachedOptions;
}
Expand Down
40 changes: 40 additions & 0 deletions pkl-gradle/src/test/kotlin/org/pkl/gradle/EvaluatorsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,46 @@ class EvaluatorsTest : AbstractTest() {
assertThat(result5.task(":evalTestGatherImports")).isNull()
}

@Test
fun `external module reader`() {
writePklFile("import \"foo:test.pkl\"")
writeBuildFile(
"json",
additionalContents =
"""
allowedModules = ["foo:", "file:", "repl:text"]
externalModuleReaders = ["foo": new org.pkl.gradle.ExternalReader("echo")]
"""
.trimIndent(),
)

// this is not actually a valid external reader, so we expect this to fail
// this test is just asserting that Pkl is configured to use the external process
// and that it does attempt to do so
val result = runTask("evalTest", true)
assertThat(result.output).contains("IOException: Stream closed")
}

@Test
fun `external resource reader`() {
writePklFile("result = read(\"foo:test\")")
writeBuildFile(
"json",
additionalContents =
"""
allowedResources = ["foo:", "prop:"]
externalResourceReaders = ["foo": new org.pkl.gradle.ExternalReader("echo")]
"""
.trimIndent(),
)

// this is not actually a valid external reader, so we expect this to fail
// this test is just asserting that Pkl is configured to use the external process
// and that it does attempt to do so
val result = runTask("evalTest", true)
assertThat(result.output).contains("IOException: Stream closed")
}

private fun writeBuildFile(
// don't use `org.pkl.core.OutputFormat`
// because test compile class path doesn't contain pkl-core
Expand Down