Skip to content
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

Generate Bindata iff TAGS="bindata" and not up-to-date #10004

Merged
merged 12 commits into from
Jan 27, 2020
Merged
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ _testmain.go
coverage.all

/modules/options/bindata.go
/modules/options/bindata.go.hash
/modules/public/bindata.go
/modules/public/bindata.go.hash
/modules/templates/bindata.go
/modules/templates/bindata.go.hash

*.db
*.log
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ vet:

.PHONY: generate
generate: fomantic js css
GO111MODULE=on $(GO) generate -mod=vendor $(PACKAGES)
GO111MODULE=on $(GO) generate -mod=vendor -tags '$(TAGS)' $(PACKAGES)
zeripath marked this conversation as resolved.
Show resolved Hide resolved

.PHONY: generate-swagger
generate-swagger:
Expand Down
23 changes: 0 additions & 23 deletions modules/options/main.go

This file was deleted.

2 changes: 0 additions & 2 deletions modules/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

package options

//go:generate go run -mod=vendor main.go

type directorySet map[string][]string

func (s directorySet) Add(key string, value []string) {
Expand Down
9 changes: 9 additions & 0 deletions modules/options/options_bindata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// 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
23 changes: 0 additions & 23 deletions modules/public/main.go

This file was deleted.

2 changes: 0 additions & 2 deletions modules/public/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import (
"gitea.com/macaron/macaron"
)

//go:generate go run -mod=vendor main.go

// Options represents the available options to configure the macaron handler.
type Options struct {
Directory string
Expand Down
9 changes: 9 additions & 0 deletions modules/public/public_bindata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// 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
23 changes: 0 additions & 23 deletions modules/templates/main.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

//+build bindata

package templates

//go:generate go run -mod=vendor main.go
//go:generate go run -mod=vendor ../../scripts/generate-bindata.go ../../templates templates bindata.go
77 changes: 77 additions & 0 deletions scripts/generate-bindata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// 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"
"fmt"
"hash/adler32"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strconv"

"github.com/shurcooL/vfsgen"
)

func needsUpdate(dir string, filename string) bool {
oldHash, err := ioutil.ReadFile(filename + ".hash")
if err != nil {
oldHash = []byte{}
}

adlerHash := adler32.New()

err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, we're basically doing make's job. 😆

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup. That's what you get when you mix two competing build systems ...

if err != nil {
return err
}
_, _ = adlerHash.Write([]byte(info.Name()))
_, _ = adlerHash.Write([]byte(info.ModTime().String()))
_, _ = adlerHash.Write([]byte(strconv.FormatInt(info.Size(), 16)))
return nil
})
if err != nil {
return true
}

newHash := adlerHash.Sum([]byte{})

if bytes.Compare(oldHash, newHash) != 0 {
_ = ioutil.WriteFile(filename+".hash", newHash, 0666)
zeripath marked this conversation as resolved.
Show resolved Hide resolved
return true
}

return false
}

func main() {
if len(os.Args) < 4 {
zeripath marked this conversation as resolved.
Show resolved Hide resolved
log.Fatal("Insufficient number of arguments. Need: directory packageName filename")
}

dir, packageName, filename := os.Args[1], os.Args[2], os.Args[3]

if !needsUpdate(dir, filename) {
fmt.Printf("bindata for %s up-to-date refusing to generate\n", packageName)
zeripath marked this conversation as resolved.
Show resolved Hide resolved
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)
}
}