Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
Allow the buffalo binary to use the version vendored with dep (#1023)
Browse files Browse the repository at this point in the history
* wip

* Vendor the Buffalo Binary inside Applications fixes #986

* added some docs for code climate

* log packing if in debug mode

* fixed weird import

* Update vbuffalo.go

* fixed issue with gometalinter

* adding some filetests to check the buffalo-version output (#1028)

* adding some filetests to check the buffalo-version output

* making it version independent

* removing unneeded newline from broken filetest
  • Loading branch information
markbates authored and stanislas-m committed May 12, 2018
1 parent 2d3ae56 commit bad0990
Show file tree
Hide file tree
Showing 14 changed files with 292 additions and 11 deletions.
25 changes: 21 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ RUN buffalo version

RUN go get -u github.com/alecthomas/gometalinter
RUN gometalinter --install

RUN go get -v -u github.com/markbates/filetest

RUN go get -v -u github.com/gobuffalo/makr
RUN go get -v -u github.com/markbates/grift
RUN go get -v -u github.com/markbates/inflect
RUN go get -v -u github.com/markbates/refresh
RUN go get -v -u github.com/gobuffalo/tags
RUN go get -v -u github.com/gobuffalo/pop
RUN go get -v -u github.com/mattn/go-sqlite3

RUN go get -v -u github.com/markbates/grift
RUN go get -v -u github.com/markbates/inflect
RUN go get -v -u github.com/markbates/refresh
RUN go get -v -u github.com/markbates/willie

ENV BP=$GOPATH/src/github.com/gobuffalo/buffalo

RUN rm $(which buffalo)
Expand All @@ -22,7 +26,6 @@ WORKDIR $BP
ADD . .

RUN go get -v -t ./...

RUN go install -v -tags sqlite ./buffalo

RUN go test -tags sqlite -race ./...
Expand Down Expand Up @@ -123,3 +126,17 @@ RUN filetest -c $GOPATH/src/github.com/gobuffalo/buffalo/buffalo/cmd/filetests/g
RUN rm -rf bin
RUN buffalo build -k -e
RUN filetest -c $GOPATH/src/github.com/gobuffalo/buffalo/buffalo/cmd/filetests/no_assets_build.json

# Vendored buffalo version.
WORKDIR $GOPATH/src
RUN buffalo new app -f --api --with-dep
WORKDIR $GOPATH/src/app
RUN buffalo version > output.txt 2>&1
RUN filetest -c $GOPATH/src/github.com/gobuffalo/buffalo/buffalo/cmd/filetests/version-dep.json

# Non-Vendored buffalo version.
WORKDIR $GOPATH/src
RUN buffalo new app -f --api
WORKDIR $GOPATH/src/app
RUN buffalo version > output.txt 2>&1
RUN filetest -c $GOPATH/src/github.com/gobuffalo/buffalo/buffalo/cmd/filetests/version-no-dep.json
2 changes: 2 additions & 0 deletions buffalo/cmd/build/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ func (b *Builder) buildAssets() error {

if !b.Options.WithAssets {
p.IgnoredBoxes = append(p.IgnoredBoxes, "../public/assets")
} else {
p.IgnoredFolders = p.IgnoredFolders[1:]
}

if b.ExtractAssets && b.Options.WithAssets {
Expand Down
6 changes: 6 additions & 0 deletions buffalo/cmd/build/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ func (b *Builder) Run() error {
defer b.Cleanup()
logrus.Debug(b.Options)

if b.Debug {
builder.DebugLog = func(m string, a ...interface{}) {
logrus.Debugf(m, a...)
}
}

for _, s := range b.steps {
err := s()
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion buffalo/cmd/build/templates/main.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"

"github.com/markbates/grift/grift"
"github.com/gobuffalo/buffalo/buffalo/cmd"
_ "<%= opts.PackagePkg %>/a"
_ "<%= opts.ActionsPkg %>"
<%= if (opts.WithPop) { %>
Expand Down Expand Up @@ -47,7 +48,7 @@ func main() {
log.Fatal(err)
}
default:
originalMain()
cmd.Execute()
}
}

Expand Down
6 changes: 6 additions & 0 deletions buffalo/cmd/filetests/version-dep.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[{
"path": "output.txt",
"contains": [
"msg=\"Buffalo version is: v"
]
}]
9 changes: 9 additions & 0 deletions buffalo/cmd/filetests/version-no-dep.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[{
"path": "output.txt",
"contains": [
"msg=\"Buffalo version is: development"
],
"!contains": [
"msg=\"Buffalo version is: v"
]
}]
7 changes: 7 additions & 0 deletions buffalo/cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ func execIfExists(infoCmd infoCommand) error {
bb := os.Stdout
bb.WriteString(infoCmd.InfoLabel)

if infoCmd.Name == "dep" {
if _, err := os.Stat("Gopkg.toml"); err != nil {
bb.WriteString("could not find a Gopkg.toml file\n")
return nil
}
}

if _, err := exec.LookPath(infoCmd.PathName); err != nil {
bb.WriteString(fmt.Sprintf("%s Not Found\n", infoCmd.Name))
return nil
Expand Down
5 changes: 4 additions & 1 deletion buffalo/cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ var newCmd = &cobra.Command{
app.WithWebpack = false
}

if err := app.Run(app.Root, makr.Data{}); err != nil {
data := makr.Data{
"version": Version,
}
if err := app.Run(app.Root, data); err != nil {
return errors.WithStack(err)
}

Expand Down
15 changes: 13 additions & 2 deletions buffalo/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
package main

import "github.com/gobuffalo/buffalo/buffalo/cmd"
import (
"log"

"github.com/gobuffalo/buffalo/buffalo/cmd"
"github.com/gobuffalo/buffalo/internal/vbuffalo"
)

func main() {
cmd.Execute()
err := vbuffalo.Execute(func() error {
cmd.Execute()
return nil
})
if err != nil {
log.Fatal(err)
}
}
25 changes: 24 additions & 1 deletion generators/newapp/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ func (a Generator) Run(root string, data makr.Data) error {
}

if a.WithDep {
data["addPrune"] = true
g.Add(makr.NewFile("Gopkg.toml", GopkgTomlTmpl))
if _, err := exec.LookPath("dep"); err != nil {
g.Add(makr.NewCommand(makr.GoGet("github.com/golang/dep/cmd/dep", "-u")))
}
Expand Down Expand Up @@ -199,7 +201,7 @@ func (a Generator) goGet() *exec.Cmd {
os.Chdir(a.Root)
if a.WithDep {
if _, err := exec.LookPath("dep"); err == nil {
return exec.Command("dep", "init")
return exec.Command("dep", "ensure", "-v")
}
}
appArgs := []string{"get", "-t"}
Expand Down Expand Up @@ -361,3 +363,24 @@ public/assets/
.grifter/
.env
`

// GopkgTomlTmpl is the default dep Gopkg.toml
const GopkgTomlTmpl = `
{{ if .addPrune }}
[prune]
go-tests = true
unused-packages = true
{{ end }}
# DO NOT DELETE
[[prune.project]] # buffalo
name = "github.com/gobuffalo/buffalo"
unused-packages = false
{{ if .opts.WithPop }}
# DO NOT DELETE
[[prune.project]] # pop
name = "github.com/gobuffalo/pop"
unused-packages = false
{{ end }}
`
40 changes: 40 additions & 0 deletions internal/vbuffalo/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package vbuffalo

import (
"os"

"github.com/gobuffalo/buffalo/buffalo/cmd"
"github.com/gobuffalo/plush"
"github.com/pkg/errors"
)

func writeMain() error {
f, err := os.Create(mainPath)
if err != nil {
return errors.WithStack(err)
}
defer f.Close()
s, err := plush.Render(mainTemplate, plush.NewContextWith(map[string]interface{}{
"app": app,
"cmdPkg": cmdPkg,
"version": cmd.Version,
}))
if err != nil {
return errors.WithStack(err)
}
_, err = f.WriteString(s)
return err
}

const mainTemplate = `package main
import (
"fmt"
"<%= cmdPkg %>"
)
func main() {
fmt.Printf("%s [<%= version %>]\n\n", cmd.Version)
cmd.Execute()
}
`
60 changes: 60 additions & 0 deletions internal/vbuffalo/dep.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package vbuffalo

import (
"bytes"
"io/ioutil"
"os"
"os/exec"
"path/filepath"

"html/template"

"github.com/gobuffalo/buffalo/generators/newapp"
"github.com/pkg/errors"
)

func depEnsure() error {
toml := filepath.Join(pwd, "Gopkg.toml")
b, err := ioutil.ReadFile(toml)
if err != nil {
return errors.WithStack(err)
}

addPrune := !bytes.Contains(b, []byte("[prune]"))

make := func() error {
f, err := os.Create(toml)
if err != nil {
return errors.WithStack(err)
}
defer f.Close()
f.Write(b)

t, err := template.New("toml template").Parse(newapp.GopkgTomlTmpl)
if err != nil {
return errors.WithStack(err)
}

err = t.Execute(f, map[string]interface{}{
"opts": app,
"addPrune": addPrune,
})
if err != nil {
return errors.WithStack(err)
}

return run(exec.Command("dep", "ensure", "-v"))
}

if addPrune {
if err := make(); err != nil {
return errors.WithStack(err)
}
}
if !bytes.Contains(b, []byte("[[prune.project]] # buffalo")) {
if err := make(); err != nil {
return errors.WithStack(err)
}
}
return nil
}
90 changes: 90 additions & 0 deletions internal/vbuffalo/vbuffalo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package vbuffalo

import (
"os"
"os/exec"
"path/filepath"
"runtime"

"github.com/gobuffalo/buffalo/meta"
"github.com/pkg/errors"
)

const cmdPkg = "github.com/gobuffalo/buffalo/buffalo/cmd"

var pwd, _ = os.Getwd()
var mainPath = filepath.Join(pwd, ".grifter", "main.go")
var binPath = filepath.Join(pwd, "bin", "vbuffalo")
var app meta.App

func init() {
if runtime.GOOS == "windows" {
binPath += ".exe"
}
}

// Execute using vbuffalo. If this doesn't meet the vbuffalo
// requirements then it should use the passed in function instead.
func Execute(ex func() error) error {
if !exists(".buffalo.dev.yml") {
return ex()
}
app = meta.New(".")
// not using dep or there isn't a vendor folder
if !app.WithDep || !exists("vendor") {
return ex()
}
return execute()
}

func execute() error {
dir := filepath.Dir(mainPath)
err := os.MkdirAll(dir, 0755)
if err != nil {
return errors.WithStack(err)
}
defer os.RemoveAll(dir)

if err := depEnsure(); err != nil {
return errors.WithStack(err)
}

if err = writeMain(); err != nil {
return errors.WithStack(err)
}

err = cd(filepath.Dir(mainPath), func() error {
args := []string{"build", "-v"}
if app.WithSQLite {
args = append(args, "--tags", "sqlite")
}
args = append(args, "-o", binPath)
cmd := exec.Command("go", args...)
return run(cmd)
})
if err != nil {
return errors.WithStack(err)
}

cmd := exec.Command(binPath, os.Args[1:]...)
return run(cmd)
}

func cd(dir string, fn func() error) error {
defer os.Chdir(pwd)
os.Chdir(dir)
return fn()
}

func run(cmd *exec.Cmd) error {
// fmt.Println(strings.Join(cmd.Args, " "))
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}

func exists(f string) bool {
_, err := os.Stat(f)
return err == nil
}
Loading

0 comments on commit bad0990

Please sign in to comment.