forked from goretk/gore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen.go
367 lines (326 loc) · 9.07 KB
/
gen.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// This file is part of GoRE.
//
// Copyright (C) 2019-2021 GoRE Authors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//go:build ignore
// +build ignore
// This program generates stdpkgs_gen.go. It can be invoked by running
// go generate
package main
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"text/template"
"time"
)
var packageTemplate = template.Must(template.New("").Parse(`// This file is part of GoRE.
//
// Copyright (C) 2019-2021 GoRE Authors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// Code generated by go generate; DO NOT EDIT.
// This file was generated at
// {{ .Timestamp }}
package gore
var stdPkgs = map[string]struct{}{
{{- range .StdPkg }}
{{ printf "\"%s\": {}" . }},
{{- end }}
}
`))
var goversionTemplate = template.Must(template.New("").Parse(`// This file is part of GoRE.
//
// Copyright (C) 2019-2021 GoRE Authors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// Code generated by go generate; DO NOT EDIT.
// This file was generated at
// {{ .Timestamp }}
package gore
var goversions = map[string]*GoVersion{
{{- range .GoVersions }}
{{ printf "\"%s\": {Name: \"%s\", SHA: \"%s\", Timestamp: \"%s\"}" .Name .Name .Sha .Date }},
{{- end }}
}
`))
type ghResp struct {
Sha string `json:"sha"`
Url string `json:"url"`
Trees []ghTree `json:"tree"`
Truncated bool `json:"truncated"`
}
type ghTree struct {
Path string `json:"path"`
Mode string `json:"mode"`
Gittype string `json:"type"`
Sha string `json:"sha"`
Size int `json:"size"`
Url string `json:"url"`
}
const (
requestURLFormatStr = "https://api.github.com/repos/golang/go/git/trees/%s?recursive=0"
commitRequestURLFormatStr = "https://api.github.com/repos/golang/go/git/commits/%s"
outputFile = "stdpkg_gen.go"
goversionOutputFile = "goversion_gen.go"
)
var (
tagsRequestURL = "https://api.github.com/repos/golang/go/tags"
)
var (
excludedPaths = []string{"src/cmd"}
)
type tagResp struct {
Name string
Commit *commitShort
}
type commitShort struct {
Sha string
URL string
}
type commitLong struct {
Sha string
Committer committer
}
type committer struct {
Name string
Date string
}
type goversion struct {
Name string
Sha string
Date string
}
func processGoVersions() {
client := http.DefaultClient
tags := make([]*tagResp, 0)
// Fetch all tags
var requestURL *string
requestURL = &tagsRequestURL
for *requestURL != "" {
fmt.Println("Fetching latests tags")
resp, err := client.Get(tagsRequestURL)
if err != nil {
fmt.Println("Error when fetching tags:", err.Error())
resp.Body.Close()
continue
}
next := getNextPageURL(resp)
*requestURL = next
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
fmt.Println("Error when ready response body:", err)
continue
}
var newTags []*tagResp
err = json.Unmarshal(body, &newTags)
if err != nil {
fmt.Println("Error when parsing the json:", err)
continue
}
tags = append(tags, newTags...)
}
// Get mode commit info for new tags
f, err := os.OpenFile(filepath.Join("resources", "goversions.csv"), os.O_CREATE|os.O_RDWR, 0664)
if err != nil {
fmt.Println("Error when opening goversions.csv:", err)
return
}
defer f.Close()
knownVersions, err := getStoredGoversions(f)
if err != nil {
fmt.Println("Error when getting stored go versions:", err)
return
}
_, err = fmt.Fprintln(f, "version,sha,date")
if err != nil {
fmt.Println("Error when writing csv header:", err)
return
}
for _, tag := range tags {
if strings.HasPrefix(tag.Name, "weekly") || strings.HasPrefix(tag.Name, "release") {
continue
}
if v, known := knownVersions[tag.Name]; known {
fmt.Fprintf(f, "%s,%s,%s\n", v.Name, v.Sha, v.Date)
continue
}
resp, err := client.Get(fmt.Sprintf(commitRequestURLFormatStr, tag.Commit.Sha))
if err != nil {
fmt.Println("Error when fetching commit info:", err)
resp.Body.Close()
continue
}
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
var commit commitLong
err = json.Unmarshal(body, &commit)
if err != nil {
fmt.Println("Error when parsing commit json:", err)
continue
}
fmt.Fprintf(f, "%s,%s,%s\n", tag.Name, commit.Sha, commit.Committer.Date)
fmt.Println("New tag found:", tag.Name)
knownVersions[tag.Name] = &goversion{Name: tag.Name, Sha: commit.Sha, Date: commit.Committer.Date}
}
// Generate the code.
f, err = os.Create(goversionOutputFile)
if err != nil {
fmt.Println("Failed to open the file:", err)
return
}
defer f.Close()
goversionTemplate.Execute(f, struct {
Timestamp time.Time
GoVersions map[string]*goversion
}{
Timestamp: time.Now().UTC(),
GoVersions: knownVersions,
})
}
func getStoredGoversions(f *os.File) (map[string]*goversion, error) {
vers := make(map[string]*goversion)
r := bufio.NewScanner(f)
// Read header
if !r.Scan() {
return nil, errors.New("empty file")
}
r.Text()
for r.Scan() {
row := r.Text()
if row == "" {
continue
}
data := strings.Split(row, ",")
if data[0] == "" {
// No version
continue
}
version := strings.TrimSpace(data[0])
sha := strings.TrimSpace(data[1])
date := strings.TrimSpace(data[2])
vers[version] = &goversion{Name: version, Sha: sha, Date: date}
}
_, err := f.Seek(0, 0)
return vers, err
}
func getNextPageURL(r *http.Response) string {
h := r.Header.Get("Link")
if h == "" {
return ""
}
// Either we this type:
// <https://api.github.com/repositories/23096959/tags?page=2>; rel="next", <https://api.github.com/repositories/23096959/tags?page=8>; rel="last"
// or this type:
// <https://api.github.com/repositories/23096959/tags?page=7>; rel="prev", <https://api.github.com/repositories/23096959/tags?page=1>; rel="first"
data := strings.Split(h, ",")
for _, l := range data {
ll := strings.Split(l, ";")
if len(ll) != 2 {
continue
}
if strings.TrimSpace(ll[1]) != "rel=\"next\"" {
continue
}
return strings.TrimLeft(strings.TrimRight(strings.TrimSpace(ll[0]), ">"), "<")
}
return ""
}
func main() {
processGoVersions()
client := http.DefaultClient
resp, err := client.Get(fmt.Sprintf(requestURLFormatStr, "master"))
if err != nil {
fmt.Println("Error when fetching go src data:", err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error when reading response body:", err)
return
}
var master ghResp
err = json.Unmarshal(body, &master)
if err != nil {
fmt.Println("Error when decoding the response body:", err)
return
}
var stdPkgs []string
for _, tree := range master.Trees {
if tree.Gittype != "tree" {
continue
}
if !strings.HasPrefix(tree.Path, "src") || skipPath(tree.Path) {
continue
}
// Skip src folder.
if tree.Path == "src" {
continue
}
// Strip "src/" and add to list.
stdPkgs = append(stdPkgs, strings.TrimPrefix(tree.Path, "src/"))
}
f, err := os.Create(outputFile)
if err != nil {
fmt.Println("Failed to open the file:", err)
return
}
defer f.Close()
packageTemplate.Execute(f, struct {
Timestamp time.Time
StdPkg []string
}{
Timestamp: time.Now().UTC(),
StdPkg: stdPkgs,
})
}
func skipPath(path string) bool {
for _, exclude := range excludedPaths {
if strings.HasPrefix(path, exclude) {
return true
}
}
return false
}