-
Notifications
You must be signed in to change notification settings - Fork 70
chore(librariangen): Generate to use languagecontainer.Run #3968
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b68161a
chore(librariangen): Generate to use languagecontainer.Run
suztomo d7ce698
Test to validate the nil functions
suztomo 1160ca4
Validation moved to languagecontainer
suztomo d159ee2
Do not pass the command itself in the argument.
suztomo ef288e5
Merge remote-tracking branch 'upstream/main' into languagecontainer_g…
suztomo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -17,7 +17,6 @@ package generate | |
| import ( | ||
| "archive/zip" | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "log/slog" | ||
|
|
@@ -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 { | ||
| // 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) | ||
| } | ||
|
|
||
|
|
@@ -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) | ||
| } | ||
|
|
@@ -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) { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This has moved to |
||
| 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) | ||
|
|
||
This file contains hidden or 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
87 changes: 87 additions & 0 deletions
87
internal/librariangen/languagecontainer/generate/generate.go
This file contains hidden or 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,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 | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.