@@ -17,7 +17,6 @@ package generate
1717import (
1818 "archive/zip"
1919 "context"
20- "errors"
2120 "fmt"
2221 "io"
2322 "log/slog"
@@ -27,82 +26,47 @@ import (
2726
2827 "cloud.google.com/java/internal/librariangen/bazel"
2928 "cloud.google.com/java/internal/librariangen/execv"
29+ "cloud.google.com/java/internal/librariangen/languagecontainer/generate"
3030 "cloud.google.com/java/internal/librariangen/message"
3131 "cloud.google.com/java/internal/librariangen/protoc"
3232)
3333
3434// Test substitution vars.
3535var (
36- bazelParse = bazel .Parse
37- execvRun = execv .Run
38- requestParse = message .ParseLibrary
39- protocBuild = protoc .Build
36+ bazelParse = bazel .Parse
37+ execvRun = execv .Run
38+ protocBuild = protoc .Build
4039)
4140
42- // Config holds the internal librariangen configuration for the generate command.
43- type Config struct {
44- // LibrarianDir is the path to the librarian-tool input directory.
45- // It is expected to contain the generate-request.json file.
46- LibrarianDir string
47- // InputDir is the path to the .librarian/generator-input directory from the
48- // language repository.
49- InputDir string
50- // OutputDir is the path to the empty directory where librariangen writes
51- // its output.
52- OutputDir string
53- // SourceDir is the path to a complete checkout of the googleapis repository.
54- SourceDir string
55- }
56-
57- // Validate ensures that the configuration is valid.
58- func (c * Config ) Validate () error {
59- if c .LibrarianDir == "" {
60- return errors .New ("librariangen: librarian directory must be set" )
61- }
62- if c .InputDir == "" {
63- return errors .New ("librariangen: input directory must be set" )
64- }
65- if c .OutputDir == "" {
66- return errors .New ("librariangen: output directory must be set" )
67- }
68- if c .SourceDir == "" {
69- return errors .New ("librariangen: source directory must be set" )
70- }
71- return nil
72- }
73-
7441// Generate is the main entrypoint for the `generate` command. It orchestrates
7542// the entire generation process.
76- func Generate (ctx context.Context , cfg * Config ) error {
77- if err := cfg .Validate (); err != nil {
43+ func Generate (ctx context.Context , cfg * generate. Config ) error {
44+ if err := cfg .Context . Validate (); err != nil {
7845 return fmt .Errorf ("librariangen: invalid configuration: %w" , err )
7946 }
8047 slog .Debug ("librariangen: generate command started" )
81- defer cleanupIntermediateFiles (cfg .OutputDir )
48+ defer cleanupIntermediateFiles (cfg .Context . OutputDir )
8249
83- generateReq , err := readGenerateReq (cfg .LibrarianDir )
84- if err != nil {
85- return fmt .Errorf ("librariangen: failed to read request: %w" , err )
86- }
50+ generateReq := cfg .Request
8751
88- if err := invokeProtoc (ctx , cfg , generateReq ); err != nil {
52+ if err := invokeProtoc (ctx , cfg . Context , generateReq ); err != nil {
8953 return fmt .Errorf ("librariangen: gapic generation failed: %w" , err )
9054 }
9155
9256 // Unzip the generated zip file.
93- zipPath := filepath .Join (cfg .OutputDir , "java_gapic.zip" )
94- if err := unzip (zipPath , cfg .OutputDir ); err != nil {
57+ zipPath := filepath .Join (cfg .Context . OutputDir , "java_gapic.zip" )
58+ if err := unzip (zipPath , cfg .Context . OutputDir ); err != nil {
9559 return fmt .Errorf ("librariangen: failed to unzip %s: %w" , zipPath , err )
9660 }
9761
9862 // Unzip the inner temp-codegen.srcjar.
99- srcjarPath := filepath .Join (cfg .OutputDir , "temp-codegen.srcjar" )
100- srcjarDest := filepath .Join (cfg .OutputDir , "java_gapic_srcjar" )
63+ srcjarPath := filepath .Join (cfg .Context . OutputDir , "temp-codegen.srcjar" )
64+ srcjarDest := filepath .Join (cfg .Context . OutputDir , "java_gapic_srcjar" )
10165 if err := unzip (srcjarPath , srcjarDest ); err != nil {
10266 return fmt .Errorf ("librariangen: failed to unzip %s: %w" , srcjarPath , err )
10367 }
10468
105- if err := restructureOutput (cfg .OutputDir , generateReq .ID ); err != nil {
69+ if err := restructureOutput (cfg .Context . OutputDir , generateReq .ID ); err != nil {
10670 return fmt .Errorf ("librariangen: failed to restructure output: %w" , err )
10771 }
10872
@@ -113,40 +77,25 @@ func Generate(ctx context.Context, cfg *Config) error {
11377// invokeProtoc handles the protoc GAPIC generation logic for the 'generate' CLI command.
11478// It reads a request file, and for each API specified, it invokes protoc
11579// to generate the client library. It returns the module path and the path to the service YAML.
116- func invokeProtoc (ctx context.Context , cfg * Config , generateReq * message.Library ) error {
80+ func invokeProtoc (ctx context.Context , genCtx * generate. Context , generateReq * message.Library ) error {
11781 for _ , api := range generateReq .APIs {
118- apiServiceDir := filepath .Join (cfg .SourceDir , api .Path )
82+ apiServiceDir := filepath .Join (genCtx .SourceDir , api .Path )
11983 slog .Info ("processing api" , "service_dir" , apiServiceDir )
12084 bazelConfig , err := bazelParse (apiServiceDir )
12185 if err != nil {
12286 return fmt .Errorf ("librariangen: failed to parse BUILD.bazel for %s: %w" , apiServiceDir , err )
12387 }
124- args , err := protocBuild (apiServiceDir , bazelConfig , cfg .SourceDir , cfg .OutputDir )
88+ args , err := protocBuild (apiServiceDir , bazelConfig , genCtx .SourceDir , genCtx .OutputDir )
12589 if err != nil {
12690 return fmt .Errorf ("librariangen: failed to build protoc command for api %q in library %q: %w" , api .Path , generateReq .ID , err )
12791 }
128- if err := execvRun (ctx , args , cfg .OutputDir ); err != nil {
92+ if err := execvRun (ctx , args , genCtx .OutputDir ); err != nil {
12993 return fmt .Errorf ("librariangen: protoc failed for api %q in library %q: %w" , api .Path , generateReq .ID , err )
13094 }
13195 }
13296 return nil
13397}
13498
135- // readGenerateReq reads generate-request.json from the librarian-tool input directory.
136- // The request file tells librariangen which library and APIs to generate.
137- // It is prepared by the Librarian tool and mounted at /librarian.
138- func readGenerateReq (librarianDir string ) (* message.Library , error ) {
139- reqPath := filepath .Join (librarianDir , "generate-request.json" )
140- slog .Debug ("librariangen: reading generate request" , "path" , reqPath )
141-
142- generateReq , err := requestParse (reqPath )
143- if err != nil {
144- return nil , err
145- }
146- slog .Debug ("librariangen: successfully unmarshalled request" , "library_id" , generateReq .ID )
147- return generateReq , nil
148- }
149-
15099// moveFiles moves all files (and directories) from sourceDir to targetDir.
151100func moveFiles (sourceDir , targetDir string ) error {
152101 files , err := os .ReadDir (sourceDir )
0 commit comments