forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This change does a few things: * It reworks the queries in terms of eachother in-memory. This is better than the previous iteration whereby it'd generate the results and then rely on the output of that query. Instead, we just build up bigger query expressions and pass them to bazel using the --query_file flag. * It avoids exploring the pkg/ui directory (and the pkg/gen directory) because those can cause problems. The pkg/ui directory ends up bringing in npm, which hurts. * It stops rewriting the files before executing the queries. It no longer needs to rewrite them up front because they aren't referenced by later queries. * It removes the excluded target which was problematic because those files weren't properly visible. Fixes cockroachdb#76521 Fixes cockroachdb#76503 Release note: None
- Loading branch information
Showing
7 changed files
with
252 additions
and
675 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// Copyright 2022 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package main | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"sort" | ||
"strings" | ||
"text/template" | ||
|
||
"github.com/cockroachdb/errors" | ||
) | ||
|
||
type target string | ||
|
||
func (t target) query() *template.Template { | ||
return queryTemplates.Lookup(string(t)) | ||
} | ||
|
||
func (t target) execQuery(qd *queryData) (results []string, _ error) { | ||
f, err := ioutil.TempFile("", "") | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer func() { _ = os.Remove(f.Name()) }() | ||
if err := t.query().Execute(f, qd); err != nil { | ||
return nil, err | ||
} | ||
if err := f.Close(); err != nil { | ||
return nil, err | ||
} | ||
cmd := exec.Command("bazel", "query", "--query_file", f.Name()) | ||
var stdout bytes.Buffer | ||
var stderr strings.Builder | ||
cmd.Stdout = &stdout | ||
cmd.Stderr = &stderr | ||
if err := cmd.Run(); err != nil { | ||
return nil, errors.Wrapf(err, | ||
"failed to run %s: (stderr)\n%s", cmd, &stderr) | ||
} | ||
for sc := bufio.NewScanner(&stdout); sc.Scan(); { | ||
results = append(results, sc.Text()) | ||
} | ||
sort.Strings(results) | ||
return results, nil | ||
} | ||
|
||
func (t target) filename() string { | ||
return string(t) + ".bzl" | ||
} | ||
|
||
func (t target) variable() string { | ||
return strings.ToUpper(string(t)) + "_SRCS" | ||
} | ||
|
||
func (t target) write(outDir string, out []string) error { | ||
var buf bytes.Buffer | ||
if err := outputVariableTemplate.Execute(&buf, variableData{ | ||
Variable: t.variable(), | ||
Targets: out, | ||
}); err != nil { | ||
return errors.Wrapf(err, "failed to execute template for %s", t) | ||
} | ||
f, err := os.Create(filepath.Join(outDir, t.filename())) | ||
if err != nil { | ||
return errors.Wrapf(err, "failed to open file for %s", t) | ||
} | ||
if _, err := io.Copy(f, &buf); err != nil { | ||
return errors.Wrapf(err, "failed to write file for %s", t) | ||
} | ||
if err := f.Close(); err != nil { | ||
return errors.Wrapf(err, "failed to write file for %s", t) | ||
} | ||
return nil | ||
} | ||
|
||
var outputVariableTemplate = template.Must(template.New("file").Parse( | ||
`# Generated by genbzl | ||
{{ .Variable }} = [{{ range .Targets }} | ||
"{{ . }}",{{end}} | ||
] | ||
`)) | ||
|
||
type variableData struct { | ||
Variable string | ||
Targets []string | ||
} |
Oops, something went wrong.