panych
4/29/2014 - 8:04 AM

Create block files (stylesheet, kitchen page, layout)

Create block files (stylesheet, kitchen page, layout)

desc 'Create block stylesheet file and kitchen page'
task :create_block, [:name] do |t, args|
  block_name = args[:name]
  if block_name == nil
    print "Block name is required \n"
  else
    # TODO Check name for convention validity ('my-super-block', not 'mySuper_block')
    
    # Create stylesheet file
    style_file_path = "#{Rails.root}/app/assets/stylesheets/blocks/_#{block_name}.scss"
    unless File.exists?(style_file_path)
      File.open(style_file_path, 'w+') do |f|
        f.write(".#{block_name} {}")
        print "File #{style_file_path} created. \n"
      end
    else
      print "File #{style_file_path} already exist. \n"
    end
    
    # Create kitchen page
    kitchen_file_name = block_name.gsub('-', '_')
    kitchen_file_path = "#{Rails.root}/app/views/kitchen/#{kitchen_file_name}.slim"
    unless File.exists?(kitchen_file_path)
      File.open(kitchen_file_path, 'w+') do |f|
        f.write("h1 #{block_name}")
        print "File #{kitchen_file_path} created. \n"
      end
    else
      print "File #{kitchen_file_path} already exist. \n"
    end
    
    # Create template file
    template_file_name = kitchen_file_name
    template_file_dir = "#{Rails.root}/app/views/layouts/blocks/#{template_file_name}"
    template_file_path = "#{Rails.root}/app/views/layouts/blocks/#{template_file_name}/_default.slim"
    unless File.exists?(template_file_dir)
      Dir.mkdir(template_file_dir)
      File.open(template_file_path, 'w+') do |f|
        f.write(".#{block_name} #{block_name}")
        print "File #{template_file_path} created. \n"
      end
    else
      print "File #{template_file_path} already exist. \n"
    end
  end
end