Skip to content

Commit ab989d5

Browse files
committed
Implement fmt
1 parent 39d1383 commit ab989d5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1761
-12
lines changed

bootstrap/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func BuildConfig(root *hroot.State, profiles []string) (*config.Config, error) {
2525
},
2626
}
2727
cfg.Watch.Ignore = append(cfg.Watch.Ignore, root.Home.Join("**/*").Abs())
28+
cfg.Fmt.IndentSize = 4
2829

2930
err := config.ParseAndApply("/etc/.hephconfig", &cfg)
3031
if err != nil && !errors.Is(err, os.ErrNotExist) {

cmd/heph/init.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,15 @@ func schedulerInit(ctx context.Context, postBoot func(bootstrap.BaseBootstrap) e
9191
return bs, nil
9292
}
9393

94+
func bootstrapBase(ctx context.Context) (bootstrap.BaseBootstrap, error) {
95+
opts, err := bootstrapOptions()
96+
if err != nil {
97+
return bootstrap.BaseBootstrap{}, err
98+
}
99+
100+
return bootstrap.BootBase(ctx, opts)
101+
}
102+
94103
func bootstrapInit(ctx context.Context) (bootstrap.Bootstrap, error) {
95104
opts, err := bootstrapOptions()
96105
if err != nil {

cmd/heph/query.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ var configCmd = &cobra.Command{
212212
RunE: func(cmd *cobra.Command, args []string) error {
213213
ctx := cmd.Context()
214214

215-
bs, err := bootstrapInit(ctx)
215+
bs, err := bootstrapBase(ctx)
216216
if err != nil {
217217
return err
218218
}
@@ -758,7 +758,7 @@ var outRootCmd = &cobra.Command{
758758
RunE: func(cmd *cobra.Command, args []string) error {
759759
ctx := cmd.Context()
760760

761-
bs, err := bootstrapInit(ctx)
761+
bs, err := bootstrapBase(ctx)
762762
if err != nil {
763763
return err
764764
}

cmd/heph/root.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@ package main
33
import (
44
"fmt"
55
"github.com/hephbuild/heph/bootstrap"
6+
"github.com/hephbuild/heph/buildfiles"
67
"github.com/hephbuild/heph/config"
78
"github.com/hephbuild/heph/log/log"
9+
"github.com/hephbuild/heph/packages"
810
"github.com/hephbuild/heph/targetrun"
911
"github.com/hephbuild/heph/utils"
12+
"github.com/hephbuild/heph/utils/ads"
1013
"github.com/hephbuild/heph/utils/finalizers"
14+
"github.com/hephbuild/heph/utils/xstarlark"
1115
"github.com/spf13/cobra"
16+
"go.uber.org/multierr"
1217
"os"
1318
"runtime"
1419
"runtime/pprof"
@@ -34,6 +39,7 @@ var params *[]string
3439
var summary *bool
3540
var summaryGen *bool
3641
var jaegerEndpoint *string
42+
var check *bool
3743

3844
func getRunOpts() bootstrap.RunOpts {
3945
return bootstrap.RunOpts{
@@ -70,6 +76,8 @@ func init() {
7076
runCmd.Flags().AddFlag(NewBoolStrFlag(&catOutput, "cat-out", "", "Print target output content, --cat-out=<name> to filter output"))
7177
alwaysOut = runCmd.Flags().Bool("always-out", false, "Ensure output will be present in cache")
7278

79+
check = fmtCmd.Flags().Bool("check", false, "Only check formatting")
80+
7381
rootCmd.AddCommand(runCmd)
7482
rootCmd.AddCommand(cleanCmd)
7583
rootCmd.AddCommand(queryCmd)
@@ -78,6 +86,7 @@ func init() {
7886
rootCmd.AddCommand(validateCmd)
7987
rootCmd.AddCommand(setupCmd)
8088
rootCmd.AddCommand(searchCmd)
89+
rootCmd.AddCommand(fmtCmd)
8190

8291
cpuprofile = rootCmd.PersistentFlags().String("cpuprofile", "", "CPU Profile file")
8392
memprofile = rootCmd.PersistentFlags().String("memprofile", "", "Mem Profile file")
@@ -299,3 +308,54 @@ var setupCmd = &cobra.Command{
299308
return nil
300309
},
301310
}
311+
312+
var fmtCmd = &cobra.Command{
313+
Use: "fmt",
314+
Short: "Format build files",
315+
Args: cobra.ArbitraryArgs,
316+
RunE: func(cmd *cobra.Command, args []string) error {
317+
ctx := cmd.Context()
318+
319+
bs, err := bootstrapBase(ctx)
320+
if err != nil {
321+
return err
322+
}
323+
324+
buildfilesState := buildfiles.NewState(buildfiles.State{
325+
Ignore: bs.Config.BuildFiles.Ignore,
326+
})
327+
328+
var files []string
329+
if len(args) == 0 {
330+
cfiles, err := buildfilesState.CollectFiles(ctx, bs.Root.Root.Abs())
331+
if err != nil {
332+
return err
333+
}
334+
335+
files = ads.Map(cfiles, func(f *packages.SourceFile) string {
336+
return f.Path
337+
})
338+
} else {
339+
files = args
340+
}
341+
342+
cfg := xstarlark.FmtConfig{
343+
IndentSize: bs.Config.Fmt.IndentSize,
344+
}
345+
346+
var errs error
347+
for _, file := range files {
348+
f := xstarlark.FmtFix
349+
if *check {
350+
f = xstarlark.FmtCheck
351+
}
352+
353+
err := f(file, cfg)
354+
if err != nil {
355+
errs = multierr.Append(errs, fmt.Errorf("%v: %w", file, err))
356+
}
357+
}
358+
359+
return errs
360+
},
361+
}

config/config.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ import (
55
"github.com/hephbuild/heph/log/log"
66
"github.com/hephbuild/heph/utils/mds"
77
"golang.org/x/exp/slices"
8+
"gopkg.in/yaml.v3"
89
"os"
910
"path/filepath"
1011
)
11-
import (
12-
"gopkg.in/yaml.v3"
13-
)
1412

1513
type BaseConfig struct {
1614
Version Version
@@ -48,6 +46,9 @@ type Config struct {
4846
Watch struct {
4947
Ignore []string `yaml:"ignore"`
5048
} `yaml:"watch"`
49+
Fmt struct {
50+
IndentSize int `yaml:"indent_size,omitempty"`
51+
} `yaml:"fmt"`
5152
Params map[string]string `yaml:"params"`
5253

5354
Extras `yaml:",inline"`

config/file_config.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ type FileConfig struct {
2828
Watch struct {
2929
Ignore []string `yaml:"ignore,omitempty"`
3030
} `yaml:"watch"`
31+
Fmt struct {
32+
IndentSize int `yaml:"indent_size,omitempty"`
33+
} `yaml:"fmt"`
3134
Params map[string]string `yaml:"params"`
3235
Extras `yaml:",inline"`
3336
}
@@ -108,6 +111,10 @@ func (fc FileConfig) ApplyTo(c Config) Config {
108111
c.BuildFiles.Glob.Exclude = append(c.BuildFiles.Glob.Exclude, fc.BuildFiles.Glob.Exclude...)
109112
c.Watch.Ignore = append(c.Watch.Ignore, fc.Watch.Ignore...)
110113

114+
if fc.Fmt.IndentSize != 0 {
115+
c.Fmt.IndentSize = fc.Fmt.IndentSize
116+
}
117+
111118
if c.Extras == nil {
112119
c.Extras = map[string]interface{}{}
113120
}

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ go 1.20
44

55
replace github.com/spf13/cobra v1.7.0 => github.com/raphaelvigee/cobra v0.0.0-20221020122344-217ca52feee0
66

7+
replace go.starlark.net => github.com/raphaelvigee/starlark-go v0.0.0-20230906105503-b71494f27e4a
8+
79
require (
810
github.com/Khan/genqlient v0.6.0
911
github.com/aarondl/json v0.0.0-20221020222930-8b0db17ef1bf

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
248248
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
249249
github.com/raphaelvigee/cobra v0.0.0-20221020122344-217ca52feee0 h1:M4TK6Jyh2wDKpJcK0JLEYxC+EFiGLk/OQsSFi7cALUw=
250250
github.com/raphaelvigee/cobra v0.0.0-20221020122344-217ca52feee0/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY=
251+
github.com/raphaelvigee/starlark-go v0.0.0-20230906105503-b71494f27e4a h1:04b1y8M6+U7kjw11X/zcZ0VCzgz7JIcIGcrVGeBRXFM=
252+
github.com/raphaelvigee/starlark-go v0.0.0-20230906105503-b71494f27e4a/go.mod h1:jxU+3+j+71eXOW14274+SmmuW82qJzl6iZSeqEtTGds=
251253
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
252254
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
253255
github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw=
@@ -303,8 +305,6 @@ go.opentelemetry.io/otel/sdk v1.11.1/go.mod h1:/l3FE4SupHJ12TduVjUkZtlfFqDCQJlOl
303305
go.opentelemetry.io/otel/trace v1.11.1 h1:ofxdnzsNrGBYXbP7t7zpUK281+go5rF7dvdIZXF8gdQ=
304306
go.opentelemetry.io/otel/trace v1.11.1/go.mod h1:f/Q9G7vzk5u91PhbmKbg1Qn0rzH1LJ4vbPHFGkTPtOk=
305307
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
306-
go.starlark.net v0.0.0-20230302034142-4b1e35fe2254 h1:Ss6D3hLXTM0KobyBYEAygXzFfGcjnmfEJOBgSbemCtg=
307-
go.starlark.net v0.0.0-20230302034142-4b1e35fe2254/go.mod h1:jxU+3+j+71eXOW14274+SmmuW82qJzl6iZSeqEtTGds=
308308
go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ=
309309
go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
310310
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=

targetrun/prepare.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func (e *Runner) toolAbsPath(tt graph.TargetTool) string {
129129
return tt.File.WithRoot(e.LocalCache.Metas.Find(tt.Target).OutExpansionRoot().Abs()).Abs()
130130
}
131131

132-
func (e *Runner) runPrepare(ctx context.Context, target *graph.Target, mode string) (_ *Target, rerr error) {
132+
func (e *Runner) runPrepare(ctx context.Context, target *graph.Target, rr Request) (_ *Target, rerr error) {
133133
ctx, span := e.Observability.SpanRunPrepare(ctx, target)
134134
defer span.EndError(rerr)
135135

@@ -243,7 +243,7 @@ func (e *Runner) runPrepare(ctx context.Context, target *graph.Target, mode stri
243243

244244
if dep.Mode == specs.DepModeLink {
245245
outDir := e.LocalCache.Metas.Find(dept).OutExpansionRoot().Abs()
246-
for _, file := range dept.ActualOutFiles().WithRoot(outDir).Name(dep.Output) {
246+
for _, file := range dept.ActualOutFiles().Name(dep.Output).WithRoot(outDir) {
247247
linkSrcRec.Add("", file.Abs(), file.RelRoot(), "")
248248
}
249249
} else {
@@ -366,7 +366,7 @@ func (e *Runner) runPrepare(ctx context.Context, target *graph.Target, mode stri
366366
env["ROOT"] = rtarget.WorkdirRoot.Abs()
367367
env["SANDBOX"] = rtarget.SandboxRoot.Abs()
368368
if !target.Cache.Enabled {
369-
mode := mode
369+
mode := rr.Mode
370370
if mode == "" {
371371
mode = "run"
372372
}
@@ -396,7 +396,7 @@ func (e *Runner) runPrepare(ctx context.Context, target *graph.Target, mode stri
396396
env[hephprovider.EnvDistRoot] = hephDistRoot
397397
}
398398

399-
if !(target.SrcEnv.Default == specs.FileEnvIgnore && len(target.SrcEnv.Named) == 0) {
399+
if target.SrcEnv.Default != specs.FileEnvIgnore && len(envSrcRec.Named()) > 0 {
400400
for name, paths := range envSrcRec.Named() {
401401
if strings.HasPrefix(name, "_") {
402402
continue

targetrun/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (e *Runner) Run(ctx context.Context, rr Request, iocfg sandbox.IOConfig) (*
6969
log.Tracef("Target DONE %v", target.Addr)
7070
}()
7171

72-
rtarget, err := e.runPrepare(ctx, target, rr.Mode)
72+
rtarget, err := e.runPrepare(ctx, target, rr)
7373
if err != nil {
7474
return nil, fmt.Errorf("prepare: %w", err)
7575
}

0 commit comments

Comments
 (0)