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
23 changes: 19 additions & 4 deletions cmd/internal/gen-fixtures/gen-fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,19 @@ func (t testFs) Open(name string) (fs.File, error) {

func evaluateCollections(evaluator pkl.Evaluator, fixturesDir string) {
for _, expr := range []string{"res1", "res2", "res9"} {
outBytes, err := evaluator.EvaluateExpressionRaw(context.Background(), pkl.FileSource(fixturesDir, "collections.pkl"), expr)
outBytes, err := evaluator.EvaluateExpressionRaw(
context.Background(),
pkl.FileSource(fixturesDir, "collections.pkl"),
expr,
)
if err != nil {
panic(err)
}
outPath := filepath.Join(fixturesDir, "msgpack", fmt.Sprintf("collections.%s.msgpack", expr))
outPath := filepath.Join(
fixturesDir,
"msgpack",
fmt.Sprintf("collections.%s.msgpack", expr),
)
if err = os.WriteFile(outPath, outBytes, 0o666); err != nil {
panic(err)
}
Expand All @@ -61,7 +69,11 @@ func makeMsgpack(evaluator pkl.Evaluator, fixturesDir string, files []os.DirEntr
if file.IsDir() {
continue
}
outBytes, err := evaluator.EvaluateExpressionRaw(context.Background(), pkl.UriSource("pklgo:/pkl/test_fixtures/"+file.Name()), "")
outBytes, err := evaluator.EvaluateExpressionRaw(
context.Background(),
pkl.UriSource("pklgo:/pkl/test_fixtures/"+file.Name()),
"",
)
if err != nil {
panic(err)
}
Expand All @@ -82,7 +94,10 @@ func makeGoCode(evaluator pkl.Evaluator, fixturesDir string, files []os.DirEntry
if err := os.RemoveAll(genDir); err != nil {
panic(err)
}
settings, err := generatorsettings.LoadFromPath(context.Background(), "codegen/snippet-tests/generator-settings.pkl")
settings, err := generatorsettings.LoadFromPath(
context.Background(),
"codegen/snippet-tests/generator-settings.pkl",
)
if err != nil {
panic(err)
}
Expand Down
5 changes: 4 additions & 1 deletion cmd/internal/gen-snippets/gen-snippets.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ func makeGoCode(evaluator pkl.Evaluator, snippetsDir string) {
panic(err)
}
codegenDir := filepath.Join(snippetsDir, "..")
settings, err := generatorsettings.LoadFromPath(context.Background(), "codegen/snippet-tests/generator-settings.pkl")
settings, err := generatorsettings.LoadFromPath(
context.Background(),
"codegen/snippet-tests/generator-settings.pkl",
)
if err != nil {
panic(err)
}
Expand Down
31 changes: 26 additions & 5 deletions cmd/pkl-gen-go/pkg/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,19 @@ func doFormat(src string) ([]byte, string, error) {
return formatted, cmp.Diff(src, strFormatted), nil
}

func generateDryRun(evaluator pkl.Evaluator, tmpFile *os.File, outputPath string, settings *generatorsettings.GeneratorSettings) error {
func generateDryRun(
evaluator pkl.Evaluator,
tmpFile *os.File,
outputPath string,
settings *generatorsettings.GeneratorSettings,
) error {
var filenames []string
err := evaluator.EvaluateExpression(context.Background(), pkl.FileSource(tmpFile.Name()), "output.files.toMap().keys.toList()", &filenames)
err := evaluator.EvaluateExpression(
context.Background(),
pkl.FileSource(tmpFile.Name()),
"output.files.toMap().keys.toList()",
&filenames,
)
if err != nil {
return err
}
Expand Down Expand Up @@ -143,23 +153,34 @@ func GenerateGo(
if settings.DryRun {
return generateDryRun(evaluator, tmpFile, outputPath, settings)
}
files, err := evaluator.EvaluateOutputFiles(context.Background(), pkl.FileSource(tmpFile.Name()))
files, err := evaluator.EvaluateOutputFiles(
context.Background(),
pkl.FileSource(tmpFile.Name()),
)
if err != nil {
return err
}
diffs := make(map[string]string)
for filename, contents := range files {
if settings.BasePath != "" {
if !strings.HasPrefix(filename, settings.BasePath) {
log("Skipping codegen for file \033[36m%s\033[0m because it does not exist in base path \033[36m%s\033[0m\n", filename, settings.BasePath)
log(
"Skipping codegen for file \033[36m%s\033[0m because it does not exist in base path \033[36m%s\033[0m\n",
filename,
settings.BasePath,
)
continue
}
filename = strings.TrimPrefix(filename, settings.BasePath)
}

formatted, diff, err := doFormat(contents)
if err != nil {
log("[warning] Attempted to format file %s but it produced an unexpected error. Error: %s\n", filename, err.Error())
log(
"[warning] Attempted to format file %s but it produced an unexpected error. Error: %s\n",
filename,
err.Error(),
)
formatted = []byte(contents)
}
if diff != "" {
Expand Down
79 changes: 67 additions & 12 deletions cmd/pkl-gen-go/pkl-gen-go.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ var Version = "development"

func init() {
info, ok := debug.ReadBuildInfo()
if !ok || info.Main.Version == "" || info.Main.Version == "(devel)" || Version != "development" {
if !ok || info.Main.Version == "" || info.Main.Version == "(devel)" ||
Version != "development" {
return
}
Version = strings.TrimPrefix(info.Main.Version, "v")
Expand All @@ -161,7 +162,12 @@ func generatorSettingsSource() *pkl.ModuleSource {
dirPath := filepath.Dir(filename)
return pkl.FileSource(dirPath, "../../codegen/src/GeneratorSettings.pkl")
}
return pkl.UriSource(fmt.Sprintf("package://pkg.pkl-lang.org/pkl-go/pkl.golang@%s#/GeneratorSettings.pkl", Version))
return pkl.UriSource(
fmt.Sprintf(
"package://pkg.pkl-lang.org/pkl-go/pkl.golang@%s#/GeneratorSettings.pkl",
Version,
),
)
}

// mimick logic for finding project dir in the pkl CLI.
Expand Down Expand Up @@ -189,7 +195,11 @@ func findProjectDir(projectDirFlag string) string {

// Loads the settings for controlling codegen.
// Uses a Pkl evaluator that is separate from what's used for actually running codegen.
func loadGeneratorSettings(generatorSettingsPath string, projectDirFlag string, cacheDirFlag string) (*generatorsettings.GeneratorSettings, error) {
func loadGeneratorSettings(
generatorSettingsPath string,
projectDirFlag string,
cacheDirFlag string,
) (*generatorsettings.GeneratorSettings, error) {
projectDir := findProjectDir(projectDirFlag)
var evaluator pkl.Evaluator
var err error
Expand All @@ -199,7 +209,12 @@ func loadGeneratorSettings(generatorSettingsPath string, projectDirFlag string,
}
}
if projectDir != "" {
evaluator, err = pkl.NewProjectEvaluator(context.Background(), projectDir, pkl.PreconfiguredOptions, opts)
evaluator, err = pkl.NewProjectEvaluator(
context.Background(),
projectDir,
pkl.PreconfiguredOptions,
opts,
)
} else {
evaluator, err = pkl.NewEvaluator(context.Background(), pkl.PreconfiguredOptions, opts)
}
Expand Down Expand Up @@ -241,17 +256,57 @@ func init() {
var dryRun bool
var projectDir string
var cacheDir string
flags.StringVar(&generatorSettingsPath, "generator-settings", "", "The path to a generator settings file")
flags.StringVar(
&generatorSettingsPath,
"generator-settings",
"",
"The path to a generator settings file",
)
flags.StringVar(&generateScript, "generate-script", "", "The Generate.pkl script to use")
flags.StringToStringVar(&mappings, "mapping", nil, "The mapping of a Pkl module name to a Go package name")
flags.StringToStringVar(
&mappings,
"mapping",
nil,
"The mapping of a Pkl module name to a Go package name",
)
flags.StringVar(&basePath, "base-path", "", "The base path used to determine relative output")
flags.StringVar(&outputPath, "output-path", "", "The output directory to write generated sources into")
flags.BoolVar(&suppressWarnings, "suppress-format-warning", false, "Suppress warnings around formatting issues")
flags.StringSliceVar(&allowedModules, "allowed-modules", nil, "URI patterns that determine which modules can be loaded and evaluated")
flags.StringSliceVar(&allowedResources, "allowed-resources", nil, "URI patterns that determine which resources can be loaded and evaluated")
flags.StringVar(&projectDir, "project-dir", "", "The project directory to load dependency and evaluator settings from")
flags.StringVar(
&outputPath,
"output-path",
"",
"The output directory to write generated sources into",
)
flags.BoolVar(
&suppressWarnings,
"suppress-format-warning",
false,
"Suppress warnings around formatting issues",
)
flags.StringSliceVar(
&allowedModules,
"allowed-modules",
nil,
"URI patterns that determine which modules can be loaded and evaluated",
)
flags.StringSliceVar(
&allowedResources,
"allowed-resources",
nil,
"URI patterns that determine which resources can be loaded and evaluated",
)
flags.StringVar(
&projectDir,
"project-dir",
"",
"The project directory to load dependency and evaluator settings from",
)
flags.StringVar(&cacheDir, "cache-dir", "", "The cache directory for storing packages")
flags.BoolVar(&dryRun, "dry-run", false, "Print out the names of the files that will be generated, but don't write any files")
flags.BoolVar(
&dryRun,
"dry-run",
false,
"Print out the names of the files that will be generated, but don't write any files",
)
flags.BoolVar(&printVersion, "version", false, "Print the version and exit")
var err error
if err = flags.Parse(os.Args); err != nil && !errors.Is(err, pflag.ErrHelp) {
Expand Down
7 changes: 6 additions & 1 deletion pkl/decode_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ func (d *decoder) decodeSlice(inType reflect.Type) (*reflect.Value, error) {
return nil, fmt.Errorf("expected array length 2 but got %d", length)
}
if code != codeList && code != codeListing {
return nil, fmt.Errorf("invalid code for slices: %d. Expected %d or %d", code, codeList, codeListing)
return nil, fmt.Errorf(
"invalid code for slices: %d. Expected %d or %d",
code,
codeList,
codeListing,
)
}
return d.decodeSliceImpl(inType)
}
Expand Down
3 changes: 2 additions & 1 deletion pkl/decode_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ func (d *decoder) decodeStructField(fields map[string]structField, out *reflect.
}
sf, exists := fields[propertyName]
if !exists {
log.Default().Printf("warn: Cannot find field on Go struct `%s` matching Pkl property `%s`. Ensure the Go structs are up to date with Pkl classes either through codegen or manually adding `pkl` tags.", out.Type().String(), propertyName)
log.Default().
Printf("warn: Cannot find field on Go struct `%s` matching Pkl property `%s`. Ensure the Go structs are up to date with Pkl classes either through codegen or manually adding `pkl` tags.", out.Type().String(), propertyName)
return d.dec.Skip()
}
code, err := d.dec.PeekCode()
Expand Down
32 changes: 25 additions & 7 deletions pkl/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ type Evaluator interface {
}

type evaluator struct {
evaluatorId int64
logger Logger
manager *evaluatorManager
pendingRequests *sync.Map
closed bool
resourceReaders []ResourceReader
moduleReaders []ModuleReader
evaluatorId int64
closed bool
}

var _ Evaluator = (*evaluator)(nil)
Expand All @@ -88,21 +88,38 @@ func (e *evaluator) EvaluateOutputValue(ctx context.Context, source *ModuleSourc
return e.EvaluateExpression(ctx, source, "output.value", out)
}

func (e *evaluator) EvaluateOutputFiles(ctx context.Context, source *ModuleSource) (map[string]string, error) {
func (e *evaluator) EvaluateOutputFiles(
ctx context.Context,
source *ModuleSource,
) (map[string]string, error) {
var out map[string]string
err := e.EvaluateExpression(ctx, source, "output.files.toMap().mapValues((_, it) -> it.text)", &out)
err := e.EvaluateExpression(
ctx,
source,
"output.files.toMap().mapValues((_, it) -> it.text)",
&out,
)
return out, err
}

func (e *evaluator) EvaluateExpression(ctx context.Context, source *ModuleSource, expr string, out any) error {
func (e *evaluator) EvaluateExpression(
ctx context.Context,
source *ModuleSource,
expr string,
out any,
) error {
bytes, err := e.EvaluateExpressionRaw(ctx, source, expr)
if err != nil {
return err
}
return Unmarshal(bytes, out)
}

func (e *evaluator) EvaluateExpressionRaw(ctx context.Context, source *ModuleSource, expr string) ([]byte, error) {
func (e *evaluator) EvaluateExpressionRaw(
ctx context.Context,
source *ModuleSource,
expr string,
) ([]byte, error) {
if e.Closed() {
return nil, fmt.Errorf("evaluator is closed")
}
Expand Down Expand Up @@ -146,7 +163,8 @@ func (e *evaluator) Closed() bool {
func (e *evaluator) handleEvaluateResponse(resp *msgapi.EvaluateResponse) {
c, exists := e.pendingRequests.Load(resp.RequestId)
if !exists {
log.Default().Printf("warn: received a message for an unknown request id: %d", resp.RequestId)
log.Default().
Printf("warn: received a message for an unknown request id: %d", resp.RequestId)
return
}
ch := c.(chan *msgapi.EvaluateResponse)
Expand Down
19 changes: 16 additions & 3 deletions pkl/evaluator_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ func NewEvaluator(ctx context.Context, opts ...func(options *EvaluatorOptions))
//
// When using project dependencies, they must first be resolved using the `pkl project resolve`
// CLI command.
func NewProjectEvaluator(ctx context.Context, projectDir string, opts ...func(options *EvaluatorOptions)) (Evaluator, error) {
func NewProjectEvaluator(
ctx context.Context,
projectDir string,
opts ...func(options *EvaluatorOptions),
) (Evaluator, error) {
return NewProjectEvaluatorWithCommand(ctx, projectDir, nil, opts...)
}

Expand All @@ -52,7 +56,12 @@ func NewProjectEvaluator(ctx context.Context, projectDir string, opts ...func(op
//
// If creating multiple evaluators, prefer using EvaluatorManager.NewProjectEvaluator instead,
// because it lessens the overhead of each successive evaluator.
func NewProjectEvaluatorWithCommand(ctx context.Context, projectDir string, pklCmd []string, opts ...func(options *EvaluatorOptions)) (Evaluator, error) {
func NewProjectEvaluatorWithCommand(
ctx context.Context,
projectDir string,
pklCmd []string,
opts ...func(options *EvaluatorOptions),
) (Evaluator, error) {
manager := NewEvaluatorManagerWithCommand(pklCmd)
projectEvaluator, err := manager.NewEvaluator(ctx, opts...)
if err != nil {
Expand Down Expand Up @@ -87,7 +96,11 @@ func NewProjectEvaluatorWithCommand(ctx context.Context, projectDir string, pklC
//
// If creating multiple evaluators, prefer using EvaluatorManager.NewEvaluator instead,
// because it lessens the overhead of each successive evaluator.
func NewEvaluatorWithCommand(ctx context.Context, pklCmd []string, opts ...func(options *EvaluatorOptions)) (Evaluator, error) {
func NewEvaluatorWithCommand(
ctx context.Context,
pklCmd []string,
opts ...func(options *EvaluatorOptions),
) (Evaluator, error) {
manager := NewEvaluatorManagerWithCommand(pklCmd)
ev, err := manager.NewEvaluator(ctx, opts...)
if err != nil {
Expand Down
Loading