Debian Codegraph @

internal/librariangen/build/builder.go

raw
1 // Copyright 2025 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package build
16
17 import (
18 "context"
19 "errors"
20 "fmt"
21 "log/slog"
22 "path/filepath"
23
24 "cloud.google.com/go/internal/postprocessor/librarian/librariangen/execv"
25 "cloud.google.com/go/internal/postprocessor/librarian/librariangen/request"
26 )
27
28 // Test substitution vars.
29 var (
30 execvRun = execv.Run
31 requestParse = request.ParseLibrary
32 )
33
34 // Config holds the internal librariangen configuration for the build command.
35 type Config struct {
36 // LibrarianDir is the path to the librarian-tool input directory.
37 // It is expected to contain the build-request.json file.
38 LibrarianDir string
39 // RepoDir is the path to ehte entire language repository.
40 RepoDir string
41 }
42
43 // Validate ensures that the configuration is valid.
44 func (c *Config) Validate() error {
45 if c.LibrarianDir == "" {
46 return errors.New("librariangen: librarian directory must be set")
47 }
48 if c.RepoDir == "" {
49 return errors.New("librariangen: repo directory must be set")
50 }
51 return nil
52 }
53
54 // Build is the main entrypoint for the `build` command. It runs `go build`
55 // and then `go test`.
56 func Build(ctx context.Context, cfg *Config) error {
57 if err := cfg.Validate(); err != nil {
58 return fmt.Errorf("librariangen: invalid configuration: %w", err)
59 }
60 slog.Debug("librariangen: generate command started")
61
62 buildReq, err := readBuildReq(cfg.LibrarianDir)
63 if err != nil {
64 return fmt.Errorf("librariangen: failed to read request: %w", err)
65 }
66 moduleDir := filepath.Join(cfg.RepoDir, buildReq.ID)
67 if err := goBuild(ctx, moduleDir, buildReq.ID); err != nil {
68 return fmt.Errorf("librariangen: failed to run 'go build': %w", err)
69 }
70 if err := goTest(ctx, moduleDir, buildReq.ID); err != nil {
71 return fmt.Errorf("librariangen: failed to run 'go test': %w", err)
72 }
73 return nil
74 }
75
76 // goBuild builds all the code under the specified directory
77 func goBuild(ctx context.Context, dir, module string) error {
78 slog.Info("librariangen: building", "module", module)
79 args := []string{"go", "build", "./..."}
80 return execvRun(ctx, args, dir)
81 }
82
83 // goTest builds all the code under the specified directory
84 func goTest(ctx context.Context, dir, module string) error {
85 slog.Info("librariangen: testing", "module", module)
86 args := []string{"go", "test", "./...", "-short"}
87 return execvRun(ctx, args, dir)
88 }
89
90 // readBuildReq reads generate-request.json from the librarian-tool input directory.
91 // The request file tells librariangen which library and APIs to generate.
92 // It is prepared by the Librarian tool and mounted at /librarian.
93 func readBuildReq(librarianDir string) (*request.Library, error) {
94 reqPath := filepath.Join(librarianDir, "build-request.json")
95 slog.Debug("librariangen: reading build request", "path", reqPath)
96
97 buildReq, err := requestParse(reqPath)
98 if err != nil {
99 return nil, err
100 }
101 slog.Debug("librariangen: successfully unmarshalled request", "library_id", buildReq.ID)
102 return buildReq, nil
103 }