2022-09-07 21:35:54 +00:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-09-07 21:35:54 +00:00
|
|
|
|
|
|
|
//go:build ignore
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2023-04-08 06:21:50 +00:00
|
|
|
"fmt"
|
2022-09-07 21:35:54 +00:00
|
|
|
"io/fs"
|
|
|
|
"os"
|
2023-04-08 06:21:50 +00:00
|
|
|
"path"
|
2022-09-07 21:35:54 +00: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 06:21:50 +00: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 21:35:54 +00:00
|
|
|
base, out := os.Args[1], os.Args[2]
|
|
|
|
|
2023-05-05 13:46:17 +00:00
|
|
|
// Add ext for excluded files because license_test.go will be included for some reason.
|
|
|
|
// And there are more files that should be excluded, check with:
|
|
|
|
//
|
|
|
|
// go run github.com/google/go-licenses@v1.6.0 save . --force --save_path=.go-licenses 2>/dev/null
|
|
|
|
// find .go-licenses -type f | while read FILE; do echo "${$(basename $FILE)##*.}"; done | sort -u
|
|
|
|
// AUTHORS
|
|
|
|
// COPYING
|
|
|
|
// LICENSE
|
|
|
|
// Makefile
|
|
|
|
// NOTICE
|
|
|
|
// gitignore
|
|
|
|
// go
|
|
|
|
// md
|
|
|
|
// mod
|
|
|
|
// sum
|
|
|
|
// toml
|
|
|
|
// txt
|
|
|
|
// yml
|
|
|
|
//
|
|
|
|
// It could be removed once we have a better regex.
|
|
|
|
excludedExt := map[string]bool{
|
|
|
|
".gitignore": true,
|
|
|
|
".go": true,
|
|
|
|
".mod": true,
|
|
|
|
".sum": true,
|
|
|
|
".toml": true,
|
|
|
|
".yml": true,
|
|
|
|
}
|
2023-04-08 06:21:50 +00:00
|
|
|
var paths []string
|
2022-09-07 21:35:54 +00:00
|
|
|
err := filepath.WalkDir(base, func(path string, entry fs.DirEntry, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-05-05 13:46:17 +00:00
|
|
|
if entry.IsDir() || !licenseRe.MatchString(entry.Name()) || excludedExt[filepath.Ext(entry.Name())] {
|
2022-09-07 21:35:54 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
paths = append(paths, path)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(paths)
|
|
|
|
|
2023-04-08 06:21:50 +00:00
|
|
|
var entries []LicenseEntry
|
|
|
|
for _, filePath := range paths {
|
|
|
|
licenseText, err := os.ReadFile(filePath)
|
2022-09-07 21:35:54 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2023-04-08 06:21:50 +00:00
|
|
|
pkgPath := filepath.ToSlash(filePath)
|
|
|
|
pkgPath = strings.TrimPrefix(pkgPath, base+"/")
|
|
|
|
pkgName := path.Dir(pkgPath)
|
2022-09-09 15:33:01 +00: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 06:21:50 +00:00
|
|
|
if pkgName == "." || pkgName == "code.gitea.io/gitea" {
|
2022-09-09 15:33:01 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-09-07 21:35:54 +00:00
|
|
|
|
|
|
|
entries = append(entries, LicenseEntry{
|
2023-04-08 06:21:50 +00:00
|
|
|
Name: pkgName,
|
|
|
|
Path: pkgPath,
|
2022-09-07 21:35:54 +00:00
|
|
|
LicenseText: string(licenseText),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonBytes, err := json.MarshalIndent(entries, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2023-04-28 17:39:18 +00:00
|
|
|
// Ensure file has a final newline
|
|
|
|
if jsonBytes[len(jsonBytes)-1] != '\n' {
|
|
|
|
jsonBytes = append(jsonBytes, '\n')
|
|
|
|
}
|
|
|
|
|
2022-09-07 21:35:54 +00:00
|
|
|
err = os.WriteFile(out, jsonBytes, 0o644)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|