-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpom.go
84 lines (72 loc) · 1.9 KB
/
pom.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
package main
import (
"encoding/xml"
"fmt"
"os"
"strings"
)
type PomDependency struct {
GroupId string `xml:"groupId"`
ArtifactId string `xml:"artifactId"`
Version string `xml:"version"`
Scope string `xml:"scope"`
}
func (p PomDependency) String() string {
return fmt.Sprintf("%s:%s:%s", p.GroupId, p.ArtifactId, p.Version)
}
type PomDependencies struct {
XMLName xml.Name `xml:"dependencies"`
Dependency []PomDependency `xml:"dependency"`
}
type Pom struct {
XMLName xml.Name `xml:"project"`
GroupId string `xml:"groupId"`
ArtifactId string `xml:"artifactId"`
Version string `xml:"version"`
Dependencies PomDependencies `xml:"dependencies"`
}
func (p Pom) String() string {
return fmt.Sprintf("%s:%s:%s -> %s", p.GroupId, p.ArtifactId, p.Version, p.Dependencies)
}
func (p PomDependencies) byScope(scope string) []PomDependency {
found := make([]PomDependency, 0)
for _, d := range p.Dependency {
if d.Scope == scope {
found = append(found, d)
}
}
return found
}
func (p PomDependencies) ToArtifacts(scope string) []Artifact {
deps := p.byScope(scope)
list := make([]Artifact, len(deps), len(deps))
for i, dep := range deps {
list[i] = toArtifact(dep)
}
return list
}
func (p PomDependencies) ToArtifactsAll() []Artifact {
deps := p.Dependency
list := make([]Artifact, len(deps), len(deps))
for i, dep := range deps {
list[i] = toArtifact(dep)
}
return list
}
func toArtifact(dep PomDependency) Artifact {
return Artifact{dep.GroupId, dep.ArtifactId, dep.Version, "", "jar"}
}
func ParsePomFromString(xmlStr string) (error, Pom) {
v := Pom{}
err := xml.NewDecoder(strings.NewReader(xmlStr)).Decode(&v)
return err, v
}
func ParsePomFromFile(file string) (error, Pom) {
v := Pom{}
xmlFile, err := os.Open(file)
if err == nil {
defer xmlFile.Close()
err = xml.NewDecoder(xmlFile).Decode(&v)
}
return err, v
}