-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1852 from carolynvs/common-magefile-functions
Add package for mixins magefile targets
- Loading branch information
Showing
9 changed files
with
178 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package mage | ||
package releases | ||
|
||
import ( | ||
"os" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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. | ||
|
@@ -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": | ||
|
@@ -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) | ||
|
@@ -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") | ||
} | ||
|
@@ -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() | ||
} | ||
|
@@ -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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.