Skip to content

Commit 3a8988c

Browse files
committed
Handle plugins
Signed-off-by: Pierangelo Di Pilato <[email protected]>
1 parent 51a852c commit 3a8988c

File tree

1 file changed

+105
-63
lines changed

1 file changed

+105
-63
lines changed

pkg/maven/maven_pom.go

+105-63
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type Pom struct {
1818
Version string `xml:"version"`
1919
Dependencies Dependencies `xml:"dependencies"`
2020
DependencyManagement DependencyManagement `xml:"dependencyManagement"`
21+
Build Build `xml:"build"`
2122
Properties Properties `xml:"properties"`
2223
}
2324

@@ -31,6 +32,19 @@ type Dependencies struct {
3132
Dependency []Dependency `xml:"dependency"`
3233
}
3334

35+
type Plugins struct {
36+
Plugin []Dependency `xml:"plugin"`
37+
}
38+
39+
type PluginManagement struct {
40+
Plugins Plugins `xml:"plugins"`
41+
}
42+
43+
type Build struct {
44+
PluginManagement PluginManagement `xml:"pluginManagement"`
45+
Plugins Plugins `xml:"plugins"`
46+
}
47+
3448
// Dependency represents a single dependency block
3549
type Dependency struct {
3650
GroupID string `xml:"groupId"`
@@ -59,11 +73,6 @@ func UpdatePomFile(metadata []Metadata, url string, path string) error {
5973
metadataByGA[depKey(m.GroupID, m.ArtifactID)] = m
6074
}
6175

62-
propVariable := regexp.MustCompile("\\$\\{(\\S+)}")
63-
propValue := regexp.MustCompile("([0-9a-z._-]+)")
64-
65-
versionRegex := regexp.MustCompile("([0-9]+)\\.?([0-9]+)?\\.?([0-9]+)?")
66-
6776
pomFileStr := string(pomFileBytes)
6877

6978
if !strings.Contains(pomFileStr, url) {
@@ -92,82 +101,115 @@ func UpdatePomFile(metadata []Metadata, url string, path string) error {
92101
}
93102

94103
for _, d := range p.DependencyManagement.Dependencies.Dependency {
95-
groupId := d.GroupID
96-
if d.GroupID == "io.quarkus" {
97-
groupId = "com.redhat.quarkus.platform"
104+
pomFileStr, err = replaceDependency(metadataByGA, p, d, pomFileStr)
105+
if err != nil {
106+
return err
98107
}
99-
m, ok := metadataByGA[depKey(groupId, d.ArtifactID)]
100-
if !ok {
101-
continue
108+
}
109+
for _, d := range p.Dependencies.Dependency {
110+
pomFileStr, err = replaceDependency(metadataByGA, p, d, pomFileStr)
111+
if err != nil {
112+
return err
102113
}
103-
version := d.Version
104-
propNameMatch := propVariable.FindStringSubmatch(d.Version)
105-
if len(propNameMatch) > 1 {
106-
version, ok = p.Properties.Entries[propNameMatch[1]]
107-
if !ok {
108-
return fmt.Errorf("failed to replace variable %q from properties %#v", d.Version, p.Properties.Entries)
109-
}
110-
if match := propValue.FindStringSubmatch(version); len(match) > 1 {
111-
version = match[1]
112-
}
114+
}
115+
for _, d := range p.Build.PluginManagement.Plugins.Plugin {
116+
pomFileStr, err = replaceDependency(metadataByGA, p, d, pomFileStr)
117+
if err != nil {
118+
return err
119+
}
120+
}
121+
for _, d := range p.Build.Plugins.Plugin {
122+
pomFileStr, err = replaceDependency(metadataByGA, p, d, pomFileStr)
123+
if err != nil {
124+
return err
113125
}
126+
}
127+
128+
if err := os.WriteFile(path, []byte(pomFileStr), 0666); err != nil {
129+
return fmt.Errorf("failed to update POM file %q: %w", path, err)
130+
}
131+
return nil
132+
}
114133

115-
log.Printf("Dependency %q:%q:%q\n%#v\n", d.GroupID, d.ArtifactID, version, m)
134+
func replaceDependency(metadataByGA map[string]Metadata, p *Pom, d Dependency, pomFileStr string) (string, error) {
116135

117-
versionPieces := versionRegex.FindStringSubmatch(version)
118-
if len(versionPieces) < 2 {
119-
log.Printf("Version %q is not parsable", version)
120-
continue
136+
propVariable := regexp.MustCompile("\\$\\{(\\S+)}")
137+
propValue := regexp.MustCompile("([0-9a-z._-]+)")
138+
139+
versionRegex := regexp.MustCompile("([0-9]+)\\.?([0-9]+)?\\.?([0-9]+)?")
140+
141+
groupId := d.GroupID
142+
if d.GroupID == "io.quarkus" {
143+
groupId = "com.redhat.quarkus.platform"
144+
}
145+
m, ok := metadataByGA[depKey(groupId, d.ArtifactID)]
146+
if !ok {
147+
return pomFileStr, nil
148+
}
149+
version := d.Version
150+
propNameMatch := propVariable.FindStringSubmatch(d.Version)
151+
if len(propNameMatch) > 1 {
152+
version, ok = p.Properties.Entries[propNameMatch[1]]
153+
if !ok {
154+
return pomFileStr, fmt.Errorf("failed to replace variable %q from properties %#v", d.Version, p.Properties.Entries)
155+
}
156+
if match := propValue.FindStringSubmatch(version); len(match) > 1 {
157+
version = match[1]
121158
}
122-
log.Printf("Version %q, pieces %#v", version, versionPieces)
159+
}
123160

124-
versions := make([]string, len(m.Versioning.Versions.Version))
125-
copy(versions, m.Versioning.Versions.Version)
126-
slices.Reverse(versions)
161+
log.Printf("Dependency %q:%q:%q\n%#v\n", d.GroupID, d.ArtifactID, version, m)
127162

128-
for _, v := range versions {
129-
if v == version {
130-
break
131-
}
163+
versionPieces := versionRegex.FindStringSubmatch(version)
164+
if len(versionPieces) < 2 {
165+
log.Printf("Version %q is not parsable", version)
166+
return pomFileStr, nil
167+
}
168+
log.Printf("Version %q, pieces %#v", version, versionPieces)
132169

133-
vPieces := versionRegex.FindStringSubmatch(v)
134-
if len(vPieces) < 2 {
135-
log.Printf("Version %q in metadata is not parsable (%s:%s)", version, m.GroupID, m.ArtifactID)
136-
continue
137-
}
138-
log.Printf("Red Hat Version %q, pieces %#v", v, vPieces)
170+
versions := make([]string, len(m.Versioning.Versions.Version))
171+
copy(versions, m.Versioning.Versions.Version)
172+
slices.Reverse(versions)
139173

140-
if vPieces[1] != versionPieces[1] {
141-
// major version doesn't match
142-
log.Printf("Version %q doesn't match major version in %q", version, v)
143-
continue
144-
}
174+
for _, v := range versions {
175+
if v == version {
176+
break
177+
}
145178

146-
if len(versionPieces) >= 3 && len(vPieces) >= 3 && vPieces[2] != versionPieces[2] {
147-
// minor version doesn't match
148-
log.Printf("Version %q doesn't match minor version in %q", version, v)
149-
continue
150-
}
179+
vPieces := versionRegex.FindStringSubmatch(v)
180+
if len(vPieces) < 2 {
181+
log.Printf("Version %q in metadata is not parsable (%s:%s)", version, m.GroupID, m.ArtifactID)
182+
continue
183+
}
184+
log.Printf("Red Hat Version %q, pieces %#v", v, vPieces)
185+
186+
if vPieces[1] != versionPieces[1] {
187+
// major version doesn't match
188+
log.Printf("Version %q doesn't match major version in %q", version, v)
189+
continue
190+
}
151191

152-
rg := fmt.Sprintf("(.*<groupId>\\s*)%s(\\s*</groupId>\\s*<artifactId>\\s*)%s(\\s*</artifactId>\\s*<version>\\s*)%s(\\s*</version>.*)", regexp.QuoteMeta(d.GroupID), regexp.QuoteMeta(d.ArtifactID), regexp.QuoteMeta(d.Version))
153-
log.Printf("Replacing %q:%q:%q with %q:%q:%q (regex %q)", d.GroupID, d.ArtifactID, version, m.GroupID, m.ArtifactID, v, rg)
192+
if len(versionPieces) >= 3 && len(vPieces) >= 3 && vPieces[2] != versionPieces[2] {
193+
// minor version doesn't match
194+
log.Printf("Version %q doesn't match minor version in %q", version, v)
195+
continue
196+
}
154197

155-
artifactRegex := regexp.MustCompile(rg)
156-
for _, match := range artifactRegex.FindAllStringSubmatch(pomFileStr, -1) {
157-
oldM := fmt.Sprintf("%s%s%s%s%s%s%s", match[1], d.GroupID, match[2], d.ArtifactID, match[3], d.Version, match[4])
158-
newM := fmt.Sprintf("%s%s%s%s%s%s%s", match[1], m.GroupID, match[2], m.ArtifactID, match[3], v, match[4])
198+
rg := fmt.Sprintf("(.*<groupId>\\s*)%s(\\s*</groupId>\\s*<artifactId>\\s*)%s(\\s*</artifactId>\\s*<version>\\s*)%s(\\s*</version>.*)", regexp.QuoteMeta(d.GroupID), regexp.QuoteMeta(d.ArtifactID), regexp.QuoteMeta(d.Version))
199+
log.Printf("Replacing %q:%q:%q with %q:%q:%q (regex %q)", d.GroupID, d.ArtifactID, version, m.GroupID, m.ArtifactID, v, rg)
159200

160-
log.Printf("match:\n%s\n%s\n", oldM, newM)
201+
artifactRegex := regexp.MustCompile(rg)
202+
for _, match := range artifactRegex.FindAllStringSubmatch(pomFileStr, -1) {
203+
oldM := fmt.Sprintf("%s%s%s%s%s%s%s", match[1], d.GroupID, match[2], d.ArtifactID, match[3], d.Version, match[4])
204+
newM := fmt.Sprintf("%s%s%s%s%s%s%s", match[1], m.GroupID, match[2], m.ArtifactID, match[3], v, match[4])
161205

162-
pomFileStr = strings.ReplaceAll(pomFileStr, oldM, newM)
163-
}
206+
log.Printf("match:\n%s\n%s\n", oldM, newM)
207+
208+
pomFileStr = strings.ReplaceAll(pomFileStr, oldM, newM)
164209
}
165210
}
166211

167-
if err := os.WriteFile(path, []byte(pomFileStr), 0666); err != nil {
168-
return fmt.Errorf("failed to update POM file %q: %w", path, err)
169-
}
170-
return nil
212+
return pomFileStr, nil
171213
}
172214

173215
func depKey(groupId, artifactId string) string {

0 commit comments

Comments
 (0)