Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 15 additions & 70 deletions internal/librariangen/generate/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package generate
import (
"archive/zip"
"context"
"errors"
"fmt"
"io"
"log/slog"
Expand All @@ -27,85 +26,46 @@ import (

"cloud.google.com/java/internal/librariangen/bazel"
"cloud.google.com/java/internal/librariangen/execv"
"cloud.google.com/java/internal/librariangen/languagecontainer/generate"
"cloud.google.com/java/internal/librariangen/message"
"cloud.google.com/java/internal/librariangen/protoc"
)

// Test substitution vars.
var (
bazelParse = bazel.Parse
execvRun = execv.Run
requestParse = message.ParseLibrary
protocBuild = protoc.Build
bazelParse = bazel.Parse
execvRun = execv.Run
protocBuild = protoc.Build
)

// Config holds the internal librariangen configuration for the generate command.
type Config struct {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has moved to languagecontainer.Context.

// LibrarianDir is the path to the librarian-tool input directory.
// It is expected to contain the generate-request.json file.
LibrarianDir string
// InputDir is the path to the .librarian/generator-input directory from the
// language repository.
InputDir string
// OutputDir is the path to the empty directory where librariangen writes
// its output.
OutputDir string
// SourceDir is the path to a complete checkout of the googleapis repository.
SourceDir string
}

// Validate ensures that the configuration is valid.
func (c *Config) Validate() error {
if c.LibrarianDir == "" {
return errors.New("librariangen: librarian directory must be set")
}
if c.InputDir == "" {
return errors.New("librariangen: input directory must be set")
}
if c.OutputDir == "" {
return errors.New("librariangen: output directory must be set")
}
if c.SourceDir == "" {
return errors.New("librariangen: source directory must be set")
}
return nil
}

// Generate is the main entrypoint for the `generate` command. It orchestrates
// the entire generation process.
func Generate(ctx context.Context, cfg *Config) error {
if err := cfg.Validate(); err != nil {
return fmt.Errorf("librariangen: invalid configuration: %w", err)
}
func Generate(ctx context.Context, cfg *generate.Config) error {
slog.Debug("librariangen: generate command started")
outputConfig := &protoc.OutputConfig{
GAPICDir: filepath.Join(cfg.OutputDir, "gapic"),
GRPCDir: filepath.Join(cfg.OutputDir, "grpc"),
ProtoDir: filepath.Join(cfg.OutputDir, "proto"),
GAPICDir: filepath.Join(cfg.Context.OutputDir, "gapic"),
GRPCDir: filepath.Join(cfg.Context.OutputDir, "grpc"),
ProtoDir: filepath.Join(cfg.Context.OutputDir, "proto"),
}
defer func() {
if err := cleanupIntermediateFiles(outputConfig); err != nil {
slog.Error("librariangen: failed to clean up intermediate files", "error", err)
}
}()

generateReq, err := readGenerateReq(cfg.LibrarianDir)
if err != nil {
return fmt.Errorf("librariangen: failed to read request: %w", err)
}
generateReq := cfg.Request

if err := invokeProtoc(ctx, cfg, generateReq, outputConfig); err != nil {
if err := invokeProtoc(ctx, cfg.Context, generateReq, outputConfig); err != nil {
return fmt.Errorf("librariangen: gapic generation failed: %w", err)
}

// Unzip the temp-codegen.srcjar.
srcjarPath := filepath.Join(outputConfig.GAPICDir, "temp-codegen.srcjar")
srcjarDest := outputConfig.GAPICDir
if err := unzip(srcjarPath, srcjarDest); err != nil {
return fmt.Errorf("librariangen: failed to unzip %s: %w", srcjarPath, err)
}

if err := restructureOutput(cfg.OutputDir, generateReq.ID); err != nil {
if err := restructureOutput(cfg.Context.OutputDir, generateReq.ID); err != nil {
return fmt.Errorf("librariangen: failed to restructure output: %w", err)
}

Expand All @@ -116,15 +76,15 @@ func Generate(ctx context.Context, cfg *Config) error {
// invokeProtoc handles the protoc GAPIC generation logic for the 'generate' CLI command.
// It reads a request file, and for each API specified, it invokes protoc
// to generate the client library. It returns the module path and the path to the service YAML.
func invokeProtoc(ctx context.Context, cfg *Config, generateReq *message.Library, outputConfig *protoc.OutputConfig) error {
func invokeProtoc(ctx context.Context, genCtx *generate.Context, generateReq *message.Library, outputConfig *protoc.OutputConfig) error {
for _, api := range generateReq.APIs {
apiServiceDir := filepath.Join(cfg.SourceDir, api.Path)
apiServiceDir := filepath.Join(genCtx.SourceDir, api.Path)
slog.Info("processing api", "service_dir", apiServiceDir)
bazelConfig, err := bazelParse(apiServiceDir)
if err != nil {
return fmt.Errorf("librariangen: failed to parse BUILD.bazel for %s: %w", apiServiceDir, err)
}
args, err := protocBuild(apiServiceDir, bazelConfig, cfg.SourceDir, outputConfig)
args, err := protocBuild(apiServiceDir, bazelConfig, genCtx.SourceDir, outputConfig)
if err != nil {
return fmt.Errorf("librariangen: failed to build protoc command for api %q in library %q: %w", api.Path, generateReq.ID, err)
}
Expand All @@ -136,28 +96,13 @@ func invokeProtoc(ctx context.Context, cfg *Config, generateReq *message.Library
}
}

if err := execvRun(ctx, args, cfg.OutputDir); err != nil {
if err := execvRun(ctx, args, genCtx.OutputDir); err != nil {
return fmt.Errorf("librariangen: protoc failed for api %q in library %q: %w, execvRun error: %v", api.Path, generateReq.ID, err, err)
}
}
return nil
}

// readGenerateReq reads generate-request.json from the librarian-tool input directory.
// The request file tells librariangen which library and APIs to generate.
// It is prepared by the Librarian tool and mounted at /librarian.
func readGenerateReq(librarianDir string) (*message.Library, error) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has moved to languagecontainer.generate.NewConfig.

reqPath := filepath.Join(librarianDir, "generate-request.json")
slog.Debug("librariangen: reading generate request", "path", reqPath)

generateReq, err := requestParse(reqPath)
if err != nil {
return nil, err
}
slog.Debug("librariangen: successfully unmarshalled request", "library_id", generateReq.ID)
return generateReq, nil
}

// moveFiles moves all files (and directories) from sourceDir to targetDir.
func moveFiles(sourceDir, targetDir string) error {
files, err := os.ReadDir(sourceDir)
Expand Down
73 changes: 10 additions & 63 deletions internal/librariangen/generate/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"path/filepath"
"testing"

"cloud.google.com/java/internal/librariangen/languagecontainer/generate"
"cloud.google.com/java/internal/librariangen/protoc"
)

Expand Down Expand Up @@ -252,12 +253,20 @@ java_gapic_library(
protocRunCount++
return tt.protocErr
}
cfg := &Config{
genCtx := &generate.Context{
LibrarianDir: e.librarianDir,
InputDir: "fake-input",
OutputDir: e.outputDir,
SourceDir: e.sourceDir,
}
cfg, err := generate.NewConfig(genCtx)
if err != nil && tt.wantErr {
// If we expect an error, and NewConfig fails, that's ok.
return
}
if err != nil {
t.Fatalf("failed to create generate config: %v", err)
}
if err := Generate(context.Background(), cfg); (err != nil) != tt.wantErr {
t.Errorf("Generate() error = %v, wantErr %v", err, tt.wantErr)
}
Expand All @@ -268,68 +277,6 @@ java_gapic_library(
}
}

func TestConfig_Validate(t *testing.T) {
tests := []struct {
name string
cfg *Config
wantErr bool
}{
{
name: "valid",
cfg: &Config{
LibrarianDir: "a",
InputDir: "b",
OutputDir: "c",
SourceDir: "d",
},
wantErr: false,
},
{
name: "missing librarian dir",
cfg: &Config{
InputDir: "b",
OutputDir: "c",
SourceDir: "d",
},
wantErr: true,
},
{
name: "missing input dir",
cfg: &Config{
LibrarianDir: "a",
OutputDir: "c",
SourceDir: "d",
},
wantErr: true,
},
{
name: "missing output dir",
cfg: &Config{
LibrarianDir: "a",
InputDir: "b",
SourceDir: "d",
},
wantErr: true,
},
{
name: "missing source dir",
cfg: &Config{
LibrarianDir: "a",
InputDir: "b",
OutputDir: "c",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.cfg.Validate(); (err != nil) != tt.wantErr {
t.Errorf("Config.Validate() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func TestRestructureOutput(t *testing.T) {
e := newTestEnv(t)
defer e.cleanup(t)
Expand Down
87 changes: 87 additions & 0 deletions internal/librariangen/languagecontainer/generate/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright 2025 Google LLC
//
// 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.

// Package generate contains types for language container's generate command.
package generate

import (
"errors"
"fmt"
"log/slog"
"path/filepath"

"cloud.google.com/java/internal/librariangen/message"
)

// Context holds the directory paths for the generate command.
// https://github.com/googleapis/librarian/blob/main/doc/language-onboarding.md#generate
type Context struct {
// LibrarianDir is the path to the librarian-tool input directory.
// It is expected to contain the generate-request.json file.
LibrarianDir string
// InputDir is the path to the .librarian/generator-input directory from the
// language repository.
InputDir string
// OutputDir is the path to the empty directory where librariangen writes
// its output.
OutputDir string
// SourceDir is the path to a complete checkout of the googleapis repository.
SourceDir string
}

// Validate ensures that the context is valid.
func (c *Context) Validate() error {
if c.LibrarianDir == "" {
return errors.New("languagecontainer: librarian directory must be set")
}
if c.InputDir == "" {
return errors.New("languagecontainer: input directory must be set")
}
if c.OutputDir == "" {
return errors.New("languagecontainer: output directory must be set")
}
if c.SourceDir == "" {
return errors.New("languagecontainer: source directory must be set")
}
return nil
}

// Config for the generate command. This holds the context (the directory paths)
// and the request parsed from the generate-request.json file.
type Config struct {
Context *Context
// This request is parsed from the generate-request.json file in
// the LibrarianDir of the context.
Request *message.Library
}

// NewConfig creates a new Config, parsing the generate-request.json file
// from the LibrarianDir in the given Context.
func NewConfig(ctx *Context) (*Config, error) {
if err := ctx.Validate(); err != nil {
return nil, fmt.Errorf("invalid context: %w", err)
}
reqPath := filepath.Join(ctx.LibrarianDir, "generate-request.json")
slog.Debug("languagecontainer: reading generate request", "path", reqPath)

generateReq, err := message.ParseLibrary(reqPath)
if err != nil {
return nil, err
}
slog.Debug("languagecontainer: successfully unmarshalled request", "library_id", generateReq.ID)
return &Config{
Context: ctx,
Request: generateReq,
}, nil
}
Loading
Loading