2022-09-07 15:35:54 -06:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
2022-11-27 11:20:29 -07:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-09-07 15:35:54 -06:00
|
|
|
|
|
|
|
//go:build ignore
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2023-04-08 00:21:50 -06:00
|
|
|
"fmt"
|
2022-09-07 15:35:54 -06:00
|
|
|
"io/fs"
|
|
|
|
"os"
|
2023-04-08 00:21:50 -06:00
|
|
|
"path"
|
2022-09-07 15:35:54 -06:00
|
|
|
"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() {
|
2023-04-08 00:21:50 -06:00
|
|
|
if len(os.Args) != 3 {
|
|
|
|
fmt.Println("usage: go run generate-go-licenses.go <base-dir> <out-json-file>")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2022-09-07 15:35:54 -06:00
|
|
|
base, out := os.Args[1], os.Args[2]
|
|
|
|
|
2023-04-08 00:21:50 -06:00
|
|
|
var paths []string
|
2022-09-07 15:35:54 -06:00
|
|
|
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)
|
|
|
|
|
2023-04-08 00:21:50 -06:00
|
|
|
var entries []LicenseEntry
|
|
|
|
for _, filePath := range paths {
|
|
|
|
licenseText, err := os.ReadFile(filePath)
|
2022-09-07 15:35:54 -06:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2023-04-08 00:21:50 -06:00
|
|
|
pkgPath := filepath.ToSlash(filePath)
|
|
|
|
pkgPath = strings.TrimPrefix(pkgPath, base+"/")
|
|
|
|
pkgName := path.Dir(pkgPath)
|
2022-09-09 09:33:01 -06:00
|
|
|
|
|
|
|
// There might be a bug somewhere in go-licenses that sometimes interprets the
|
|
|
|
// root package as "." and sometimes as "code.gitea.io/gitea". Workaround by
|
|
|
|
// removing both of them for the sake of stable output.
|
2023-04-08 00:21:50 -06:00
|
|
|
if pkgName == "." || pkgName == "code.gitea.io/gitea" {
|
2022-09-09 09:33:01 -06:00
|
|
|
continue
|
|
|
|
}
|
2022-09-07 15:35:54 -06:00
|
|
|
|
|
|
|
entries = append(entries, LicenseEntry{
|
2023-04-08 00:21:50 -06:00
|
|
|
Name: pkgName,
|
|
|
|
Path: pkgPath,
|
2022-09-07 15:35:54 -06:00
|
|
|
LicenseText: string(licenseText),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonBytes, err := json.MarshalIndent(entries, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2023-04-28 11:39:18 -06:00
|
|
|
// Ensure file has a final newline
|
|
|
|
if jsonBytes[len(jsonBytes)-1] != '\n' {
|
|
|
|
jsonBytes = append(jsonBytes, '\n')
|
|
|
|
}
|
|
|
|
|
2022-09-07 15:35:54 -06:00
|
|
|
err = os.WriteFile(out, jsonBytes, 0o644)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|