Skip to content

Commit

Permalink
Merge pull request #7 from carolynvs/ensuremixins
Browse files Browse the repository at this point in the history
Improve helper methods for porter
  • Loading branch information
carolynvs authored Apr 12, 2022
2 parents dfcc01a + 061d351 commit c52bd9a
Show file tree
Hide file tree
Showing 17 changed files with 325 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Test

on: [push]
on: [push,pull_request]

jobs:
test:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.17

require (
github.com/Masterminds/semver/v3 v3.1.1
github.com/carolynvs/magex v0.7.2
github.com/carolynvs/magex v0.8.0
github.com/magefile/mage v1.11.0
github.com/stretchr/testify v1.7.0
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030I
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/andybalholm/brotli v1.0.0 h1:7UCwP93aiSfvWpapti8g88vVVGp2qqtGyePsSuDafo4=
github.com/andybalholm/brotli v1.0.0/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
github.com/carolynvs/magex v0.7.2 h1:6IpoxUorSDOXtZmd72ExPCzQBxkeUj09JwJfnzf+9y8=
github.com/carolynvs/magex v0.7.2/go.mod h1:vZB3BkRfkd5ZMtkxJkCGbdFyWGoZiuNPKhx6uEQARmY=
github.com/carolynvs/magex v0.8.0 h1:BCY0A6fLs37NZrIFBKuOMXda5wEIrUeTF4sHB601JVM=
github.com/carolynvs/magex v0.8.0/go.mod h1:vZB3BkRfkd5ZMtkxJkCGbdFyWGoZiuNPKhx6uEQARmY=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down
5 changes: 5 additions & 0 deletions magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ package main

import (
"get.porter.sh/magefiles/ci"
"get.porter.sh/magefiles/porter"
"github.com/carolynvs/magex/mgx"
"github.com/carolynvs/magex/shx"
"github.com/magefile/mage/mg"
)

var must = shx.CommandBuilder{StopOnError: true}
Expand All @@ -20,5 +22,8 @@ func Build() {
}

func Test() {
// Set up the bin with porter installed, tests can use that to initialize their local test bin to save time re-downloading it
mg.SerialDeps(porter.UseBinForPorterHome, porter.EnsurePorter)

must.RunV("go", "test", "./...")
}
14 changes: 4 additions & 10 deletions mixins/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"os"
"path/filepath"

"get.porter.sh/magefiles/porter"
"get.porter.sh/magefiles/releases"
"get.porter.sh/magefiles/tools"
"github.com/carolynvs/magex/mgx"
"github.com/carolynvs/magex/shx"
"github.com/carolynvs/magex/xplat"
Expand Down Expand Up @@ -64,14 +64,15 @@ func (m Magefile) Publish() {
// Publish 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.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)
mg.SerialDeps(porter.UseBinForPorterHome, porter.EnsurePorter)
releases.PublishMixinFeed(m.MixinName)
}

Expand All @@ -90,14 +91,7 @@ func (m Magefile) TestPublish(username string) {

// 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")
}
porterHome := porter.GetPorterHome()
fmt.Printf("Installing the %s mixin into %s\n", m.MixinName, porterHome)

os.MkdirAll(filepath.Join(porterHome, "mixins", m.MixinName, "runtimes"), 0770)
Expand Down
43 changes: 43 additions & 0 deletions porter/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package porter

import (
"os"
"path/filepath"

"github.com/carolynvs/magex/xplat"
)

var (
// DefaultMixinVersion is the default version of mixins installed when it's not present
DefaultMixinVersion = "canary"
)

// GetPorterHome determines the current PORTER_HOME directory
func GetPorterHome() string {
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")
}
return porterHome
}

// UseBinForPorterHome sets the bin/ directory to be PORTER_HOME
func UseBinForPorterHome() {
// use bin as PORTER_HOME
wd, _ := os.Getwd()
home := filepath.Join(wd, "bin")
os.Mkdir(home, 0770)
UsePorterHome(home)
}

