Class: Jets::Builders::ReconfigureRails

Inherits:
Object
  • Object
show all
Defined in:
lib/jets/builders/reconfigure_rails.rb

Instance Method Summary collapse

Constructor Details

#initialize(full_app_root) ⇒ ReconfigureRails

Returns a new instance of ReconfigureRails.



4
5
6
7
# File 'lib/jets/builders/reconfigure_rails.rb', line 4

def initialize(full_app_root)
  # IE: @app_root: /tmp/jets/demo/stage/code/rack
  @app_root = full_app_root
end

Instance Method Details

#add_jets_rails_gem(lines) ⇒ Object



38
39
40
41
42
43
# File 'lib/jets/builders/reconfigure_rails.rb', line 38

def add_jets_rails_gem(lines)
  return lines if lines.detect { |l| l =~ /jets-rails/ }
  lines << "\n"
  lines << %Q|gem "jets-rails"\n|
  lines
end

#comment_out_ruby_declaration(lines) ⇒ Object

Jets packages up and uses only one version of ruby and different declarations of ruby can cause issues.



47
48
49
50
51
# File 'lib/jets/builders/reconfigure_rails.rb', line 47

def comment_out_ruby_declaration(lines)
  lines.map do |l|
    l =~ /^(ruby[ (].*)/ ? "# #{$1} # commented out by jets" : l
  end
end

#copy_initializerObject



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

def copy_initializer
  templates = File.expand_path("./reconfigure_rails", File.dirname(__FILE__))
  relative_path = "config/initializers/jets.rb"
  src = "#{templates}/#{relative_path}"
  dest = "#{@app_root}/#{relative_path}"
  FileUtils.mkdir_p(File.dirname(dest))
  FileUtils.cp(src, dest)
end

#rails?Boolean

Rudimentary rails detection

Returns:

  • (Boolean)


93
94
95
96
97
# File 'lib/jets/builders/reconfigure_rails.rb', line 93

def rails?
  config_ru = "#{@app_root}/config.ru"
  return false unless File.exist?(config_ru)
  !IO.readlines(config_ru).grep(/Rails.application/).empty?
end

#runObject

Only support Rails right now. Maybe move into plugin when adding support to more frameworks. Or maybe better to just abstract but maintain in Jets.



11
12
13
14
15
16
17
18
19
# File 'lib/jets/builders/reconfigure_rails.rb', line 11

def run
  return unless rails?

  puts "Mega Mode: Reconfiguring Rails app."
  copy_initializer
  update_gemfile
  update_development_environment
  set_favicon
end

#set_faviconObject

Evaluate the application layout and see if has a custom favicon defined yet. If not insert one and rewrite the application layout.

This avoids serving binary assets from API gateway, instead serve it from s3 directly.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/jets/builders/reconfigure_rails.rb', line 70

def set_favicon
  app_layout = "#{@app_root}/app/views/layouts/application.html.erb"
  return  unless File.exist?(app_layout)

  favicon = '<link rel="shortcut icon" href="<%= asset_path("/favicon.ico") %>">'

  lines = IO.readlines(app_layout)
  has_favicon = !!lines.find { |l| l.include?(favicon) }
  return if has_favicon

  content = IO.read(app_layout)
  content = content.sub('</head>', "\n    #{favicon}\n  </head>")

  IO.write(app_layout, content)
end

#update_development_environmentObject



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/jets/builders/reconfigure_rails.rb', line 53

def update_development_environment
  env_file = "#{@app_root}/config/environments/development.rb"
  lines = IO.readlines(env_file)
  new_lines = lines.map do |line|
    if line =~ /(config\.file_watcher.*)/
      "  # #{$1} # commented out by jets\n"
    else
      line
    end
  end
  write_content(env_file, new_lines)
end

#update_gemfileObject



30
31
32
33
34
35
36
# File 'lib/jets/builders/reconfigure_rails.rb', line 30

def update_gemfile
  gemfile = "#{@app_root}/Gemfile"
  lines = IO.readlines(gemfile)
  lines = add_jets_rails_gem(lines)
  lines = comment_out_ruby_declaration(lines)
  write_content(gemfile, lines)
end

#write_content(path, lines) ⇒ Object

lines is an Array



87
88
89
90
# File 'lib/jets/builders/reconfigure_rails.rb', line 87

def write_content(path, lines)
  content = lines.join + "\n"
  IO.write(path, content)
end