-
Notifications
You must be signed in to change notification settings - Fork 582
/
model.go
110 lines (95 loc) · 2.9 KB
/
model.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package maven
import (
"encoding/xml"
"fmt"
"github.com/SAP/jenkins-library/pkg/piperutils"
"path/filepath"
)
// Project describes the Maven object model.
type Project struct {
XMLName xml.Name `xml:"project"`
Parent Parent `xml:"parent"`
GroupID string `xml:"groupId"`
ArtifactID string `xml:"artifactId"`
Version string `xml:"version"`
Packaging string `xml:"packaging"`
Name string `xml:"name"`
Dependencies []Dependency `xml:"dependencies>dependency"`
Modules []string `xml:"modules>module"`
}
// Parent describes the coordinates a module's parent POM.
type Parent struct {
XMLName xml.Name `xml:"parent"`
GroupID string `xml:"groupId"`
ArtifactID string `xml:"artifactId"`
Version string `xml:"version"`
}
// Dependency describes a dependency of the module.
type Dependency struct {
XMLName xml.Name `xml:"dependency"`
GroupID string `xml:"groupId"`
ArtifactID string `xml:"artifactId"`
Version string `xml:"version"`
Classifier string `xml:"classifier"`
Type string `xml:"type"`
Scope string `xml:"scope"`
Exclusions []Exclusion `xml:"exclusions>exclusion"`
}
// Exclusion describes an exclusion within a dependency.
type Exclusion struct {
XMLName xml.Name `xml:"exclusion"`
GroupID string `xml:"groupId"`
ArtifactID string `xml:"artifactId"`
}
// ParsePOM parses the provided XML raw data into a Project.
func ParsePOM(xmlData []byte) (*Project, error) {
project := Project{}
err := xml.Unmarshal(xmlData, &project)
if err != nil {
return nil, fmt.Errorf("failed to parse POM data: %w", err)
}
return &project, nil
}
// ModuleInfo describes a location and Project of a maven module.
type ModuleInfo struct {
PomXMLPath string
Project *Project
}
type visitUtils interface {
FileExists(path string) (bool, error)
FileRead(path string) ([]byte, error)
}
// VisitAllMavenModules ...
func VisitAllMavenModules(path string, utils visitUtils, excludes []string, callback func(info ModuleInfo) error) error {
pomXMLPath := filepath.Join(path, "pom.xml")
if piperutils.ContainsString(excludes, pomXMLPath) {
return nil
}
exists, _ := utils.FileExists(pomXMLPath)
if !exists {
return nil
}
pomXMLContents, err := utils.FileRead(pomXMLPath)
if err != nil {
return fmt.Errorf("failed to read file contents of '%s': %w", pomXMLPath, err)
}
project, err := ParsePOM(pomXMLContents)
if err != nil {
return fmt.Errorf("failed to parse file contents of '%s': %w", pomXMLPath, err)
}
err = callback(ModuleInfo{PomXMLPath: pomXMLPath, Project: project})
if err != nil {
return err
}
if len(project.Modules) == 0 {
return nil
}
for _, module := range project.Modules {
subPomPath := filepath.Join(path, module)
err = VisitAllMavenModules(subPomPath, utils, excludes, callback)
if err != nil {
return err
}
}
return nil
}