-
Notifications
You must be signed in to change notification settings - Fork 12
/
hlb.go
62 lines (53 loc) · 1.56 KB
/
hlb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package hlb
import (
"context"
"fmt"
"io"
"github.com/moby/buildkit/client"
"github.com/openllb/hlb/builtin"
"github.com/openllb/hlb/checker"
"github.com/openllb/hlb/codegen"
"github.com/openllb/hlb/diagnostic"
"github.com/openllb/hlb/linter"
"github.com/openllb/hlb/module"
"github.com/openllb/hlb/parser/ast"
"github.com/openllb/hlb/pkg/filebuffer"
"github.com/openllb/hlb/solver"
"golang.org/x/sync/semaphore"
)
const defaultMaxConcurrency = 20
// WithDefaultContext adds common context values to the context.
func WithDefaultContext(ctx context.Context, cln *client.Client) context.Context {
ctx = filebuffer.WithBuffers(ctx, builtin.Buffers())
ctx = ast.WithModules(ctx, builtin.Modules())
if cln != nil {
ctx = codegen.WithImageResolver(ctx, codegen.NewCachedImageResolver(cln))
}
return ctx
}
// Compile compiles targets in a module and returns a solver.Request.
func Compile(ctx context.Context, cln *client.Client, w io.Writer, mod *ast.Module, targets []codegen.Target) (solver.Request, error) {
err := checker.SemanticPass(mod)
if err != nil {
return nil, err
}
err = linter.Lint(ctx, mod)
if err != nil {
for _, span := range diagnostic.Spans(err) {
fmt.Fprintln(w, span.Pretty(ctx))
}
}
err = checker.Check(mod)
if err != nil {
return nil, err
}
resolver, err := module.NewResolver(cln)
if err != nil {
return nil, err
}
cg := codegen.New(cln, resolver)
if solver.ConcurrencyLimiter(ctx) == nil {
ctx = solver.WithConcurrencyLimiter(ctx, semaphore.NewWeighted(defaultMaxConcurrency))
}
return cg.Generate(ctx, mod, targets)
}