Skip to content

Commit 52c2ef7

Browse files
silverwind6543lunnyzeripath
authored
Rewrite go license generator in go (#21078)
This removes the JS dependency in the checks pipeline. JSON output is different because the previous JS did indent the license data differently and a JSON key was changed, but the end result is the same as it gets re-indented by wepack. Co-authored-by: 6543 <[email protected]> Co-authored-by: Lunny Xiao <[email protected]> Co-authored-by: zeripath <[email protected]>
1 parent 8b8bdb3 commit 52c2ef7

File tree

6 files changed

+449
-254
lines changed

6 files changed

+449
-254
lines changed

.drone.yml

-3
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,7 @@ steps:
9090
- name: checks-backend
9191
image: golang:1.19
9292
commands:
93-
- curl -sL https://deb.nodesource.com/setup_18.x | bash - && apt-get -qqy install nodejs
9493
- make checks-backend
95-
environment:
96-
DEBIAN_FRONTEND: noninteractive
9794
depends_on: [deps-backend]
9895
volumes:
9996
- name: deps

Makefile

+4-4
Original file line numberDiff line numberDiff line change
@@ -422,10 +422,10 @@ tidy-check: tidy
422422
.PHONY: go-licenses
423423
go-licenses: assets/go-licenses.json
424424

425-
assets/go-licenses.json: go.mod go.sum build/generate-go-licenses.js
426-
-$(GO) run $(GO_LICENSES_PACKAGE) save . --force --save_path="$(GO_LICENSE_TMP_DIR)" 2>/dev/null
427-
node build/generate-go-licenses.js "$(GO_LICENSE_TMP_DIR)" "$(GO_LICENSE_FILE)"
428-
@rm -rf "$(GO_LICENSE_TMP_DIR)"
425+
assets/go-licenses.json: go.mod go.sum
426+
-$(GO) run $(GO_LICENSES_PACKAGE) save . --force --save_path=$(GO_LICENSE_TMP_DIR) 2>/dev/null
427+
$(GO) run build/generate-go-licenses.go $(GO_LICENSE_TMP_DIR) $(GO_LICENSE_FILE)
428+
@rm -rf $(GO_LICENSE_TMP_DIR)
429429

430430
generate-ini-sqlite:
431431
sed -e 's|{{REPO_TEST_DIR}}|${REPO_TEST_DIR}|g' \

assets/go-licenses.json

+364-214
Large diffs are not rendered by default.

build/generate-go-licenses.go

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright 2022 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build ignore
6+
7+
package main
8+
9+
import (
10+
"encoding/json"
11+
"io/fs"
12+
"os"
13+
"path/filepath"
14+
"regexp"
15+
"sort"
16+
"strings"
17+
)
18+
19+
// regexp is based on go-license, excluding README and NOTICE
20+
// https://github.com/google/go-licenses/blob/master/licenses/find.go
21+
var licenseRe = regexp.MustCompile(`^(?i)((UN)?LICEN(S|C)E|COPYING).*$`)
22+
23+
type LicenseEntry struct {
24+
Name string `json:"name"`
25+
Path string `json:"path"`
26+
LicenseText string `json:"licenseText"`
27+
}
28+
29+
func main() {
30+
base, out := os.Args[1], os.Args[2]
31+
32+
paths := []string{}
33+
err := filepath.WalkDir(base, func(path string, entry fs.DirEntry, err error) error {
34+
if err != nil {
35+
return err
36+
}
37+
if entry.IsDir() || !licenseRe.MatchString(entry.Name()) {
38+
return nil
39+
}
40+
paths = append(paths, path)
41+
return nil
42+
})
43+
if err != nil {
44+
panic(err)
45+
}
46+
47+
sort.Strings(paths)
48+
49+
entries := []LicenseEntry{}
50+
for _, path := range paths {
51+
licenseText, err := os.ReadFile(path)
52+
if err != nil {
53+
panic(err)
54+
}
55+
56+
path := strings.Replace(path, base+string(os.PathSeparator), "", 1)
57+
58+
entries = append(entries, LicenseEntry{
59+
Name: filepath.Dir(path),
60+
Path: path,
61+
LicenseText: string(licenseText),
62+
})
63+
}
64+
65+
jsonBytes, err := json.MarshalIndent(entries, "", " ")
66+
if err != nil {
67+
panic(err)
68+
}
69+
70+
err = os.WriteFile(out, jsonBytes, 0o644)
71+
if err != nil {
72+
panic(err)
73+
}
74+
}

build/generate-go-licenses.js

-30
This file was deleted.

webpack.config.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import {readFileSync} from 'fs';
1414
const {VueLoaderPlugin} = VueLoader;
1515
const {ESBuildMinifyPlugin} = EsBuildLoader;
1616
const {SourceMapDevToolPlugin} = webpack;
17+
const formatLicenseText = (licenseText) => wrapAnsi(licenseText || '', 80).trim();
18+
1719
const glob = (pattern) => fastGlob.sync(pattern, {
1820
cwd: dirname(fileURLToPath(new URL(import.meta.url))),
1921
absolute: true,
@@ -206,10 +208,12 @@ export default {
206208
outputFilename: 'js/licenses.txt',
207209
outputWriter: ({dependencies}) => {
208210
const line = '-'.repeat(80);
209-
const goModules = JSON.parse(readFileSync('assets/go-licenses.json', 'utf8'));
211+
const goJson = readFileSync('assets/go-licenses.json', 'utf8');
212+
const goModules = JSON.parse(goJson).map(({name, licenseText}) => {
213+
return {name, body: formatLicenseText(licenseText)};
214+
});
210215
const jsModules = dependencies.map(({name, version, licenseName, licenseText}) => {
211-
const body = wrapAnsi(licenseText || '', 80);
212-
return {name, version, licenseName, body};
216+
return {name, version, licenseName, body: formatLicenseText(licenseText)};
213217
});
214218

215219
const modules = [...goModules, ...jsModules].sort((a, b) => a.name.localeCompare(b.name));

0 commit comments

Comments
 (0)