From a24336e5d0da83b1f7b2f71e984b07a871f5be79 Mon Sep 17 00:00:00 2001 From: Matti Ranta Date: Mon, 4 Feb 2019 01:53:35 -0500 Subject: [PATCH 1/7] switch to use packr to embed static assets --- .gitignore | 7 ++++--- Makefile | 33 ++++++++++++++++++++++++++------- modules/options/options.go | 5 ----- modules/options/static.go | 23 ++++++++++++++++++----- modules/public/public.go | 6 +----- modules/public/static.go | 11 +++-------- modules/templates/static.go | 14 ++++++++++---- modules/templates/templates.go | 10 ---------- 8 files changed, 62 insertions(+), 47 deletions(-) delete mode 100644 modules/templates/templates.go diff --git a/.gitignore b/.gitignore index 941ec41e045f1..beec2e6c3b1da 100644 --- a/.gitignore +++ b/.gitignore @@ -32,9 +32,10 @@ _testmain.go *coverage.out coverage.all -/modules/options/bindata.go -/modules/public/bindata.go -/modules/templates/bindata.go +/modules/templates/templates-packr.go +/modules/options/options-packr.go +/modules/public/public-packr.go +/packrd/* *.db *.log diff --git a/Makefile b/Makefile index 5201e99b04d56..87b5d010dbce2 100644 --- a/Makefile +++ b/Makefile @@ -14,8 +14,8 @@ else endif endif -BINDATA := modules/{options,public,templates}/bindata.go -GOFILES := $(shell find . -name "*.go" -type f ! -path "./vendor/*" ! -path "*/bindata.go") +BINDATA := modules/{options,public,templates}/*-packr.go packrd/* +GOFILES := $(shell find . -name "*.go" -type f ! -path "./vendor/*" ! -path "*/*-packr.go") GOFMT ?= gofmt -s GOFLAGS := -i -v @@ -74,7 +74,7 @@ all: build include docker/Makefile .PHONY: clean -clean: +clean: packr-clean $(GO) clean -i ./... rm -rf $(EXECUTABLE) $(DIST) $(BINDATA) \ integrations*.test \ @@ -91,12 +91,31 @@ vet: $(GO) vet $(PACKAGES) .PHONY: generate -generate: - @hash go-bindata > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ - $(GO) get -u github.com/jteeuwen/go-bindata/go-bindata; \ - fi +generate: bindata $(GO) generate $(PACKAGES) +.PHONY: bindata +bindata: packr + +.PHONY: packr +packr: + @hash packr2 > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ + $(GO) get -u github.com/gobuffalo/packr/v2/packr2; \ + fi + packr2 -v + # Add buildtag to all packr files + echo "// +build bindata" | cat - packrd/packed-packr.go > packrd/packed-packr.go.bak && mv packrd/packed-packr.go.bak packrd/packed-packr.go + echo "// +build bindata" | cat - modules/templates/templates-packr.go > modules/templates/templates-packr.go.bak && mv modules/templates/templates-packr.go.bak modules/templates/templates-packr.go + echo "// +build bindata" | cat - modules/options/options-packr.go > modules/options/options-packr.go.bak && mv modules/options/options-packr.go.bak modules/options/options-packr.go + echo "// +build bindata" | cat - modules/public/public-packr.go > modules/public/public-packr.go.bak && mv modules/public/public-packr.go.bak modules/public/public-packr.go + +.PHONY: packr-clean +packr-clean: + @hash packr2 > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ + $(GO) get -u github.com/gobuffalo/packr/v2/packr2; \ + fi + packr2 clean + .PHONY: generate-swagger generate-swagger: @hash swagger > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ diff --git a/modules/options/options.go b/modules/options/options.go index 4f0c69b335582..6ba3bd6a868ad 100644 --- a/modules/options/options.go +++ b/modules/options/options.go @@ -4,11 +4,6 @@ package options -//go:generate go-bindata -tags "bindata" -ignore "TRANSLATORS" -pkg "options" -o "bindata.go" ../../options/... -//go:generate go fmt bindata.go -//go:generate sed -i.bak s/..\/..\/options\/// bindata.go -//go:generate rm -f bindata.go.bak - type directorySet map[string][]string func (s directorySet) Add(key string, value []string) { diff --git a/modules/options/static.go b/modules/options/static.go index 3301f45171207..b874367a36ff8 100644 --- a/modules/options/static.go +++ b/modules/options/static.go @@ -10,9 +10,12 @@ import ( "fmt" "io/ioutil" "path" + "strings" "code.gitea.io/gitea/modules/setting" + "github.com/Unknwon/com" + "github.com/gobuffalo/packr/v2" ) var ( @@ -27,6 +30,7 @@ func Dir(name string) ([]string, error) { var ( result []string + files []string ) customDir := path.Join(setting.CustomPath, "options", name) @@ -41,11 +45,18 @@ func Dir(name string) ([]string, error) { result = append(result, files...) } - files, err := AssetDir(path.Join("..", "..", "options", name)) + box := packr.New("options", "../../options") - if err != nil { - return []string{}, fmt.Errorf("Failed to read embedded directory. %v", err) - } + box.WalkPrefix(name, func(path string, info packr.File) error { + if info == nil { + return nil + } + finfo, _ := info.FileInfo() + if !finfo.IsDir() { + files = append(files, strings.TrimPrefix(path, name+"/")) + } + return nil + }) result = append(result, files...) @@ -85,5 +96,7 @@ func fileFromDir(name string) ([]byte, error) { return ioutil.ReadFile(customPath) } - return Asset(name) + box := packr.New("options", "../../options") + + return box.Find(name) } diff --git a/modules/public/public.go b/modules/public/public.go index 2e004536f894e..ec280bf1e4dd0 100644 --- a/modules/public/public.go +++ b/modules/public/public.go @@ -14,14 +14,10 @@ import ( "time" "code.gitea.io/gitea/modules/setting" + "gopkg.in/macaron.v1" ) -//go:generate go-bindata -tags "bindata" -ignore "\\.go|\\.less" -pkg "public" -o "bindata.go" ../../public/... -//go:generate go fmt bindata.go -//go:generate sed -i.bak s/..\/..\/public\/// bindata.go -//go:generate rm -f bindata.go.bak - // Options represents the available options to configure the macaron handler. type Options struct { Directory string diff --git a/modules/public/static.go b/modules/public/static.go index 10e32dbd10f1d..114ab243feb29 100644 --- a/modules/public/static.go +++ b/modules/public/static.go @@ -7,19 +7,14 @@ package public import ( - "github.com/go-macaron/bindata" + "github.com/gobuffalo/packr/v2" "gopkg.in/macaron.v1" ) // Static implements the macaron static handler for serving assets. func Static(opts *Options) macaron.Handler { - opts.FileSystem = bindata.Static(bindata.Options{ - Asset: Asset, - AssetDir: AssetDir, - AssetInfo: AssetInfo, - AssetNames: AssetNames, - Prefix: "", - }) + box := packr.New("public", "../../public") + opts.FileSystem = box // we don't need to pass the directory, because the directory var is only // used when in the options there is no FileSystem. return opts.staticHandler("") diff --git a/modules/templates/static.go b/modules/templates/static.go index e69e1cae48152..2ac4296c9df2c 100644 --- a/modules/templates/static.go +++ b/modules/templates/static.go @@ -17,7 +17,9 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" + "github.com/Unknwon/com" + "github.com/gobuffalo/packr/v2" "gopkg.in/macaron.v1" ) @@ -47,7 +49,9 @@ func NewTemplateFileSystem() templateFileSystem { fs := templateFileSystem{} fs.files = make([]macaron.TemplateFile, 0, 10) - for _, assetPath := range AssetNames() { + box := packr.New("templates", "../../templates") + + for _, assetPath := range box.List() { if strings.HasPrefix(assetPath, "mail/") { continue } @@ -56,7 +60,7 @@ func NewTemplateFileSystem() templateFileSystem { continue } - content, err := Asset(assetPath) + content, err := box.Find(assetPath) if err != nil { log.Warn("Failed to read embedded %s template. %v", assetPath, err) @@ -144,7 +148,9 @@ func Mailer() *template.Template { templates.Funcs(funcs) } - for _, assetPath := range AssetNames() { + box := packr.New("templates", "../../templates") + + for _, assetPath := range box.List() { if !strings.HasPrefix(assetPath, "mail/") { continue } @@ -153,7 +159,7 @@ func Mailer() *template.Template { continue } - content, err := Asset(assetPath) + content, err := box.Find(assetPath) if err != nil { log.Warn("Failed to read embedded %s template. %v", assetPath, err) diff --git a/modules/templates/templates.go b/modules/templates/templates.go deleted file mode 100644 index 91c8db522894e..0000000000000 --- a/modules/templates/templates.go +++ /dev/null @@ -1,10 +0,0 @@ -// Copyright 2016 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. - -package templates - -//go:generate go-bindata -tags "bindata" -ignore "\\.go" -pkg "templates" -o "bindata.go" ../../templates/... -//go:generate go fmt bindata.go -//go:generate sed -i.bak s/..\/..\/templates\/// bindata.go -//go:generate rm -f bindata.go.bak From 729d566a5b96d72f5ef9ce8c00a82bec8b7b8f71 Mon Sep 17 00:00:00 2001 From: techknowlogick Date: Mon, 4 Feb 2019 02:36:42 -0500 Subject: [PATCH 2/7] Update snapcraft.yaml --- snap/snapcraft.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index aab1c2b05c52f..d1ea4689cfab3 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -65,7 +65,6 @@ parts: build: | export PATH=$SNAPCRAFT_PART_INSTALL/../go/bin/:$SNAPCRAFT_PART_INSTALL/../../go/install/bin:$PATH export GOPATH=$SNAPCRAFT_PART_INSTALL/../go - go get -u github.com/jteeuwen/go-bindata/... cd $GOPATH/src/code.gitea.io/gitea TAGS="bindata sqlite sqlite_unlock_notify pam cert" make generate build install: | From dbe6306974435de3aa88eb3bd7a9af6d08d8c3b1 Mon Sep 17 00:00:00 2001 From: techknowlogick Date: Mon, 4 Feb 2019 16:45:23 -0500 Subject: [PATCH 3/7] Less verbose --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 87b5d010dbce2..983475d49bd8e 100644 --- a/Makefile +++ b/Makefile @@ -102,7 +102,7 @@ packr: @hash packr2 > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ $(GO) get -u github.com/gobuffalo/packr/v2/packr2; \ fi - packr2 -v + packr2 # Add buildtag to all packr files echo "// +build bindata" | cat - packrd/packed-packr.go > packrd/packed-packr.go.bak && mv packrd/packed-packr.go.bak packrd/packed-packr.go echo "// +build bindata" | cat - modules/templates/templates-packr.go > modules/templates/templates-packr.go.bak && mv modules/templates/templates-packr.go.bak modules/templates/templates-packr.go From 02678f80fe41c411a54775cef6657f355a200ae3 Mon Sep 17 00:00:00 2001 From: Matti Ranta Date: Mon, 4 Feb 2019 22:54:50 -0500 Subject: [PATCH 4/7] less open files --- .gitignore | 4 +++- Makefile | 18 +++--------------- modules/options/options.go | 6 ++++++ modules/public/public.go | 7 +++++++ modules/templates/templates.go | 11 +++++++++++ 5 files changed, 30 insertions(+), 16 deletions(-) create mode 100644 modules/templates/templates.go diff --git a/.gitignore b/.gitignore index beec2e6c3b1da..9fa825292671e 100644 --- a/.gitignore +++ b/.gitignore @@ -35,7 +35,9 @@ coverage.all /modules/templates/templates-packr.go /modules/options/options-packr.go /modules/public/public-packr.go -/packrd/* +/modules/public/packrd/* +/modules/options/packrd/* +/modules/templates/packrd/* *.db *.log diff --git a/Makefile b/Makefile index 87b5d010dbce2..03c5742dbc060 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ else endif endif -BINDATA := modules/{options,public,templates}/*-packr.go packrd/* +BINDATA := modules/{options,public,templates}/*-packr.go modules/{options,public,templates}/packrd/* GOFILES := $(shell find . -name "*.go" -type f ! -path "./vendor/*" ! -path "*/*-packr.go") GOFMT ?= gofmt -s @@ -91,23 +91,11 @@ vet: $(GO) vet $(PACKAGES) .PHONY: generate -generate: bindata - $(GO) generate $(PACKAGES) - -.PHONY: bindata -bindata: packr - -.PHONY: packr -packr: +generate: @hash packr2 > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ $(GO) get -u github.com/gobuffalo/packr/v2/packr2; \ fi - packr2 -v - # Add buildtag to all packr files - echo "// +build bindata" | cat - packrd/packed-packr.go > packrd/packed-packr.go.bak && mv packrd/packed-packr.go.bak packrd/packed-packr.go - echo "// +build bindata" | cat - modules/templates/templates-packr.go > modules/templates/templates-packr.go.bak && mv modules/templates/templates-packr.go.bak modules/templates/templates-packr.go - echo "// +build bindata" | cat - modules/options/options-packr.go > modules/options/options-packr.go.bak && mv modules/options/options-packr.go.bak modules/options/options-packr.go - echo "// +build bindata" | cat - modules/public/public-packr.go > modules/public/public-packr.go.bak && mv modules/public/public-packr.go.bak modules/public/public-packr.go + $(GO) generate $(PACKAGES) .PHONY: packr-clean packr-clean: diff --git a/modules/options/options.go b/modules/options/options.go index 6ba3bd6a868ad..bdeb1c69d80d1 100644 --- a/modules/options/options.go +++ b/modules/options/options.go @@ -4,6 +4,12 @@ package options +//go:generate packr2 +//go:generate sh -c "echo '// +build bindata' | cat - packrd/packed-packr.go > packrd/packed-packr.go.bak" +//go:generate sh -c "echo '// +build bindata' | cat - options-packr.go > options-packr.go.bak" +//go:generate sh -c "mv packrd/packed-packr.go.bak packrd/packed-packr.go" +//go:generate sh -c "mv options-packr.go.bak options-packr.go" + type directorySet map[string][]string func (s directorySet) Add(key string, value []string) { diff --git a/modules/public/public.go b/modules/public/public.go index ec280bf1e4dd0..0ecbab2a43d16 100644 --- a/modules/public/public.go +++ b/modules/public/public.go @@ -4,6 +4,13 @@ package public +//go:generate packr2 +//go:generate sh -c "echo '// +build bindata' | cat - packrd/packed-packr.go > packrd/packed-packr.go.bak" +//go:generate sh -c "echo '// +build bindata' | cat - public-packr.go > public-packr.go.bak" +//go:generate sh -c "mv packrd/packed-packr.go.bak packrd/packed-packr.go" +//go:generate sh -c "mv public-packr.go.bak public-packr.go" + + import ( "encoding/base64" "log" diff --git a/modules/templates/templates.go b/modules/templates/templates.go new file mode 100644 index 0000000000000..bb5624d160e39 --- /dev/null +++ b/modules/templates/templates.go @@ -0,0 +1,11 @@ +// Copyright 2016 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. + +package templates + +//go:generate packr2 +//go:generate sh -c "echo '// +build bindata' | cat - packrd/packed-packr.go > packrd/packed-packr.go.bak" +//go:generate sh -c "echo '// +build bindata' | cat - templates-packr.go > templates-packr.go.bak" +//go:generate sh -c "mv packrd/packed-packr.go.bak packrd/packed-packr.go" +//go:generate sh -c "mv templates-packr.go.bak templates-packr.go" From 0ed0ba30f0288bafb7435fe85648e125e720d863 Mon Sep 17 00:00:00 2001 From: Matti Ranta Date: Mon, 4 Feb 2019 23:02:28 -0500 Subject: [PATCH 5/7] fix make vet --- modules/options/options.go | 1 + modules/public/public.go | 1 + modules/templates/templates.go | 1 + 3 files changed, 3 insertions(+) diff --git a/modules/options/options.go b/modules/options/options.go index bdeb1c69d80d1..edf9d67c42f5b 100644 --- a/modules/options/options.go +++ b/modules/options/options.go @@ -6,6 +6,7 @@ package options //go:generate packr2 //go:generate sh -c "echo '// +build bindata' | cat - packrd/packed-packr.go > packrd/packed-packr.go.bak" +//go:generate sh -c "echo 'package packrd' > packrd/packr.go" //go:generate sh -c "echo '// +build bindata' | cat - options-packr.go > options-packr.go.bak" //go:generate sh -c "mv packrd/packed-packr.go.bak packrd/packed-packr.go" //go:generate sh -c "mv options-packr.go.bak options-packr.go" diff --git a/modules/public/public.go b/modules/public/public.go index 0ecbab2a43d16..679ae2ebc829e 100644 --- a/modules/public/public.go +++ b/modules/public/public.go @@ -6,6 +6,7 @@ package public //go:generate packr2 //go:generate sh -c "echo '// +build bindata' | cat - packrd/packed-packr.go > packrd/packed-packr.go.bak" +//go:generate sh -c "echo 'package packrd' > packrd/packr.go" //go:generate sh -c "echo '// +build bindata' | cat - public-packr.go > public-packr.go.bak" //go:generate sh -c "mv packrd/packed-packr.go.bak packrd/packed-packr.go" //go:generate sh -c "mv public-packr.go.bak public-packr.go" diff --git a/modules/templates/templates.go b/modules/templates/templates.go index bb5624d160e39..43ba6c0e97702 100644 --- a/modules/templates/templates.go +++ b/modules/templates/templates.go @@ -6,6 +6,7 @@ package templates //go:generate packr2 //go:generate sh -c "echo '// +build bindata' | cat - packrd/packed-packr.go > packrd/packed-packr.go.bak" +//go:generate sh -c "echo 'package packrd' > packrd/packr.go" //go:generate sh -c "echo '// +build bindata' | cat - templates-packr.go > templates-packr.go.bak" //go:generate sh -c "mv packrd/packed-packr.go.bak packrd/packed-packr.go" //go:generate sh -c "mv templates-packr.go.bak templates-packr.go" From 7185b6a1fd80b11b3bd74bcdc244a754c996c791 Mon Sep 17 00:00:00 2001 From: Matti Ranta Date: Mon, 4 Feb 2019 23:06:30 -0500 Subject: [PATCH 6/7] make fmt --- modules/public/public.go | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/public/public.go b/modules/public/public.go index 679ae2ebc829e..dda459c0a8719 100644 --- a/modules/public/public.go +++ b/modules/public/public.go @@ -11,7 +11,6 @@ package public //go:generate sh -c "mv packrd/packed-packr.go.bak packrd/packed-packr.go" //go:generate sh -c "mv public-packr.go.bak public-packr.go" - import ( "encoding/base64" "log" From f0085d417ebec116cf1f9b27b69ee7b724e4548c Mon Sep 17 00:00:00 2001 From: Matti Ranta Date: Mon, 4 Feb 2019 23:11:02 -0500 Subject: [PATCH 7/7] add docs --- docs/content/doc/installation/from-source.en-us.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/content/doc/installation/from-source.en-us.md b/docs/content/doc/installation/from-source.en-us.md index f59e9681ef577..98dd9a81d6850 100644 --- a/docs/content/doc/installation/from-source.en-us.md +++ b/docs/content/doc/installation/from-source.en-us.md @@ -65,7 +65,9 @@ provided to keep the build process as simple as possible. See here how to get Make. Depending on requirements, the following build tags can be included. -* `bindata`: Build a single monolithic binary, with all assets included. +* `bindata`: Build a single monolithic binary, with all assets included. This uses + [packr2](https://github.com/gobuffalo/packr) to embed static assets from options, templates, and public directory + in the golang binary. Note: `make generate` must be first used before building to embed assets. * `sqlite sqlite_unlock_notify`: Enable support for a [SQLite3](https://sqlite.org/) database. Suggested only for tiny installations. * `pam`: Enable support for PAM (Linux Pluggable Authentication Modules). Can be used to