// UsePorterHome sets the specified directory to be PORTER_HOME
func UsePorterHome(home string) {
os.Setenv("PORTER_HOME", home)

// Add PORTER_HOME to the PATH
xplat.EnsureInPath(home)
}
64 changes: 64 additions & 0 deletions porter/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package porter

import (
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/carolynvs/magex/xplat"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestGetPorterHome(t *testing.T) {
t.Run("PORTER_HOME set", func(t *testing.T) {
tmpPorterHome, err := ioutil.TempDir("", "magefiles")
require.NoError(t, err)
defer os.RemoveAll(tmpPorterHome)

os.Setenv("PORTER_HOME", tmpPorterHome)
defer os.Unsetenv("PORTER_HOME")

gotHome := GetPorterHome()
assert.Equal(t, tmpPorterHome, gotHome)
})

t.Run("Default to HOME/.porter", func(t *testing.T) {
tmpUserHome, err := ioutil.TempDir("", "magefiles")
require.NoError(t, err)
defer os.RemoveAll(tmpUserHome)
tmpPorterHome := filepath.Join(tmpUserHome, ".porter")
err = os.Mkdir(tmpPorterHome, 0700)
require.NoError(t, err)

os.Setenv("HOME", tmpUserHome)
defer os.Unsetenv("HOME")

gotHome := GetPorterHome()
assert.Equal(t, tmpPorterHome, gotHome)
})

t.Run("no home found", func(t *testing.T) {
os.Unsetenv("HOME")

defer func() {
panicObj := recover()
require.Contains(t, panicObj, "Could not find a Porter installation", "Expected a panic since there's no home for porter")
}()

GetPorterHome()
})
}

func TestUseBinForPorterHome(t *testing.T) {
defer os.RemoveAll("bin")

UseBinForPorterHome()

pwd, _ := os.Getwd()
binDir := filepath.Join(pwd, "bin")
assert.Equal(t, binDir, os.Getenv("PORTER_HOME"), "PORTER_HOME was not set correctly")

assert.True(t, xplat.InPath(binDir), "expected the bin directory to be in the PATH")
}
90 changes: 90 additions & 0 deletions porter/porter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package porter

import (
"log"
"os"
"path/filepath"
"runtime"

"github.com/carolynvs/magex/mgx"
"github.com/carolynvs/magex/pkg"
"github.com/carolynvs/magex/pkg/downloads"
"github.com/carolynvs/magex/shx"
"github.com/carolynvs/magex/xplat"
)

var (
// DefaultPorterVersion is the default version of Porter that is installed when it's not present
DefaultPorterVersion = "v1.0.0-alpha.19"
)

// Install the default version of porter
func EnsurePorter() {
EnsurePorterAt(DefaultPorterVersion)
}

// Install the specified version of porter
func EnsurePorterAt(version string) {
home := GetPorterHome()
runtimesDir := filepath.Join(home, "runtimes")
os.MkdirAll(runtimesDir, 0770)

clientPath := filepath.Join(home, "porter"+xplat.FileExt())
if clientFound, _ := pkg.IsCommandAvailable(clientPath, version, "--version"); !clientFound {
log.Println("Porter client not found at", clientPath)
log.Println("Installing porter into", home)
opts := downloads.DownloadOptions{
UrlTemplate: "https://cdn.porter.sh/{{.VERSION}}/porter-{{.GOOS}}-{{.GOARCH}}{{.EXT}}",
Name: "porter",
Version: version,
Ext: xplat.FileExt(),
}
if runtime.GOOS == "darwin" && runtime.GOARCH == "arm64" {
// we don't yet publish arm64 for porter
opts.UrlTemplate = "https://cdn.porter.sh/{{.VERSION}}/porter-darwin-amd64"
}
mgx.Must(downloads.Download(home, opts))
}

runtimePath := filepath.Join(home, "runtimes", "porter-runtime")
if runtimeFound, _ := pkg.IsCommandAvailable(runtimePath, version, "--version"); !runtimeFound {
log.Println("Porter runtime not found at", runtimePath)
opts := downloads.DownloadOptions{
UrlTemplate: "https://cdn.porter.sh/{{.VERSION}}/porter-linux-amd64",
Name: "porter-runtime",
Version: version,
}
mgx.Must(downloads.Download(runtimesDir, opts))
}
}

