Skip to content

Commit

Permalink
cue/load: add tool and test files to BuildFiles if requested
Browse files Browse the repository at this point in the history
This prepares for hoising the injection logic from
cmd/cue to this package. This, in turn, also prepares
for file-level build tag support.

The main change is that for the "special" reorganzation
of tool files in `cue/cmd` it now removes the tool
files, rather than manually addes them. This is weirder,
but coincides with our intention to get rid of this special
behavior.

Change-Id: I7b61966c709282c7c4b55de303cba8e5a87d2a87
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/7061
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Sep 14, 2020
1 parent 480e1a1 commit bc101b3
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 28 deletions.
15 changes: 11 additions & 4 deletions cmd/cue/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,12 +579,19 @@ func buildTools(cmd *Command, tags, args []string) (*cue.Instance, error) {

ti := binst[0].Context().NewInstance(binst[0].Root, nil)
for _, inst := range binst {
for _, f := range inst.ToolCUEFiles {
if file := inst.Abs(f); !included[file] {
_ = ti.AddFile(file, nil)
included[file] = true
k := 0
for _, f := range inst.Files {
if strings.HasSuffix(f.Filename, "_tool.cue") {
if !included[f.Filename] {
_ = ti.AddSyntax(f)
included[f.Filename] = true
}
continue
}
inst.Files[k] = f
k++
}
inst.Files = inst.Files[:k]
}
decorateInstances(cmd, tags, append(binst, ti))

Expand Down
11 changes: 0 additions & 11 deletions cmd/cue/cmd/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,6 @@ func runFixAll(cmd *Command, args []string) error {
Tools: true,
})

for _, i := range instances {
a := append(i.ToolCUEFiles, i.TestCUEFiles...)
for _, f := range a {
file := i.Abs(f)
_ = i.AddFile(file, nil)
}
if i.Err != nil {
return i.Err
}
}

errs := fix.Instances(instances)

if errs != nil && flagForce.Bool(cmd) {
Expand Down
10 changes: 1 addition & 9 deletions cmd/cue/cmd/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,7 @@ func newFmtCmd(c *Command) *cobra.Command {
exitOnErr(cmd, inst.Err, false)
continue
}
all := []*build.File{}
all = append(all, inst.BuildFiles...)
for _, name := range append(inst.ToolCUEFiles, inst.TestCUEFiles...) {
all = append(all, &build.File{
Filename: name,
Encoding: build.CUE,
})
}
for _, file := range all {
for _, file := range inst.BuildFiles {
files := []*ast.File{}
d := encoding.NewDecoder(file, &cfg)
defer d.Close()
Expand Down
12 changes: 10 additions & 2 deletions cue/load/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,10 +433,18 @@ func (fp *fileProcessor) add(pos token.Pos, root string, file *build.File, mode
switch {
case isTest:
p.TestCUEFiles = append(p.TestCUEFiles, fullPath)
// TODO: what is the BuildFiles equivalent?
if fp.c.loader.cfg.Tests {
p.BuildFiles = append(p.BuildFiles, file)
} else {
p.IgnoredFiles = append(p.IgnoredFiles, file)
}
case isTool:
p.ToolCUEFiles = append(p.ToolCUEFiles, fullPath)
// TODO: what is the BuildFiles equivalent?
if fp.c.loader.cfg.Tools {
p.BuildFiles = append(p.BuildFiles, file)
} else {
p.IgnoredFiles = append(p.IgnoredFiles, file)
}
default:
p.CUEFiles = append(p.CUEFiles, fullPath)
p.BuildFiles = append(p.BuildFiles, file)
Expand Down
4 changes: 3 additions & 1 deletion cue/load/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,9 @@ path: example.org/test/toolonly:foo
module: example.org/test
root: $CWD/testdata
dir: $CWD/testdata/toolonly
display:./toolonly`,
display:./toolonly
files:
$CWD/testdata/toolonly/foo_tool.cue`,
}, {
cfg: &Config{
Dir: testdataDir,
Expand Down
2 changes: 1 addition & 1 deletion cue/load/testdata/toolonly/foo_tool.cue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package foo

import "tool/cli"

command foo task: {
command: foo: task: {
foo: cli.Print & {
text: "foo"
}
Expand Down

0 comments on commit bc101b3

Please sign in to comment.