forked from gitea/gitea
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 <6543@obermui.de> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: zeripath <art27@cantab.net>
This commit is contained in:
parent
8b8bdb30fb
commit
52c2ef7902
|
@ -90,10 +90,7 @@ steps:
|
|||
- name: checks-backend
|
||||
image: golang:1.19
|
||||
commands:
|
||||
- curl -sL https://deb.nodesource.com/setup_18.x | bash - && apt-get -qqy install nodejs
|
||||
- make checks-backend
|
||||
environment:
|
||||
DEBIAN_FRONTEND: noninteractive
|
||||
depends_on: [deps-backend]
|
||||
volumes:
|
||||
- name: deps
|
||||
|
|
8
Makefile
8
Makefile
|
@ -422,10 +422,10 @@ tidy-check: tidy
|
|||
.PHONY: go-licenses
|
||||
go-licenses: assets/go-licenses.json
|
||||
|
||||
assets/go-licenses.json: go.mod go.sum build/generate-go-licenses.js
|
||||
-$(GO) run $(GO_LICENSES_PACKAGE) save . --force --save_path="$(GO_LICENSE_TMP_DIR)" 2>/dev/null
|
||||
node build/generate-go-licenses.js "$(GO_LICENSE_TMP_DIR)" "$(GO_LICENSE_FILE)"
|
||||
@rm -rf "$(GO_LICENSE_TMP_DIR)"
|
||||
assets/go-licenses.json: go.mod go.sum
|
||||
-$(GO) run $(GO_LICENSES_PACKAGE) save . --force --save_path=$(GO_LICENSE_TMP_DIR) 2>/dev/null
|
||||
$(GO) run build/generate-go-licenses.go $(GO_LICENSE_TMP_DIR) $(GO_LICENSE_FILE)
|
||||
@rm -rf $(GO_LICENSE_TMP_DIR)
|
||||
|
||||
generate-ini-sqlite:
|
||||
sed -e 's|{{REPO_TEST_DIR}}|${REPO_TEST_DIR}|g' \
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,74 @@
|
|||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build ignore
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// regexp is based on go-license, excluding README and NOTICE
|
||||
// https://github.com/google/go-licenses/blob/master/licenses/find.go
|
||||
var licenseRe = regexp.MustCompile(`^(?i)((UN)?LICEN(S|C)E|COPYING).*$`)
|
||||
|
||||
type LicenseEntry struct {
|
||||
Name string `json:"name"`
|
||||
Path string `json:"path"`
|
||||
LicenseText string `json:"licenseText"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
base, out := os.Args[1], os.Args[2]
|
||||
|
||||
paths := []string{}
|
||||
err := filepath.WalkDir(base, func(path string, entry fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if entry.IsDir() || !licenseRe.MatchString(entry.Name()) {
|
||||
return nil
|
||||
}
|
||||
paths = append(paths, path)
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
sort.Strings(paths)
|
||||
|
||||
entries := []LicenseEntry{}
|
||||
for _, path := range paths {
|
||||
licenseText, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
path := strings.Replace(path, base+string(os.PathSeparator), "", 1)
|
||||
|
||||
entries = append(entries, LicenseEntry{
|
||||
Name: filepath.Dir(path),
|
||||
Path: path,
|
||||
LicenseText: string(licenseText),
|
||||
})
|
||||
}
|
||||
|
||||
jsonBytes, err := json.MarshalIndent(entries, "", " ")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
err = os.WriteFile(out, jsonBytes, 0o644)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
|
@ -1,30 +0,0 @@
|
|||
#!/usr/bin/env node
|
||||
import fastGlob from 'fast-glob';
|
||||
import {fileURLToPath} from 'url';
|
||||
import {readFileSync, writeFileSync} from 'fs';
|
||||
import wrapAnsi from 'wrap-ansi';
|
||||
import {join, dirname} from 'path';
|
||||
|
||||
const base = process.argv[2];
|
||||
const out = process.argv[3];
|
||||
|
||||
function exit(err) {
|
||||
if (err) console.error(err);
|
||||
process.exit(err ? 1 : 0);
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const data = fastGlob.sync('**/*', {
|
||||
cwd: fileURLToPath(new URL(`../${base}`, import.meta.url)),
|
||||
}).filter((path) => {
|
||||
return /\/((UN)?LICEN(S|C)E|COPYING|NOTICE)/i.test(path);
|
||||
}).sort().map((path) => {
|
||||
return {
|
||||
name: dirname(path),
|
||||
body: wrapAnsi(readFileSync(join(base, path), 'utf8') || '', 80)
|
||||
};
|
||||
});
|
||||
writeFileSync(out, JSON.stringify(data, null, 2));
|
||||
}
|
||||
|
||||
main().then(exit).catch(exit);
|
|
@ -14,6 +14,8 @@ import {readFileSync} from 'fs';
|
|||
const {VueLoaderPlugin} = VueLoader;
|
||||
const {ESBuildMinifyPlugin} = EsBuildLoader;
|
||||
const {SourceMapDevToolPlugin} = webpack;
|
||||
const formatLicenseText = (licenseText) => wrapAnsi(licenseText || '', 80).trim();
|
||||
|
||||
const glob = (pattern) => fastGlob.sync(pattern, {
|
||||
cwd: dirname(fileURLToPath(new URL(import.meta.url))),
|
||||
absolute: true,
|
||||
|
@ -206,10 +208,12 @@ export default {
|
|||
outputFilename: 'js/licenses.txt',
|
||||
outputWriter: ({dependencies}) => {
|
||||
const line = '-'.repeat(80);
|
||||
const goModules = JSON.parse(readFileSync('assets/go-licenses.json', 'utf8'));
|
||||
const goJson = readFileSync('assets/go-licenses.json', 'utf8');
|
||||
const goModules = JSON.parse(goJson).map(({name, licenseText}) => {
|
||||
return {name, body: formatLicenseText(licenseText)};
|
||||
});
|
||||
const jsModules = dependencies.map(({name, version, licenseName, licenseText}) => {
|
||||
const body = wrapAnsi(licenseText || '', 80);
|
||||
return {name, version, licenseName, body};
|
||||
return {name, version, licenseName, body: formatLicenseText(licenseText)};
|
||||
});
|
||||
|
||||
const modules = [...goModules, ...jsModules].sort((a, b) => a.name.localeCompare(b.name));
|
||||
|
|
Loading…
Reference in New Issue