-
Notifications
You must be signed in to change notification settings - Fork 70
feat(librariangen): add generate package #3952
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
d585c36
feat(librariangen): add generate package
meltsufin ae0d7e9
Merge branch 'main' into librarian-generate
meltsufin be36414
fix: zip slip vulnerability
meltsufin 0823d90
fix: potential file descriptor leak
meltsufin bc5d135
address gemini feedback
meltsufin 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
Some comments aren't visible on the classic Files Changed page.
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 |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| go 1.24.7 | ||
|
|
||
| use ./internal/librariangen | ||
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 @@ | ||
| workspace/ |
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
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
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,267 @@ | ||
| // 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 | ||
|
|
||
| import ( | ||
| "archive/zip" | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "log/slog" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "cloud.google.com/java/internal/librariangen/bazel" | ||
| "cloud.google.com/java/internal/librariangen/execv" | ||
| "cloud.google.com/java/internal/librariangen/protoc" | ||
| "cloud.google.com/java/internal/librariangen/request" | ||
| ) | ||
|
|
||
| // Test substitution vars. | ||
| var ( | ||
| bazelParse = bazel.Parse | ||
| execvRun = execv.Run | ||
| requestParse = request.ParseLibrary | ||
| 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) | ||
| } | ||
| slog.Debug("librariangen: generate command started") | ||
| defer cleanupIntermediateFiles(cfg.OutputDir) | ||
|
|
||
| generateReq, err := readGenerateReq(cfg.LibrarianDir) | ||
| if err != nil { | ||
| return fmt.Errorf("librariangen: failed to read request: %w", err) | ||
| } | ||
|
|
||
| if err := invokeProtoc(ctx, cfg, generateReq); err != nil { | ||
| return fmt.Errorf("librariangen: gapic generation failed: %w", err) | ||
| } | ||
|
|
||
| // Unzip the generated zip file. | ||
| zipPath := filepath.Join(cfg.OutputDir, "java_gapic.zip") | ||
| if err := unzip(zipPath, cfg.OutputDir); err != nil { | ||
| return fmt.Errorf("librariangen: failed to unzip %s: %w", zipPath, err) | ||
| } | ||
|
|
||
| // Unzip the inner temp-codegen.srcjar. | ||
| srcjarPath := filepath.Join(cfg.OutputDir, "temp-codegen.srcjar") | ||
| srcjarDest := filepath.Join(cfg.OutputDir, "java_gapic_srcjar") | ||
| 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 { | ||
| return fmt.Errorf("librariangen: failed to restructure output: %w", err) | ||
| } | ||
|
|
||
| slog.Debug("librariangen: generate command finished") | ||
| return nil | ||
| } | ||
|
|
||
| // 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 *request.Library) error { | ||
| for _, api := range generateReq.APIs { | ||
| apiServiceDir := filepath.Join(cfg.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, cfg.OutputDir) | ||
| if err != nil { | ||
| return fmt.Errorf("librariangen: failed to build protoc command for api %q in library %q: %w", api.Path, generateReq.ID, err) | ||
| } | ||
| if err := execvRun(ctx, args, cfg.OutputDir); err != nil { | ||
| return fmt.Errorf("librariangen: protoc failed for api %q in library %q: %w", api.Path, generateReq.ID, 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) (*request.Library, error) { | ||
| 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) | ||
| if err != nil { | ||
| return fmt.Errorf("librariangen: failed to read dir %s: %w", sourceDir, err) | ||
| } | ||
| for _, f := range files { | ||
| oldPath := filepath.Join(sourceDir, f.Name()) | ||
| newPath := filepath.Join(targetDir, f.Name()) | ||
| slog.Debug("librariangen: moving file", "from", oldPath, "to", newPath) | ||
| if err := os.Rename(oldPath, newPath); err != nil { | ||
| return fmt.Errorf("librariangen: failed to move %s to %s: %w", oldPath, newPath, err) | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func restructureOutput(outputDir, libraryID string) error { | ||
| slog.Debug("librariangen: restructuring output directory", "dir", outputDir) | ||
|
|
||
| // Define source and destination directories. | ||
| gapicSrcDir := filepath.Join(outputDir, "java_gapic_srcjar", "src", "main", "java") | ||
| gapicTestDir := filepath.Join(outputDir, "java_gapic_srcjar", "src", "test", "java") | ||
| protoSrcDir := filepath.Join(outputDir, "com") | ||
| samplesDir := filepath.Join(outputDir, "java_gapic_srcjar", "samples", "snippets") | ||
|
|
||
| gapicDestDir := filepath.Join(outputDir, fmt.Sprintf("google-cloud-%s", libraryID), "src", "main", "java") | ||
| gapicTestDestDir := filepath.Join(outputDir, fmt.Sprintf("google-cloud-%s", libraryID), "src", "test", "java") | ||
| protoDestDir := filepath.Join(outputDir, fmt.Sprintf("proto-google-cloud-%s-v1", libraryID), "src", "main", "java") | ||
| samplesDestDir := filepath.Join(outputDir, "samples", "snippets") | ||
|
|
||
| // Create destination directories. | ||
| destDirs := []string{gapicDestDir, gapicTestDestDir, protoDestDir, samplesDestDir} | ||
| for _, dir := range destDirs { | ||
| if err := os.MkdirAll(dir, 0755); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| // Move files. | ||
| moves := map[string]string{ | ||
| gapicSrcDir: gapicDestDir, | ||
| gapicTestDir: gapicTestDestDir, | ||
| protoSrcDir: protoDestDir, | ||
| samplesDir: samplesDestDir, | ||
| } | ||
| for src, dest := range moves { | ||
| if err := moveFiles(src, dest); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func cleanupIntermediateFiles(outputDir string) { | ||
| slog.Debug("librariangen: cleaning up intermediate files", "dir", outputDir) | ||
| filesToRemove := []string{ | ||
| "java_gapic_srcjar", | ||
| "com", | ||
| "java_gapic.zip", | ||
| "temp-codegen.srcjar", | ||
| } | ||
| for _, file := range filesToRemove { | ||
| path := filepath.Join(outputDir, file) | ||
| if err := os.RemoveAll(path); err != nil { | ||
| slog.Error("librariangen: failed to clean up intermediate file", "path", path, "error", err) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func unzip(src, dest string) error { | ||
| r, err := zip.OpenReader(src) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer r.Close() | ||
|
|
||
| for _, f := range r.File { | ||
| fpath := filepath.Join(dest, f.Name) | ||
|
|
||
| if !strings.HasPrefix(fpath, filepath.Clean(dest)+string(os.PathSeparator)) { | ||
| return fmt.Errorf("librariangen: illegal file path: %s", fpath) | ||
| } | ||
|
|
||
| if f.FileInfo().IsDir() { | ||
| os.MkdirAll(fpath, os.ModePerm) | ||
| continue | ||
| } | ||
|
|
||
| if err := os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| outFile, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode()) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| rc, err := f.Open() | ||
| if err != nil { | ||
| outFile.Close() | ||
| return err | ||
| } | ||
|
|
||
| _, copyErr := io.Copy(outFile, rc) | ||
| rc.Close() // Error on read-only file close is less critical | ||
| closeErr := outFile.Close() | ||
|
|
||
| if copyErr != nil { | ||
| return copyErr | ||
| } | ||
| if closeErr != nil { | ||
| return closeErr | ||
| } | ||
| } | ||
| return 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.
Uh oh!
There was an error while loading. Please reload this page.