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

Commit

Permalink
allows for buffalo new and buffalo build to work with apps using go m…
Browse files Browse the repository at this point in the history
…odules (#1237)

* allows for buffalo new and buffalo build to work with apps using go
modules

* removed unused method

* allow creation of buffalo apps outside of gopath when using modules

* adds a message about go modules support being enabled

* fixed issue with breaking GOPATH new apps

* updated deps

* mod tidy

* fixed linter issue
  • Loading branch information
markbates authored Aug 22, 2018
1 parent 433568f commit a1138ed
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 73 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ install: deps
packr clean

test:
$(GO_BIN) test -vet off -tags ${TAGS} ./...
$(GO_BIN) test -tags ${TAGS} ./...

ci-test:
$(GO_BIN) test -vet off -tags ${TAGS} -race -v ./...
$(GO_BIN) test -tags ${TAGS} -race -v ./...
docker build .

lint:
Expand Down
34 changes: 4 additions & 30 deletions buffalo/cmd/build/bin.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,13 @@
package build

import (
"bytes"
"fmt"
"os"
"os/exec"
"regexp"
"strings"
"time"

"github.com/gobuffalo/envy"
"github.com/pkg/errors"
)

func version() (string, string) {
_, err := exec.LookPath("git")
buildTime := fmt.Sprintf("\"%s\"", time.Now().Format(time.RFC3339))
version := buildTime
if err == nil {
cmd := exec.Command("git", "rev-parse", "--short", "HEAD")
out := &bytes.Buffer{}
cmd.Stdout = out
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
err = cmd.Run()
if err == nil && out.String() != "" {
version = strings.TrimSpace(out.String())
}
}
return version, buildTime
}

func (b *Builder) buildBin() error {
buildArgs := []string{"build", "-i"}
if b.Debug {
Expand All @@ -42,11 +19,7 @@ func (b *Builder) buildBin() error {

buildArgs = append(buildArgs, "-o", b.Bin)

version, buildTime := version()
flags := []string{
fmt.Sprintf("-X main.BuildVersion=%s", version),
fmt.Sprintf("-X main.BuildTime=%s", buildTime),
}
flags := []string{}

if b.Static {
flags = append(flags, "-linkmode external", "-extldflags \"-static\"")
Expand All @@ -62,8 +35,9 @@ func (b *Builder) buildBin() error {
}
flags = append(flags, b.LDFlags)
}

buildArgs = append(buildArgs, "-ldflags", strings.Join(flags, " "))
if len(flags) > 0 {
buildArgs = append(buildArgs, "-ldflags", strings.Join(flags, " "))
}

return b.exec(envy.Get("GO_BIN", "go"), buildArgs...)
}
9 changes: 2 additions & 7 deletions buffalo/cmd/build/templates/main.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"os"
"os/exec"
"time"
"strings"

"github.com/markbates/grift/grift"
"github.com/gobuffalo/buffalo/runtime"
Expand All @@ -22,17 +21,13 @@ import (
<% } %>
)

var BuildVersion = ""
var BuildTime = ""

func init() {
BuildTime = strings.Trim(BuildTime, "\"")
t, err := time.Parse(time.RFC3339, BuildTime)
t, err := time.Parse(time.RFC3339, "<%= buildTime %>")
if err != nil {
fmt.Println(err)
}
runtime.SetBuild(runtime.BuildInfo{
Version: BuildVersion,
Version: "<%= buildVersion %>",
Time: t,
})
}
Expand Down
24 changes: 24 additions & 0 deletions buffalo/cmd/build/transform_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import (
"bytes"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"time"

"github.com/gobuffalo/plush"
"github.com/pkg/errors"
Expand All @@ -24,10 +27,31 @@ func (b *Builder) transformMain() error {
})
}

func (b *Builder) buildVersion(version string) string {
_, err := exec.LookPath("git")
if err != nil {
return version
}
cmd := exec.Command("git", "rev-parse", "--short", "HEAD")
out := &bytes.Buffer{}
cmd.Stdout = out
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
err = cmd.Run()
if err == nil && out.String() != "" {
version = strings.TrimSpace(out.String())
}
return version
}

func (b *Builder) createBuildMain() error {
ctx := plush.NewContext()
ctx.Set("opts", b.Options)

bt := time.Now().Format(time.RFC3339)
ctx.Set("buildTime", bt)
ctx.Set("buildVersion", b.buildVersion(bt))

t, err := templates.MustString("main.go.tmpl")
if err != nil {
return errors.WithStack(err)
Expand Down
3 changes: 3 additions & 0 deletions generators/newapp/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ func (g Generator) Validate() error {
}

func (g Generator) validateInGoPath() error {
if g.App.WithModules {
return nil
}
gpMultiple := envy.GoPaths()

larp := strings.ToLower(meta.ResolveSymlinks(filepath.Dir(g.Root)))
Expand Down
18 changes: 18 additions & 0 deletions generators/newapp/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ func (a Generator) Run(root string, data makr.Data) error {
return errors.WithStack(err)
}

if a.WithModules {
g.Add(makr.NewCommand(exec.Command(envy.Get("GO_BIN", "go"), "mod", "init", a.PackagePkg)))
}

g.Add(makr.NewCommand(a.goGet()))

g.Add(makr.Func{
Expand Down Expand Up @@ -204,9 +208,15 @@ func (a Generator) goGet() *exec.Cmd {
cd, _ := os.Getwd()
defer os.Chdir(cd)
os.Chdir(a.Root)

if a.WithDep {
return exec.Command("dep", "ensure", "-v")
}

if a.WithModules {
return a.goGetMod()
}

appArgs := []string{"get", "-t"}
if a.Verbose {
appArgs = append(appArgs, "-v")
Expand All @@ -215,6 +225,14 @@ func (a Generator) goGet() *exec.Cmd {
return exec.Command(envy.Get("GO_BIN", "go"), appArgs...)
}

func (a Generator) goGetMod() *exec.Cmd {
cmd := exec.Command(envy.Get("GO_BIN", "go"), "mod", "tidy")
if a.Verbose {
cmd.Args = append(cmd.Args, "-v")
}
return cmd
}

const nTravis = `language: go
go:
Expand Down
2 changes: 1 addition & 1 deletion generators/soda/soda.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (sd Generator) Run(root string, data makr.Data) error {
f.Should = should
g.Add(f)

if !sd.App.WithDep {
if !sd.App.WithModules {
c := makr.NewCommand(makr.GoGet("github.com/gobuffalo/pop/..."))
c.Should = should
g.Add(c)
Expand Down
16 changes: 11 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,38 @@ require (
github.com/dustin/go-humanize v0.0.0-20180713052910-9f541cc9db5d
github.com/fatih/color v1.7.0
github.com/gobuffalo/buffalo-plugins v0.0.0-20180820141205-03ceb944b112
github.com/gobuffalo/buffalo-pop v0.0.0-20180818124224-e3bbd0b11d3e
github.com/gobuffalo/buffalo-pop v0.0.0-20180818124224-e3bbd0b11d3e // indirect
github.com/gobuffalo/envy v1.6.4
github.com/gobuffalo/fizz v1.0.9 // indirect
github.com/gobuffalo/fizz v1.0.10 // indirect
github.com/gobuffalo/github_flavored_markdown v1.0.0
github.com/gobuffalo/makr v1.1.2
github.com/gobuffalo/makr v1.1.4
github.com/gobuffalo/mw-basicauth v0.0.0-20180802152105-15d67c4ac152
github.com/gobuffalo/mw-contenttype v0.0.0-20180802152300-74f5a47f4d56
github.com/gobuffalo/mw-csrf v0.0.0-20180802151833-446ff26e108b
github.com/gobuffalo/mw-forcessl v0.0.0-20180802152810-73921ae7a130
github.com/gobuffalo/mw-i18n v0.0.0-20180802152014-e3060b7e13d6
github.com/gobuffalo/mw-paramlogger v0.0.0-20180807082017-6b90b69a724a
github.com/gobuffalo/mw-poptx v0.0.0-20180814214442-e78b7f0a596a
github.com/gobuffalo/mw-tokenauth v0.0.0-20180802152212-d09751da96e0
github.com/gobuffalo/packr v1.13.2
github.com/gobuffalo/plush v3.7.16+incompatible
github.com/gobuffalo/pop v4.6.7+incompatible
github.com/gobuffalo/pop v4.6.8+incompatible
github.com/gobuffalo/shoulders v0.0.0-20180815165021-5569039eac2f
github.com/gobuffalo/tags v2.0.9+incompatible
github.com/gobuffalo/uuid v2.0.3+incompatible
github.com/gobuffalo/x v0.0.0-20180816203319-dc54d929c4a2
github.com/golang/protobuf v1.2.0 // indirect
github.com/gorilla/context v1.1.1
github.com/gorilla/mux v1.6.2
github.com/gorilla/sessions v1.1.1
github.com/lib/pq v0.0.0-20180822103908-55f6f2718ccb // indirect
github.com/markbates/deplist v1.0.2
github.com/markbates/grift v1.0.1
github.com/markbates/inflect v1.0.0
github.com/markbates/refresh v1.4.2
github.com/markbates/sigtx v1.0.0
github.com/markbates/willie v1.0.7
github.com/microcosm-cc/bluemonday v1.0.1 // indirect
github.com/monoculum/formam v0.0.0-20180818005819-0a5cdaa81e2e
github.com/pkg/errors v0.8.0
github.com/sirupsen/logrus v1.0.6
Expand All @@ -42,6 +46,8 @@ require (
github.com/spf13/viper v1.1.0
github.com/stretchr/testify v1.2.2
golang.org/x/crypto v0.0.0-20180820150726-614d502a4dac
golang.org/x/net v0.0.0-20180821023952-922f4815f713 // indirect
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f
golang.org/x/sys v0.0.0-20180821140842-3b58ed4ad339 // indirect
gopkg.in/mail.v2 v2.0.0-20180731213649-a0242b2233b4
)
)
Loading

0 comments on commit a1138ed

Please sign in to comment.