Skip to content

Commit

Permalink
Bugfix/install bundler (#46)
Browse files Browse the repository at this point in the history
* Install bundler if a Gemfile.lock is present, use the version specified in the Gemfile.lock (by BUNDLED WITH)

* Specify bundler version in `bundle install` command.

* Update imports due to org migration

* Bump bitrise.yml version
  • Loading branch information
lpusok authored Jun 13, 2019
1 parent 53fde93 commit a6e1b28
Show file tree
Hide file tree
Showing 41 changed files with 357 additions and 9,242 deletions.
57 changes: 15 additions & 42 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 7 additions & 10 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@

[[constraint]]
branch = "master"
name = "github.com/bitrise-io/go-steputils"

[[constraint]]
branch = "master"
name = "github.com/bitrise-io/go-utils"

[[constraint]]
branch = "master"
name = "github.com/kballard/go-shellquote"
name = "github.com/bitrise-steplib/steps-deploy-to-itunesconnect-deliver"

[[constraint]]
name = "github.com/stretchr/testify"
branch = "master"
name = "github.com/kballard/go-shellquote"

[prune]
go-tests = true
unused-packages = true

[[constraint]]
branch = "master"
name = "github.com/bitrise-tools/go-steputils"

[[constraint]]
branch = "master"
name = "github.com/bitrise-io/steps-deploy-to-itunesconnect-deliver"
2 changes: 1 addition & 1 deletion bitrise.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
format_version: 6
format_version: 7
default_step_lib_source: https://github.com/bitrise-io/bitrise-steplib.git

app:
Expand Down
25 changes: 25 additions & 0 deletions bundler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"github.com/bitrise-io/go-utils/command"
"github.com/bitrise-io/go-utils/command/rubycommand"
)

func getInstallBundlerCommand(gemfileLockVersion gemVersion) (*command.Model, error) {
installBundlerCmdParams := []string{"gem", "install", "bundler", "--force", "--no-document"}
if gemfileLockVersion.found {
installBundlerCmdParams = append(installBundlerCmdParams, []string{"-v", gemfileLockVersion.version}...)
}

return command.NewFromSlice(installBundlerCmdParams)
}

func getBundleInstallCommand(gemfileLockVersion gemVersion) (*command.Model, error) {
bundleInstallCmdParams := []string{"bundle"}
if gemfileLockVersion.found {
bundleInstallCmdParams = append(bundleInstallCmdParams, "_"+gemfileLockVersion.version+"_")
}
bundleInstallCmdParams = append(bundleInstallCmdParams, []string{"install", "--jobs", "20", "--retry", "5"}...)

return rubycommand.NewFromSlice(bundleInstallCmdParams)
}
118 changes: 118 additions & 0 deletions gemfile_lock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package main

import (
"fmt"
"path/filepath"
"regexp"
"strings"

"github.com/bitrise-io/go-utils/fileutil"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-io/go-utils/pathutil"
)

type gemVersion struct {
version string
found bool
}

type gemVersions struct {
fastlane, bundler gemVersion
}

func parseGemfileLock(searchDir string) (gemVersions, error) {
gemfileLockPth := filepath.Join(searchDir, "Gemfile.lock")
log.Printf("Checking Gemfile.lock (%s) for fastlane and bundler gem", gemfileLockPth)

if exist, err := pathutil.IsPathExists(gemfileLockPth); err != nil {
return gemVersions{}, fmt.Errorf("failed to check if Gemfile.lock exist at (%s), error: %s", gemfileLockPth, err)
} else if !exist {
log.Printf("Gemfile.lock does not exist")
return gemVersions{}, nil
}

content, err := fileutil.ReadStringFromFile(gemfileLockPth)
if err != nil {
return gemVersions{}, err
}

var gemVersions gemVersions

gemVersions.fastlane = parseFastlaneVersion(content)
if gemVersions.fastlane.found {
log.Infof("Gemfile.lock defined fastlane version: %s", gemVersions.fastlane.version)
} else {
log.Infof("No fastlane version defined in Gemfile.lock")
}

gemVersions.bundler = parseBundlerVersion(content)
if gemVersions.bundler.found {
log.Infof("Gemfile.lock defined bundler version: %s", gemVersions.bundler.version)
} else {
log.Infof("No bundler version defined in Gemfile.lock")
}

return gemVersions, nil
}

func parseFastlaneVersion(gemfileLockContent string) gemVersion {
return parseGemVersion("fastlane", gemfileLockContent)
}

func parseGemVersion(gemName string, content string) gemVersion {
relevantLines := []string{}
lines := strings.Split(content, "\n")

specsStart := false
for _, line := range lines {
if strings.Contains(line, "specs:") {
specsStart = true
}

trimmed := strings.Trim(line, " ")
if trimmed == "" {
specsStart = false
}

if specsStart {
relevantLines = append(relevantLines, line)
}
}

// fastlane (1.109.0)
exp := regexp.MustCompile(fmt.Sprintf(`^%s \((.+)\)`, regexp.QuoteMeta(gemName)))
for _, line := range relevantLines {
match := exp.FindStringSubmatch(strings.TrimSpace(line))
if len(match) == 2 {
return gemVersion{
version: match[1],
found: true,
}
}
}

return gemVersion{}
}

func parseBundlerVersion(gemfileLockContent string) gemVersion {
/*
BUNDLED WITH
1.17.1
*/
bundlerRegexp := regexp.MustCompile(`(?m)^BUNDLED WITH\n\s+(\S+)`)
match := bundlerRegexp.FindStringSubmatch(gemfileLockContent)
if match == nil {
log.Warnf("failed to parse bundler version in Gemfile.lock: %s", gemfileLockContent)
fmt.Println()
return gemVersion{}
}
if len(match) != 2 {
log.Warnf("unexpected regexp match: %v", match)
return gemVersion{}
}

return gemVersion{
version: match[1],
found: true,
}
}
59 changes: 54 additions & 5 deletions main_test.go → gemfile_lock_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,63 @@
package main

import (
"reflect"
"testing"

"github.com/stretchr/testify/require"
)

func testFastlaneVersionFromGemfileLockContent(t *testing.T) {
version := fastlaneVersionFromGemfileLockContent(gemfileLockContent)
require.Equal(t, "2.13.0", version)
func Test_parseGemVersion(t *testing.T) {
want := gemVersion{
version: "2.13.0",
found: true,
}
if got := parseGemVersion("fastlane", gemfileLockContent); !reflect.DeepEqual(got, want) {
t.Errorf("gemVersionFromGemfileLockContent() = %+v, want: %+v", got, want)
}
}

func Test_parseBundlerVersion(t *testing.T) {
tests := []struct {
name string
gemfileLockContent string
want gemVersion
}{
{
name: "should match",
gemfileLockContent: gemfileLockContent,
want: gemVersion{
version: "1.13.6",
found: true,
},
},
{
name: "newline after version",
gemfileLockContent: `BUNDLED WITH
1.13.6
`,
want: gemVersion{
version: "1.13.6",
found: true,
},
},
{
name: "newline before version",
gemfileLockContent: `BUNDLED WITH
1.13.6`,
want: gemVersion{
version: "1.13.6",
found: true,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := parseBundlerVersion(tt.gemfileLockContent); !reflect.DeepEqual(got, tt.want) {
t.Errorf("parseBundlerVersion() = %v, want %v", got, tt.want)
}
})
}
}

const gemfileLockContent = `GIT
Expand Down
Loading

0 comments on commit a6e1b28

Please sign in to comment.