Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite go license generator in go #21078

Merged
merged 7 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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' \
Expand Down
396 changes: 182 additions & 214 deletions assets/go-licenses.json

Large diffs are not rendered by default.

70 changes: 70 additions & 0 deletions build/generate-go-licenses.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// 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"`
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)
}

entries = append(entries, LicenseEntry{
Name: filepath.Dir(strings.Replace(path, base+string(os.PathSeparator), "", 1)),
silverwind marked this conversation as resolved.
Show resolved Hide resolved
LicenseText: string(licenseText),
})
}

jsonBytes, err := json.MarshalIndent(entries, "", " ")
if err != nil {
panic(err)
}

err = os.WriteFile(out, jsonBytes, 0o644)
if err != nil {
panic(err)
}
}
30 changes: 0 additions & 30 deletions build/generate-go-licenses.js

This file was deleted.

10 changes: 7 additions & 3 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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));
Expand Down