mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-05 01:39:13 +01:00
04c97aa364
As suggest by Go developers, use `filepath.WalkDir` instead of `filepath.Walk` because [*Walk is less efficient than WalkDir, introduced in Go 1.16, which avoids calling `os.Lstat` on every file or directory visited](https://pkg.go.dev/path/filepath#Walk). This proposition address that, in a similar way as https://github.com/go-gitea/gitea/pull/22392 did. Co-authored-by: zeripath <art27@cantab.net> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
92 lines
1.9 KiB
Go
92 lines
1.9 KiB
Go
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
//go:build ignore
|
|
|
|
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/sha1"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
|
|
"github.com/shurcooL/vfsgen"
|
|
)
|
|
|
|
func needsUpdate(dir, filename string) (bool, []byte) {
|
|
needRegen := false
|
|
_, err := os.Stat(filename)
|
|
if err != nil {
|
|
needRegen = true
|
|
}
|
|
|
|
oldHash, err := os.ReadFile(filename + ".hash")
|
|
if err != nil {
|
|
oldHash = []byte{}
|
|
}
|
|
|
|
hasher := sha1.New()
|
|
|
|
err = filepath.WalkDir(dir, func(path string, d os.DirEntry, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
info, err := d.Info()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, _ = hasher.Write([]byte(d.Name()))
|
|
_, _ = hasher.Write([]byte(info.ModTime().String()))
|
|
_, _ = hasher.Write([]byte(strconv.FormatInt(info.Size(), 16)))
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return true, oldHash
|
|
}
|
|
|
|
newHash := hasher.Sum([]byte{})
|
|
|
|
if bytes.Compare(oldHash, newHash) != 0 {
|
|
return true, newHash
|
|
}
|
|
|
|
return needRegen, newHash
|
|
}
|
|
|
|
func main() {
|
|
if len(os.Args) < 4 {
|
|
log.Fatal("Insufficient number of arguments. Need: directory packageName filename")
|
|
}
|
|
|
|
dir, packageName, filename := os.Args[1], os.Args[2], os.Args[3]
|
|
var useGlobalModTime bool
|
|
if len(os.Args) == 5 {
|
|
useGlobalModTime, _ = strconv.ParseBool(os.Args[4])
|
|
}
|
|
|
|
update, newHash := needsUpdate(dir, filename)
|
|
|
|
if !update {
|
|
fmt.Printf("bindata for %s already up-to-date\n", packageName)
|
|
return
|
|
}
|
|
|
|
fmt.Printf("generating bindata for %s\n", packageName)
|
|
var fsTemplates http.FileSystem = http.Dir(dir)
|
|
err := vfsgen.Generate(fsTemplates, vfsgen.Options{
|
|
PackageName: packageName,
|
|
BuildTags: "bindata",
|
|
VariableName: "Assets",
|
|
Filename: filename,
|
|
UseGlobalModTime: useGlobalModTime,
|
|
})
|
|
if err != nil {
|
|
log.Fatalf("%v\n", err)
|
|
}
|
|
_ = os.WriteFile(filename+".hash", newHash, 0o666)
|
|
}
|