|
| 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 | +// https://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 pom |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "os" |
| 20 | + "path/filepath" |
| 21 | + "regexp" |
| 22 | +) |
| 23 | + |
| 24 | +var ( |
| 25 | + versionRegex = regexp.MustCompile(`(<version>)([^<]+)(</version>\s*<!-- \{x-version-update:([^:]+):current\} -->)`) |
| 26 | +) |
| 27 | + |
| 28 | +// UpdateVersions updates the versions of all pom.xml files in a given directory. |
| 29 | +// It appends the "-SNAPSHOT" suffix to the version given the version parameter. |
| 30 | +// If the directory is not present, this function creates it. |
| 31 | +func UpdateVersions(repoDir, sourcePath, outputDir, libraryID, version string) error { |
| 32 | + pomFiles, err := findPomFiles(sourcePath) |
| 33 | + if err != nil { |
| 34 | + return fmt.Errorf("failed to find pom files: %w", err) |
| 35 | + } |
| 36 | + for _, pomFile := range pomFiles { |
| 37 | + relPath, err := filepath.Rel(repoDir, pomFile) |
| 38 | + if err != nil { |
| 39 | + return fmt.Errorf("failed to get relative path for %s: %w", pomFile, err) |
| 40 | + } |
| 41 | + outputPomFile := filepath.Join(outputDir, relPath) |
| 42 | + if err := os.MkdirAll(filepath.Dir(outputPomFile), 0755); err != nil { |
| 43 | + return fmt.Errorf("failed to create output directory for %s: %w", outputPomFile, err) |
| 44 | + } |
| 45 | + if err := updateVersion(pomFile, outputPomFile, libraryID, version); err != nil { |
| 46 | + return fmt.Errorf("failed to update version in %s: %w", pomFile, err) |
| 47 | + } |
| 48 | + } |
| 49 | + return nil |
| 50 | +} |
| 51 | + |
| 52 | +// updateVersion updates the version in a single pom.xml file. |
| 53 | +// It appends the "-SNAPSHOT" suffix to the the version parameter. |
| 54 | +// The directory for outputPath must already exist. |
| 55 | +func updateVersion(inputPath, outputPath, libraryID, version string) error { |
| 56 | + content, err := os.ReadFile(inputPath) |
| 57 | + if err != nil { |
| 58 | + return fmt.Errorf("failed to read file: %w", err) |
| 59 | + } |
| 60 | + |
| 61 | + newContent := versionRegex.ReplaceAllStringFunc(string(content), func(s string) string { |
| 62 | + matches := versionRegex.FindStringSubmatch(s) |
| 63 | + if len(matches) > 4 && matches[4] == libraryID { |
| 64 | + // matches[1] is "<version>" |
| 65 | + // matches[2] is the old version |
| 66 | + // matches[3] is " <!-- {x-version-update:libraryID:current} --> </version>" |
| 67 | + // matches[4] is libraryID |
| 68 | + return fmt.Sprintf("%s%s-SNAPSHOT%s", matches[1], version, matches[3]) |
| 69 | + } |
| 70 | + return s |
| 71 | + }) |
| 72 | + |
| 73 | + if err := os.WriteFile(outputPath, []byte(newContent), 0644); err != nil { |
| 74 | + return fmt.Errorf("failed to write file: %w", err) |
| 75 | + } |
| 76 | + return nil |
| 77 | +} |
| 78 | + |
| 79 | +func findPomFiles(path string) ([]string, error) { |
| 80 | + var pomFiles []string |
| 81 | + // Return empty if there's no matching directory. |
| 82 | + if _, err := os.Stat(path); os.IsNotExist(err) { |
| 83 | + return []string{}, nil |
| 84 | + } |
| 85 | + |
| 86 | + err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error { |
| 87 | + if err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + if !info.IsDir() && info.Name() == "pom.xml" { |
| 91 | + pomFiles = append(pomFiles, path) |
| 92 | + } |
| 93 | + return nil |
| 94 | + }) |
| 95 | + if err != nil { |
| 96 | + return nil, fmt.Errorf("failed to walk path: %w", err) |
| 97 | + } |
| 98 | + return pomFiles, nil |
| 99 | +} |
0 commit comments