-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
releases.go
143 lines (122 loc) · 3.47 KB
/
releases.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
/*
Copyright 2023 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
// The changelog directory is composed of a README that lists
// and links to all major releases of Vitess. It has one
// sub-directory for each major version. Each sub-directory is
// composed of another README that also lists and links all the
// patch releases of this major release. Those sub-directories
// are composed of one directory per patch release. Finally,
// the patch release directory contains the old files markdown:
// summary, release_notes, changelog.
//
// This tool is solely responsible for generating the READMEs
// and making sure they are up-to-date with the list of major
// and patch releases we have.
import (
"log"
"os"
"path"
"sort"
"strings"
"text/template"
)
const (
rootDir = "./changelog/"
rootFileTmpl = `## Releases
{{- range $r := .SubDirs }}
* [{{ $r.Name }}]({{ $r.Name }})
{{- end -}}
`
majorVersionTmpl = `## v{{ .Name }}
{{- if .Team }}
The dedicated team for this release can be found [here]({{.Team}}).{{ end }}
{{- range $r := .SubDirs }}
* **[{{ $r.Name }}]({{ $r.Name }})**
{{ if $r.Changelog }} * [Changelog]({{ $r.Name }}/{{ $r.Changelog }})
{{ end -}}
{{ if $r.ReleaseNotes }} * [Release Notes]({{ $r.Name }}/{{ $r.ReleaseNotes }})
{{ end -}}
{{- end -}}
`
)
type dir struct {
Name string
Path string
Changelog string
ReleaseNotes string
Team string
SubDirs []dir
}
func main() {
rootDir, err := getDirs(dir{Path: rootDir})
if err != nil {
log.Fatal(err)
}
err = execReadMeTemplateWithDir(rootDir, rootFileTmpl)
if err != nil {
log.Fatal(err)
}
for _, subDir := range rootDir.SubDirs {
err := execReadMeTemplateWithDir(subDir, majorVersionTmpl)
if err != nil {
log.Fatal(err)
}
}
}
func execReadMeTemplateWithDir(d dir, tmpl string) error {
rootRM, err := os.OpenFile(path.Join(d.Path, "README.md"), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0640)
if err != nil {
return err
}
t := template.Must(template.New("root_readme").Parse(tmpl))
err = t.ExecuteTemplate(rootRM, "root_readme", d)
if err != nil {
return err
}
return nil
}
func getDirs(curDir dir) (dir, error) {
entries, err := os.ReadDir(curDir.Path)
if err != nil {
return dir{}, err
}
for _, entry := range entries {
if entry.IsDir() {
subDir, err := getDirs(dir{
Name: entry.Name(),
Path: path.Join(curDir.Path, entry.Name()),
})
if err != nil {
return dir{}, err
}
curDir.SubDirs = append(curDir.SubDirs, subDir)
continue
}
switch {
case strings.Contains(entry.Name(), "changelog.md"):
curDir.Changelog = entry.Name()
case strings.Contains(entry.Name(), "release_notes.md"):
curDir.ReleaseNotes = entry.Name()
case strings.Contains(entry.Name(), "team.md"):
curDir.Team = entry.Name()
}
}
sort.Slice(curDir.SubDirs, func(i, j int) bool {
if len(curDir.SubDirs[i].Name) < len(curDir.SubDirs[j].Name) {
return false
}
return curDir.SubDirs[i].Name > curDir.SubDirs[j].Name
})
return curDir, nil
}