Class: Jets::Turbo::Rail

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRail

Returns a new instance of Rail.



4
5
6
7
# File 'lib/jets/turbo/rail.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/rail.rb', line 3

def build_area
  @build_area
end

Instance Method Details

#afterburners_messageObject



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

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

#apply_dot_jets_appObject

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

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

This useful for DATABASE_URL and other env vars.



76
77
78
79
80
81
82
83
84
# File 'lib/jets/turbo/rail.rb', line 76

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

  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



98
99
100
101
102
# File 'lib/jets/turbo/rail.rb', line 98

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

#cleanObject



109
110
111
# File 'lib/jets/turbo/rail.rb', line 109

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

#copy_rack_projectObject



90
91
92
93
94
# File 'lib/jets/turbo/rail.rb', line 90

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



30
31
32
33
34
# File 'lib/jets/turbo/rail.rb', line 30

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



60
61
62
63
64
65
66
67
68
# File 'lib/jets/turbo/rail.rb', line 60

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



86
87
88
# File 'lib/jets/turbo/rail.rb', line 86

def reconfigure_database_yml
  DatabaseYaml.new.reconfigure
end

#set_project_nameObject



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/jets/turbo/rail.rb', line 47

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
# File 'lib/jets/turbo/rail.rb', line 9

def setup
  afterburners_message
  clean
  override_env_vars
  wrapper_jets_project
  copy_rack_project
  reconfigure_database_yml
  apply_dot_jets_app
  set_project_name
end

#sh(command, quiet: false) ⇒ Object



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

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

#wrapper_jets_projectObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/jets/turbo/rail.rb', line 36

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