From 3ade70ef1bcb7269c573156cb0c2bfa9e87fee4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Tue, 17 Sep 2024 16:42:38 +0100 Subject: [PATCH] pkg: generate register.go automatically as well MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Resolves a TODO, and avoids us having to maintain the file manually. Signed-off-by: Daniel Martí Change-Id: I79d852f1822fedb89a63b810115b5dd8fbee4c78 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1201391 TryBot-Result: CUEcueckoo Reviewed-by: Roger Peppe Unity-Result: CUE porcuepine --- pkg/gen.go | 27 +++++++++++++++++---------- pkg/register.go | 14 +------------- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/pkg/gen.go b/pkg/gen.go index 35cb57c551d..f229e416ec5 100644 --- a/pkg/gen.go +++ b/pkg/gen.go @@ -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" @@ -55,8 +53,6 @@ import ( "cuelang.org/go/internal" ) -const genFile = "pkg.go" - type headerParams struct { GoPkg string CUEPkg string @@ -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 { @@ -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 diff --git a/pkg/register.go b/pkg/register.go index 6718dbd3fa3..5c318155866 100644 --- a/pkg/register.go +++ b/pkg/register.go @@ -1,16 +1,4 @@ -// Copyright 2020 CUE Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +// Code generated by cuelang.org/go/pkg/gen. DO NOT EDIT. package pkg