diff --git a/buffalo/cmd/build/apkg.go b/buffalo/cmd/build/apkg.go index 822704920..2637ddae9 100644 --- a/buffalo/cmd/build/apkg.go +++ b/buffalo/cmd/build/apkg.go @@ -99,7 +99,6 @@ func (b *Builder) buildADatabase() error { return errors.WithStack(err) } if bytes.Contains(bb.Bytes(), []byte("sqlite")) { - b.Tags = append(b.Tags, "sqlite") if !b.Static { logrus.Debug("you are building a SQLite application, please consider using the `--static` flag to compile a static binary") } diff --git a/buffalo/cmd/build/bin.go b/buffalo/cmd/build/bin.go index 5eedddae3..60b5384ef 100644 --- a/buffalo/cmd/build/bin.go +++ b/buffalo/cmd/build/bin.go @@ -36,8 +36,9 @@ func (b *Builder) buildBin() error { if b.Debug { buildArgs = append(buildArgs, "-v") } - b.Tags = append(b.Tags, b.Environment) - buildArgs = append(buildArgs, "-tags", strings.Join(b.Tags, " ")) + + tf := b.App.BuildTags(b.Environment, b.Tags...) + buildArgs = append(buildArgs, "-tags", tf.String()) buildArgs = append(buildArgs, "-o", b.Bin) diff --git a/buffalo/cmd/dev.go b/buffalo/cmd/dev.go index d4fb9b3a9..ef90db260 100644 --- a/buffalo/cmd/dev.go +++ b/buffalo/cmd/dev.go @@ -1,9 +1,7 @@ package cmd import ( - "bytes" "context" - "io/ioutil" "os" "os/exec" "path/filepath" @@ -114,13 +112,14 @@ func startDevServer(ctx context.Context) error { return err } c.Debug = devOptions.Debug - tags := []string{"-tags", "development"} - if b, err := ioutil.ReadFile("database.yml"); err == nil { - if bytes.Contains(b, []byte("sqlite")) { - tags = append(tags, "sqlite") - } + + app := meta.New(".") + bt := app.BuildTags("development") + var tf []string + for _, b := range bt { + tf = append(tf, "-tags", b) } - c.BuildFlags = append(c.BuildFlags, tags...) + c.BuildFlags = append(c.BuildFlags, tf...) r := refresh.NewWithContext(c, ctx) return r.Start() } diff --git a/buffalo/cmd/test.go b/buffalo/cmd/test.go index a70387ee8..8d2814533 100644 --- a/buffalo/cmd/test.go +++ b/buffalo/cmd/test.go @@ -3,7 +3,6 @@ package cmd import ( "bytes" "io" - "io/ioutil" "os" "os/exec" "path/filepath" @@ -214,11 +213,9 @@ func testPackages(givenArgs []string) ([]string, error) { func newTestCmd(args []string) *exec.Cmd { cargs := []string{"test", "-p", "1"} - if b, err := ioutil.ReadFile("database.yml"); err == nil { - if bytes.Contains(b, []byte("sqlite")) { - cargs = append(cargs, "-tags", "sqlite") - } - } + app := meta.New(".") + tf := "-tags " + app.BuildTags("development").String() + cargs = append(cargs, tf) cargs = append(cargs, args...) cmd := exec.Command(envy.Get("GO_BIN", "go"), cargs...) cmd.Stdin = os.Stdin diff --git a/meta/tags.go b/meta/tags.go new file mode 100644 index 000000000..19dcf4c62 --- /dev/null +++ b/meta/tags.go @@ -0,0 +1,30 @@ +package meta + +import ( + "bytes" + "io/ioutil" + "strings" +) + +// BuildTags are tags used for building apps +type BuildTags []string + +// String returns the tags in the form of: +// "foo bar baz" (with the quotes!) +func (t BuildTags) String() string { + return `"` + strings.Join(t, " ") + `"` +} + +// BuildTags combines the passed in env, and any additional tags, +// with tags that Buffalo decides the build process requires. +// An example would be adding the "sqlite" build tag if using +// SQLite3. +func (a App) BuildTags(env string, tags ...string) BuildTags { + tags = append(tags, env) + if b, err := ioutil.ReadFile("database.yml"); err == nil { + if bytes.Contains(b, []byte("sqlite")) { + tags = append(tags, "sqlite") + } + } + return BuildTags(tags) +} diff --git a/meta/tags_test.go b/meta/tags_test.go new file mode 100644 index 000000000..87f5042d6 --- /dev/null +++ b/meta/tags_test.go @@ -0,0 +1,47 @@ +package meta + +import ( + "os" + "testing" + + "github.com/stretchr/testify/require" +) + +func Test_BuildTags(t *testing.T) { + defer os.Remove("database.yml") + app := New(".") + t.Run("without database.yml", func(st *testing.T) { + r := require.New(st) + tags := app.BuildTags("dev") + r.Len(tags, 1) + r.Equal("dev", tags[0]) + r.Equal(`"dev"`, tags.String()) + }) + t.Run("with database.yml", func(st *testing.T) { + t.Run("with sqlite", func(st *testing.T) { + r := require.New(st) + f, err := os.Create("database.yml") + r.NoError(err) + _, err = f.WriteString("sqlite") + r.NoError(err) + + tags := app.BuildTags("dev") + r.Len(tags, 2) + r.Equal("dev", tags[0]) + r.Equal("sqlite", tags[1]) + r.Equal(`"dev sqlite"`, tags.String()) + }) + t.Run("without sqlite", func(st *testing.T) { + r := require.New(st) + f, err := os.Create("database.yml") + r.NoError(err) + _, err = f.WriteString("mysql") + r.NoError(err) + + tags := app.BuildTags("dev") + r.Len(tags, 1) + r.Equal("dev", tags[0]) + r.Equal(`"dev"`, tags.String()) + }) + }) +}