Class: Watchdocs::Generators::InstallGenerator

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

Instance Method Summary collapse

Instance Method Details

#create_initializer_fileObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/generators/watchdocs/install_generator.rb', line 14

def create_initializer_file
  if !options['app_id'].present? || !options['app_secret'].present?
    raise MissingCredentialsError, <<-ERROR.strip_heredoc
    --app_id or/and --app_secret options is missing
    Please specify both with credentials from Settings tab of your project.
    ERROR
  end

  initializer 'watchdocs.rb' do
    <<-END.gsub(/^\s+\|/, '')
      |Watchdocs::Rails.configure do |c|
      |  c.app_id = '#{options['app_id']}'
      |  c.app_secret = '#{options['app_secret']}'
      |end
    END
  end
end

#install_with_runtimeObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/generators/watchdocs/install_generator.rb', line 80

def install_with_runtime
  if yes?('Do you want to record your API endpoints in developent environment? [y,n]')
    application(nil, env: 'development') do
      'config.middleware.insert(0, Watchdocs::Rails::Middleware)'
    end
    if yes?('  Do you use Procfile to run your app? [y,n]')
      append_file 'Procfile', 'watchdocs: watchdocs --every 60.seconds'
      puts 'Watchdocs worker added to your Procfile!'.green
    else
      warning = <<-END.gsub(/^\s+\|/, '')
        |  To make sure we will receive all recorded request please run this worker command with your app:
        |  -----------------------------
        |  watchdocs --every 60.seconds
        |  -----------------------------
      END
      puts warning.yellow

    end
    info = <<-END.gsub(/^\s+\|/, '')
      |  If you would like to enable recording in any other environment (f.e. dev server, staging)
      |  just add the following line to you 'config/environments/{env}.rb':
      |  -----------------------------------------------------------
      |    config.middleware.insert(0, Watchdocs::Rails::Middleware)
      |  -----------------------------------------------------------
    END
    puts info.light_blue
  end
  puts 'Setup is completed. Now run your app and make some requests!
  Check app.watchdocs.io after a minute or so.'.green
end

#install_with_specsObject



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
71
72
73
74
75
76
77
78
# File 'lib/generators/watchdocs/install_generator.rb', line 32

def install_with_specs
  if yes?('Do you have any request specs? Would you like to use Watchdocs with them in test environment? [y,n]')
    application(nil, env: 'test') do
      'config.middleware.insert(0, Watchdocs::Rails::Middleware)'
    end
    if yes?('  Do you test with RSpec? [y,n]')
      template 'rspec.rb', 'spec/support/watchdocs.rb'
      begin
        append_file 'spec/rails_helper.rb', "require 'support/watchdocs.rb'"
      rescue Errno::ENOENT
        warning = <<-END.gsub(/^\s+\|/, '')
          |  You don't have rails_helper.rb, so please add the following line
          |  to the file where you have your RSpec configuration.
          |  -------------------------------
          |  require 'support/watchdocs.rb'
          |  -------------------------------
        END
        puts warning.yellow
      end
    elsif yes?('  Ok, maybe you test with a Minitest? [y,n]')
      begin
        append_file 'test/test_helper.rb',
                  'Minitest.after_run { Watchdocs::Rails::Recordings.export }'
      rescue Errno::ENOENT
        warning = <<-END.gsub(/^\s+\|/, '')
          |  You don't have test_helper.rb, so please add the following line
          |  to the file where you have your Minitest configuration.
          |  ----------------------------------------------------------
          |  Minitest.after_run { Watchdocs::Rails::Recordings.export }
          |  ----------------------------------------------------------
        END
        puts warning.yellow
      end
    else
      warning = <<-END.gsub(/^\s+\|/, '')
        |  We are sorry, but we don't have auto setup for you testing framework.
        |  You need to manually add the following line to be executed after all your specs:
        |  -----------------------------------
        |  Watchdocs::Rails::Recordings.export
        |  -----------------------------------
      END
      puts warning.yellow
    end
    puts 'Watchdocs enabled for test environment!'.green

  end
end