Class: AtcGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/atc_generator.rb

Instance Method Summary collapse

Instance Method Details

#generate_appObject



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/generators/atc_generator.rb', line 2

def generate_app
  source_paths.push File.expand_path("./templates", __dir__)

  ### config/routes.rb

  @routes = IO.read("config/routes.rb")

  unless @routes.include? "fly_atc_scope"
    _, prolog, routes = @routes.split(/(.*Rails.application.routes.draw do\n)/m,2)
    routes, epilog, _ = routes.split(/^(end.*)/m,2)
    routes = routes.split(/\n\s*\n/)
    scoped = routes.select {|route| route =~ /^\s*\w/ && !route.include?('as:')}

    @routes = <<~EOF
      #{prolog.rstrip}
        fly_atc_scope = ENV.fetch("FLY_ATC_SCOPE", "")

        unless fly_atc_scope == ""
          mount ActionCable.server => "/\#{fly_atc_scope}/cable"
        end

        scope fly_atc_scope do
      #{scoped.join("\n\n").gsub(/^ /, "   ")}
        end

      #{(routes-scoped).join("\n\n").rstrip}
      #{epilog.rstrip}
    EOF
  end

  template "routes.erb", "config/routes.rb"

  ### app/views/layouts/application.html.erb

  @layout = IO.read("app/views/layouts/application.html.erb")

  unless @layout.include? "action_cable_meta_tag_dynamic"
    @layout[/<meta.*?\n()\r?\n/m, 1] = "    <%= action_cable_meta_tag_dynamic %>\n"
  end

  template "application.html.erb", "app/views/layouts/application.html.erb"

  ### bin/fly-atc

  unless File.exist?("bin/fly-atc")
    system "bundle binstubs fly-atc"
  end

  ### Dockerfile

  gsub_file "Dockerfile", "bin/thrust", "bin/fly-atc"
end