Skip to content

Commit

Permalink
Merge pull request #1852 from carolynvs/common-magefile-functions
Browse files Browse the repository at this point in the history
Add package for mixins magefile targets
  • Loading branch information
carolynvs authored Jan 12, 2022
2 parents f881421 + 698fb43 commit d7d2341
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 43 deletions.
3 changes: 3 additions & 0 deletions mage/mixins/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// mixins page contains magefile targets that perform common tasks
// that all mixins perform
package mixins
111 changes: 111 additions & 0 deletions mage/mixins/magefile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package mixins

import (
"fmt"
"os"
"path/filepath"

"github.com/carolynvs/magex/mgx"

"get.porter.sh/porter/mage/releases"
"get.porter.sh/porter/mage/tools"
"github.com/carolynvs/magex/shx"
"github.com/carolynvs/magex/xplat"
"github.com/magefile/mage/mg"
)

type Magefile struct {
Pkg string
MixinName string
BinDir string
}

// Create a magefile helper for a mixin
func NewMagefile(pkg, mixinName, binDir string) Magefile {
return Magefile{Pkg: pkg, MixinName: mixinName, BinDir: binDir}
}

var must = shx.CommandBuilder{StopOnError: true}

// Build the mixin
func (m Magefile) Build() {
must.RunV("go", "mod", "tidy")
releases.BuildAll(m.Pkg, m.MixinName, m.BinDir)
}

// Cross-compile the mixin before a release
func (m Magefile) XBuildAll() {
releases.XBuildAll(m.Pkg, m.MixinName, m.BinDir)
}

// Run unit tests
func (m Magefile) TestUnit() {
v := ""
if mg.Verbose() {
v = "-v"
}
must.Command("go", "test", v, "./pkg/...").CollapseArgs().RunV()
}

// Run all tests
func (m Magefile) Test() {
m.TestUnit()

// Check that we can call `mixin version`
m.Build()
must.RunV(filepath.Join(m.BinDir, m.MixinName+xplat.FileExt()), "version")
}

// Publish the mixin and its mixin feed
func (m Magefile) Publish() {
mg.SerialDeps(m.PublishBinaries, m.PublishMixinFeed)
}

// Publish binaries to a github release
// Requires PORTER_RELEASE_REPOSITORY to be set to github.com/USERNAME/REPO
func (m Magefile) PublishBinaries() {
releases.PrepareMixinForPublish(m.MixinName)
releases.PublishMixin(m.MixinName)
}

// Publish a mixin feed
// Requires PORTER_PACKAGES_REMOTE to be set to [email protected]:USERNAME/REPO.git
func (m Magefile) PublishMixinFeed() {
mg.Deps(tools.EnsurePorter)
releases.PublishMixinFeed(m.MixinName)
}

// Test out publish locally, with your github forks
// Assumes that you forked and kept the repository name unchanged.
func (m Magefile) TestPublish(username string) {
mixinRepo := fmt.Sprintf("github.com/%s/%s-mixin", username, m.MixinName)
pkgRepo := fmt.Sprintf("https://github.com/%s/packages.git", username)
fmt.Printf("Publishing a release to %s and committing a mixin feed to %s\n", mixinRepo, pkgRepo)
fmt.Printf("If you use different repository names, set %s and %s then call mage Publish instead.\n", releases.ReleaseRepository, releases.PackagesRemote)
os.Setenv(releases.ReleaseRepository, mixinRepo)
os.Setenv(releases.PackagesRemote, pkgRepo)

m.Publish()
}

// Install the mixin
func (m Magefile) Install() {
porterHome := os.Getenv("PORTER_HOME")
if porterHome == "" {
home, _ := os.UserHomeDir()
porterHome = filepath.Join(home, ".porter")
}
if _, err := os.Stat(porterHome); err != nil {
panic("Could not find a Porter installation. Make sure that Porter is installed and set PORTER_HOME if you are using a non-standard installation path")
}
fmt.Printf("Installing the %s mixin into %s\n", m.MixinName, porterHome)

os.MkdirAll(filepath.Join(porterHome, "mixins", m.MixinName, "runtimes"), 0700)
mgx.Must(shx.Copy(filepath.Join(m.BinDir, m.MixinName+xplat.FileExt()), filepath.Join(porterHome, "mixins", m.MixinName)))
mgx.Must(shx.Copy(filepath.Join(m.BinDir, "runtimes", m.MixinName+"-runtime"+xplat.FileExt()), filepath.Join(porterHome, "mixins/runtimes")))
}

