Skip to content

Commit

Permalink
pkg: generate register.go automatically as well
Browse files Browse the repository at this point in the history
Resolves a TODO, and avoids us having to maintain the file manually.

Signed-off-by: Daniel Martí <[email protected]>
Change-Id: I79d852f1822fedb89a63b810115b5dd8fbee4c78
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1201391
TryBot-Result: CUEcueckoo <[email protected]>
Reviewed-by: Roger Peppe <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
  • Loading branch information
mvdan committed Sep 18, 2024
1 parent 1e19a97 commit 3ade70e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 23 deletions.
27 changes: 17 additions & 10 deletions pkg/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
// Be sure to also update an entry in pkg/pkg.go, if so desired.
package main

// TODO generate ../register.go too.

import (
"bytes"
_ "embed"
Expand All @@ -55,8 +53,6 @@ import (
"cuelang.org/go/internal"
)

const genFile = "pkg.go"

type headerParams struct {
GoPkg string
CUEPkg string
Expand Down Expand Up @@ -104,23 +100,34 @@ func main() {
if packages.PrintErrors(pkgs) > 0 {
os.Exit(1)
}
regBuf := new(bytes.Buffer)
fmt.Fprintf(regBuf, "// Code generated by cuelang.org/go/pkg/gen. DO NOT EDIT.\n\n")
fmt.Fprintf(regBuf, "package pkg\n\n")
fmt.Fprintf(regBuf, "import (\n")
for _, pkg := range pkgs {
switch {
case pkg.PkgPath == pkgParent:
// The pkg package itself should not be generated.
case strings.Contains(pkg.PkgPath, "/internal"):
// Internal packages are not for public use.
case pkg.PkgPath == "cuelang.org/go/pkg/path":
// TODO remove this special case. Currently the path
// pkg.go file cannot be generated automatically but that
// will be possible when we can attach arbitrary signatures
// to builtin functions.
default:
fmt.Fprintf(regBuf, "\t_ %q\n", pkg.PkgPath)
if pkg.PkgPath == "cuelang.org/go/pkg/path" {
// TODO remove this special case. Currently the path
// pkg.go file cannot be generated automatically but that
// will be possible when we can attach arbitrary signatures
// to builtin functions.
break
}
if err := generate(pkg); err != nil {
log.Fatalf("%s: %v", pkg, err)
}
}
}
fmt.Fprintf(regBuf, ")\n")
if err := os.WriteFile("register.go", regBuf.Bytes(), 0o666); err != nil {
log.Fatal(err)
}
}

type generator struct {
Expand Down Expand Up @@ -190,7 +197,7 @@ func generate(pkg *packages.Package) error {
b = g.w.Bytes() // write the unformatted source
}

filename := filepath.Join(pkgDir, genFile)
filename := filepath.Join(pkgDir, "pkg.go")

if err := os.WriteFile(filename, b, 0666); err != nil {
return err
Expand Down
14 changes: 1 addition & 13 deletions pkg/register.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3ade70e

Please sign in to comment.