Skip to content

Commit

Permalink
Fix runtime builder
Browse files Browse the repository at this point in the history
The `RuntimeBuilder` has the same issue as the `BundleBuilder`, as seen
in a858e4e

This commit fixes that.
  • Loading branch information
huguesalary committed Oct 4, 2024
1 parent c51ef99 commit 091b074
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cmd/timoni/runtime_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func buildRuntime(files []string) (*apiv1.Runtime, error) {
return nil, describeErr(tmpDir, "failed to init runtime", err)
}

v, err := rb.Build()
v, err := rb.Build(tmpDir)
if err != nil {
return nil, describeErr(tmpDir, "failed to parse runtime", err)
}
Expand Down
18 changes: 10 additions & 8 deletions internal/engine/runtime_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ import (

// RuntimeBuilder compiles CUE definitions to Go Runtime objects.
type RuntimeBuilder struct {
ctx *cue.Context
files []string
ctx *cue.Context
files []string
workspacesFiles map[string][]string
}

// NewRuntimeBuilder creates a RuntimeBuilder for the given module and package.
Expand All @@ -45,8 +46,9 @@ func NewRuntimeBuilder(ctx *cue.Context, files []string) *RuntimeBuilder {
ctx = cuecontext.New()
}
b := &RuntimeBuilder{
ctx: ctx,
files: files,
ctx: ctx,
files: files,
workspacesFiles: make(map[string][]string),
}
return b
}
Expand Down Expand Up @@ -96,26 +98,26 @@ func (b *RuntimeBuilder) InitWorkspace(workspace string) error {
files = append(files, dstFile)
}

schemaFile := filepath.Join(workspace, fmt.Sprintf("%v.schema.cue", len(b.files)+1))
schemaFile := filepath.Join(workspace, fmt.Sprintf("%v.schema.cue", len(b.workspacesFiles[workspace])+1))
files = append(files, schemaFile)
if err := os.WriteFile(schemaFile, []byte(apiv1.RuntimeSchema), os.ModePerm); err != nil {
return err
}

b.files = files
b.workspacesFiles[workspace] = files
return nil
}

// Build builds a CUE instance for the specified files and returns the CUE value.
// A workspace must be initialised with InitWorkspace before calling this function.
func (b *RuntimeBuilder) Build() (cue.Value, error) {
func (b *RuntimeBuilder) Build(workspace string) (cue.Value, error) {
var value cue.Value
cfg := &load.Config{
Package: "_",
DataFiles: true,
}

ix := load.Instances(b.files, cfg)
ix := load.Instances(b.workspacesFiles[workspace], cfg)
if len(ix) == 0 {
return value, fmt.Errorf("no instances found")
}
Expand Down

0 comments on commit 091b074

Please sign in to comment.