// Remove generated build files
func (m Magefile) Clean() {
os.RemoveAll("bin")
}
7 changes: 3 additions & 4 deletions mage/releases/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"path/filepath"
"runtime"

"get.porter.sh/porter/mage"
"github.com/carolynvs/magex/mgx"
"github.com/carolynvs/magex/shx"
"golang.org/x/sync/errgroup"
Expand All @@ -19,7 +18,7 @@ var (
)

func getLDFLAGS(pkg string) string {
info := mage.LoadMetadata()
info := LoadMetadata()
return fmt.Sprintf("-w -X %s/pkg.Version=%s -X %s/pkg.Commit=%s", pkg, info.Version, pkg, info.Commit)
}

Expand Down Expand Up @@ -64,7 +63,7 @@ func BuildAll(pkg string, name string, binDir string) error {
}

func XBuild(pkg string, name string, binDir string, goos string, goarch string) error {
info := mage.LoadMetadata()
info := LoadMetadata()
// file extension is added by the build call
outPathPrefix := filepath.Join(binDir, info.Version, fmt.Sprintf("%s-%s-%s", name, goos, goarch))
return build(pkg, name, outPathPrefix, goos, goarch)
Expand All @@ -81,7 +80,7 @@ func XBuildAll(pkg string, name string, binDir string) {

mgx.Must(g.Wait())

info := mage.LoadMetadata()
info := LoadMetadata()

// Copy most recent build into bin/dev so that subsequent build steps can easily find it, not used for publishing
os.RemoveAll(filepath.Join(binDir, "dev"))
Expand Down
11 changes: 6 additions & 5 deletions mage/git.go → mage/releases/git.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mage
package releases

import (
"fmt"
Expand All @@ -14,9 +14,10 @@ import (
"github.com/pkg/errors"
)

var gitMetadata GitMetadata
var loadMetadata sync.Once
var must = shx.CommandBuilder{StopOnError: true}
var (
gitMetadata GitMetadata
loadMetadata sync.Once
)

type GitMetadata struct {
// Permalink is the version alias, e.g. latest, or canary
Expand Down Expand Up @@ -70,7 +71,7 @@ func getCommit() string {

// Get a description of the commit, e.g. v0.30.1 (latest) or v0.30.1-32-gfe72ff73 (canary)
func getVersion() string {
version, _ := must.OutputS("git", "describe", "--tags", "--match=v*")
version, _ := shx.OutputS("git", "describe", "--tags", "--match=v*")
if version != "" {
return version
}
Expand Down
2 changes: 1 addition & 1 deletion mage/git_test.go → mage/releases/git_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mage
package releases

import (
"os"
Expand Down
21 changes: 10 additions & 11 deletions mage/releases/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"path/filepath"
"strings"

"get.porter.sh/porter/mage"
"get.porter.sh/porter/mage/tools"
"github.com/carolynvs/magex/mgx"
"github.com/carolynvs/magex/shx"
Expand All @@ -19,12 +18,14 @@ import (
var must = shx.CommandBuilder{StopOnError: true}

const (
packagesRepo = "bin/mixins/.packages"
packagesRepo = "bin/mixins/.packages"
ReleaseRepository = "PORTER_RELEASE_REPOSITORY"
PackagesRemote = "PORTER_PACKAGES_REMOTE"
)

// Prepares bin directory for publishing a package
func preparePackageForPublish(pkgType string, name string) {
info := mage.LoadMetadata()
info := LoadMetadata()

// Prepare the bin directory for generating a package feed
// We want the bin to contain either a version directory (v1.2.3) or a canary directory.
Expand Down Expand Up @@ -72,17 +73,15 @@ exec echo "$GITHUB_TOKEN"
pwd, _ := os.Getwd()
script := filepath.Join(pwd, askpass)

must.Command("git", "config", "user.name", "Porter Bot").In(dir).RunV()
must.Command("git", "config", "user.email", "[email protected]").In(dir).RunV()
must.Command("git", "config", "core.askPass", script).In(dir).RunV()
}

func publishPackage(pkgType string, name string) {
mg.Deps(tools.EnsureGitHubClient, ConfigureGitBot)

info := mage.LoadMetadata()
info := LoadMetadata()

repo := os.Getenv("PORTER_RELEASE_REPOSITORY")
repo := os.Getenv(ReleaseRepository)
if repo == "" {
switch pkgType {
case "mixin":
Expand Down Expand Up @@ -124,7 +123,7 @@ func PublishPlugin(plugin string) {
}

func publishPackageFeed(pkgType string, name string) {
info := mage.LoadMetadata()
info := LoadMetadata()

if !(info.Permalink == "canary" || info.IsTaggedRelease) {
fmt.Println("Skipping publish package feed for permalink", info.Permalink)
Expand All @@ -135,7 +134,7 @@ func publishPackageFeed(pkgType string, name string) {
if _, err := os.Stat(packagesRepo); !os.IsNotExist(err) {
os.RemoveAll(packagesRepo)
}
remote := os.Getenv("PORTER_PACKAGES_REMOTE")
remote := os.Getenv(PackagesRemote)
if remote == "" {
remote = fmt.Sprintf("https://github.com/getporter/packages.git")
}
Expand All @@ -144,7 +143,7 @@ func publishPackageFeed(pkgType string, name string) {

generatePackageFeed(pkgType)

must.Command("git", "commit", "--signoff", "--author='Porter Bot<[email protected]>'", "-am", fmt.Sprintf("Add %s@%s to %s feed", name, info.Version, pkgType)).
must.Command("git", "-c", "user.name='Porter Bot'", "-c", "user.email=[email protected]", "commit", "--signoff", "-am", fmt.Sprintf("Add %s@%s to %s feed", name, info.Version, pkgType)).
In(packagesRepo).RunV()
must.Command("git", "push").In(packagesRepo).RunV()
}
Expand All @@ -162,7 +161,7 @@ func PublishPluginFeed(plugin string) {
func generatePackageFeed(pkgType string) {
pkgDir := pkgType + "s"
feedFile := filepath.Join(packagesRepo, pkgDir, "atom.xml")
must.RunV("bin/porter", "mixins", "feed", "generate", "-d", filepath.Join("bin", pkgDir), "-f", feedFile, "-t", "build/atom-template.xml")
must.RunV("porter", "mixins", "feed", "generate", "-d", filepath.Join("bin", pkgDir), "-f", feedFile, "-t", "build/atom-template.xml")
}

// Generate a mixin feed from any mixin versions in bin/mixins.
Expand Down
29 changes: 29 additions & 0 deletions mage/setup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package mage

import (
"fmt"
"os"

"get.porter.sh/porter/mage/tools"
"github.com/carolynvs/magex/pkg/gopath"
"github.com/pkg/errors"
)

// ConfigureAgent sets up an Azure DevOps agent with EnsureMage and ensures
// that GOPATH/bin is in PATH.
func ConfigureAgent() error {
err := tools.EnsureMage()
if err != nil {
return err
}

// Instruct Azure DevOps to add GOPATH/bin to PATH
gobin := gopath.GetGopathBin()
err = os.MkdirAll(gobin, 0700)
if err != nil {
return errors.Wrapf(err, "could not mkdir -p %s", gobin)
}
fmt.Printf("Adding %s to the PATH\n", gobin)
fmt.Printf("##vso[task.prependpath]%s\n", gobin)
return nil
}
6 changes: 6 additions & 0 deletions mage/tools/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,9 @@ func getKindVersion() string {
}
return DefaultKindVersion
}

// Install the latest version of porter
func EnsurePorter() {
err := pkg.DownloadToGopathBin("https://cdn.porter.sh/{{.VERSION}}/porter-{{.GOOS}}-{{.GOARCH}}{{.EXT}}", "porter", "latest")
mgx.Must(err)
}
Loading

0 comments on commit d7d2341

Please sign in to comment.