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

Inherits:
BaseCommand show all
Defined in:
lib/brut/cli/apps/scaffold.rb

Instance Attribute Summary

Attributes inherited from Commands::BaseCommand

#parent_command

Instance Method Summary collapse

Methods inherited from BaseCommand

#bootstrap?, #default_rack_env

Methods inherited from Commands::BaseCommand

#accepts, #argv, #bootstrap?, #commands, #default_rack_env, #delegate_to_command, #env, #env_vars, #execute, #name, #options, #puts, #stdin, #system!, #theme

Instance Method Details

#args_descriptionObject



197
# File 'lib/brut/cli/apps/scaffold.rb', line 197

def args_description = "ComponentName"

#descriptionObject



196
# File 'lib/brut/cli/apps/scaffold.rb', line 196

def description = "Create a new component and associated test"

#detailed_descriptionObject



198
# File 'lib/brut/cli/apps/scaffold.rb', line 198

def detailed_description = "New components go in the `components/` folder of your app, however using --page will create a 'page private' component.  To do that, the component name must be an inner class of an existing page, for example HomePage::Welcome. This component goes in a sub-folder inside the `pages/` area of your app"

#optsObject



200
201
202
# File 'lib/brut/cli/apps/scaffold.rb', line 200

def opts = super + [
  [ "--page","If set, this component is for a specific page and won't go with the other components"],
]

#runObject



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
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/brut/cli/apps/scaffold.rb', line 204

def run
  if argv.length != 1
    raise "component requires exactly one argument, got #{argv.length}"
  end
  class_name = RichString.new(argv[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? && !options.overwrite?
    exists.each do |path|
      error "'#{path.relative_path_from(Brut.container.project_root)}' exists already"
    end
    error "Re-run with --overwrite to overwrite these files"
    return 1
  end

  if options.dry_run?
    puts "FileUtils.mkdir_p #{source_path.dirname}"
    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
  puts "Component source is in #{source_path.relative_path_from(Brut.container.project_root)}"
  puts "Component test is in   #{spec_path.relative_path_from(Brut.container.project_root)}"
  0
end