Skip to content

Commit d7d2341

Browse files
authored
Merge pull request #1852 from carolynvs/common-magefile-functions
Add package for mixins magefile targets
2 parents f881421 + 698fb43 commit d7d2341

9 files changed

Lines changed: 178 additions & 43 deletions

File tree

mage/mixins/doc.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// mixins page contains magefile targets that perform common tasks
2+
// that all mixins perform
3+
package mixins

mage/mixins/magefile.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package mixins
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
8+
"github.com/carolynvs/magex/mgx"
9+
10+
"get.porter.sh/porter/mage/releases"
11+
"get.porter.sh/porter/mage/tools"
12+
"github.com/carolynvs/magex/shx"
13+
"github.com/carolynvs/magex/xplat"
14+
"github.com/magefile/mage/mg"
15+
)
16+
17+
type Magefile struct {
18+
Pkg string
19+
MixinName string
20+
BinDir string
21+
}
22+
23+
// Create a magefile helper for a mixin
24+
func NewMagefile(pkg, mixinName, binDir string) Magefile {
25+
return Magefile{Pkg: pkg, MixinName: mixinName, BinDir: binDir}
26+
}
27+
28+
var must = shx.CommandBuilder{StopOnError: true}
29+
30+
// Build the mixin
31+
func (m Magefile) Build() {
32+
must.RunV("go", "mod", "tidy")
33+
releases.BuildAll(m.Pkg, m.MixinName, m.BinDir)
34+
}
35+
36+
// Cross-compile the mixin before a release
37+
func (m Magefile) XBuildAll() {
38+
releases.XBuildAll(m.Pkg, m.MixinName, m.BinDir)
39+
}
40+
41+
// Run unit tests
42+
func (m Magefile) TestUnit() {
43+
v := ""
44+
if mg.Verbose() {
45+
v = "-v"
46+
}
47+
must.Command("go", "test", v, "./pkg/...").CollapseArgs().RunV()
48+
}
49+
50+
// Run all tests
51+
func (m Magefile) Test() {
52+
m.TestUnit()
53+
54+
// Check that we can call `mixin version`
55+
m.Build()
56+
must.RunV(filepath.Join(m.BinDir, m.MixinName+xplat.FileExt()), "version")
57+
}
58+
59+
// Publish the mixin and its mixin feed
60+
func (m Magefile) Publish() {
61+
mg.SerialDeps(m.PublishBinaries, m.PublishMixinFeed)
62+
}
63+
64+
// Publish binaries to a github release
65+
// Requires PORTER_RELEASE_REPOSITORY to be set to github.com/USERNAME/REPO
66+
func (m Magefile) PublishBinaries() {
67+
releases.PrepareMixinForPublish(m.MixinName)
68+
releases.PublishMixin(m.MixinName)
69+
}
70+
71+
// Publish a mixin feed
72+
// Requires PORTER_PACKAGES_REMOTE to be set to git@github.com:USERNAME/REPO.git
73+
func (m Magefile) PublishMixinFeed() {
74+
mg.Deps(tools.EnsurePorter)
75+
releases.PublishMixinFeed(m.MixinName)
76+
}
77+
78+
// Test out publish locally, with your github forks
79+
// Assumes that you forked and kept the repository name unchanged.
80+
func (m Magefile) TestPublish(username string) {
81+
mixinRepo := fmt.Sprintf("github.com/%s/%s-mixin", username, m.MixinName)
82+
pkgRepo := fmt.Sprintf("https://github.com/%s/packages.git", username)
83+
fmt.Printf("Publishing a release to %s and committing a mixin feed to %s\n", mixinRepo, pkgRepo)
84+
fmt.Printf("If you use different repository names, set %s and %s then call mage Publish instead.\n", releases.ReleaseRepository, releases.PackagesRemote)
85+
os.Setenv(releases.ReleaseRepository, mixinRepo)
86+
os.Setenv(releases.PackagesRemote, pkgRepo)
87+
88+
m.Publish()
89+
}
90+
91+
// Install the mixin
92+
func (m Magefile) Install() {
93+
porterHome := os.Getenv("PORTER_HOME")
94+
if porterHome == "" {
95+
home, _ := os.UserHomeDir()
96+
porterHome = filepath.Join(home, ".porter")
97+
}
98+
if _, err := os.Stat(porterHome); err != nil {
99+
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")
100+
}
101+
fmt.Printf("Installing the %s mixin into %s\n", m.MixinName, porterHome)
102+
103+
os.MkdirAll(filepath.Join(porterHome, "mixins", m.MixinName, "runtimes"), 0700)
104+
mgx.Must(shx.Copy(filepath.Join(m.BinDir, m.MixinName+xplat.FileExt()), filepath.Join(porterHome, "mixins", m.MixinName)))
105+
mgx.Must(shx.Copy(filepath.Join(m.BinDir, "runtimes", m.MixinName+"-runtime"+xplat.FileExt()), filepath.Join(porterHome, "mixins/runtimes")))
106+
}
107+
108+
// Remove generated build files
109+
func (m Magefile) Clean() {
110+
os.RemoveAll("bin")
111+
}

mage/releases/build.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"path/filepath"
77
"runtime"
88

9-
"get.porter.sh/porter/mage"
109
"github.com/carolynvs/magex/mgx"
1110
"github.com/carolynvs/magex/shx"
1211
"golang.org/x/sync/errgroup"
@@ -19,7 +18,7 @@ var (
1918
)
2019

2120
func getLDFLAGS(pkg string) string {
22-
info := mage.LoadMetadata()
21+
info := LoadMetadata()
2322
return fmt.Sprintf("-w -X %s/pkg.Version=%s -X %s/pkg.Commit=%s", pkg, info.Version, pkg, info.Commit)
2423
}
2524

