class Clash::Scaffold
Attributes
options[RW]
Public Class Methods
new(options = {})
click to toggle source
# File lib/clash/scaffold.rb, line 5 def initialize(options = {}) @options = { path: 'test', title: 'Test Build', dir: 'test-site', force: false }.merge(options) @options[:path] = File.expand_path(@options[:path], Dir.pwd) end
Public Instance Methods
add_test()
click to toggle source
# File lib/clash/scaffold.rb, line 16 def add_test config = File.join(@options[:path], '_clash.yml') prevent_dir_name_collisions content = test_content if !File.exist?(config) content.lstrip! end path = File.join(@options[:path], @options[:dir]) FileUtils.mkdir_p path FileUtils.cp_r test_template + '/.', path File.open(config, 'a') do |f| f.write content end puts "New Clash test added to " + @options[:path].yellow print_test_files(path) puts "Tests:" end
Private Instance Methods
dasherize(string)
click to toggle source
# File lib/clash/scaffold.rb, line 91 def dasherize(string) string.gsub(/ /,'-').gsub(/[^\w-]/,'').gsub(/-{2,}/,'-').downcase end
preserve_source_location?()
click to toggle source
# File lib/clash/scaffold.rb, line 87 def preserve_source_location? !@options[:force] && !Dir["#{@options[:path]}/**/*"].empty? end
prevent_dir_name_collisions()
click to toggle source
If necessary append a number to directory to avoid directory collision
# File lib/clash/scaffold.rb, line 71 def prevent_dir_name_collisions # Find directories beginning with test directory name # dirs = Dir.glob("#{@options[:path]}/*").select { |d| File.directory?(d) && d.match(/#{@options[:dir]}($|-\d+$)/) }.size # If matching directories are found, increment the dir name # e.g. "test-site-2" # if dirs > 0 @options[:dir] << "-#{dirs += 1}" end end
print_test_files(path)
click to toggle source
# File lib/clash/scaffold.rb, line 43 def print_test_files(path) FileUtils.cd path do files = sort_file_list(Dir['**/*']) files.map! { |f| if f.match /\// f.gsub!(/[^\/]+\//, ' ') end if File.directory?(f) f += '/' end "+ #{f}" } puts "\n+ #{@options[:dir]}/\n#{files.join("\n")}\n".green end end
sort_file_list(files)
click to toggle source
# File lib/clash/scaffold.rb, line 59 def sort_file_list(files) plain_files = files.select { |f| !f.match(/\//) && !File.directory?(f) } files_in_dirs = files - plain_files files_in_dirs.sort.concat plain_files.sort end
test_content()
click to toggle source
# File lib/clash/scaffold.rb, line 95 def test_content %Q{ - title: "#{@options[:title]}" dir: #{@options[:dir]} build: true compare: _expected _site } end
test_template()
click to toggle source
# File lib/clash/scaffold.rb, line 65 def test_template File.expand_path("../../scaffold/site", File.dirname(__FILE__)) end