@@ -49,12 +49,7 @@ func Generate(ctx context.Context, library *config.Library, srcs *sources.Source
4949 if err := os .MkdirAll (outDir , 0755 ); err != nil {
5050 return fmt .Errorf ("failed to create output directory: %w" , err )
5151 }
52- tempDir , err := os .MkdirTemp ("" , "librarian-gen-" )
53- defer func () {
54- if removeErr := os .RemoveAll (tempDir ); removeErr != nil {
55- err = errors .Join (err , removeErr )
56- }
57- }()
52+
5853 // For preview libraries, the API protos are rooted in the
5954 // googleapis/preview subdirectory, so change the googleapisDir to target
6055 // that root.
@@ -64,14 +59,16 @@ func Generate(ctx context.Context, library *config.Library, srcs *sources.Source
6459 }
6560
6661 for i , api := range library .APIs {
62+ // TODO(https://github.com/googleapis/librarian/issues/4777): Generate APIs in a temp
63+ // directory.
6764 goAPI := findGoAPI (library , api .Path )
6865 if goAPI == nil {
6966 return fmt .Errorf ("error finding goAPI associated with API %s: %w" , api .Path , errGoAPINotFound )
7067 }
71- if err := generateAPI (ctx , goAPI , googleapisDir , tempDir ); err != nil {
68+ if err := generateAPI (ctx , goAPI , googleapisDir , outDir ); err != nil {
7269 return fmt .Errorf ("api %q: %w" , api .Path , err )
7370 }
74- if err := moveGeneratedFiles (library , goAPI , tempDir , outDir ); err != nil {
71+ if err := moveGeneratedFiles (library , goAPI , outDir ); err != nil {
7572 return err
7673 }
7774 if err := generateClientVersionFile (library , goAPI ); err != nil {
@@ -101,6 +98,9 @@ func Generate(ctx context.Context, library *config.Library, srcs *sources.Source
10198 }
10299 }
103100 }
101+ if err := os .RemoveAll (filepath .Join (outDir , "cloud.google.com" )); err != nil {
102+ return err
103+ }
104104 if _ , err := os .Stat (filepath .Join (outDir , "go.mod" )); err != nil {
105105 if errors .Is (err , fs .ErrNotExist ) {
106106 // New client, init the module.
@@ -189,17 +189,17 @@ func buildGAPICImportPath(goAPI *config.GoAPI) string {
189189
190190// moveGeneratedFiles moves generated API and snippet files from the protoc output
191191// directory to their destination in the repository.
192- func moveGeneratedFiles (library * config.Library , goAPI * config.GoAPI , srcDir , outDir string ) error {
193- if err := moveAPIDirectory (library , goAPI , srcDir , outDir ); err != nil {
192+ func moveGeneratedFiles (library * config.Library , goAPI * config.GoAPI , outDir string ) error {
193+ if err := moveAPIDirectory (library , goAPI , outDir ); err != nil {
194194 return err
195195 }
196- return moveAndUpdateSnippets (library , goAPI , srcDir , outDir )
196+ return moveAndUpdateSnippets (library , goAPI , outDir )
197197}
198198
199199// moveAPIDirectory moves the generated API directory from the temporary location to its
200200// final destination in the repository.
201- func moveAPIDirectory (library * config.Library , goAPI * config.GoAPI , srcDir , outDir string ) error {
202- libraryDirPrefix := filepath .Join (srcDir , "cloud.google.com" , "go" )
201+ func moveAPIDirectory (library * config.Library , goAPI * config.GoAPI , outDir string ) error {
202+ libraryDirPrefix := filepath .Join (outDir , "cloud.google.com" , "go" )
203203 librarySrc := filepath .Join (libraryDirPrefix , goAPI .ImportPath )
204204 libraryDest := filepath .Join (repoRootPath (outDir , library .Name ), clientPathFromRepoRoot (library , goAPI ))
205205 if err := os .MkdirAll (libraryDest , 0755 ); err != nil {
@@ -210,15 +210,15 @@ func moveAPIDirectory(library *config.Library, goAPI *config.GoAPI, srcDir, outD
210210
211211// moveAndUpdateSnippets moves the generated snippets from the temporary location to their final
212212// destination and updates their library versions.
213- func moveAndUpdateSnippets (library * config.Library , goAPI * config.GoAPI , srcDir , outDir string ) error {
213+ func moveAndUpdateSnippets (library * config.Library , goAPI * config.GoAPI , outDir string ) error {
214214 snippetDest := findSnippetDirectory (library , goAPI , outDir )
215215 if snippetDest == "" {
216216 return nil
217217 }
218218 if err := os .MkdirAll (snippetDest , 0755 ); err != nil {
219219 return err
220220 }
221- snippetDirPrefix := filepath .Join (srcDir , "cloud.google.com" , "go" , "internal" , "generated" , "snippets" )
221+ snippetDirPrefix := filepath .Join (outDir , "cloud.google.com" , "go" , "internal" , "generated" , "snippets" )
222222 snippetSrc := filepath .Join (snippetDirPrefix , goAPI .ImportPath )
223223 if err := filesystem .MoveAndMerge (snippetSrc , snippetDest ); err != nil {
224224 return err
0 commit comments