Class: Brut::CLI::Apps::Scaffold::Component

Inherits:
Command
  • Object
show all
Defined in:
lib/brut/cli/apps/scaffold.rb

Instance Method Summary collapse

Methods inherited from Command

#after_bootstrap, args, #args, #before_execute, command_name, default_env, #delegate_to_commands, description, detailed_description, env_var, #err, #global_options, #handle_bootstrap_exception, #initialize, name_matches?, option_parser, #options, opts, #out, requires_project_env, requires_project_env?, #system!

Methods included from Framework::Errors

#abstract_method!, #bug!

Methods included from I18n::ForCLI

#capture, #html_escape, #safe

Methods included from I18n::BaseMethods

#l, #t, #t_direct, #this_field_value

Methods included from ExecutionResults

#abort_execution, #as_execution_result, #cli_usage_error, #continue_execution, #show_cli_usage, #stop_execution

Constructor Details

This class inherits a constructor from Brut::CLI::Command

Instance Method Details

#executeObject



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/brut/cli/apps/scaffold.rb', line 191

def execute
  if args.length != 1
    raise "component requires exactly one argument, got #{args.length}"
  end
  class_name = RichString.new(args[0]).capitalize(:first_only)
  if class_name.to_s !~ /Component$/
    class_name = RichString.new(class_name.to_s + "Component")
  end

  relative_path = class_name.underscorized

  components_src_dir   = Brut.container.components_src_dir
  components_specs_dir = Brut.container.components_specs_dir

  if options.page?
    components_src_dir   = Brut.container.pages_src_dir
    components_specs_dir = Brut.container.pages_specs_dir
    if class_name.to_s !~ /::/
      raise "component #{class_name} cannot be a page component - it must be an inner class of an existing page"
    else
      existing_page = RichString.new(class_name.to_s.split(/::/)[0..-2].join("::")).underscorized.to_s + ".rb"

      if !(components_src_dir / existing_page).exist?
        raise "#{class_name} was set as a page component, however we cannot find the page it belongs in.  File #{existing_page} does not exist and should contain that page"
      end
    end
  end

  source_path      = Pathname( (components_src_dir / relative_path).to_s + ".rb" )
  spec_path        = Pathname( (components_specs_dir / relative_path).to_s + ".spec.rb" )

  exists = [
    source_path,
    spec_path,
  ].select(&:exist?)

  if exists.any? && !global_options.overwrite?
    exists.each do |path|
      err.puts "'#{path.relative_path_from(Brut.container.project_root)}' exists already"
    end
    err.puts "Re-run with --overwrite to overwrite these files"
    return 1
  end

  if global_options.dry_run?
    out.puts "FileUtils.mkdir_p #{source_path.dirname}"
    out.puts "FileUtils.mkdir_p #{spec_path.dirname}"
  else
    FileUtils.mkdir_p source_path.dirname
    FileUtils.mkdir_p spec_path.dirname

    File.open(source_path,"w") do |file|
      file.puts %{class #{class_name} < AppComponent
  def initialize
  end

  def view_template
h2 { "Welcome to your new template" }
  end
end}
    end
    File.open(spec_path,"w") do |file|
      file.puts %{require "spec_helper"

RSpec.describe #{class_name} do
  it "should have tests" do
expect(true).to eq(false)
  end
end}
    end
  end
  out.puts "Component source is in #{source_path.relative_path_from(Brut.container.project_root)}"
  out.puts "Component test is in   #{spec_path.relative_path_from(Brut.container.project_root)}"
  0
end