forked from dizzyd/mcdex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maven.go
118 lines (100 loc) · 2.94 KB
/
maven.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
111
112
113
114
115
116
117
118
package main
import (
"encoding/xml"
"fmt"
"path"
"strings"
)
type MavenModule struct {
groupId string
artifactId string
version string
extension string
suffix string
}
type MavenMetadata struct {
XmlName xml.Name `xml:"metadata"`
GroupId string `xml:"groupId"`
ArtifactId string `xml:"artifactId"`
VersionInfo MavenMetadataVersionInfo `xml:"versioning"`
}
type MavenMetadataVersionInfo struct {
XmlName xml.Name `xml:"versioning"`
Latest string `xml:"latest"`
Release string `xml:"release"`
Versions []string `xml:"versions>version"`
}
func NewMavenModule(module string) (MavenModule, error) {
// First, break up the string into maven components: group, artifact and version
parts := strings.SplitN(module, ":", 3)
if len(parts) < 2 {
return MavenModule{}, fmt.Errorf("maven module requires at least group and artifact IDs")
}
groupID := parts[0]
artifactID := parts[1]
var vsn string
if len(parts) > 2 {
vsn = parts[2]
}
ext := "jar"
suffix := ""
// The version string MAY contain an @ that indicates an alternate file extension (i.e. not .jar)
if strings.Contains(vsn, "@") {
vsnParts := strings.SplitN(vsn, "@", 2)
vsn = vsnParts[0]
ext = vsnParts[1]
}
// The version string MAY also have a suffix, delimited by :
if strings.Contains(vsn, ":") {
vsnParts := strings.SplitN(vsn, ":", 2)
vsn = vsnParts[0]
suffix = vsnParts[1]
}
return MavenModule{
groupId: groupID,
artifactId: artifactID,
version: vsn,
extension: ext,
suffix: suffix,
}, nil
}
func (m MavenModule) String() string {
base := fmt.Sprintf("%s:%s:%s", m.groupId, m.artifactId, m.version)
if m.suffix != "" {
base = base + ":" + m.suffix
}
if m.extension != "" {
base = base + "@" + m.extension
}
return base
}
func (m MavenModule) toRepositoryPath(repo string) (string, error) {
if m.version == "" {
return "", fmt.Errorf("version not available; repository path incomplete for %s", m)
}
var filename string
if m.suffix != "" {
filename = fmt.Sprintf("%s-%s-%s.%s", m.artifactId, m.version, m.suffix, m.extension)
} else {
filename = fmt.Sprintf("%s-%s.%s", m.artifactId, m.version, m.extension)
}
groupPath := path.Join(strings.Split(m.groupId, ".")...)
return urlJoin(repo, groupPath, m.artifactId, m.version, filename)
}
func (m MavenModule) loadMetadata(repo string) (MavenMetadata, error) {
groupPath := path.Join(strings.Split(m.groupId, ".")...)
metadataUrl, err := urlJoin(repo, groupPath, m.artifactId, "maven-metadata.xml")
if err != nil {
return MavenMetadata{}, err
}
metadataXml, err := readStringFromUrl(metadataUrl)
if err != nil {
return MavenMetadata{}, fmt.Errorf("unable to retrieve %s: %+v", metadataUrl, err)
}
var metadata MavenMetadata
err = xml.Unmarshal([]byte(metadataXml), &metadata)
if err != nil {
return MavenMetadata{}, fmt.Errorf("unable to parse %s: %+v", metadataUrl, err)
}
return metadata, nil
}