/
main.go
134 lines (106 loc) · 2.7 KB
/
main.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
// Package main ...
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"path/filepath"
"sort"
"strconv"
"strings"
"github.com/go-rod/rod/lib/utils"
"github.com/ysmood/gson"
)
const mirror = "https://registry.npmmirror.com/-/binary/chromium-browser-snapshots/"
func main() {
list := getList(mirror)
revLists := [][]int{}
for _, os := range list {
// skip win32
if os == "Win" {
continue
}
revList := []int{}
for _, s := range getList(mirror + os + "/") {
rev, err := strconv.ParseInt(s, 10, 32)
if err != nil {
log.Fatal(err)
}
revList = append(revList, int(rev))
}
sort.Ints(revList)
revLists = append(revLists, revList)
}
rev := largestCommonRevision(revLists)
if rev < 969819 {
utils.E(fmt.Errorf("cannot match version of the latest chromium from %s", mirror))
}
playwright := getFromPlaywright()
out := utils.S(`// generated by "lib/launcher/revision"
package launcher
// RevisionDefault for chromium
const RevisionDefault = {{.default}}
// RevisionPlaywright for arm linux
const RevisionPlaywright = {{.playwright}}
`,
"default", rev,
"playwright", playwright,
)
utils.E(utils.OutputFile(filepath.FromSlash("lib/launcher/revision.go"), out))
}
func getList(path string) []string {
res, err := http.Get(path)
utils.E(err)
defer func() { _ = res.Body.Close() }()
var data interface{}
err = json.NewDecoder(res.Body).Decode(&data)
utils.E(err)
list := data.([]interface{})
names := []string{}
for _, it := range list {
name := it.(map[string]interface{})["name"].(string)
names = append(names, strings.TrimRight(name, "/"))
}
return names
}
func largestCommonRevision(revLists [][]int) int {
sort.Slice(revLists, func(i, j int) bool {
return len(revLists[i]) < len(revLists[j])
})
shortest := revLists[0]
for i := len(shortest) - 1; i >= 0; i-- {
r := shortest[i]
isCommon := true
for i := 1; i < len(revLists); i++ {
if !has(revLists[i], r) {
isCommon = false
break
}
}
if isCommon {
return r
}
}
return 0
}
func has(list []int, i int) bool {
index := sort.SearchInts(list, i)
return index < len(list) && list[index] == i
}
func getFromPlaywright() int {
pv := strings.TrimSpace(utils.ExecLine(false, "npm --no-update-notifier -s show playwright version"))
out := fetch(fmt.Sprintf("https://raw.githubusercontent.com/microsoft/playwright/v%s/packages/playwright-core/browsers.json", pv))
rev, err := strconv.ParseInt(gson.NewFrom(out).Get("browsers.0.revision").Str(), 10, 32)
utils.E(err)
return int(rev)
}
func fetch(u string) string {
res, err := http.Get(u)
utils.E(err)
defer func() { _ = res.Body.Close() }()
b, err := ioutil.ReadAll(res.Body)
utils.E(err)
return string(b)
}