# This generator bootstraps a Rails project for use with Cucumber module Cucumber module Generators class SkeletonGenerator < Rails::Generators::Base argument :language, :type => :string, :required => false, :default => 'en' class_option :shebang, :type => :boolean, :aliases => "--shebang", :group => :cucumber, :desc => "Adds shebang marker to script files" class_option :webrat, :type => :boolean, :aliases => "--webrat", :group => :cucumber, :desc => "Use webrat" class_option :capybara, :type => :boolean, :aliases => "--capybara", :group => :cucumber, :desc => "Use capybara" class_option :rspec, :type => :boolean, :aliases => "--rspec", :group => :cucumber, :desc => "Use rspec" class_option :testunit, :type => :boolean, :aliases => "--testunit", :group => :cucumber, :desc => "Use testunit" class_option :spork, :type => :boolean, :aliases => "--spork", :group => :cucumber, :desc => "Use spork" DEFAULT_SHEBANG = File.join(Config::CONFIG['bindir'], Config::CONFIG['ruby_install_name']) def self.source_root @source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates')) end def initialize(*args, &block) super # @language = args.empty? ? 'en' : args.first end def my_options @my_options ||= {} end def spork? options[:spork] end def framework my_options[:framework] ||= detect_current_framework || detect_default_framework end def driver my_options[:driver] ||= detect_current_driver || detect_default_driver end # def embed_template(source, indent='') # template = File.join(File.dirname(__FILE__), 'templates', source) # ERB.new(IO.read(template), nil, '-').result(binding).gsub(/^/, indent) # end def version filename = File.dirname(__FILE__) + '/../../../../VERSION' path = File.expand_path(filename) IO.read(filename).chomp end def create_files if File.exist?('features/step_definitions/webrat_steps.rb') STDERR.puts "Please remove features/step_definitions/webrat_steps.rb\n" + "See upgrading instructions for 0.2.0 in History.txt" exit(1) end if File.exist?('features/support/version_check.rb') STDERR.puts "Please remove features/support/version_check.rb\n" + "See upgrading instructions for 0.2.0 in History.txt" exit(1) end template 'config/cucumber.yml.erb', 'config/cucumber.yml' template 'environments/cucumber.rb.erb', 'config/environments/cucumber.rb' copy_file 'script/cucumber', 'script/cucumber' chmod 'script/cucumber', 0755, :verbose => false, :shebang => options[:shebang] == DEFAULT_SHEBANG ? nil : options[:shebang] empty_directory 'features/step_definitions' template "step_definitions/#{driver}_steps.rb.erb", 'features/step_definitions/web_steps.rb' if language != 'en' template "step_definitions/web_steps_#{language}.rb.erb", "features/step_definitions/web_steps_#{language}.rb" end empty_directory 'features/support' if spork? template 'support/rails_spork.rb.erb', 'features/support/env.rb' else template 'support/rails.rb.erb', 'features/support/env.rb' end copy_file 'support/paths.rb', 'features/support/paths.rb' empty_directory 'lib/tasks' template 'tasks/cucumber.rake.erb', 'lib/tasks/cucumber.rake' gsub_file 'config/database.yml', /test:.*\n/, "test: &TEST\n" unless File.read('config/database.yml').include? 'cucumber:' gsub_file 'config/database.yml', /\z/, "\ncucumber:\n <<: *TEST" end end no_tasks do def embed_file(source, indent='') IO.read(File.join(File.dirname(__FILE__), 'templates', source)).gsub(/^/, indent) end def embed_template(source, indent='') template = File.join(File.dirname(__FILE__), 'templates', source) ERB.new(IO.read(template), nil, '-').result(binding).gsub(/^/, indent) end end def banner "Usage: #{$0} cucumber (language)" end private def empty_directory_with_gitkeep(destination, config = {}) empty_directory(destination, config) create_file("#{destination}/.gitkeep") unless options[:skip_git] end def first_loadable(libraries) require 'rubygems' libraries.each do |library| begin require library[0] return library[1] rescue LoadError => ignore end end return nil end def detect_current_driver detect_in_env([['capybara', :capybara], ['webrat', :webrat ]]) end def detect_default_driver @default_driver = first_loadable([['capybara', :capybara], ['webrat', :webrat ]]) raise "I don't know which driver you want. Use --capybara or --webrat, or gem install capybara or webrat." unless @default_driver @default_driver end def detect_current_framework detect_in_env([['spec', :rspec], ['test/unit', :testunit]]) end def detect_default_framework @default_framework = first_loadable([['spec', :rspec], ['test/unit', :testunit]]) raise "I don't know what test framework you want. Use --rspec or --testunit, or gem install rspec or test-unit." unless @default_framework @default_framework end def detect_in_env(choices) env = File.file?("features/support/env.rb") ? IO.read("features/support/env.rb") : '' choices.each do |choice| detected = choice[1] if env =~ /#{choice[0]}/n return detected if detected end return nil end end end end