summaryrefslogtreecommitdiffstats
path: root/build/generate-bindata.go
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2020-04-04 03:29:12 +0800
committerGitHub <noreply@github.com>2020-04-03 22:29:12 +0300
commit4f63f283c47dcf9e705ce5b8e8857f2b42cff8ad (patch)
treedd5dc2cae6ebae21826ffcce937533559be45a07 /build/generate-bindata.go
parent4af7c47b38d382d105726f9553a1a68d46882cbf (diff)
downloadgitea-4f63f283c47dcf9e705ce5b8e8857f2b42cff8ad.tar.gz
gitea-4f63f283c47dcf9e705ce5b8e8857f2b42cff8ad.zip
Rename scripts to build and add revive command as a new build tool command (#10942)
Co-authored-by: techknowlogick <techknowlogick@gitea.io>
Diffstat (limited to 'build/generate-bindata.go')
-rw-r--r--build/generate-bindata.go86
1 files changed, 86 insertions, 0 deletions
diff --git a/build/generate-bindata.go b/build/generate-bindata.go
new file mode 100644
index 0000000000..fa1669fcf9
--- /dev/null
+++ b/build/generate-bindata.go
@@ -0,0 +1,86 @@
+// Copyright 2020 The Gitea Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+// +build ignore
+
+package main
+
+import (
+ "bytes"
+ "crypto/sha1"
+ "fmt"
+ "io/ioutil"
+ "log"
+ "net/http"
+ "os"
+ "path/filepath"
+ "strconv"
+
+ "github.com/shurcooL/vfsgen"
+)
+
+func needsUpdate(dir string, filename string) (bool, []byte) {
+ needRegen := false
+ _, err := os.Stat(filename)
+ if err != nil {
+ needRegen = true
+ }
+
+ oldHash, err := ioutil.ReadFile(filename + ".hash")
+ if err != nil {
+ oldHash = []byte{}
+ }
+
+ hasher := sha1.New()
+
+ err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
+ if err != nil {
+ return err
+ }
+ _, _ = hasher.Write([]byte(info.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]
+
+ 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,
+ })
+ if err != nil {
+ log.Fatalf("%v\n", err)
+ }
+ _ = ioutil.WriteFile(filename+".hash", newHash, 0666)
+}