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



42
43
44
45
46
47
# File 'lib/jets/builders/reconfigure_rails.rb', line 42

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

#api_mode?Boolean

Returns:

  • (Boolean)


31
32
# File 'lib/jets/builders/reconfigure_rails.rb', line 31

def api_mode?
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.



51
52
53
54
55
# File 'lib/jets/builders/reconfigure_rails.rb', line 51

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
29
# 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}"
  result = Jets::Erb.result(src, api_mode: rails_api?)
  dest = "#{@app_root}/#{relative_path}"
  FileUtils.mkdir_p(File.dirname(dest))
  IO.write(dest, result)
end

#rails?Boolean

Rudimentary rails detection Duplicated in builders/code_builders.rb

Returns:

  • (Boolean)


98
99
100
101
102
# File 'lib/jets/builders/reconfigure_rails.rb', line 98

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

#rails_api?Boolean

Rudimentary rails api detection Duplicated in builders/code_builders.rb Another way of checking is loading a rails console and checking Rails.application.config.api_only Using this way for simplicity.

Returns:

  • (Boolean)


108
109
110
111
112
# File 'lib/jets/builders/reconfigure_rails.rb', line 108

def rails_api?
  config_app = "#{@app_root}/config/application.rb"
  return false unless File.exist?(config_app)
  !IO.readlines(config_app).grep(/config.api_only.*=.*true/).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.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/jets/builders/reconfigure_rails.rb', line 74

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



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/jets/builders/reconfigure_rails.rb', line 57

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



34
35
36
37
38
39
40
# File 'lib/jets/builders/reconfigure_rails.rb', line 34

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



91
92
93
94
# File 'lib/jets/builders/reconfigure_rails.rb', line 91

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