diff --git a/.gitignore b/.gitignore index 2fe0134f7d884..9bbccc9802005 100644 --- a/.gitignore +++ b/.gitignore @@ -32,9 +32,12 @@ _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 +/modules/public/packrd/* +/modules/options/packrd/* +/modules/templates/packrd/* *.db *.log diff --git a/Makefile b/Makefile index 01e4a13f15f3f..293301e713aa1 100644 --- a/Makefile +++ b/Makefile @@ -16,8 +16,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 modules/{options,public,templates}/packrd/* +GOFILES := $(shell find . -name "*.go" -type f ! -path "./vendor/*" ! -path "*/*-packr.go") GOFMT ?= gofmt -s GOFLAGS := -i -v @@ -76,7 +76,7 @@ all: build include docker/Makefile .PHONY: clean -clean: +clean: packr-clean $(GO) clean -i ./... rm -rf $(EXECUTABLE) $(DIST) $(BINDATA) \ integrations*.test \ @@ -94,11 +94,18 @@ vet: .PHONY: generate generate: - @hash go-bindata > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ - $(GO) get -u github.com/jteeuwen/go-bindata/go-bindata; \ + @hash packr2 > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ + $(GO) get -u github.com/gobuffalo/packr/v2/packr2; \ fi $(GO) generate $(PACKAGES) +.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/docs/content/doc/installation/from-source.en-us.md b/docs/content/doc/installation/from-source.en-us.md index 5806b0bfcc452..d7ac38b017282 100644 --- a/docs/content/doc/installation/from-source.en-us.md +++ b/docs/content/doc/installation/from-source.en-us.md @@ -83,7 +83,9 @@ are provided to keep the build process as simple as possible. 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. diff --git a/modules/options/options.go b/modules/options/options.go index 4f0c69b335582..edf9d67c42f5b 100644 --- a/modules/options/options.go +++ b/modules/options/options.go @@ -4,10 +4,12 @@ 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 +//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" type directorySet map[string][]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..dda459c0a8719 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 '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" + import ( "encoding/base64" "log" @@ -14,14 +21,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 index 91c8db522894e..43ba6c0e97702 100644 --- a/modules/templates/templates.go +++ b/modules/templates/templates.go @@ -4,7 +4,9 @@ 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 +//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" 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: |