Skip to content

Commit 76f92d0

Browse files
authored
Ruby planner (without Rails) (#164)
## Summary Simple first pass at a Ruby planner. Runs `app.rb` inside the container. ## How was it tested? `devbox shell` and `devbox build`
1 parent f0e3d06 commit 76f92d0

File tree

6 files changed

+92
-3
lines changed

6 files changed

+92
-3
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.19
55
require (
66
cuelang.org/go v0.4.3
77
github.com/bmatcuk/doublestar/v4 v4.2.0
8+
github.com/creekorful/mvnparser v1.5.0
89
github.com/denisbrodbeck/machineid v1.0.1
910
github.com/fatih/color v1.13.0
1011
github.com/google/go-cmp v0.4.0
@@ -25,7 +26,6 @@ require golang.org/x/net v0.0.0-20200226121028-0de0cce0169b // indirect
2526
require (
2627
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
2728
github.com/cockroachdb/apd/v2 v2.0.1 // indirect
28-
github.com/creekorful/mvnparser v1.5.0 // indirect
2929
github.com/davecgh/go-spew v1.1.1 // indirect
3030
github.com/google/uuid v1.2.0 // indirect
3131
github.com/inconshreveable/mousetrap v1.0.0 // indirect

planner/languages/ruby/ruby_planner.go

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,78 @@
44
package ruby
55

66
import (
7+
"bufio"
8+
"os"
9+
"path/filepath"
10+
"regexp"
11+
712
"go.jetpack.io/devbox/planner/plansdk"
13+
"golang.org/x/mod/semver"
814
)
915

1016
type Planner struct{}
1117

1218
// Implements interface Planner (compile-time check)
1319
var _ plansdk.Planner = (*Planner)(nil)
1420

21+
var nixPackages = map[string]string{
22+
"3.1": "ruby_3_1",
23+
"3.0": "ruby_3_0",
24+
"2.7": "ruby",
25+
}
26+
27+
const defaultPkg = "ruby_3_1"
28+
1529
func (p *Planner) Name() string {
1630
return "ruby.Planner"
1731
}
1832

1933
func (p *Planner) IsRelevant(srcDir string) bool {
20-
return false
34+
return plansdk.FileExists(filepath.Join(srcDir, "Gemfile"))
2135
}
2236

2337
func (p *Planner) GetPlan(srcDir string) *plansdk.Plan {
24-
return &plansdk.Plan{}
38+
v := parseRubyVersion(filepath.Join(srcDir, "Gemfile"))
39+
pkg, ok := nixPackages[semver.MajorMinor(v)]
40+
if !ok {
41+
pkg = defaultPkg
42+
}
43+
return &plansdk.Plan{
44+
DevPackages: []string{
45+
pkg,
46+
},
47+
RuntimePackages: []string{
48+
pkg,
49+
},
50+
InstallStage: &plansdk.Stage{
51+
InputFiles: plansdk.AllFiles(),
52+
Command: "bundle config set --local deployment 'true' && bundle install",
53+
},
54+
StartStage: &plansdk.Stage{
55+
InputFiles: plansdk.AllFiles(),
56+
Command: "bundle exec ruby app.rb",
57+
},
58+
}
59+
}
60+
61+
var rubyVersionRegex = regexp.MustCompile(`ruby\s+"(<|>|<=|>=|~>|=|)\s*([\d|\\.]+)"`)
62+
63+
func parseRubyVersion(gemfile string) string {
64+
f, err := os.Open(gemfile)
65+
if err != nil {
66+
return ""
67+
}
68+
s := bufio.NewScanner(f)
69+
for s.Scan() {
70+
line := s.Text()
71+
matches := rubyVersionRegex.FindStringSubmatch(line)
72+
if matches != nil {
73+
// TODO: return and use comparator as well.
74+
return matches[2]
75+
}
76+
}
77+
if err := s.Err(); err != nil {
78+
return ""
79+
}
80+
return "" // not found
2581
}

testdata/ruby/ruby-3.1/Gemfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
source "https://rubygems.org"
2+
3+
ruby "~> 3.1.0"
4+
5+
gem "colorize", "~> 0.8.1"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
colorize (0.8.1)
5+
6+
PLATFORMS
7+
x86_64-darwin-17
8+
x86_64-linux
9+
10+
DEPENDENCIES
11+
colorize (~> 0.8.1)
12+
13+
RUBY VERSION
14+
ruby 3.1.2p20
15+
16+
BUNDLED WITH
17+
2.3.7

testdata/ruby/ruby-3.1/app.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
require 'colorize'
2+
3+
puts "Hello world in red!".colorize(:red)
4+

testdata/ruby/ruby-3.1/devbox.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"packages": [
3+
],
4+
"shell": {
5+
"init_hook": null
6+
}
7+
}

0 commit comments

Comments
 (0)