Skip to content

Commit

Permalink
implement glob & codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelvigee committed Dec 30, 2024
1 parent ee731da commit ad7af52
Show file tree
Hide file tree
Showing 22 changed files with 552 additions and 74 deletions.
2 changes: 2 additions & 0 deletions example/codegen/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
file
dir
19 changes: 19 additions & 0 deletions example/codegen/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
target(
name="gen_file",
driver = "bash",
run = "echo hello > $OUT",
out = "file",
codegen = 'copy',
)

target(
name="gen_dir",
driver = "bash",
run = [
"mkdir $OUT",
"echo hello > $OUT/hello",
"echo world > $OUT/world",
],
out = "dir",
codegen = 'copy',
)
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ replace github.com/charmbracelet/bubbletea => github.com/raphaelvigee/bubbletea
require (
connectrpc.com/connect v1.17.0
github.com/Code-Hex/go-generics-cache v1.5.1
github.com/bmatcuk/doublestar/v4 v4.7.1
github.com/charmbracelet/bubbletea v1.2.4
github.com/charmbracelet/lipgloss v1.0.0
github.com/charmbracelet/x/term v0.2.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiE
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/aymanbagabas/go-udiff v0.2.0 h1:TK0fH4MteXUDspT88n8CKzvK0X9O2xu9yQjWpi6yML8=
github.com/aymanbagabas/go-udiff v0.2.0/go.mod h1:RE4Ex0qsGkTAJoQdQQCA0uG+nAzJO/pI/QwceO5fgrA=
github.com/bmatcuk/doublestar/v4 v4.7.1 h1:fdDeAqgT47acgwd9bd9HxJRDmc9UAmPpc+2m0CXv75Q=
github.com/bmatcuk/doublestar/v4 v4.7.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
github.com/charmbracelet/lipgloss v1.0.0 h1:O7VkGDvqEdGi93X+DeqsQ7PKHDgtQfF8j8/O2qFMQNg=
github.com/charmbracelet/lipgloss v1.0.0/go.mod h1:U5fy9Z+C38obMs+T+tJqst9VGzlOYGj4ri9reL3qUlo=
github.com/charmbracelet/x/ansi v0.6.0 h1:qOznutrb93gx9oMiGf7caF7bqqubh6YIM0SWKyA08pA=
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func init() {
return res.Err
}

outputs := res.Outputs
outputs := res.Artifacts

// TODO how to render res natively without exec
_, err = hbbtexec.Run(m.Exec, send, func(args hbbtexec.RunArgs) (*engine.ExecuteResult, error) {
Expand Down
76 changes: 76 additions & 0 deletions internal/engine/codegen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package engine

import (
"context"
"fmt"
"path/filepath"
"slices"
"strings"

"github.com/hephbuild/heph/internal/hartifact"
pluginv1 "github.com/hephbuild/heph/plugin/gen/heph/plugin/v1"
)

func (e *Engine) codegenTree(ctx context.Context, def *LightLinkedTarget, outputs []ExecuteResultArtifact) error {
if def.CodegenTree == nil {
return nil
}

switch def.CodegenTree.GetMode() {
case pluginv1.TargetDef_CodegenTree_CODEGEN_MODE_COPY:
err := e.codegenCopyTree(ctx, def, outputs)
if err != nil {
return fmt.Errorf("copy: %w", err)
}
case pluginv1.TargetDef_CodegenTree_CODEGEN_MODE_LINK:
err := e.codegenLinkTree(ctx, def, outputs)
if err != nil {
return fmt.Errorf("link: %w", err)
}
case pluginv1.TargetDef_CodegenTree_CODEGEN_MODE_UNSPECIFIED:
default:
return fmt.Errorf("unknown codegen mode %v", def.CodegenTree.GetMode())
}

return nil
}

func (e *Engine) codegenCopyTree(ctx context.Context, def *LightLinkedTarget, outputs []ExecuteResultArtifact) error {
codegenPaths := make([]string, 0, len(outputs))
for _, path := range def.CodegenTree.GetPaths() {
codegenPaths = append(codegenPaths, filepath.Join(def.Ref.GetPackage(), path))
}

isUnderCodegenPath := func(p string) bool {
if slices.Contains(codegenPaths, p) {
return true
}

for _, codegenPath := range codegenPaths {
if strings.HasPrefix(p, codegenPath) {
return true
}
}

return false
}

for _, output := range outputs {
if output.Type != pluginv1.Artifact_TYPE_OUTPUT {
continue
}

err := hartifact.Unpack(ctx, output.Artifact, e.Root, hartifact.WithFilter(isUnderCodegenPath))
if err != nil {
return fmt.Errorf("unpack: %v: %w", output.Group, err)
}
}

return nil
}

func (e *Engine) codegenLinkTree(ctx context.Context, def *LightLinkedTarget, outputs []ExecuteResultArtifact) error {
// TODO

return e.codegenCopyTree(ctx, def, outputs)
}
19 changes: 10 additions & 9 deletions internal/engine/local_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ func (e *Engine) hashout(ctx context.Context, artifact *pluginv1.Artifact) (stri
return hex.EncodeToString(h.Sum(nil)), nil
}

func (e *Engine) CacheLocally(ctx context.Context, def *LightLinkedTarget, hashin string, sandboxArtifacts []ExecuteResultOutput) ([]ExecuteResultOutput, error) {
func (e *Engine) CacheLocally(ctx context.Context, def *LightLinkedTarget, hashin string, sandboxArtifacts []ExecuteResultArtifact) ([]ExecuteResultArtifact, error) {
// TODO: locks

cachedir := hfs.At(e.Cache, def.Ref.GetPackage(), "__"+def.Ref.GetName(), hashin)

cacheArtifacts := make([]ExecuteResultOutput, 0, len(sandboxArtifacts))
cacheArtifacts := make([]ExecuteResultArtifact, 0, len(sandboxArtifacts))

for _, artifact := range sandboxArtifacts {
scheme, rest, err := hartifact.ParseURI(artifact.Uri)
Expand Down Expand Up @@ -103,7 +103,7 @@ func (e *Engine) CacheLocally(ctx context.Context, def *LightLinkedTarget, hashi
}
}

cacheArtifacts = append(cacheArtifacts, ExecuteResultOutput{
cacheArtifacts = append(cacheArtifacts, ExecuteResultArtifact{
Hashout: hashout,
Artifact: cachedArtifact,
})
Expand Down Expand Up @@ -136,7 +136,7 @@ func (e *Engine) CacheLocally(ctx context.Context, def *LightLinkedTarget, hashi
return nil, err
}

cacheArtifacts = append(cacheArtifacts, ExecuteResultOutput{
cacheArtifacts = append(cacheArtifacts, ExecuteResultArtifact{
Artifact: manifestV1Artifact(cachedir),
})

Expand Down Expand Up @@ -217,9 +217,9 @@ func (e *Engine) resultFromLocalCacheInner(
locks.Add(l.RUnlock)
}

execOutputs := make([]ExecuteResultOutput, 0, len(artifacts))
execArtifacts := make([]ExecuteResultArtifact, 0, len(artifacts))
for _, artifact := range artifacts {
execOutputs = append(execOutputs, ExecuteResultOutput{
execArtifacts = append(execArtifacts, ExecuteResultArtifact{
Hashout: artifact.Hashout,
Artifact: &pluginv1.Artifact{
Group: artifact.Group,
Expand All @@ -231,12 +231,13 @@ func (e *Engine) resultFromLocalCacheInner(
})
}

execOutputs = append(execOutputs, ExecuteResultOutput{
execArtifacts = append(execArtifacts, ExecuteResultArtifact{
Artifact: manifestV1Artifact(dirfs),
})

return &ExecuteResult{
Hashin: manifest.Hashin,
Outputs: execOutputs,
Def: def,
Hashin: manifest.Hashin,
Artifacts: execArtifacts,
}, true, nil
}
28 changes: 24 additions & 4 deletions internal/engine/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,28 @@ package engine
import (
"context"
"fmt"
"os"

"github.com/hephbuild/heph/internal/hartifact"
"github.com/hephbuild/heph/internal/hfs"
pluginv1 "github.com/hephbuild/heph/plugin/gen/heph/plugin/v1"
)

func SetupSandbox(ctx context.Context, depResults []*ExecuteResultWithOrigin, fs hfs.FS) ([]*pluginv1.ArtifactWithOrigin, error) {
func SetupSandbox(ctx context.Context, def *LightLinkedTarget, depResults []*ExecuteResultWithOrigin, workdirfs, cwdfs hfs.FS) ([]*pluginv1.ArtifactWithOrigin, error) {
err := workdirfs.MkdirAll(def.Ref.GetPackage(), hfs.ModePerm)
if err != nil {
return nil, err
}

err = cwdfs.MkdirAll("", os.ModePerm)
if err != nil {
return nil, err
}

var artifacts []*pluginv1.ArtifactWithOrigin

for _, depResult := range depResults {
for _, res := range depResult.Outputs {
for _, res := range depResult.Artifacts {
if res.Type != pluginv1.Artifact_TYPE_OUTPUT {
return nil, fmt.Errorf("unexpected artifact type: %s", res.Type)
}
Expand All @@ -23,7 +34,7 @@ func SetupSandbox(ctx context.Context, depResults []*ExecuteResultWithOrigin, fs
Dep: depResult.Origin,
})

listArtifact, err := SetupSandboxArtifact(ctx, res, fs)
listArtifact, err := SetupSandboxArtifact(ctx, res, workdirfs)
if err != nil {
return nil, err
}
Expand All @@ -35,10 +46,19 @@ func SetupSandbox(ctx context.Context, depResults []*ExecuteResultWithOrigin, fs
}
}

for _, output := range def.CollectOutputs {
for _, path := range output.GetPaths() {
err := hfs.CreateParentDir(cwdfs, path)
if err != nil {
return nil, err
}
}
}

return artifacts, nil
}

func SetupSandboxArtifact(ctx context.Context, artifact ExecuteResultOutput, fs hfs.FS) (*pluginv1.Artifact, error) {
func SetupSandboxArtifact(ctx context.Context, artifact ExecuteResultArtifact, fs hfs.FS) (*pluginv1.Artifact, error) {
listf, err := hfs.Create(fs, artifact.Hashout+".list")
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit ad7af52

Please sign in to comment.