@@ -64,7 +63,7 @@ func BuildAll(pkg string, name string, binDir string) error {
6463
}
6564

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

8281
mgx.Must(g.Wait())
8382

84-
info := mage.LoadMetadata()
83+
info := LoadMetadata()
8584

8685
// Copy most recent build into bin/dev so that subsequent build steps can easily find it, not used for publishing
8786
os.RemoveAll(filepath.Join(binDir, "dev"))

mage/git.go renamed to mage/releases/git.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package mage
1+
package releases
22

33
import (
44
"fmt"
@@ -14,9 +14,10 @@ import (
1414
"github.com/pkg/errors"
1515
)
1616

17-
var gitMetadata GitMetadata
18-
var loadMetadata sync.Once
19-
var must = shx.CommandBuilder{StopOnError: true}
17+
var (
18+
gitMetadata GitMetadata
19+
loadMetadata sync.Once
20+
)
2021

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

7172
// Get a description of the commit, e.g. v0.30.1 (latest) or v0.30.1-32-gfe72ff73 (canary)
7273
func getVersion() string {
73-
version, _ := must.OutputS("git", "describe", "--tags", "--match=v*")
74+
version, _ := shx.OutputS("git", "describe", "--tags", "--match=v*")
7475
if version != "" {
7576
return version
7677
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package mage
1+
package releases
22

33
import (
44
"os"

mage/releases/publish.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"path/filepath"
99
"strings"
1010

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

2120
const (
22-
packagesRepo = "bin/mixins/.packages"
21+
packagesRepo = "bin/mixins/.packages"
22+
ReleaseRepository = "PORTER_RELEASE_REPOSITORY"
23+
PackagesRemote = "PORTER_PACKAGES_REMOTE"
2324
)
2425

2526
// Prepares bin directory for publishing a package
2627
func preparePackageForPublish(pkgType string, name string) {
27-
info := mage.LoadMetadata()
28+
info := LoadMetadata()
2829

2930
// Prepare the bin directory for generating a package feed
3031
// 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"
7273
pwd, _ := os.Getwd()
7374
script := filepath.Join(pwd, askpass)
7475

75-
must.Command("git", "config", "user.name", "Porter Bot").In(dir).RunV()
76-
must.Command("git", "config", "user.email", "bot@porter.sh").In(dir).RunV()
7776
must.Command("git", "config", "core.askPass", script).In(dir).RunV()
7877
}
7978

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

83-
info := mage.LoadMetadata()
82+
info := LoadMetadata()
8483

85-
repo := os.Getenv("PORTER_RELEASE_REPOSITORY")
84+
repo := os.Getenv(ReleaseRepository)
8685
if repo == "" {
8786
switch pkgType {
8887
case "mixin":
@@ -124,7 +123,7 @@ func PublishPlugin(plugin string) {
124123
}
125124

126125
func publishPackageFeed(pkgType string, name string) {
127-
info := mage.LoadMetadata()
126+
info := LoadMetadata()
128127

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

145144
generatePackageFeed(pkgType)
146145

147-
must.Command("git", "commit", "--signoff", "--author='Porter Bot<bot@porter.sh>'", "-am", fmt.Sprintf("Add %s@%s to %s feed", name, info.Version, pkgType)).
146+
must.Command("git", "-c", "user.name='Porter Bot'", "-c", "user.email=bot@porter.sh", "commit", "--signoff", "-am", fmt.Sprintf("Add %s@%s to %s feed", name, info.Version, pkgType)).
148147
In(packagesRepo).RunV()
149148
must.Command("git", "push").In(packagesRepo).RunV()
150149
}
@@ -162,7 +161,7 @@ func PublishPluginFeed(plugin string) {
162161
func generatePackageFeed(pkgType string) {
163162
pkgDir := pkgType + "s"
164163
feedFile := filepath.Join(packagesRepo, pkgDir, "atom.xml")
165-
must.RunV("bin/porter", "mixins", "feed", "generate", "-d", filepath.Join("bin", pkgDir), "-f", feedFile, "-t", "build/atom-template.xml")
164+
must.RunV("porter", "mixins", "feed", "generate", "-d", filepath.Join("bin", pkgDir), "-f", feedFile, "-t", "build/atom-template.xml")
166165
}
167166

168167
// Generate a mixin feed from any mixin versions in bin/mixins.

mage/setup.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package mage
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"get.porter.sh/porter/mage/tools"
8+
"github.com/carolynvs/magex/pkg/gopath"
9+
"github.com/pkg/errors"
10+
)
11+
12+
// ConfigureAgent sets up an Azure DevOps agent with EnsureMage and ensures
13+
// that GOPATH/bin is in PATH.
14+
func ConfigureAgent() error {
15+
err := tools.EnsureMage()
16+
if err != nil {
17+
return err
18+
}
19+
20+
// Instruct Azure DevOps to add GOPATH/bin to PATH
21+
gobin := gopath.GetGopathBin()
22+
err = os.MkdirAll(gobin, 0700)
23+
if err != nil {
24+
return errors.Wrapf(err, "could not mkdir -p %s", gobin)
25+
}
26+
fmt.Printf("Adding %s to the PATH\n", gobin)
27+
fmt.Printf("##vso[task.prependpath]%s\n", gobin)
28+
return nil
29+
}

mage/tools/install.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,9 @@ func getKindVersion() string {
9090
}
9191
return DefaultKindVersion
9292
}
93+
94+
// Install the latest version of porter
95+
func EnsurePorter() {
96+
err := pkg.DownloadToGopathBin("https://cdn.porter.sh/{{.VERSION}}/porter-{{.GOOS}}-{{.GOARCH}}{{.EXT}}", "porter", "latest")
97+
mgx.Must(err)
98+
}

0 commit comments

Comments
 (0)