Skip to content

switch to use packr to embed static assets #5952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 12 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 \
Expand All @@ -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 \
Expand Down
4 changes: 3 additions & 1 deletion docs/content/doc/installation/from-source.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 6 additions & 4 deletions modules/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
23 changes: 18 additions & 5 deletions modules/options/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -27,6 +30,7 @@ func Dir(name string) ([]string, error) {

var (
result []string
files []string
)

customDir := path.Join(setting.CustomPath, "options", name)
Expand All @@ -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...)

Expand Down Expand Up @@ -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)
}
13 changes: 8 additions & 5 deletions modules/public/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
11 changes: 3 additions & 8 deletions modules/public/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("")
Expand Down
14 changes: 10 additions & 4 deletions modules/templates/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
Expand Down
10 changes: 6 additions & 4 deletions modules/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
1 change: 0 additions & 1 deletion snap/snapcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down