2020-01-26 18:05:38 -07:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 11:20:29 -07:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-01-26 18:05:38 -07:00
|
|
|
|
2021-08-24 09:47:09 -07:00
|
|
|
//go:build ignore
|
2020-01-26 18:05:38 -07:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/sha1"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/shurcooL/vfsgen"
|
|
|
|
)
|
|
|
|
|
2022-01-20 10:46:10 -07:00
|
|
|
func needsUpdate(dir, filename string) (bool, []byte) {
|
2020-01-26 18:05:38 -07:00
|
|
|
needRegen := false
|
|
|
|
_, err := os.Stat(filename)
|
|
|
|
if err != nil {
|
|
|
|
needRegen = true
|
|
|
|
}
|
|
|
|
|
2021-09-21 22:38:34 -07:00
|
|
|
oldHash, err := os.ReadFile(filename + ".hash")
|
2020-01-26 18:05:38 -07:00
|
|
|
if err != nil {
|
|
|
|
oldHash = []byte{}
|
|
|
|
}
|
|
|
|
|
|
|
|
hasher := sha1.New()
|
|
|
|
|
2023-01-16 09:21:44 -07:00
|
|
|
err = filepath.WalkDir(dir, func(path string, d os.DirEntry, err error) error {
|
2020-01-26 18:05:38 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-16 09:21:44 -07:00
|
|
|
info, err := d.Info()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, _ = hasher.Write([]byte(d.Name()))
|
2020-01-26 18:05:38 -07:00
|
|
|
_, _ = 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() {
|
2022-01-06 19:33:17 -07:00
|
|
|
if len(os.Args) < 4 {
|
2020-01-26 18:05:38 -07:00
|
|
|
log.Fatal("Insufficient number of arguments. Need: directory packageName filename")
|
|
|
|
}
|
|
|
|
|
|
|
|
dir, packageName, filename := os.Args[1], os.Args[2], os.Args[3]
|
2022-01-06 19:33:17 -07:00
|
|
|
var useGlobalModTime bool
|
|
|
|
if len(os.Args) == 5 {
|
|
|
|
useGlobalModTime, _ = strconv.ParseBool(os.Args[4])
|
|
|
|
}
|
2020-01-26 18:05:38 -07:00
|
|
|
|
|
|
|
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{
|
2022-01-06 19:33:17 -07:00
|
|
|
PackageName: packageName,
|
|
|
|
BuildTags: "bindata",
|
|
|
|
VariableName: "Assets",
|
|
|
|
Filename: filename,
|
|
|
|
UseGlobalModTime: useGlobalModTime,
|
2020-01-26 18:05:38 -07:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("%v\n", err)
|
|
|
|
}
|
2022-01-20 10:46:10 -07:00
|
|
|
_ = os.WriteFile(filename+".hash", newHash, 0o666)
|
2020-01-26 18:05:38 -07:00
|
|
|
}
|