Skip to content

Commit

Permalink
add an init generator
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Sep 5, 2018
1 parent 00c8a86 commit 45fe1dc
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 2 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ update:
$(GO_BIN) mod tidy
packr
make test
make install

release-test:
$(GO_BIN) test -tags ${TAGS} -race ./...
Expand Down
2 changes: 2 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import (

"github.com/gobuffalo/genny"
"github.com/gobuffalo/release/genny/goreleaser"
"github.com/gobuffalo/release/genny/init"
"github.com/gobuffalo/release/genny/makefile"
"github.com/gobuffalo/release/genny/release"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

var initOptions = struct {
*init.Options
dryRun bool
force bool
mainFile string
Expand Down
46 changes: 46 additions & 0 deletions genny/init/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package init

import (
"github.com/gobuffalo/genny"
"github.com/gobuffalo/release/genny/goreleaser"
"github.com/gobuffalo/release/genny/makefile"
"github.com/gobuffalo/release/genny/release"
"github.com/pkg/errors"
)

func New(opts *Options) (*genny.Group, error) {
gg := &genny.Group{}

g := genny.New()

if err := opts.Validate(); err != nil {
return gg, errors.WithStack(err)
}

g.RunFn(release.WriteVersionFile(&release.Options{
VersionFile: opts.VersionFile,
Version: opts.Version,
}))
gg.Add(g)

g, err := makefile.New(&makefile.Options{
Force: opts.Force,
VersionFile: opts.VersionFile,
})
if err != nil {
return gg, errors.WithStack(err)
}
gg.Add(g)

if len(opts.MainFile) != 0 {
g, err = goreleaser.New(&goreleaser.Options{
Force: opts.Force,
MainFile: opts.MainFile,
})
if err != nil {
return gg, errors.WithStack(err)
}
gg.Add(g)
}
return gg, nil
}
39 changes: 39 additions & 0 deletions genny/init/init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package init

import (
"context"
"testing"

"github.com/gobuffalo/genny"
"github.com/stretchr/testify/require"
)

func Test_New(t *testing.T) {
r := require.New(t)

gg, err := New(&Options{
VersionFile: "foo/bar/version.go",
MainFile: "./main.go",
})
r.NoError(err)

run := genny.DryRunner(context.Background())
run.WithGroup(gg)

r.NoError(run.Run())

res := run.Results()

r.Len(res.Commands, 0)
r.Len(res.Files, 3)

f := res.Files[0]
r.Equal(".goreleaser.yml", f.Name())

f = res.Files[1]
r.Equal("Makefile", f.Name())

f = res.Files[2]
r.Equal("foo/bar/version.go", f.Name())
r.Contains(f.String(), `const Version = "v0.0.1"`)
}
19 changes: 19 additions & 0 deletions genny/init/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package init

type Options struct {
VersionFile string
Version string
MainFile string
Force bool
}

// Validate that options are usuable
func (opts *Options) Validate() error {
if len(opts.Version) == 0 {
opts.Version = "v0.0.1"
}
if len(opts.VersionFile) == 0 {
opts.Version = "version.go"
}
return nil
}
2 changes: 1 addition & 1 deletion genny/makefile/a_makefile-packr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion genny/makefile/templates/Makefile.plush
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ update:
$(GO_BIN) get -u
$(GO_BIN) mod tidy
packr
make test
make test
make install

release-test:
$(GO_BIN) test -tags ${TAGS} -race ./...
Expand Down

0 comments on commit 45fe1dc

Please sign in to comment.