2022-09-07 23:35:54 +02:00
// Copyright 2022 The Gitea Authors. All rights reserved.
2022-11-27 13:20:29 -05:00
// SPDX-License-Identifier: MIT
2022-09-07 23:35:54 +02:00
//go:build ignore
package main
import (
"encoding/json"
"io/fs"
"os"
2023-01-29 09:49:51 -06:00
goPath "path"
2022-09-07 23:35:54 +02: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 ( ) {
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 {
2023-01-29 09:49:51 -06:00
path := filepath . ToSlash ( path )
2022-09-07 23:35:54 +02:00
licenseText , err := os . ReadFile ( path )
if err != nil {
panic ( err )
}
2023-01-29 09:49:51 -06:00
path = strings . Replace ( path , base + "/" , "" , 1 )
name := goPath . Dir ( path )
2022-09-09 17:33:01 +02: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.
if name == "." || name == "code.gitea.io/gitea" {
continue
}
2022-09-07 23:35:54 +02:00
entries = append ( entries , LicenseEntry {
2022-09-19 14:02:29 +02:00
Name : name ,
Path : path ,
2022-09-07 23:35:54 +02:00
LicenseText : string ( licenseText ) ,
} )
}
jsonBytes , err := json . MarshalIndent ( entries , "" , " " )
if err != nil {
panic ( err )
}
err = os . WriteFile ( out , jsonBytes , 0 o644 )
if err != nil {
panic ( err )
}
}