Skip to content

Commit

Permalink
Merge pull request #212 from spk/rails-stack
Browse files Browse the repository at this point in the history
Rails stack
  • Loading branch information
akalipetis authored Jul 18, 2024
2 parents 1aa87e2 + 206e47c commit 2ddf43f
Show file tree
Hide file tree
Showing 15 changed files with 119 additions and 7 deletions.
12 changes: 12 additions & 0 deletions internal/question/build_steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ func (q *BuildSteps) Ask(ctx context.Context) error {
answers.BuildSteps,
"composer --no-ansi --no-interaction install --no-progress --prefer-dist --optimize-autoloader --no-dev",
)
case models.Bundler:
answers.BuildSteps = append(
answers.BuildSteps,
"bundle install",
)
}
}

Expand Down Expand Up @@ -125,6 +130,13 @@ func (q *BuildSteps) Ask(ctx context.Context) error {
}
answers.BuildSteps = append(answers.BuildSteps, cmd)
}
case models.Rails:
answers.Environment["RAILS_ENV"] = "production"
answers.Environment["PIDFILE"] = "tmp/server.pid"
answers.BuildSteps = append(
answers.BuildSteps,
"bundle exec rails assets:precompile",
)
}

return nil
Expand Down
26 changes: 26 additions & 0 deletions internal/question/build_steps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,32 @@ func TestBuildSteps_Ask(t *testing.T) {
buildSteps: []string{"npm i", "npm exec next build"},
wantErr: false,
},
{
name: "Bundler",
q: &BuildSteps{},
args: args{models.Answers{
Stack: models.GenericStack,
Type: models.RuntimeType{Runtime: models.Ruby, Version: "3.3"},
Dependencies: map[string]map[string]string{},
DependencyManagers: []models.DepManager{models.Bundler},
Environment: map[string]string{},
}},
buildSteps: []string{"bundle install"},
wantErr: false,
},
{
name: "Bundler with Rails",
q: &BuildSteps{},
args: args{models.Answers{
Stack: models.Rails,
Type: models.RuntimeType{Runtime: models.Ruby, Version: "3.3"},
Dependencies: map[string]map[string]string{},
DependencyManagers: []models.DepManager{models.Bundler},
Environment: map[string]string{},
}},
buildSteps: []string{"bundle install", "bundle exec rails assets:precompile"},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions internal/question/dependency_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (
pipenvLockFile = "Pipfile.lock"
pipLockFile = "requirements.txt"
composerLockFile = "composer.lock"
bundlerLockFile = "Gemfile.lock"
)

type DependencyManager struct{}
Expand Down Expand Up @@ -77,5 +78,9 @@ func (q *DependencyManager) Ask(ctx context.Context) error {
answers.DependencyManagers = append(answers.DependencyManagers, models.Npm)
}

if exists := utils.FileExists(answers.WorkingDirectory, bundlerLockFile); exists {
answers.DependencyManagers = append(answers.DependencyManagers, models.Bundler)
}

return nil
}
4 changes: 4 additions & 0 deletions internal/question/deploy_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ func (q *DeployCommand) Ask(ctx context.Context) error {
"php artisan migrate --force",
"php artisan optimize:clear",
)
case models.Rails:
answers.DeployCommand = append(answers.DeployCommand,
"bundle exec rake db:migrate",
)
}

