def recipes; @recipes end def recipe?(name); @recipes.include?(name) end def prefs; @prefs end def prefer(key, value); @prefs[key].eql? value end def gems; @gems end def diagnostics_recipes; @diagnostics_recipes end def diagnostics_prefs; @diagnostics_prefs end def say_custom(tag, text); say "\033[1m\033[36m" + tag.to_s.rjust(10) + "\033[0m" + " #{text}" end def say_recipe(name); say "\033[1m\033[36m" + "recipe".rjust(10) + "\033[0m" + " Running #{name} recipe..." end def say_wizard(text); say_custom(@current_recipe || 'composer', text) end def ask_wizard(question) ask "\033[1m\033[30m\033[46m" + (@current_recipe || "prompt").rjust(10) + "\033[1m\033[36m" + " #{question}\033[0m" end def yes_wizard?(question) answer = ask_wizard(question + " \033[33m(y/n)\033[0m") case answer.downcase when "yes", "y" true when "no", "n" false else yes_wizard?(question) end end def no_wizard?(question); !yes_wizard?(question) end def multiple_choice(question, choices) say_custom('question', question) values = {} choices.each_with_index do |choice,i| values[(i + 1).to_s] = choice[1] say_custom( (i + 1).to_s + ')', choice[0] ) end answer = ask_wizard("Enter your selection:") while !values.keys.include?(answer) values[answer] end @current_recipe = nil @configs = {} @after_blocks = [] def after_bundler(&block); @after_blocks << [@current_recipe, block]; end @after_everything_blocks = [] def after_everything(&block); @after_everything_blocks << [@current_recipe, block]; end @before_configs = {} def before_config(&block); @before_configs[@current_recipe] = block; end def copy_from(source, destination) begin remove_file destination get source, destination rescue OpenURI::HTTPError say_wizard "Unable to obtain #{source}" end end def copy_from_repo(filename, opts = {}) repo = 'https://raw.github.com/RailsApps/rails-composer/master/files/' repo = opts[:repo] unless opts[:repo].nil? if (!opts[:prefs].nil?) && (!prefs.has_value? opts[:prefs]) return end source_filename = filename destination_filename = filename unless opts[:prefs].nil? if filename.include? opts[:prefs] destination_filename = filename.gsub(/\-#{opts[:prefs]}/, '') end end if (prefer :templates, 'haml') && (filename.include? 'views') remove_file destination_filename destination_filename = destination_filename.gsub(/.erb/, '.haml') end if (prefer :templates, 'slim') && (filename.include? 'views') remove_file destination_filename destination_filename = destination_filename.gsub(/.erb/, '.slim') end begin remove_file destination_filename if (prefer :templates, 'haml') && (filename.include? 'views') create_file destination_filename, html_to_haml(repo + source_filename) elsif (prefer :templates, 'slim') && (filename.include? 'views') create_file destination_filename, html_to_slim(repo + source_filename) else get repo + source_filename, destination_filename end rescue OpenURI::HTTPError say_wizard "Unable to obtain #{source_filename} from the repo #{repo}" end end def html_to_haml(source) html = open(source) {|input| input.binmode.read } Haml::HTML.new(html, :erb => true, :xhtml => true).render end def html_to_slim(source) html = open(source) {|input| input.binmode.read } haml = Haml::HTML.new(html, :erb => true, :xhtml => true).render Haml2Slim.convert!(haml) end # full credit to @mislav in this StackOverflow answer for the #which() method: # - http://stackoverflow.com/a/5471032 def which(cmd) exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : [''] ENV['PATH'].split(File::PATH_SEPARATOR).each do |path| exts.each do |ext| exe = "#{path}#{File::SEPARATOR}#{cmd}#{ext}" return exe if File.executable? exe end end return nil end