From f8bfaf830dab01da96717759a13ffbe3bef3e95c Mon Sep 17 00:00:00 2001 From: Nate Finch Date: Sat, 29 Sep 2018 09:19:08 -0400 Subject: [PATCH] start using goreleaser (#110) * start using goreleaser --- .goreleaser.yml | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ mage.go | 40 +++++++++++++++++++------------------ mage_helpers.go | 11 +++++++--- 3 files changed, 82 insertions(+), 22 deletions(-) create mode 100644 .goreleaser.yml diff --git a/.goreleaser.yml b/.goreleaser.yml new file mode 100644 index 0000000..63d76ad --- /dev/null +++ b/.goreleaser.yml @@ -0,0 +1,53 @@ +project_name: gnorm +release: + github: + owner: gnormal + name: gnorm + draft: true +build: + binary: gnorm + main: . + ldflags: -s -w -X gnorm.org/gnorm/cli.timestamp={{.Date}} -X gnorm.org/gnorm/cli.commitHash={{.Commit}} -X gnorm.org/gnorm/cli.version={{.Version}} + goos: + - darwin + - linux + - windows + - freebsd + - netbsd + - openbsd + - dragonfly + goarch: + - amd64 + - 386 + - arm + - arm64 + ignore: + - goos: openbsd + goarch: arm + goarm: 6 + env: + - CGO_ENABLED=0 +archive: + name_template: "{{.Binary}}_{{.Version}}_{{.Os}}-{{.Arch}}" + replacements: + amd64: 64bit + 386: 32bit + arm: ARM + arm64: ARM64 + darwin: macOS + linux: Linux + windows: Windows + openbsd: OpenBSD + netbsd: NetBSD + freebsd: FreeBSD + dragonfly: DragonFlyBSD + format: tar.gz + format_overrides: + - goos: windows + format: zip + files: + - LICENSE +snapshot: + name_template: SNAPSHOT-{{ .Commit }} +checksum: + name_template: '{{ .ProjectName }}_{{ .Version }}_checksums.txt' diff --git a/mage.go b/mage.go index 54cef51..0bcfa1b 100644 --- a/mage.go +++ b/mage.go @@ -6,7 +6,10 @@ package main import ( + "errors" "log" + "os" + "regexp" "github.com/magefile/mage/mg" "github.com/magefile/mage/sh" @@ -36,30 +39,29 @@ func Generate() error { return sh.Run("go", "generate", "./...") } -// Generates binaries for all supported versions. Currently that means a -// combination of windows, linux, and OSX in 32 bit and 64 bit formats. The -// files will be dumped in the local directory with names according to their -// supported platform. -func All() error { - if err := genSite(); err != nil { - return err +// Generates a new release. Expects the TAG environment variable to be set, +// which will create a new tag with that name. +func Release() (err error) { + releaseTag := regexp.MustCompile(`^v1\.[0-9]+\.[0-9]+$`) + + tag := os.Getenv("TAG") + if !releaseTag.MatchString(tag) { + return errors.New("TAG environment variable must be in semver v1.x.x format, but was " + tag) } - defer cleanup() - ldf, err := flags() - if err != nil { + if err := sh.RunV("git", "tag", "-a", tag, "-m", tag); err != nil { return err } - for _, OS := range []string{"windows", "darwin", "linux"} { - for _, ARCH := range []string{"amd64", "386"} { - log.Printf("running go build for GOOS=%s GOARCH=%s", OS, ARCH) - env := []string{"GOOS=" + OS, "GOARCH=" + ARCH} - if err := runWith(env, "go", "build", "-tags", "make", "-o", "gnorm_"+OS+"_"+ARCH, "--ldflags="+ldf); err != nil { - return err - } - } + if err := sh.RunV("git", "push", "origin", tag); err != nil { + return err } - return err + defer func() { + if err != nil { + sh.RunV("git", "tag", "--delete", "$TAG") + sh.RunV("git", "push", "--delete", "origin", "$TAG") + } + }() + return sh.RunV("goreleaser") } // Removes generated cruft. This target shouldn't ever be necessary, since the diff --git a/mage_helpers.go b/mage_helpers.go index 4cbeee2..016d147 100644 --- a/mage_helpers.go +++ b/mage_helpers.go @@ -43,11 +43,16 @@ func gitTag() (string, error) { func cleanup() error { log.Print("removing generated hugo site") err := rm("./cli/public") + if err != nil { + fmt.Println("error removing generated hugo folder:", err) + } log.Print("removing generated statik package") - if err2 := rm("./cli/statik"); err2 != nil { - return err2 + if err := rm("./cli/statik"); err != nil { + fmt.Println("error removing statik folder:", err) + } + if err := rm("dist"); err != nil { + fmt.Println("error removing release folder:", err) } - return err } func genSite() error {