This repository has been archived by the owner on Feb 24, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 578
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow the buffalo binary to use the version vendored with dep (#1023)
* 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
1 parent
2d3ae56
commit bad0990
Showing
14 changed files
with
292 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[{ | ||
"path": "output.txt", | ||
"contains": [ | ||
"msg=\"Buffalo version is: v" | ||
] | ||
}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.