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
|
package main
import (
"bytes"
_ "embed"
"encoding/json"
"flag"
"fmt"
"html/template"
"maps"
"os"
"os/exec"
"regexp"
"slices"
"strconv"
"strings"
"github.com/BurntSushi/toml"
log "github.com/sirupsen/logrus"
)
const caniuseRepoURL = "https://github.com/jplatte/caniuse.rs"
const rustRepoURL = "https://github.com/rust-lang/rust"
var codeRegex = regexp.MustCompile("`(.+?)`")
//go:embed template.html.tmpl
var templateText string
//go:embed script.js
var script []byte
//go:embed style.css
var style []byte
//go:embed find-lib-feats.sh
var findLibFeatsShellCommand string
func main() {
caniuseRepo := "caniuse.rs"
rustRepo := "rust"
outDir := "out"
skipDownload := flag.Bool("skip-download", false,
fmt.Sprintf("skip cloning/updating the %s and %s repos", caniuseRepo, rustRepo))
flag.Parse()
if !*skipDownload {
if _, err := os.Stat(caniuseRepo); os.IsNotExist(err) {
runCommand("git", "clone", caniuseRepoURL, caniuseRepo)
} else {
runCommand("git", "-C", caniuseRepo, "pull")
}
if _, err := os.Stat(rustRepo); os.IsNotExist(err) {
runCommand("git", "clone", rustRepoURL, rustRepo, "--depth", "1")
} else {
runCommand("git", "-C", rustRepo, "fetch", "--depth", "1")
runCommand("git", "-C", rustRepo, "reset", "--hard", "origin/master")
}
}
cmd := exec.Command("sh", "-c", findLibFeatsShellCommand, "find-lib-feats.sh", rustRepo)
var stderr bytes.Buffer
cmd.Stderr = &stderr
libFeaturesText, err := cmd.Output()
if err != nil {
log.WithField("stderr", stderr.String()).Fatalf("failed to run shell command to find library features: %s", err)
}
libFeatureFlags := strings.Split(string(libFeaturesText), "\n")
versionInfos := make(map[string]VersionInfo)
versionsPath := fmt.Sprintf(caniuseRepo + "/data/versions.toml")
_, err = toml.DecodeFile(versionsPath, &versionInfos)
if err != nil {
log.Fatalf("error parsing %s: %s", versionsPath, err)
}
if len(versionInfos) == 0 {
log.Fatal("found no versions in versions.toml")
}
versions := make([]Version, 0)
sortedVersions := slices.SortedFunc(maps.Keys(versionInfos), compareVersion)
slices.Reverse(sortedVersions)
for _, name := range append([]string{"unstable"}, sortedVersions...) {
features := getFeatures(caniuseRepo, name)
if len(features) == 0 {
log.Infof("no features found for %s", name)
}
libFeatures := make([]Feature, 0)
nonLibFeatures := make([]Feature, 0)
for _, feature := range features {
if slices.Contains(libFeatureFlags, feature.Flag) || strings.Contains(feature.Title, " impl for ") {
libFeatures = append(libFeatures, feature)
} else {
nonLibFeatures = append(nonLibFeatures, feature)
}
}
version := Version{
Name: name,
LibFeatures: nilAsEmptyArray(slices.SortedFunc(slices.Values(libFeatures), compareFeature)),
NonLibFeatures: nilAsEmptyArray(slices.SortedFunc(slices.Values(nonLibFeatures), compareFeature)),
}
versionInfo := versionInfos[name]
if versionInfo.BlogPostPath != nil {
url := fmt.Sprintf("https://blog.rust-lang.org/%s", *versionInfo.BlogPostPath)
version.BlogPostURL = &url
}
versions = append(versions, version)
}
tmpl := template.New("template.html.tmpl").Funcs(template.FuncMap{
"formatBackticks": func(s string) template.HTML {
return template.HTML(codeRegex.ReplaceAllStringFunc(s, func(code string) string {
return fmt.Sprintf(
"<code>%s</code>",
template.HTMLEscapeString(codeRegex.FindStringSubmatch(code)[1]),
)
}))
},
})
tmpl, err = tmpl.Parse(templateText)
if err != nil {
log.Fatalf("error parsing template: %s", err)
}
err = os.MkdirAll(outDir, 0o755)
if err != nil {
log.Fatalf("error creating directory: %s", err)
}
outputFile, err := os.Create(outDir + "/index.html")
if err != nil {
log.Fatalf("error creating index.html: %s", err)
}
defer outputFile.Close()
err = tmpl.Execute(outputFile,
map[string]any{
"Versions": versions,
"CaniuseRepoURL": caniuseRepoURL,
},
)
if err != nil {
log.Fatalf("error executing template: %s", err)
}
outputFile, err = os.Create(outDir + "/data.json")
if err != nil {
log.Fatalf("error creating data.json: %s", err)
}
defer outputFile.Close()
encoder := json.NewEncoder(outputFile)
err = encoder.Encode(versions)
if err != nil {
log.Fatalf("error encoding JSON: %s", err)
}
err = os.WriteFile(outDir+"/script.js", script, 0o644)
if err != nil {
log.Fatalf("error creating script.js: %s", err)
}
err = os.WriteFile(outDir+"/style.css", style, 0o644)
if err != nil {
log.Fatalf("error creating style.css: %s", err)
}
}
type Version struct {
Name string `json:"name"`
BlogPostURL *string `json:"-"`
LibFeatures []Feature `json:"lib"`
NonLibFeatures []Feature `json:"non_lib"`
}
// Version info from "caniuse.rs/data/versions.toml".
type VersionInfo struct {
BlogPostPath *string `toml:"blog_post_path"`
}
// Data from a .toml file in "caniuse.rs/data/{version}/".
type Feature struct {
Title string `toml:"title" json:"title"`
Flag string `toml:"flag" json:"flag"`
TrackingIssueId *uint `toml:"tracking_issue_id" json:"-"`
ImplPrId *uint `toml:"impl_pr_id" json:"-"`
StabilizationPrId *uint `toml:"stabilization_pr_id" json:"-"`
Aliases []string `toml:"aliases" json:"aliases"`
Items []string `toml:"items" json:"items"`
URL string `json:"url"`
}
func getFeatures(caniuseRepo string, version string) map[string]Feature {
dirPath := caniuseRepo + "/data/" + version
entries, err := os.ReadDir(dirPath)
if err != nil {
if os.IsNotExist(err) {
return nil
}
log.Fatal(err)
}
features := make(map[string]Feature)
for _, entry := range entries {
var feature Feature
_, err := toml.DecodeFile(dirPath+"/"+entry.Name(), &feature)
if err != nil {
log.Fatalf("Error parsing %s: %s\n", dirPath+entry.Name(), err)
}
if feature.TrackingIssueId != nil {
feature.URL = fmt.Sprintf("https://github.com/rust-lang/rust/issues/%d", *feature.TrackingIssueId)
} else if feature.ImplPrId != nil {
feature.URL = fmt.Sprintf("https://github.com/rust-lang/rust/pull/%d", *feature.ImplPrId)
} else if feature.StabilizationPrId != nil {
feature.URL = fmt.Sprintf("https://github.com/rust-lang/rust/pull/%d", *feature.StabilizationPrId)
}
feature.Title = strings.TrimPrefix(feature.Title, "the ")
feature.Title = strings.Replace(feature.Title, "implementation", "impl", 1)
var key string
if feature.Flag == "" {
key = entry.Name()
} else {
key = feature.Flag
}
_, exists := features[key]
if exists {
// caniuse.rs sometimes intentionally has several .toml files
// for the same feature flag (with different titles).
// In this case we only want to display the feature once.
feature.Title = feature.Flag
}
features[key] = feature
}
return features
}
func compareFeature(a, b Feature) int {
aTitle := strings.ToLower(strings.TrimLeft(a.Title, "`"))
bTitle := strings.ToLower(strings.TrimLeft(b.Title, "`"))
if aTitle > bTitle {
return 1
} else if aTitle < bTitle {
return -1
}
return 0
}
func compareVersion(a, b string) int {
aParts := strings.Split(a, ".")
bParts := strings.Split(b, ".")
aMajor, _ := strconv.Atoi(aParts[0])
bMajor, _ := strconv.Atoi(bParts[0])
if aMajor != bMajor {
return aMajor - bMajor
}
aMinor, _ := strconv.Atoi(aParts[1])
bMinor, _ := strconv.Atoi(bParts[1])
return aMinor - bMinor
}
func runCommand(name string, args ...string) {
cmd := exec.Command(name, args...)
var stderr bytes.Buffer
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
log.WithField("stderr", stderr.String()).Fatalf("command '%s %v' failed: %v", name, args, err)
}
}
// workaround for https://github.com/golang/go/issues/37711
func nilAsEmptyArray[T any](slice []T) []T {
return append([]T{}, slice...)
}
|