@@ -29,6 +29,7 @@ import (
2929 "github.com/googleapis/librarian/internal/config"
3030 "github.com/googleapis/librarian/internal/filesystem"
3131 "github.com/googleapis/librarian/internal/license"
32+ "github.com/googleapis/librarian/internal/postprocessing"
3233 "github.com/googleapis/librarian/internal/serviceconfig"
3334)
3435
@@ -70,18 +71,40 @@ type libraryPostProcessParams struct {
7071// TODO(https://github.com/googleapis/librarian/issues/6627): Remove legacy owlbot.py
7172// postprocessing execution once native Go postprocessing is enabled.
7273func postProcessLibrary (ctx context.Context , params libraryPostProcessParams ) error {
73- if err := createOrVerifyOwlbotPy (params .outDir ); err != nil {
74- return err
75- }
76- bomVersion , err := findBOMVersion (params .cfg )
77- if err != nil {
78- return err
79- }
80- if err := removeKeptFilesFromStaging (params .library , params .outDir ); err != nil {
81- return fmt .Errorf ("failed to remove kept files from staging: %w" , err )
74+ owlbotPath := filepath .Join (params .outDir , "owlbot.py" )
75+ _ , err := os .Stat (owlbotPath )
76+ if err != nil && ! errors .Is (err , fs .ErrNotExist ) {
77+ return fmt .Errorf ("failed to check for owlbot.py: %w" , err )
8278 }
83- if err := runOwlBot (ctx , params .library , params .outDir , bomVersion ); err != nil {
84- return fmt .Errorf ("%w: %w" , errRunOwlBot , err )
79+ owlbotExists := err == nil
80+
81+ if owlbotExists {
82+ if err := createOrVerifyOwlbotPy (params .outDir ); err != nil {
83+ return err
84+ }
85+ bomVersion , err := findBOMVersion (params .cfg )
86+ if err != nil {
87+ return err
88+ }
89+ if err := removeKeptFilesFromStaging (params .library , params .outDir ); err != nil {
90+ return fmt .Errorf ("failed to remove kept files from staging: %w" , err )
91+ }
92+ if err := runOwlBot (ctx , params .library , params .outDir , bomVersion ); err != nil {
93+ return fmt .Errorf ("%w: %w" , errRunOwlBot , err )
94+ }
95+ } else {
96+ if params .library != nil && params .library .Postprocess != nil {
97+ if err := postprocessing .Apply (params .outDir , params .library .Postprocess ); err != nil {
98+ return err
99+ }
100+ }
101+ var keepSet map [string ]bool
102+ if params .library != nil {
103+ keepSet = toKeepSet (params .library .Keep )
104+ }
105+ if err := renderREADME (params , keepSet ); err != nil {
106+ return fmt .Errorf ("failed to render README: %w" , err )
107+ }
85108 }
86109
87110 monorepoVersion , err := findMonorepoVersion (params .cfg )
@@ -140,23 +163,54 @@ func postProcessAPI(ctx context.Context, params postProcessParams) error {
140163 if err := copyFiles (params ); err != nil {
141164 return fmt .Errorf ("failed to copy files: %w" , err )
142165 }
143- if err := restructureToStaging (params ); err != nil {
144- return fmt .Errorf ("failed to restructure to staging: %w" , err )
145- }
146166
147- // Generate clirr-ignored-differences.xml for the proto module.
148- // We target the staging directory because runOwlBot hasn't moved the files
149- // to their final destination yet.
150- coords := params .coords ()
151- protoModuleRepoRoot := filepath .Join (params .outDir , coords .Proto .ArtifactID )
152- shouldGenerate , err := clirrIgnoreShouldGenerate (coords .Proto .ArtifactID , protoModuleRepoRoot , params .javaAPI .Monolithic )
153- if err != nil {
154- return fmt .Errorf ("failed to check for clirr ignore file: %w" , err )
167+ owlbotPath := filepath .Join (params .outDir , "owlbot.py" )
168+ _ , err := os .Stat (owlbotPath )
169+ if err != nil && ! errors .Is (err , fs .ErrNotExist ) {
170+ return fmt .Errorf ("failed to check for owlbot.py: %w" , err )
155171 }
156- if shouldGenerate {
157- protoModuleStagingRoot := filepath .Join (stagingDir (params .outDir ), params .apiBase , coords .Proto .ArtifactID )
158- if err := generateClirrIgnore (protoModuleStagingRoot ); err != nil {
159- return fmt .Errorf ("failed to generate clirr ignore file: %w" , err )
172+ owlbotExists := err == nil
173+
174+ if ! owlbotExists {
175+ var keepSet map [string ]bool
176+ if params .library != nil {
177+ keepSet = toKeepSet (params .library .Keep )
178+ }
179+ if err := restructureToLibrary (params , params .outDir , keepSet ); err != nil {
180+ return fmt .Errorf ("failed to restructure to library root: %w" , err )
181+ }
182+
183+ coords := params .coords ()
184+ // Generate clirr-ignored-differences.xml for the proto module.
185+ protoModuleRepoRoot := filepath .Join (params .outDir , coords .Proto .ArtifactID )
186+ shouldGenerate , err := clirrIgnoreShouldGenerate (coords .Proto .ArtifactID , protoModuleRepoRoot , params .javaAPI .Monolithic )
187+ if err != nil {
188+ return fmt .Errorf ("failed to check for clirr ignore file: %w" , err )
189+ }
190+ if shouldGenerate {
191+ if err := generateClirrIgnore (protoModuleRepoRoot ); err != nil {
192+ return fmt .Errorf ("failed to generate clirr ignore file: %w" , err )
193+ }
194+ }
195+ } else {
196+ if err := restructureToStaging (params ); err != nil {
197+ return fmt .Errorf ("failed to restructure to staging: %w" , err )
198+ }
199+
200+ // Generate clirr-ignored-differences.xml for the proto module.
201+ // We target the staging directory because runOwlBot hasn't moved the files
202+ // to their final destination yet.
203+ coords := params .coords ()
204+ protoModuleRepoRoot := filepath .Join (params .outDir , coords .Proto .ArtifactID )
205+ shouldGenerate , err := clirrIgnoreShouldGenerate (coords .Proto .ArtifactID , protoModuleRepoRoot , params .javaAPI .Monolithic )
206+ if err != nil {
207+ return fmt .Errorf ("failed to check for clirr ignore file: %w" , err )
208+ }
209+ if shouldGenerate {
210+ protoModuleStagingRoot := filepath .Join (stagingDir (params .outDir ), params .apiBase , coords .Proto .ArtifactID )
211+ if err := generateClirrIgnore (protoModuleStagingRoot ); err != nil {
212+ return fmt .Errorf ("failed to generate clirr ignore file: %w" , err )
213+ }
160214 }
161215 }
162216
@@ -560,8 +614,8 @@ func ApplyMoveActionsToLibrary(actions []moveAction, destRoot string, keepSet ma
560614 return nil
561615}
562616
563- // ToKeepSet normalizes a list of keep paths into a lookup map.
564- func ToKeepSet (keep []string ) map [string ]bool {
617+ // toKeepSet normalizes a list of keep paths into a lookup map.
618+ func toKeepSet (keep []string ) map [string ]bool {
565619 keepSet := make (map [string ]bool , len (keep ))
566620 for _ , k := range keep {
567621 normalized := strings .TrimSuffix (filepath .ToSlash (k ), "/" )
@@ -570,9 +624,9 @@ func ToKeepSet(keep []string) map[string]bool {
570624 return keepSet
571625}
572626
573- // RestructureToLibrary moves all generated source code to the library root directories.
627+ // restructureToLibrary moves all generated source code to the library root directories.
574628// It also removes conflicting files, and copies public proto files to the library.
575- func RestructureToLibrary (params postProcessParams , destRoot string , keepSet map [string ]bool ) error {
629+ func restructureToLibrary (params postProcessParams , destRoot string , keepSet map [string ]bool ) error {
576630 tempProtoSrcDir := params .protoDir ()
577631 isCommonProtos := params .library .Name == commonProtosLibrary
578632 if ! isCommonProtos {
0 commit comments