return nil
Expand Down
2 changes: 2 additions & 0 deletions internal/question/models/answer.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ func getStack(answersStack Stack) platformifier.Stack {
return platformifier.Django
case Laravel:
return platformifier.Laravel
case Rails:
return platformifier.Rails
case NextJS:
return platformifier.NextJS
case Strapi:
Expand Down
3 changes: 3 additions & 0 deletions internal/question/models/dependency_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const (
Composer DepManager = "composer"
Yarn DepManager = "yarn"
Npm DepManager = "npm"
Bundler DepManager = "bundler"
)

type DepManager string
Expand All @@ -32,6 +33,8 @@ func (m DepManager) Title() string {
return "Yarn"
case Npm:
return "Npm"
case Bundler:
return "Bundler"
default:
return ""
}
Expand Down
6 changes: 6 additions & 0 deletions internal/question/models/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const (
Strapi
Flask
Express
Rails
)

var (
Expand All @@ -25,6 +26,7 @@ var (
Strapi,
Flask,
Express,
Rails,
}
)

Expand All @@ -36,6 +38,8 @@ func (s Stack) Title() string {
return "Other"
case Django:
return "Django"
case Rails:
return "Rails"
case Laravel:
return "Laravel"
case NextJS:
Expand Down Expand Up @@ -88,6 +92,8 @@ func RuntimeForStack(stack Stack) Runtime {
switch stack {
case Django, Flask:
return Python
case Rails:
return Ruby
case Laravel:
return PHP
case NextJS, Strapi, Express:
Expand Down
16 changes: 16 additions & 0 deletions internal/question/mounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@ func (q *Mounts) Ask(ctx context.Context) error {
}

switch answers.Stack {
case models.Rails:
answers.Disk = "2048" // in MB
answers.Mounts = map[string]map[string]string{
"storage": {
"source": "local",
"source_path": "storage",
},
"tmp": {
"source": "tmp",
"source_path": "tmp",
},
"log": {
"source": "tmp",
"source_path": "tmp",
},
}
case models.Laravel:
answers.Disk = "2048" // in MB
answers.Mounts = map[string]map[string]string{
Expand Down
13 changes: 13 additions & 0 deletions internal/question/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (
composerJSONFile = "composer.json"
packageJSONFile = "package.json"
symfonyLockFile = "symfony.lock"
rackFile = "config.ru"
)

type Stack struct{}
Expand Down Expand Up @@ -59,6 +60,18 @@ func (q *Stack) Ask(ctx context.Context) error {
return nil
}

rackPath := utils.FindFile(answers.WorkingDirectory, rackFile)
if rackPath != "" {
f, err := os.Open(rackPath)
if err == nil {
defer f.Close()
if ok, _ := utils.ContainsStringInFile(f, "Rails.application.load_server", true); ok {
answers.Stack = models.Rails
return nil
}
}
}

requirementsPath := utils.FindFile(answers.WorkingDirectory, "requirements.txt")
if requirementsPath != "" {
f, err := os.Open(requirementsPath)
Expand Down
8 changes: 8 additions & 0 deletions internal/question/web_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ func (q *WebCommand) Ask(ctx context.Context) error {

answers.WebCommand = fmt.Sprintf("%sgunicorn %s -b unix:$SOCKET %s --log-file -", prefix, pythonPath, wsgi)
return nil
case models.Rails:
if answers.SocketFamily == models.TCP {
answers.WebCommand = "bundle exec rails server"
return nil
}

answers.WebCommand = "bundle exec puma -b unix://$SOCKET"
return nil
case models.NextJS:
answers.WebCommand = "npx next start -p $PORT"
return nil
Expand Down
3 changes: 3 additions & 0 deletions platformifier/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (
Strapi
Flask
Express
Rails
)

type Stack int
Expand All @@ -36,6 +37,8 @@ func (s Stack) Name() string {
return "generic"
case Django:
return "django"
case Rails:
return "rails"
case Laravel:
return "laravel"
case NextJS:
Expand Down
14 changes: 11 additions & 3 deletions platformifier/templates/generic/.environment
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@ export RELATIONSHIPS_JSON="$(echo ${{ .Assets.EnvPrefix }}_RELATIONSHIPS | base6
# Set database environment variables
export DB_HOST="$(echo $RELATIONSHIPS_JSON | jq -r '.{{ .Database }}[0].host')"
export DB_PORT="$(echo $RELATIONSHIPS_JSON | jq -r '.{{ .Database }}[0].port')"
export DB_DATABASE="$(echo $RELATIONSHIPS_JSON | jq -r '.{{ .Database }}[0].path')"
export DB_PATH="$(echo $RELATIONSHIPS_JSON | jq -r '.{{ .Database }}[0].path')"
export DB_DATABASE="$DB_PATH"
export DB_USERNAME="$(echo $RELATIONSHIPS_JSON | jq -r '.{{ .Database }}[0].username')"
export DB_PASSWORD="$(echo $RELATIONSHIPS_JSON | jq -r '.{{ .Database }}[0].password')"
{{ if eq .Database "postgresql" }}export DB_CONNECTION="postgresql"{{ else }}export DB_CONNECTION="$(echo $RELATIONSHIPS_JSON | jq -r '.{{ .Database }}[0].scheme')"{{ end }}
export DATABASE_URL="${DB_CONNECTION}://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_DATABASE}"
{{ if eq .Database "postgresql" }}export DB_SCHEME="postgresql"
{{- else if and (eq .Stack.Name "rails") (or (eq .Database "mariadb") (eq .Database "mysql")) }}export DB_SCHEME="mysql2" # mysql/mariadb rails adapter
{{- else }}export DB_SCHEME="$(echo $RELATIONSHIPS_JSON | jq -r '.{{ .Database }}[0].scheme')"{{ end }}
{{- end -}}
{{- if eq .Stack.Name "laravel" }}

# Set Laravel-specific environment variables
export DB_CONNECTION="$DB_SCHEME"
{{- end }}
export DATABASE_URL="${DB_SCHEME}://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_PATH}"
{{- if .Cache }}

# Set Cache environment variables
Expand Down
2 changes: 2 additions & 0 deletions platformifier/templates/generic/.platform.app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ variables:
# XDEBUG_MODE: off
{{- else if eq "nodejs" .Runtime }}
# NODE_ENV: development
{{- else if eq "ruby" .Runtime }}
# RACK_ENV: production
{{- else }}
# ENV_VARIABLE_NAME: "value"
{{- end }}
Expand Down
10 changes: 6 additions & 4 deletions platformifier/templates/upsun/.environment
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@
export DB_HOST="${{ .DatabaseUpper }}_HOST"
export DB_PORT="${{ .DatabaseUpper }}_PORT"
export DB_PATH="${{ .DatabaseUpper }}_PATH"
export DB_DATABASE="$DB_PATH"
export DB_USERNAME="${{ .DatabaseUpper }}_USERNAME"
export DB_PASSWORD="${{ .DatabaseUpper }}_PASSWORD"
{{ if eq .Database "postgresql" }}export DB_SCHEME="postgresql"{{ else }}export DB_SCHEME="${{ .DatabaseUpper }}_SCHEME"{{ end }}
export DATABASE_URL="${DB_SCHEME}://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_PATH}"
{{ if eq .Database "postgresql" }}export DB_SCHEME="postgresql"
{{- else if and (eq .Stack.Name "rails") (or (eq .Database "mariadb") (eq .Database "mysql")) }}export DB_SCHEME="mysql2" # mysql/mariadb rails adapter
{{- else }}export DB_SCHEME="${{ .DatabaseUpper }}_SCHEME"{{ end }}
{{- end -}}
{{- if eq .Stack.Name "laravel" }}

# Set Laravel-specific environment variables
export DB_CONNECTION="$DB_SCHEME"
export DB_DATABASE="$DB_PATH"
{{- end -}}
{{- end }}
export DATABASE_URL="${DB_SCHEME}://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_PATH}"
{{- if .Cache }}

# Set Cache environment variables
Expand Down
2 changes: 2 additions & 0 deletions platformifier/templates/upsun/.upsun/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ applications:
# XDEBUG_MODE: off
{{- else if eq "nodejs" .Runtime }}
# NODE_ENV: development
{{- else if eq "ruby" .Runtime }}
# RACK_ENV: production
{{- else }}
# ENV_VARIABLE_NAME: "value"
{{- end }}
Expand Down

0 comments on commit 2ddf43f

Please sign in to comment.