-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
173 lines (154 loc) · 3.9 KB
/
update.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"encoding/xml"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
version "github.com/hashicorp/go-version"
)
func main() {
gf := gradleFiles()
gr := NewGoogleRepo()
for _, filename := range gf {
gr.updateGradleFile(filename)
}
}
type Repo map[string]map[string][]string
func (g Repo) updateGradleFile(filename string) {
data, err := ioutil.ReadFile(filename)
if err != nil {
panic(err)
}
content, updated := g.updateGradleContent(string(data))
if updated {
ioutil.WriteFile(filename, []byte(content), 0655)
}
}
func (g Repo) updateGradleContent(content string) (string, bool) {
re := regexp.MustCompile("(('|\")(.*):(.*):(.*)('|\"))(\\s?//\\s*(.*))?")
matches := re.FindAllStringSubmatch(content, -1)
updated := false
for _, m := range matches {
pkg := m[3]
module := m[4]
version := m[5]
semver := m[8]
current := fmt.Sprintf("%s:%s:%s", pkg, module, version)
if v := g.fetchVersions(pkg, module); v != nil {
lv := latestVersion(v, version, semver)
newest := fmt.Sprintf("%s:%s:%s", pkg, module, lv)
if newest != current {
updated = true
fmt.Println(current, "->", lv)
content = strings.Replace(content, current, newest, -1)
}
}
}
return content, updated
}
func (g Repo) fetchVersions(pkg string, module string) []string {
group, ok := g[pkg]
if ok && group == nil {
g[pkg] = googleMaven(pkg)
}
if !ok || g[pkg][module] == nil {
m := make(map[string][]string)
m[module] = jcenter(pkg, module)
g[pkg] = m
}
return g[pkg][module]
}
func jcenter(pkg string, module string) []string {
n := Metadata{}
u := fmt.Sprintf("https://jcenter.bintray.com/%s/%s/maven-metadata.xml", strings.Replace(pkg, ".", "/", -1), module)
parseXml(u, &n)
return n.Versions
}
// Metadata generated with https://github.com/wicast/xj2s
type Metadata struct {
Latest string `xml:"versioning>latest"`
Release string `xml:"versioning>release"`
Versions []string `xml:"versioning>versions>version"`
LastUpdated string `xml:"versioning>lastUpdated"`
GroupID string `xml:"groupId"`
ArtifactID string `xml:"artifactId"`
Version string `xml:"version"`
}
func latestVersion(versions []string, currentVersion string, constraints string) string {
if constraints != "" {
valid, err := version.NewConstraint(constraints)
if err != nil {
log.Printf("constraint '%v' is invalid: %v", constraints, err)
return currentVersion
}
for i := len(versions) - 1; i >= 0; i-- {
v, err := version.NewVersion(versions[i])
if err != nil {
continue
}
if valid.Check(v) {
return versions[i]
}
}
return currentVersion
}
prerelease := isPrereleaseVersion(currentVersion)
for i := len(versions) - 1; i >= 0; i-- {
if !prerelease {
if isPrereleaseVersion(versions[i]) {
continue
}
}
return versions[i]
}
return ""
}
func isPrereleaseVersion(name string) bool {
tags := []string{"alpha", "beta", "rc", "build"}
for _, tag := range tags {
if strings.Contains(name, tag) {
return true
}
}
return false
}
func parseXml(url string, v interface{}) error {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
req.Header.Add("accept", "*/*")
b, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer b.Body.Close()
decoder := xml.NewDecoder(b.Body)
return decoder.Decode(v)
}
func gradleFiles() []string {
files := make([]string, 0)
dir, _ := os.Getwd()
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Printf("prevent panic by handling failure accessing a path %q: %v\n", dir, err)
return err
}
if info.IsDir() && (info.Name() == "build" || info.Name() == "src") {
return filepath.SkipDir
}
if info.Name() == "build.gradle" {
files = append(files, path)
}
return nil
})
if err != nil {
fmt.Printf("error walking the path %q: %v\n", dir, err)
}
return files
}