Skip to content

Make bindata.go optional, remove from repository #172

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 1 commit 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
Make bindata.go optional, remove from repository
By default will read assets from filesystems, but can embed them
by passing the "bindata" build tag (go build -tags bindata)

Fixes #77

Print a WARNING when bindata is not embedded, and
honours GITEA_WORK_DIR, when given, for finding filesystem assets.

Include automatic test for the dynamic "bin"data library
  • Loading branch information
strk committed Dec 3, 2016
commit c155be70f4c9d162f18e1858363a1065dccf494f
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
*.a
*.so

# Generated files
modules/bindata/bindata.go

# Folders
_obj
_test
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ all: build
clean:
go clean -i ./...
rm -rf $(EXECUTABLE) $(DIST)
rm -rf ./modules/bindata/bindata.go

.PHONY: fmt
fmt:
Expand Down Expand Up @@ -116,7 +117,7 @@ modules/bindata/bindata.go: $(BINDATA)
@which go-bindata > /dev/null; if [ $$? -ne 0 ]; then \
go get -u github.com/jteeuwen/go-bindata/...; \
fi
go-bindata -o=$@ -ignore="\\.go|README.md|TRANSLATORS" -pkg=bindata conf/...
go-bindata -tags bindata -o=$@ -ignore="\\.go|README.md|TRANSLATORS" -pkg=bindata conf/...
go fmt $@
sed -i.bak 's/confLocaleLocale_/confLocaleLocale/' $@
rm [email protected]
Expand Down
5,330 changes: 0 additions & 5,330 deletions modules/bindata/bindata.go

This file was deleted.

144 changes: 144 additions & 0 deletions modules/bindata/dynamic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// +build !bindata

package bindata

import (
"fmt"
"os"
"path/filepath"
"io/ioutil"
"strings"
)

var rootDir string
var binData map[string] []byte
var dirData map[string] []string

func setBinData(key string, data []byte) {
if binData == nil {
binData = make(map[string] []byte)
}
binData[key] = data
}

func setDirData(key string, data []string) {
if dirData == nil {
dirData = make(map[string] []string)
}
dirData[key] = data
}

func getRootDir() (string, error) {
if rootDir != "" {
return rootDir, nil
}

dir := os.Getenv("GITEA_WORK_DIR")
if dir == "" {
dir = "."
}

dir, err := filepath.Abs(dir)
if err != nil {
return "", fmt.Errorf("%v", err)
}
for {
_, err = ioutil.ReadDir(filepath.Join(dir, "conf"))
if err == nil {
// TODO: check if the file is a directory
break
}
// TODO: check that the error is a "file not found" error ?
newdir := filepath.Join(dir, "..")
if dir == newdir {
return "", fmt.Errorf("Could not find directory containing 'conf', try setting GITEA_WORK_DIR")
}
dir = newdir
}

fmt.Println("WARNING: this deveopment build of Gitea depends on a directory tree, we'll be using the one in ", dir)

rootDir = dir
return dir, nil
}

func resolveName(name string) (string, error) {

name = strings.Replace(name, "\\", "/", -1) // needed ?

dir, err := getRootDir()
if err != nil {
return "", fmt.Errorf("%v", err)
}

return filepath.Join(dir,name), nil
}

// MustAsset is like Asset but panics when Asset would return an error.
// It simplifies safe initialization of global variables.
func MustAsset(name string) []byte {
a, err := Asset(name)
if err != nil {
panic("asset: Asset(" + name + "): " + err.Error())
}

return a
}

// Asset loads and returns the asset for the given name.
// It returns an error if the asset could not be found or
// could not be loaded.
func Asset(name string) ([]byte, error) {

canonicalName, err := resolveName(name)
if err != nil {
return nil, fmt.Errorf("Asset %s can't read by error: %v", name, err)
}

dat := binData[canonicalName]
if dat != nil { return dat, nil }

dat, err = ioutil.ReadFile(canonicalName)
if err != nil {
return nil, fmt.Errorf("Asset %s can't read by error: %v", name, err)
}

setBinData(canonicalName, dat)
return dat, nil
}

// AssetDir returns the file names below a certain
// directory embedded in the file by go-bindata.
// For example if you run go-bindata on data/... and data contains the
// following hierarchy:
// data/
// foo.txt
// img/
// a.png
// b.png
// then AssetDir("data") would return []string{"foo.txt", "img"}
// AssetDir("data/img") would return []string{"a.png", "b.png"}
// AssetDir("foo.txt") and AssetDir("notexist") would return an error
// AssetDir("") will return []string{"data"}.
func AssetDir(name string) ([]string, error) {
canonicalName, err := resolveName(name)
if err != nil {
return nil, fmt.Errorf("Asset %s can't read by error: %v", name, err)
}

rv := dirData[canonicalName]
if rv != nil { return rv, nil }

files, err := ioutil.ReadDir(canonicalName)
if err != nil {
return nil, fmt.Errorf("Error reading directory %s: ", err)
}

rv = make([]string, 0, len(files))
for _, f := range files {
rv = append(rv, f.Name())
}

setDirData(canonicalName, rv)
return rv, nil
}
24 changes: 24 additions & 0 deletions modules/bindata/dynamic_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package bindata

import (
"testing"
"io/ioutil"
)

func Test_resolveName(t *testing.T) {

files := []string {
"modules/bindata/dynamic_test.go",
"conf/app.ini",
"templates/home.tmpl",
}

for _, c := range files {
name, _ := resolveName(c)
_, err := ioutil.ReadFile(name)
if err != nil {
t.Errorf("%s: %v", name, err)
}
}

}