Skip to content

Commit

Permalink
generate a version file
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Sep 5, 2018
1 parent 45133dd commit 0fb78de
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 8 deletions.
2 changes: 1 addition & 1 deletion cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func init() {
initCmd.Flags().BoolVarP(&initOptions.dryRun, "dry-run", "d", false, "runs the generator dry")
initCmd.Flags().BoolVarP(&initOptions.force, "force", "f", false, "force files to overwrite existing ones")
initCmd.Flags().StringVarP(&initOptions.mainFile, "main-file", "m", "", "adds a .goreleaser.yml file (only for binary applications)")
initCmd.Flags().StringVarP(&initOptions.versionFile, "version-file", "v", "", "path to a version file to maintain")
initCmd.Flags().StringVarP(&initOptions.versionFile, "version-file", "v", "version.go", "path to a version file to maintain")

// Here you will define your flags and configuration settings.

Expand Down
3 changes: 3 additions & 0 deletions genny/makefile/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ type Options struct {

// Validate that options are usuable
func (opts *Options) Validate() error {
if len(opts.VersionFile) == 0 {
opts.VersionFile = "version.go"
}
return nil
}
59 changes: 58 additions & 1 deletion genny/release/version_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ package release

import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/gobuffalo/genny"
"github.com/gobuffalo/genny/movinglater/gotools"
"github.com/gobuffalo/genny/movinglater/plushgen"
"github.com/gobuffalo/plush"
"github.com/pkg/errors"
)

Expand All @@ -16,7 +22,10 @@ func writeVersionFile(opts *Options) genny.RunFn {

f, err := r.FindFile(opts.VersionFile)
if err != nil {
return errors.WithStack(err)
f, err = defaultVersionFile(opts.VersionFile)
if err != nil {
return errors.WithStack(err)
}
}

var matches []string
Expand All @@ -43,3 +52,51 @@ func writeVersionFile(opts *Options) genny.RunFn {
return r.File(f)
}
}

func defaultVersionFile(name string) (genny.File, error) {
dir := filepath.Dir(name)
files, err := ioutil.ReadDir(dir)
_, ok := err.(*os.PathError)
if err != nil && !ok {
return nil, errors.WithStack(err)
}
var pkg string
if len(files) == 0 {
pkg = filepath.Base(dir)
} else {
for _, fi := range files {
ext := filepath.Ext(fi.Name())
if ext != ".go" {
continue
}
if strings.HasSuffix(fi.Name(), "_test.go") {
continue
}
b, err := ioutil.ReadFile(fi.Name())
if err != nil {
return nil, errors.WithStack(err)
}
xf := genny.NewFile(fi.Name(), bytes.NewReader(b))
pkg, err = gotools.PackageName(xf)
if err != nil {
return nil, errors.WithStack(err)
}
break
}
}

f := genny.NewFile(name+".plush", strings.NewReader(versionTmpl))

ctx := plush.NewContext()
ctx.Set("pkg", pkg)
t := plushgen.Transformer(ctx)
f, err = t.Transform(f)
if err != nil {
return f, errors.WithStack(err)
}
return f, nil
}

const versionTmpl = `package <%= pkg %>
const Version = ""`
45 changes: 45 additions & 0 deletions genny/release/version_file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package release

import (
"context"
"testing"

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

func Test_writeVersionFile(t *testing.T) {

table := []struct {
vf string
pkg string
}{
{"foo/version.go", "foo"},
{"version.go", "release"},
}

for _, tt := range table {
t.Run(tt.vf, func(st *testing.T) {
r := require.New(st)

opts := &Options{
VersionFile: tt.vf,
}

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

fn := writeVersionFile(opts)
r.NoError(fn(run))

res := run.Results()

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

f := res.Files[0]
r.Equal(opts.VersionFile, f.Name())
r.Contains(f.String(), "package "+tt.pkg)
})
}

}
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ module github.com/gobuffalo/release
require (
github.com/Masterminds/semver v1.4.2
github.com/gobuffalo/envy v1.6.4
github.com/gobuffalo/genny v0.0.0-20180904214332-b7dcbfc7ea0a
github.com/gobuffalo/genny v0.0.0-20180905180744-ca4140706556
github.com/gobuffalo/packr v1.13.3
github.com/gobuffalo/plush v3.7.16+incompatible
github.com/gobuffalo/shoulders v0.0.0-20180904151418-9cac5899ceee
github.com/pkg/errors v0.8.0
github.com/spf13/cobra v0.0.3
github.com/stretchr/testify v1.2.2
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33 // indirect
)
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMo
github.com/gobuffalo/envy v1.6.4 h1:kxamN+VYjPMzEdjc2mn4CIKiuYXGxc8VIwXJNixFlNY=
github.com/gobuffalo/envy v1.6.4/go.mod h1:Abh+Jfw475/NWtYMEt+hnJWRiC8INKWibIMyNt1w2Mc=
github.com/gobuffalo/flect v0.0.0-20180902163941-e03d6ccf1b21/go.mod h1:rCiQgmAE4axgBNl3jZWzS5rETRYTGOsrixTRaCPzNdA=
github.com/gobuffalo/genny v0.0.0-20180904214332-b7dcbfc7ea0a h1:LozjeWkCQ2gCbSGtXyGNuWVfjlDTHv3yT+XkImvXyIg=
github.com/gobuffalo/genny v0.0.0-20180904214332-b7dcbfc7ea0a/go.mod h1:CX+ymPzTO0Ti5qRyCFdtu78AncVEQ5QVe5cEeqMURJs=
github.com/gobuffalo/genny v0.0.0-20180905180744-ca4140706556 h1:/ZTXnwIn9IYobWzKwrsDxiPEdwD6+W/F62lRBlG41Wg=
github.com/gobuffalo/genny v0.0.0-20180905180744-ca4140706556/go.mod h1:FZNXcfF+FuV4i8BANht5bghYawahu+sIrDdES9govgw=
github.com/gobuffalo/github_flavored_markdown v1.0.0 h1:e2dK+SoHgOc/vfXuYMdXwEg2vAUlFzp8SBRwTOXckQ0=
github.com/gobuffalo/github_flavored_markdown v1.0.0/go.mod h1:c8/8gRnd6MSyyk+fp6E8O8cUTHd7P2cnDnH4G7o91l0=
github.com/gobuffalo/packr v1.13.3 h1:XjxS2G7cI2kb1KtGwO+ziCdB/D0kAuU/eG3BAieQT+4=
Expand Down Expand Up @@ -91,8 +91,6 @@ golang.org/x/net v0.0.0-20180826012351-8a410e7b638d h1:g9qWBGx4puODJTMVyoPrpoxPF
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180903190138-2b024373dcd9 h1:lkiLiLBHGoH3XnqSLUIaBsilGMUjI+Uy2Xu2JLUtTas=
golang.org/x/sys v0.0.0-20180903190138-2b024373dcd9/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33 h1:I6FyU15t786LL7oL/hn43zqTuEGr4PN7F4XJ1p4E3Y8=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
Expand Down

0 comments on commit 0fb78de

Please sign in to comment.