Class: RailsNewApp::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/rails-new-app/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(navigation: true) ⇒ Runner

Returns a new instance of Runner.



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rails-new-app/runner.rb', line 28

def initialize(navigation: true)
  @menu = MenuScreen.new SCREENS
  @current_screen = @menu
  @navigation = navigation
  @config = {}.tap do |h|
    SCREENS.each do |s|
      kls = s[:class]
      h[kls.key] = kls.default
    end
  end
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



26
27
28
# File 'lib/rails-new-app/runner.rb', line 26

def config
  @config
end

#current_pageObject

Returns the value of attribute current_page.



26
27
28
# File 'lib/rails-new-app/runner.rb', line 26

def current_page
  @current_page
end

#current_screenObject

Returns the value of attribute current_screen.



26
27
28
# File 'lib/rails-new-app/runner.rb', line 26

def current_screen
  @current_screen
end

Returns the value of attribute menu.



26
27
28
# File 'lib/rails-new-app/runner.rb', line 26

def menu
  @menu
end

Instance Method Details

#aborted_messageObject



175
176
177
# File 'lib/rails-new-app/runner.rb', line 175

def aborted_message
  puts "Aborted"
end

#after_createObject



166
167
168
169
# File 'lib/rails-new-app/runner.rb', line 166

def after_create
  fix_code_style
  initial_commit
end

#build_rails_new_commandObject

final step and creation



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/rails-new-app/runner.rb', line 180

def build_rails_new_command
  ["rails"].tap do |ar|
    # new command
    ar << "new"
    # use desired database
    ar << "--database=#{config[:database][:key]}" if config[:database][:in_rails_new] && !config[:database][:is_default]
    # ignore test if not minitest
    ar << "--skip-test" if config[:test_runner][:key] != "minitest"
    # use desired js framework
    ar << "--webpack=#{config[:java_script_framework][:key]}" if config[:java_script_framework][:in_rails_new]
    # ar << "--skip-javascript"
    # add app name
    ar << config[:app_name]
  end.join(" ")
end

#end_messageObject



171
172
173
# File 'lib/rails-new-app/runner.rb', line 171

def end_message
  puts "Your new Rails app is ready!"
end

#fix_code_styleObject



196
197
198
199
200
201
# File 'lib/rails-new-app/runner.rb', line 196

def fix_code_style
  case config[:ruby_linter][:key]
  when "rubocop" then run_cmnd("rubocop -A")
  when "standardrb" then run_cmnd("standardrb --fix")
  end
end

#initial_commitObject



203
204
205
206
# File 'lib/rails-new-app/runner.rb', line 203

def initial_commit
  run_cmnd("git add .")
  run_cmnd("git commit -a -m 'Initial commit'")
end

#introObject



63
64
65
66
# File 'lib/rails-new-app/runner.rb', line 63

def intro
  clear
  puts "Let's create a new Rails app step by step"
end

#process_configObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/rails-new-app/runner.rb', line 130

def process_config
  # cd into rails app
  Dir.chdir(config[:app_name]) do
    # add different gems
    [
      TestRunnerProcessor,
      CodeCoverageProcessor,
      TestFactoryProcessor,
      TestFakeDataProcessor,
      TemplateEngineProcessor,
      FormBuilderProcessor,
      RubyLinterProcessor,
      PaginationProcessor,
      AuthorizationProcessor,
      AuthenticationProcessor
    ].each { |p| p.update_gemfile(config) }

    # install gems
    run_cmnd("bundle install")

    # configure each gem
    [
      TestRunnerProcessor,
      CodeCoverageProcessor,
      TestFactoryProcessor,
      FormBuilderProcessor,
      RubyLinterProcessor,
      PaginationProcessor,
      AuthorizationProcessor,
      AuthenticationProcessor
    ].each { |p| p.configure(config) }

    after_create
  end
end

#process_user_input(option) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rails-new-app/runner.rb', line 89

def process_user_input(option)
  next_step, new_config = @current_screen.process_user_input(option)

  if new_config
    @config[@current_screen.class.key] = new_config
  end

  case next_step
  when :menu then @current_screen = @menu
  when :review_and_confirm then @current_screen = ReviewAndConfirmScreen.new(config)
  when :finish, :abort, :rerender then next_step
  else
    @current_screen = get_screen(next_step).new(config)
  end
end

#rails_newObject



123
124
125
126
127
128
# File 'lib/rails-new-app/runner.rb', line 123

def rails_new
  puts "Running Rails new"
  command = build_rails_new_command
  puts command
  run_cmnd(command)
end

#runObject

entry point



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rails-new-app/runner.rb', line 41

def run
  intro

  confirmation =
    if @navigation
      start
    else
      steps
      review_and_confirm
    end

  if confirmation == :finish
    rails_new
    process_config
    end_message
    0
  else
    aborted_message
    1
  end
end

#show_current_screenObject



68
69
70
# File 'lib/rails-new-app/runner.rb', line 68

def show_current_screen
  puts current_screen.screen_text
end

#startObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rails-new-app/runner.rb', line 72

def start
  next_do = nil

  loop do
    show_current_screen
    option = gets.chomp.strip

    next_do = process_user_input(option)
    case next_do
    when :finish, :abort then break
    end
    clear
  end

  next_do
end

#stepsObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/rails-new-app/runner.rb', line 105

def steps
  config[:app_name] = AppNameScreen.run(config)
  config[:database] = DatabaseScreen.run(config)
  config[:test_runner] = TestRunnerScreen.run(config)

  if config[:test_runner][:key] != ""
    config[:code_coverage] = CodeCoverageScreen.run(config)
    config[:test_factory] = TestFactoryScreen.run(config)
    config[:test_fake_data] = TestFakeDataScreen.run(config)
  end

  config[:js_framework] = JavaScriptFrameworkScreen.run(config)
  config[:ruby_linter] = RubyLinterScreen.run(config)
  config[:template_engine] = TemplateEngineScreen.run(config)
  config[:form_builder] = FormBuilderScreen.run(config)
  config[:pagination] = PaginationScreen.run(config)
end