2022-09-08 00:35:54 +03:00
// Copyright 2022 The Gitea Authors. All rights reserved.
2022-11-27 21:20:29 +03:00
// SPDX-License-Identifier: MIT
2022-09-08 00:35:54 +03:00
//go:build ignore
package main
import (
"encoding/json"
2023-04-08 09:21:50 +03:00
"fmt"
2022-09-08 00:35:54 +03:00
"io/fs"
"os"
2023-04-08 09:21:50 +03:00
"path"
2022-09-08 00:35:54 +03:00
"path/filepath"
"regexp"
"sort"
"strings"
2023-08-30 09:55:25 +03:00
"code.gitea.io/gitea/modules/container"
2022-09-08 00:35:54 +03:00
)
// 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 09:21:50 +03: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-08 00:35:54 +03:00
base , out := os . Args [ 1 ] , os . Args [ 2 ]
2023-05-05 16:46:17 +03: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.
2023-08-30 09:55:25 +03:00
excludedExt := container . SetOf ( ".gitignore" , ".go" , ".mod" , ".sum" , ".toml" , ".yml" )
2023-04-08 09:21:50 +03:00
var paths [ ] string
2022-09-08 00:35:54 +03:00
err := filepath . WalkDir ( base , func ( path string , entry fs . DirEntry , err error ) error {
if err != nil {
return err
}
2023-08-30 09:55:25 +03:00
if entry . IsDir ( ) || ! licenseRe . MatchString ( entry . Name ( ) ) || excludedExt . Contains ( filepath . Ext ( entry . Name ( ) ) ) {
2022-09-08 00:35:54 +03:00
return nil
}
paths = append ( paths , path )
return nil
} )
if err != nil {
panic ( err )
}
sort . Strings ( paths )
2023-04-08 09:21:50 +03:00
var entries [ ] LicenseEntry
for _ , filePath := range paths {
licenseText , err := os . ReadFile ( filePath )
2022-09-08 00:35:54 +03:00
if err != nil {
panic ( err )
}
2023-04-08 09:21:50 +03:00
pkgPath := filepath . ToSlash ( filePath )
pkgPath = strings . TrimPrefix ( pkgPath , base + "/" )
pkgName := path . Dir ( pkgPath )
2022-09-09 18:33:01 +03: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 09:21:50 +03:00
if pkgName == "." || pkgName == "code.gitea.io/gitea" {
2022-09-09 18:33:01 +03:00
continue
}
2022-09-08 00:35:54 +03:00
entries = append ( entries , LicenseEntry {
2023-04-08 09:21:50 +03:00
Name : pkgName ,
Path : pkgPath ,
2022-09-08 00:35:54 +03:00
LicenseText : string ( licenseText ) ,
} )
}
jsonBytes , err := json . MarshalIndent ( entries , "" , " " )
if err != nil {
panic ( err )
}
2023-04-28 20:39:18 +03:00
// Ensure file has a final newline
if jsonBytes [ len ( jsonBytes ) - 1 ] != '\n' {
jsonBytes = append ( jsonBytes , '\n' )
}
2022-09-08 00:35:54 +03:00
err = os . WriteFile ( out , jsonBytes , 0 o644 )
if err != nil {
panic ( err )
}
}