-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
176 additions
and
2 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
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,95 @@ | ||
package ruby | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/railwayapp/railpack/core/generate" | ||
"github.com/railwayapp/railpack/core/plan" | ||
) | ||
|
||
const ( | ||
DEFAULT_RUBY_VERSION = "latest" | ||
) | ||
|
||
type RubyProvider struct{} | ||
|
||
func (p *RubyProvider) Name() string { | ||
return "ruby" | ||
} | ||
|
||
func (p *RubyProvider) Detect(ctx *generate.GenerateContext) (bool, error) { | ||
hasRuby := ctx.App.HasMatch("Gemfile") | ||
|
||
return hasRuby, nil | ||
} | ||
|
||
func (p *RubyProvider) Plan(ctx *generate.GenerateContext) error { | ||
packages := p.packages(ctx) | ||
|
||
_ = p.install(ctx, packages) | ||
|
||
ctx.Start.Command = fmt.Sprintf("ruby %s", "app.rb") | ||
|
||
return nil | ||
} | ||
|
||
func (p *RubyProvider) packages(ctx *generate.GenerateContext) *generate.MiseStepBuilder { | ||
packages := ctx.GetMiseStepBuilder() | ||
|
||
ruby := packages.Default("ruby", DEFAULT_RUBY_VERSION) | ||
|
||
if envVersion, varName := ctx.Env.GetConfigVariable("RUBY_VERSION"); envVersion != "" { | ||
packages.Version(ruby, envVersion, varName) | ||
} | ||
|
||
if version := p.gemfileRubyVersion(ctx); version != "" { | ||
packages.Version(ruby, version, "Gemfile.lock") | ||
} | ||
|
||
return packages | ||
} | ||
|
||
func (p *RubyProvider) install(ctx *generate.GenerateContext, packages *generate.MiseStepBuilder) *generate.CommandStepBuilder { | ||
install := ctx.NewCommandStep("install") | ||
install.AddCommands([]plan.Command{ | ||
// make sure gem is updated | ||
plan.NewExecCommand("gem update --system --no-document"), | ||
// install bundler | ||
plan.NewExecCommand("gem install -N bundler"), | ||
plan.NewCopyCommand("Gemfile"), | ||
plan.NewCopyCommand("Gemfile.lock"), | ||
plan.NewExecCommand("bundle install"), | ||
}) | ||
|
||
install.DependsOn = []string{packages.DisplayName} | ||
|
||
return install | ||
} | ||
|
||
// scan for a Gemfile.lock and return the Ruby version | ||
func (p *RubyProvider) gemfileRubyVersion(ctx *generate.GenerateContext) string { | ||
if gemfileLock, err := ctx.App.ReadFile("Gemfile.lock"); err == nil { | ||
foundRubyVersion := false | ||
lines := strings.Split(string(gemfileLock), "\n") | ||
for _, line := range lines { | ||
// Look for the "RUBY VERSION" line | ||
if strings.HasPrefix(line, "RUBY VERSION") { | ||
foundRubyVersion = true | ||
continue | ||
} | ||
|
||
// The Ruby version follows "RUBY VERSION" | ||
if foundRubyVersion { | ||
fields := strings.Fields(line) | ||
if len(fields) >= 2 && fields[0] == "ruby" { | ||
fmt.Println("Ruby Version:", fields[1]) | ||
return fields[1] | ||
} | ||
} | ||
} | ||
// packages.Version(ruby, string(versionFile), "Gemfile.lock") | ||
} | ||
|
||
return "" | ||
} |
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,55 @@ | ||
package ruby | ||
|
||
import ( | ||
"testing" | ||
|
||
testingUtils "github.com/railwayapp/railpack/core/testing" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestDetect(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
path string | ||
want bool | ||
}{ | ||
{ | ||
name: "sinatra", | ||
path: "../../../examples/ruby-sinatra", | ||
want: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ctx := testingUtils.CreateGenerateContext(t, tt.path) | ||
provider := RubyProvider{} | ||
got, err := provider.Detect(ctx) | ||
require.NoError(t, err) | ||
require.Equal(t, tt.want, got) | ||
}) | ||
} | ||
} | ||
func TestGemfileRubyVersion(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
path string | ||
want string | ||
}{ | ||
{ | ||
name: "sinatra", | ||
path: "../../../examples/ruby-sinatra", | ||
want: "3.2", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ctx := testingUtils.CreateGenerateContext(t, tt.path) | ||
provider := RubyProvider{} | ||
version, err := provider.gemfileRubyVersion(ctx) | ||
require.NoError(t, err) | ||
require.Equal(t, tt.want, version) | ||
}) | ||
} | ||
} |
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,2 @@ | ||
|
||
source 'https://rubygems.org' |
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,14 @@ | ||
GEM | ||
remote: https://rubygems.org/ | ||
specs: | ||
|
||
PLATFORMS | ||
arm64-darwin-21 | ||
|
||
DEPENDENCIES | ||
|
||
RUBY VERSION | ||
ruby 3.2 | ||
|
||
BUNDLED WITH | ||
2.3.7 |
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 @@ | ||
web: ruby app.rb |
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,5 @@ | ||
require 'sinatra' | ||
|
||
get '/' do | ||
'Hello world!' | ||
end |