-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Carolyn Van Slyck <me@carolynvanslyck.com>
- Loading branch information
Showing
5 changed files
with
160 additions
and
0 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 @@ | ||
// Package plugins contains magefile targets that perform common tasks | ||
// that all plugins need to support | ||
package plugins |
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 plugins | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"get.porter.sh/magefiles/ci" | ||
"get.porter.sh/magefiles/porter" | ||
"get.porter.sh/magefiles/releases" | ||
"github.com/carolynvs/magex/mgx" | ||
"github.com/carolynvs/magex/shx" | ||
"github.com/carolynvs/magex/xplat" | ||
"github.com/magefile/mage/mg" | ||
) | ||
|
||
type Magefile struct { | ||
Pkg string | ||
PluginName string | ||
BinDir string | ||
} | ||
|
||
// NewMagefile creates a magefile helper for a plugin | ||
func NewMagefile(pkg, pluginName, binDir string) Magefile { | ||
return Magefile{Pkg: pkg, PluginName: pluginName, BinDir: binDir} | ||
} | ||
|
||
var must = shx.CommandBuilder{StopOnError: true} | ||
|
||
// ConfigureAgent sets up a CI worker to use Go and mage. | ||
func (m Magefile) ConfigureAgent() { | ||
mgx.Must(ci.ConfigureAgent()) | ||
} | ||
|
||
// Build the mixin | ||
func (m Magefile) Build() { | ||
must.RunV("go", "mod", "tidy") | ||
releases.BuildPlugin(m.PluginName) | ||
} | ||
|
||
// XBuildAll cross-compiles the mixin before a release | ||
func (m Magefile) XBuildAll() { | ||
releases.XBuildAll(m.Pkg, m.PluginName, m.BinDir) | ||
releases.PrepareMixinForPublish(m.PluginName) | ||
} | ||
|
||
// TestUnit runs unit tests | ||
func (m Magefile) TestUnit() { | ||
v := "" | ||
if mg.Verbose() { | ||
v = "-v" | ||
} | ||
must.Command("go", "test", v, "./pkg/...").CollapseArgs().RunV() | ||
} | ||
|
||
// Test runs a full suite of tests | ||
func (m Magefile) Test() { | ||
m.TestUnit() | ||
|
||
// Check that we can call `plugin version` | ||
m.Build() | ||
must.RunV(filepath.Join(m.BinDir, m.PluginName+xplat.FileExt()), "version") | ||
} | ||
|
||
// Publish the plugin and its plugin feed | ||
func (m Magefile) Publish() { | ||
mg.SerialDeps(m.PublishBinaries, m.PublishPluginFeed) | ||
} | ||
|
||
// PublishBinaries uploads cross-compiled binaries to a GitHub release | ||
// Requires PORTER_RELEASE_REPOSITORY to be set to github.com/USERNAME/REPO | ||
func (m Magefile) PublishBinaries() { | ||
mg.SerialDeps(porter.UseBinForPorterHome, porter.EnsurePorter) | ||
releases.PreparePluginForPublish(m.PluginName) | ||
releases.PublishPlugin(m.PluginName) | ||
} | ||
|
||
// Publish a plugin feed | ||
// Requires PORTER_PACKAGES_REMOTE to be set to git@github.com:USERNAME/REPO.git | ||
func (m Magefile) PublishPluginFeed() { | ||
mg.SerialDeps(porter.UseBinForPorterHome, porter.EnsurePorter) | ||
releases.PublishPluginFeed(m.PluginName) | ||
} | ||
|
||
// TestPublish uploads release artifacts to a fork of the pluin's repo. | ||
// If your plugin is officially hosted in a repository under your username, you will need to manually | ||
// override PORTER_RELEASE_REPOSITORY and PORTER_PACKAGES_REMOTE to test out publishing safely. | ||
func (m Magefile) TestPublish(username string) { | ||
pluginRepo := fmt.Sprintf("github.com/%s/%s-plugins", username, m.PluginName) | ||
pkgRepo := fmt.Sprintf("https://github.com/%s/packages.git", username) | ||
fmt.Printf("Publishing a release to %s and committing a plugin feed to %s\n", pluginRepo, 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, pluginRepo) | ||
os.Setenv(releases.PackagesRemote, pkgRepo) | ||
|
||
m.Publish() | ||
} | ||
|
||
// Install the plugin | ||
func (m Magefile) Install() { | ||
porterHome := porter.GetPorterHome() | ||
fmt.Printf("Installing the %s plugin into %s\n", m.PluginName, porterHome) | ||
|
||
os.MkdirAll(filepath.Join(porterHome, "plugins", m.PluginName), 0770) | ||
mgx.Must(shx.Copy(filepath.Join(m.BinDir, m.PluginName+xplat.FileExt()), filepath.Join(porterHome, "mixins", m.PluginName))) | ||
} | ||
|
||
// Clean removes 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package plugins | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestInstall(t *testing.T) { | ||
magefile := NewMagefile("github.com/mymixin/test-mixin", "testmixin", "testdata/bin/mixins/testmixin") | ||
|
||
// Change the porter home to a safe place for the test to write to | ||
require.NoError(t, os.MkdirAll("testdata/porter_home", 0770)) | ||
os.Setenv("PORTER_HOME", "testdata/porter_home") | ||
defer os.Unsetenv("PORTER_HOME") | ||
|
||
magefile.Install() | ||
|
||
assert.DirExists(t, "testdata/porter_home/mixins/testmixin", "The mixin directory doesn't exist") | ||
assert.FileExists(t, "testdata/porter_home/mixins/testmixin/testmixin", "The client wasn't installed") | ||
assert.DirExists(t, "testdata/porter_home/mixins/testmixin/runtimes", "The mixin runtimes directory doesn't exist") | ||
assert.FileExists(t, "testdata/porter_home/mixins/testmixin/runtimes/testmixin-runtime", "The runtime wasn't installed") | ||
} |
Empty file.
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