Skip to content

Commit 48abe41

Browse files
zeripathguillep2k
andauthored
Generate Bindata iff TAGS="bindata" and not up-to-date (#10004)
* Only generate bindata if necessary * Only generate bindata if they are not up-to-date * generate a hash of the fileinfo and use that to keep up-to-date * Newer test is redundant * handle missing bindata and clean * Only update hash after successful write * switch to sha1 hash * Apply suggestions from code review Co-Authored-By: guillep2k <[email protected]> Co-authored-by: guillep2k <[email protected]>
1 parent d087f4f commit 48abe41

File tree

11 files changed

+113
-76
lines changed

11 files changed

+113
-76
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@ _testmain.go
3434
coverage.all
3535

3636
/modules/options/bindata.go
37+
/modules/options/bindata.go.hash
3738
/modules/public/bindata.go
39+
/modules/public/bindata.go.hash
3840
/modules/templates/bindata.go
41+
/modules/templates/bindata.go.hash
3942

4043
*.db
4144
*.log

Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ CSS_SOURCES ?= $(shell find web_src/less -type f)
5252
JS_DEST := public/js/index.js
5353
CSS_DEST := public/css/index.css
5454
BINDATA_DEST := modules/public/bindata.go modules/options/bindata.go modules/templates/bindata.go
55+
BINDATA_HASH := $(addsuffix .hash,$(BINDATA_DEST))
5556

5657
JS_DEST_DIR := public/js
5758
CSS_DEST_DIR := public/css
@@ -145,7 +146,7 @@ clean-all: clean
145146
.PHONY: clean
146147
clean:
147148
$(GO) clean -i ./...
148-
rm -rf $(EXECUTABLE) $(DIST) $(BINDATA_DEST) \
149+
rm -rf $(EXECUTABLE) $(DIST) $(BINDATA_DEST) $(BINDATA_HASH) \
149150
integrations*.test \
150151
integrations/gitea-integration-pgsql/ integrations/gitea-integration-mysql/ integrations/gitea-integration-mysql8/ integrations/gitea-integration-sqlite/ \
151152
integrations/gitea-integration-mssql/ integrations/indexers-mysql/ integrations/indexers-mysql8/ integrations/indexers-pgsql integrations/indexers-sqlite \
@@ -161,7 +162,7 @@ vet:
161162

162163
.PHONY: generate
163164
generate: fomantic js css
164-
GO111MODULE=on $(GO) generate -mod=vendor $(PACKAGES)
165+
GO111MODULE=on $(GO) generate -mod=vendor -tags '$(TAGS)' $(PACKAGES)
165166

166167
.PHONY: generate-swagger
167168
generate-swagger:

modules/options/main.go

Lines changed: 0 additions & 23 deletions
This file was deleted.

modules/options/options.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
package options
66

7-
//go:generate go run -mod=vendor main.go
8-
97
type directorySet map[string][]string
108

119
func (s directorySet) Add(key string, value []string) {

modules/options/options_bindata.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright 2020 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
//+build bindata
6+
7+
package options
8+
9+
//go:generate go run -mod=vendor ../../scripts/generate-bindata.go ../../options options bindata.go

modules/public/main.go

Lines changed: 0 additions & 23 deletions
This file was deleted.

modules/public/public.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ import (
1818
"gitea.com/macaron/macaron"
1919
)
2020

21-
//go:generate go run -mod=vendor main.go
22-
2321
// Options represents the available options to configure the macaron handler.
2422
type Options struct {
2523
Directory string

modules/public/public_bindata.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright 2020 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
//+build bindata
6+
7+
package public
8+
9+
//go:generate go run -mod=vendor ../../scripts/generate-bindata.go ../../public public bindata.go

modules/templates/main.go

Lines changed: 0 additions & 23 deletions
This file was deleted.

modules/templates/templates.go renamed to modules/templates/templates_bindata.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Use of this source code is governed by a MIT-style
33
// license that can be found in the LICENSE file.
44

5+
//+build bindata
6+
57
package templates
68

7-
//go:generate go run -mod=vendor main.go
9+
//go:generate go run -mod=vendor ../../scripts/generate-bindata.go ../../templates templates bindata.go

scripts/generate-bindata.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright 2020 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build ignore
6+
7+
package main
8+
9+
import (
10+
"bytes"
11+
"crypto/sha1"
12+
"fmt"
13+
"io/ioutil"
14+
"log"
15+
"net/http"
16+
"os"
17+
"path/filepath"
18+
"strconv"
19+
20+
"github.com/shurcooL/vfsgen"
21+
)
22+
23+
func needsUpdate(dir string, filename string) (bool, []byte) {
24+
needRegen := false
25+
_, err := os.Stat(filename)
26+
if err != nil {
27+
needRegen = true
28+
}
29+
30+
oldHash, err := ioutil.ReadFile(filename + ".hash")
31+
if err != nil {
32+
oldHash = []byte{}
33+
}
34+
35+
hasher := sha1.New()
36+
37+
err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
38+
if err != nil {
39+
return err
40+
}
41+
_, _ = hasher.Write([]byte(info.Name()))
42+
_, _ = hasher.Write([]byte(info.ModTime().String()))
43+
_, _ = hasher.Write([]byte(strconv.FormatInt(info.Size(), 16)))
44+
return nil
45+
})
46+
if err != nil {
47+
return true, oldHash
48+
}
49+
50+
newHash := hasher.Sum([]byte{})
51+
52+
if bytes.Compare(oldHash, newHash) != 0 {
53+
54+
return true, newHash
55+
}
56+
57+
return needRegen, newHash
58+
}
59+
60+
func main() {
61+
if len(os.Args) != 4 {
62+
log.Fatal("Insufficient number of arguments. Need: directory packageName filename")
63+
}
64+
65+
dir, packageName, filename := os.Args[1], os.Args[2], os.Args[3]
66+
67+
update, newHash := needsUpdate(dir, filename)
68+
69+
if !update {
70+
fmt.Printf("bindata for %s already up-to-date\n", packageName)
71+
return
72+
}
73+
74+
fmt.Printf("generating bindata for %s\n", packageName)
75+
var fsTemplates http.FileSystem = http.Dir(dir)
76+
err := vfsgen.Generate(fsTemplates, vfsgen.Options{
77+
PackageName: packageName,
78+
BuildTags: "bindata",
79+
VariableName: "Assets",
80+
Filename: filename,
81+
})
82+
if err != nil {
83+
log.Fatalf("%v\n", err)
84+
}
85+
_ = ioutil.WriteFile(filename+".hash", newHash, 0666)
86+
}

0 commit comments

Comments
 (0)