Class: Fly::Generators::AppGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
FlyIoRails::Utils
Defined in:
lib/generators/fly/app_generator.rb

Instance Method Summary collapse

Methods included from FlyIoRails::Utils

#create_app, tee, #tee

Instance Method Details

#generate_appObject



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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/generators/fly/app_generator.rb', line 21

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

  # the plan is to make eject an option, default to false, but until
  # that is ready, have generate fly:app always eject
  opts = options.to_h.symbolize_keys
  opts[:eject] = opts[:nomad] if opts[:eject] == nil

  if File.exist? 'fly.toml'
    toml = TOML.load_file('fly.toml')
    opts[:name] ||= toml['app']
    apps = JSON.parse(`flyctl list apps --json`) rescue []

    if toml.keys.length == 1
      if opts[:name] != toml['app']
        # replace existing fly.toml
        File.unlink 'fly.toml'
        create_app(**opts.symbolize_keys)
      elsif not apps.any? {|item| item['ID'] == opts[:name]}
        create_app(**opts.symbolize_keys)
      end
    elsif opts[:name] != toml['app']
      say_status "fly:app", "Using the name in the existing toml file", :red
      opts[:name] = toml['app']
    end
  else
    create_app(**opts.symbolize_keys)
  end

  action = Fly::Actions.new(@app, opts)

  action.generate_toml
  action.generate_fly_config unless File.exist? 'config/fly.rb'

  if opts[:eject]
    action.generate_dockerfile
    action.generate_dockerignore unless File.exist? '.dockerignore'
    action.generate_nginx_conf unless File.exist? 'config/nginx.conf'
    action.generate_raketask unless File.exist? 'lib/tasks/fly.rake'
    action.generate_procfile unless File.exist? 'Procfile.rake'
    action.generate_litefs if opts[:litefs] and not File.exist? 'config/litefs'
    action.generate_patches
  end

  ips = `flyctl ips list`.strip.lines[1..].map(&:split).map(&:first)
  action.generate_ipv4 unless ips.include? 'v4'
  action.generate_ipv6 unless ips.include? 'v6'

  action.launch(action.app)
end