type InstallMixinOptions struct {
Name string
URL string
Feed string
Version string
}

// EnsureMixin installs the specified mixin.
func EnsureMixin(mixin InstallMixinOptions) error {
home := GetPorterHome()
mixinDir := filepath.Join(home, "mixins", mixin.Name)
if _, err := os.Stat(mixinDir); err == nil {
log.Println("Mixin already installed:", mixin.Name)
return nil
}

log.Println("Installing mixin:", mixin.Name)
if mixin.Version == "" {
mixin.Version = DefaultMixinVersion
}
var source string
if mixin.Feed != "" {
source = "--feed-url=" + mixin.Feed
} else {
source = "--url=" + mixin.URL
}

porterPath := filepath.Join(home, "porter")
return shx.Run(porterPath, "mixin", "install", mixin.Name, "--version", mixin.Version, source)
}
40 changes: 40 additions & 0 deletions porter/porter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package porter

import (
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/carolynvs/magex/pkg"
"github.com/carolynvs/magex/xplat"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestEnsurePorter(t *testing.T) {
testcases := []struct {
name string
wantVersion string
}{
{name: "default version", wantVersion: DefaultPorterVersion},
{name: "custom version", wantVersion: "v1.0.0-alpha.10"},
}

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
tmp, err := ioutil.TempDir("", "magefiles")
require.NoError(t, err)
defer os.RemoveAll(tmp)

UsePorterHome(tmp)
EnsurePorterAt(tc.wantVersion)
require.FileExists(t, filepath.Join(tmp, "porter"+xplat.FileExt()), "expected the porter client to be in bin")
assert.FileExists(t, filepath.Join(tmp, "runtimes", "porter-runtime"), "expected the porter runtime to be in bin")

ok, err := pkg.IsCommandAvailable("porter", tc.wantVersion, "--version")
require.NoError(t, err)
assert.True(t, ok, "could not resolve the desired porter version")
})
}
}
22 changes: 9 additions & 13 deletions releases/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,28 +160,24 @@ func PublishPluginFeed(plugin string) {
publishPackageFeed("plugin", plugin)
}

func generatePackageFeed(pkgType string) {
func generatePackageFeed(pkgType string) error {
pkgDir := pkgType + "s"
feedFile := filepath.Join(packagesRepo, pkgDir, "atom.xml")

// Try to use a local copy of porter first, otherwise use the
// one installed in GOAPTH/bin
porterPath := "bin/porter"
if _, err := os.Stat(porterPath); err != nil {
porterPath = "porter"
tools.EnsurePorter()
if err := os.MkdirAll(filepath.Dir(feedFile), 0770); err != nil {
return err
}
must.RunV(porterPath, "mixins", "feed", "generate", "-d", filepath.Join("bin", pkgDir), "-f", feedFile, "-t", "build/atom-template.xml")

return shx.RunE("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.
func GenerateMixinFeed() {
generatePackageFeed("mixin")
func GenerateMixinFeed() error {
return generatePackageFeed("mixin")
}

// Generate a plugin feed from any plugin versions in bin/plugins.
func GeneratePluginFeed() {
generatePackageFeed("plugin")
func GeneratePluginFeed() error {
return generatePackageFeed("plugin")
}

// AddFilesToRelease uploads the files in the specified directory to a GitHub release.
Expand Down
Loading

0 comments on commit c52bd9a

Please sign in to comment.