* 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 <18600385+guillep2k@users.noreply.github.com> Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com>tags/v1.10.5
coverage.all | coverage.all | ||||
/modules/options/bindata.go | /modules/options/bindata.go | ||||
/modules/options/bindata.go.hash | |||||
/modules/public/bindata.go | /modules/public/bindata.go | ||||
/modules/public/bindata.go.hash | |||||
/modules/templates/bindata.go | /modules/templates/bindata.go | ||||
/modules/templates/bindata.go.hash | |||||
*.db | *.db | ||||
*.log | *.log |
JS_DEST := public/js/index.js | JS_DEST := public/js/index.js | ||||
CSS_DEST := public/css/index.css | CSS_DEST := public/css/index.css | ||||
BINDATA_DEST := modules/public/bindata.go modules/options/bindata.go modules/templates/bindata.go | BINDATA_DEST := modules/public/bindata.go modules/options/bindata.go modules/templates/bindata.go | ||||
BINDATA_HASH := $(addsuffix .hash,$(BINDATA_DEST)) | |||||
JS_DEST_DIR := public/js | JS_DEST_DIR := public/js | ||||
CSS_DEST_DIR := public/css | CSS_DEST_DIR := public/css | ||||
.PHONY: clean | .PHONY: clean | ||||
clean: | clean: | ||||
$(GO) clean -i ./... | $(GO) clean -i ./... | ||||
rm -rf $(EXECUTABLE) $(DIST) $(BINDATA_DEST) \ | |||||
rm -rf $(EXECUTABLE) $(DIST) $(BINDATA_DEST) $(BINDATA_HASH) \ | |||||
integrations*.test \ | integrations*.test \ | ||||
integrations/gitea-integration-pgsql/ integrations/gitea-integration-mysql/ integrations/gitea-integration-mysql8/ integrations/gitea-integration-sqlite/ \ | integrations/gitea-integration-pgsql/ integrations/gitea-integration-mysql/ integrations/gitea-integration-mysql8/ integrations/gitea-integration-sqlite/ \ | ||||
integrations/gitea-integration-mssql/ integrations/indexers-mysql/ integrations/indexers-mysql8/ integrations/indexers-pgsql integrations/indexers-sqlite \ | integrations/gitea-integration-mssql/ integrations/indexers-mysql/ integrations/indexers-mysql8/ integrations/indexers-pgsql integrations/indexers-sqlite \ | ||||
.PHONY: generate | .PHONY: generate | ||||
generate: fomantic js css | generate: fomantic js css | ||||
GO111MODULE=on $(GO) generate -mod=vendor $(PACKAGES) | |||||
GO111MODULE=on $(GO) generate -mod=vendor -tags '$(TAGS)' $(PACKAGES) | |||||
.PHONY: generate-swagger | .PHONY: generate-swagger | ||||
generate-swagger: | generate-swagger: |
// +build ignore | |||||
package main | |||||
import ( | |||||
"log" | |||||
"net/http" | |||||
"github.com/shurcooL/vfsgen" | |||||
) | |||||
func main() { | |||||
var fsTemplates http.FileSystem = http.Dir("../../options") | |||||
err := vfsgen.Generate(fsTemplates, vfsgen.Options{ | |||||
PackageName: "options", | |||||
BuildTags: "bindata", | |||||
VariableName: "Assets", | |||||
Filename: "bindata.go", | |||||
}) | |||||
if err != nil { | |||||
log.Fatal("%v", err) | |||||
} | |||||
} |
package options | package options | ||||
//go:generate go run -mod=vendor main.go | |||||
type directorySet map[string][]string | type directorySet map[string][]string | ||||
func (s directorySet) Add(key string, value []string) { | func (s directorySet) Add(key string, value []string) { |
// 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 bindata | |||||
package options | |||||
//go:generate go run -mod=vendor ../../scripts/generate-bindata.go ../../options options bindata.go |
// +build ignore | |||||
package main | |||||
import ( | |||||
"log" | |||||
"net/http" | |||||
"github.com/shurcooL/vfsgen" | |||||
) | |||||
func main() { | |||||
var fsPublic http.FileSystem = http.Dir("../../public") | |||||
err := vfsgen.Generate(fsPublic, vfsgen.Options{ | |||||
PackageName: "public", | |||||
BuildTags: "bindata", | |||||
VariableName: "Assets", | |||||
Filename: "bindata.go", | |||||
}) | |||||
if err != nil { | |||||
log.Fatal("%v", err) | |||||
} | |||||
} |
"gitea.com/macaron/macaron" | "gitea.com/macaron/macaron" | ||||
) | ) | ||||
//go:generate go run -mod=vendor main.go | |||||
// Options represents the available options to configure the macaron handler. | // Options represents the available options to configure the macaron handler. | ||||
type Options struct { | type Options struct { | ||||
Directory string | Directory string |
// 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 bindata | |||||
package public | |||||
//go:generate go run -mod=vendor ../../scripts/generate-bindata.go ../../public public bindata.go |
// +build ignore | |||||
package main | |||||
import ( | |||||
"log" | |||||
"net/http" | |||||
"github.com/shurcooL/vfsgen" | |||||
) | |||||
func main() { | |||||
var fsTemplates http.FileSystem = http.Dir("../../templates") | |||||
err := vfsgen.Generate(fsTemplates, vfsgen.Options{ | |||||
PackageName: "templates", | |||||
BuildTags: "bindata", | |||||
VariableName: "Assets", | |||||
Filename: "bindata.go", | |||||
}) | |||||
if err != nil { | |||||
log.Fatal("%v", err) | |||||
} | |||||
} |
// Use of this source code is governed by a MIT-style | // Use of this source code is governed by a MIT-style | ||||
// license that can be found in the LICENSE file. | // license that can be found in the LICENSE file. | ||||
//+build bindata | |||||
package templates | package templates | ||||
//go:generate go run -mod=vendor main.go | |||||
//go:generate go run -mod=vendor ../../scripts/generate-bindata.go ../../templates templates bindata.go |
// 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) | |||||
} |