| 1 |
// Copyright 2025 Google LLC |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package postprocessor |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
_ "embed" |
| 20 |
"fmt" |
| 21 |
"log/slog" |
| 22 |
|
| 23 |
"cloud.google.com/go/internal/postprocessor/librarian/librariangen/config" |
| 24 |
"cloud.google.com/go/internal/postprocessor/librarian/librariangen/execv" |
| 25 |
"cloud.google.com/go/internal/postprocessor/librarian/librariangen/module" |
| 26 |
"cloud.google.com/go/internal/postprocessor/librarian/librariangen/request" |
| 27 |
) |
| 28 |
|
| 29 |
// Test substitution vars. |
| 30 |
var ( |
| 31 |
execvRun = execv.Run |
| 32 |
) |
| 33 |
|
| 34 |
// PostProcess is the entrypoint for post-processing generated files. It runs |
| 35 |
// formatters and other tools to ensure code quality. The high-level steps are: |
| 36 |
// |
| 37 |
// 1. Modify the generated snippets to specify the current version |
| 38 |
// 2. Run `goimports` to format the code. |
| 39 |
func PostProcess(ctx context.Context, req *request.Library, outputDir, moduleDir string, moduleConfig *config.ModuleConfig) error { |
| 40 |
slog.Debug("librariangen: starting post-processing", "directory", moduleDir) |
| 41 |
|
| 42 |
if len(req.APIs) == 0 { |
| 43 |
slog.Debug("librariangen: no APIs in request, skipping module initialization") |
| 44 |
return nil |
| 45 |
} |
| 46 |
|
| 47 |
if req.Version == "" { |
| 48 |
return fmt.Errorf("librariangen: no version for API: %s (required for post-processing)", req.ID) |
| 49 |
} |
| 50 |
|
| 51 |
if err := module.UpdateSnippetsMetadata(req, outputDir, outputDir, moduleConfig); err != nil { |
| 52 |
return fmt.Errorf("librariangen: failed to update snippets metadata: %w", err) |
| 53 |
} |
| 54 |
|
| 55 |
if err := goimports(ctx, moduleDir); err != nil { |
| 56 |
return fmt.Errorf("librariangen: failed to run 'goimports': %w", err) |
| 57 |
} |
| 58 |
|
| 59 |
slog.Debug("librariangen: post-processing finished successfully") |
| 60 |
return nil |
| 61 |
} |
| 62 |
|
| 63 |
// goimports runs the goimports tool on a directory to format Go files and |
| 64 |
// manage imports. |
| 65 |
func goimports(ctx context.Context, dir string) error { |
| 66 |
slog.Debug("librariangen: running goimports", "directory", dir) |
| 67 |
// The `.` argument will make goimports process all go files in the directory |
| 68 |
// and its subdirectories. The -w flag writes results back to source files. |
| 69 |
args := []string{"goimports", "-w", "."} |
| 70 |
return execvRun(ctx, args, dir) |
| 71 |
} |