Class: Jets::Turbo::Rails

Inherits:
Object
  • Object
show all
Defined in:
lib/jets/turbo/rails.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRails

Returns a new instance of Rails.



4
5
6
7
# File 'lib/jets/turbo/rails.rb', line 4

def initialize
  @build_area = "/tmp/jets"
  @project_folder = "turbo-wrapper-project"
end

Instance Attribute Details

#build_areaObject (readonly)

Returns the value of attribute build_area.



3
4
5
# File 'lib/jets/turbo/rails.rb', line 3

def build_area
  @build_area
end

Instance Method Details

#afterburners_messageObject



21
22
23
24
25
26
27
28
# File 'lib/jets/turbo/rails.rb', line 21

def afterburners_message
  command = ARGV.first
  if command == "deploy"
    puts "=> Rails app detected: Enabling Jets Afterburner to deploy to AWS Lambda.".color(:green)
  else
    puts "=> Rails app detected: Enabling Jets Afterburner.".color(:green)
  end
end

#apply_dot_jets_projectObject

Anything in rails_project/.jets/project will override the generic wrapper project.

rails_project/.jets/project/.env => jets_project/.env
rails_project/.jets/project/config/database.yml => jets_project/config/database.yml

This useful for DATABASE_URL and other env vars.



90
91
92
93
94
95
96
97
98
# File 'lib/jets/turbo/rails.rb', line 90

def apply_dot_jets_project
  # Dir.pwd: /home/ec2-user/environment/demo-rails
  # Jets.root: /tmp/jets/turbo-wrapper-project/
  dot_jets_app = "#{Dir.pwd}/.jets/project"

  return unless File.exist?(dot_jets_app)
  # Trailing slashes are required for both folders. Jets.root already has the trailing slash
  sh "rsync -a --links #{dot_jets_app}/ #{Jets.root}", quiet: true
end

#bundle_installObject

TODO: remove duplication, copied from jets/commands/import/base.rb And modified it slightly



112
113
114
115
116
# File 'lib/jets/turbo/rails.rb', line 112

def bundle_install
  Bundler.with_unbundled_env do
    sh "cd #{build_area}/#{@project_folder}/rack && bundle install"
  end
end

#check_old_dot_jets_app_folder!Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/jets/turbo/rails.rb', line 30

def check_old_dot_jets_app_folder!
  return unless File.exist?(".jets/app")

  puts <<~EOL.color(:red)
    ERROR: .jets/app folder exists. Starting in version jets v1.9.23 this folder should be renamed to .jets/project.
    Please run:

        mv .jets/app .jets/project

  EOL
  exit 1
end

#cleanObject



123
124
125
# File 'lib/jets/turbo/rails.rb', line 123

def clean
  FileUtils.rm_rf("#{build_area}/#{@project_folder}/rack")
end

#copy_rack_projectObject



104
105
106
107
108
# File 'lib/jets/turbo/rails.rb', line 104

def copy_rack_project
  dest = "#{build_area}/#{@project_folder}/rack"
  # puts "cp -r #{Dir.pwd} #{dest}" # uncomment to see and debug
  Jets::Util.cp_r(Dir.pwd, dest)
end

#override_env_varsObject

Hack env vars to support Jets Turbo mode



44
45
46
47
48
# File 'lib/jets/turbo/rails.rb', line 44

def override_env_vars
  ENV['BUNDLE_GEMFILE'] = "#{build_area}/#{@project_folder}/Gemfile"
  ENV['JETS_ROOT'] = "#{build_area}/#{@project_folder}"
  # Jets.root /home/ec2-user/environment/jet-pack/lib/turbo-wrapper-project
end

#project_nameObject



74
75
76
77
78
79
80
81
82
# File 'lib/jets/turbo/rails.rb', line 74

def project_name
  path = "#{build_area}/#{@project_folder}/project_name"
  name = if File.exist?(path)
            IO.read(path).strip # project_name
          else
            File.basename(Dir.pwd)
          end
  name.gsub('_','-') # project_name
end

#reconfigure_database_ymlObject



100
101
102
# File 'lib/jets/turbo/rails.rb', line 100

def reconfigure_database_yml
  DatabaseYaml.new.reconfigure
end

#set_project_nameObject



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/jets/turbo/rails.rb', line 61

def set_project_name
  path = "#{build_area}/#{@project_folder}/config/application.rb"
  lines = IO.readlines(path)
  lines.map! do |l|
    if l.include?('config.project_name = "project"')
      %Q|  config.project_name = "#{project_name}"\n|
    else
      l
    end
  end
  IO.write(path, lines.join(''))
end

#setupObject



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/jets/turbo/rails.rb', line 9

def setup
  afterburners_message
  check_old_dot_jets_app_folder!
  clean
  override_env_vars
  wrapper_jets_project
  copy_rack_project
  reconfigure_database_yml
  apply_dot_jets_project
  set_project_name
end

#sh(command, quiet: false) ⇒ Object



118
119
120
121
# File 'lib/jets/turbo/rails.rb', line 118

def sh(command, quiet: false)
  puts "=> #{command}" unless quiet
  system command
end

#wrapper_jets_projectObject



50
51
52
53
54
55
56
57
58
59
# File 'lib/jets/turbo/rails.rb', line 50

def wrapper_jets_project
  jets_project = File.expand_path("project", File.dirname(__FILE__))
  project_path = "#{build_area}/#{@project_folder}"

  FileUtils.mkdir_p(build_area)
  FileUtils.rm_rf(project_path)
  Jets::Util.cp_r(jets_project, project_path)

  IO.write("#{project_path}/date.txt", Time.now) # update date.txt file to ensure Lambda function code changes and updates
end