-
Notifications
You must be signed in to change notification settings - Fork 9
/
updatelicense.go
209 lines (172 loc) · 5.18 KB
/
updatelicense.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package debug
import (
"bytes"
"context"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/spf13/cobra"
"golang.org/x/exp/slices"
"namespacelabs.dev/foundation/internal/cli/fncobra"
"namespacelabs.dev/foundation/internal/fnfs"
)
var checkExtensions = []string{".go", ".js", ".ts", ".jsx", ".tsx", ".proto", ".hcl", ".yaml", ".yml", ".css"}
var ignoreSuffixes = []string{
".pb.go",
".fn.go",
".pb.gw.go",
".fn.ts",
".fn.js",
"_pb.ts",
"stacktrace/stacktrace.go",
"stacktrace/serializer/serializer.go"}
func newUpdateLicenseCmd() *cobra.Command {
var check bool
cmd := &cobra.Command{
Use: "update-license",
RunE: fncobra.RunE(func(ctx context.Context, args []string) error {
fsys := os.DirFS(".")
paths := args
if len(paths) == 0 {
if err := fnfs.WalkDir(fsys, ".", func(path string, d fs.DirEntry) error {
if d.Name() == "." {
return nil
}
if len(d.Name()) > 0 && d.Name()[0] == '.' {
if d.IsDir() {
return fs.SkipDir
}
return nil
}
if d.IsDir() {
// "dist" contains compiled Typescript code, for example, std/nodejs/runtime.
if d.Name() == "node_modules" || d.Name() == "dist" {
return fs.SkipDir
}
return nil
}
for _, ignore := range ignoreSuffixes {
if strings.HasSuffix(path, ignore) {
return nil
}
}
if slices.Contains(checkExtensions, filepath.Ext(path)) {
paths = append(paths, path)
}
return nil
}); err != nil {
return err
}
}
var headers []struct {
target string
prefix []byte
}
for _, lic := range []string{apacheLicense, earlyAccessLicense, shortApacheLicense} {
for _, prependComment := range []func(string) []byte{cppComment, cComment, shellComment} {
headers = append(headers, struct {
target string
prefix []byte
}{lic, prependComment(lic)})
}
}
const target = shortApacheLicense
var wouldWrite []string
file:
for _, path := range paths {
contents, err := fs.ReadFile(fsys, path)
if err != nil {
return err
}
for _, h := range headers {
if bytes.HasPrefix(contents, h.prefix) {
if h.target == target {
continue file
}
contents = bytes.TrimPrefix(contents, h.prefix)
break
}
}
for len(contents) > 0 && contents[0] == '\n' {
contents = contents[1:]
}
makeHeader := extensions[filepath.Ext(path)]
if makeHeader != nil {
if check {
wouldWrite = append(wouldWrite, path)
} else {
gen := makeHeader(target)
gen = append(gen, '\n')
if err := os.WriteFile(path, append(gen, contents...), 0644); err != nil {
return err
}
}
}
}
if len(wouldWrite) > 0 {
return fmt.Errorf("the following files need their license header updated:\n%s", strings.Join(wouldWrite, "\n"))
}
return nil
}),
}
cmd.Flags().BoolVar(&check, "check", check, "If set to true, check that all files have the appropriate header.")
return cmd
}
var extensions = map[string]func(string) []byte{
".go": cppComment,
".js": cppComment,
".ts": cppComment,
".tsx": cppComment,
".jsx": cppComment,
".proto": cppComment,
}
const apacheLicense = `Copyright 2022 Namespace Labs Inc
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.`
const earlyAccessLicense = `Copyright 2022 Namespace Labs Inc; All rights reserved.
Licensed under the EARLY ACCESS SOFTWARE LICENSE AGREEMENT
available at http://github.com/namespacelabs/foundation`
const shortApacheLicense = `Copyright 2022 Namespace Labs Inc; All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.`
func cppComment(license string) []byte {
lines := strings.Split(strings.TrimSpace(license), "\n")
for k, line := range lines {
lines[k] = prepend("//", line)
}
return []byte(strings.Join(lines, "\n") + "\n")
}
func shellComment(license string) []byte {
lines := strings.Split(strings.TrimSpace(license), "\n")
for k, line := range lines {
lines[k] = prepend("#", line)
}
return []byte(strings.Join(lines, "\n") + "\n")
}
func cComment(license string) []byte {
lines := strings.Split(strings.TrimSpace(license), "\n")
for k, line := range lines {
lines[k] = prepend(" *", line)
}
allLines := append([]string{"/**"}, lines...)
allLines = append(allLines, " */")
return []byte(strings.Join(allLines, "\n") + "\n")
}
func prepend(prefix, line string) string {
if line == "" {
return prefix
}
return prefix + " " + line
}