lib/bently/recipe.rb in bently-0.0.0 vs lib/bently/recipe.rb in bently-0.1.0

- old
+ new

@@ -1,133 +1,63 @@ module Bently - - class Recipe - - BUNDLE_INSTALL = "bundle install" - RECIPE_DIR = "#{BENTLY_REPOSITORY}/lib/bently/recipe/*.rb" - - # list all recipes - def self.list - Dir[RECIPE_DIR].map{ |f| File.basename f, '.rb' }.sort + + module RecipeMethods + def self.extended(base) + base.send(:include, InstanceMethods) if base.is_a? Class end - # get recipe class from name - def self.from_name name - "Bently::#{name.camelize}".constantize + def step(*args) + step_args << args end - - - def initialize options={} - @read_only = options[:read_only] + def step_args + @steps_args ||= [] end - # bake a recipe - def bake - puts_step "Done!" - end + private - protected - - # instance to CLI - # for access to Thor helper methods - def cli; @cli ||= CLI.new end - - # add a gem to the Gemfile - def add_gem gem_def - unless @read_only - if confirm_step "Add to Gemfile" - begin - cli.append_to_file 'Gemfile', gem_def - rescue - puts "Gemfile was not found. Aborting.." - return false - end - end - else - puts_quote_step "Add to Gemfile", gem_def - end - true + def inherited(subclass) + super + subclass.step_args.concat self.step_args end - # run bundle install - def bundle_install - command BUNDLE_INSTALL - end - - # execute a command - def command cmd - unless @read_only - puts `#{cmd}` if confirm_step cmd - else - puts_step cmd + module InstanceMethods + def steps + @steps ||= parse_steps end - end - # executes a command with user input - def ask_command cmd, prompt - unless @read_only - if confirm_step cmd - resp = cli.ask prompt - puts `#{yield resp}` + protected + + def parse_steps + self.class.step_args.map do |args| + self.send(args.shift, *args) end - else - puts_step cmd end end + end - # creates a file - def create_file destination, data - unless @read_only - cli.create_file destination, data if confirm_step "Touch #{destination}" - else - puts_quote_step "Touch #{destination}", data - end - end + class Recipe + extend RecipeMethods - # gsubs a file - def gsub_file file, search, replace - unless @read_only - if confirm_step "Edit #{file}" - cli.gsub_file file, search, replace - end - else - puts_step "Edit #{file}" - end - end + # step :shell, command + def shell(*args); StepShell.new(*args) end - # outputs a step with a blockquote - def puts_quote_step text, quote - puts_step "#{text}" - puts - puts "#{quote}" - puts - end + # step :touch, :file => filename, :with => data + def touch(*args) StepTouch.new(*args) end - # outputs a step for confirmation - def confirm_step text - cli.yes? "#{magenta('==>')} #{bold(text)}?" - end + # step :modify, :file => filename, :from => pattern, :to => replacement + def modify(*args) StepModify.new(*args) end - # outputs a step - def puts_step text - puts "#{magenta('==>')} #{bold(text)}" - end + # step :append, :file => filename, :with => data + def append(*args) StepAppend.new(*args) end - # colorizes text - def colorize(text, color_code) - "#{color_code}#{text}\033[0m" - end + # step :prepend, :file => filename, :with => data + def prepend(*args) StepPrepend.new(*args) end - def red(text); colorize(text, "\033[31m"); end - def green(text); colorize(text, "\033[32m"); end - def magenta(text); colorize(text, "\033[35m"); end - def light_green(text); colorize(text, "\033[32m"); end - def bold(text); "\033[1m#{text}\033[22m"; end + # step :insert, :file => filename, :with => data, :after => some_text + def insert(*args) StepInsert.new(*args) end + # step :remove, :file => filename + def remove(*args) StepRemove.new(*args) end end -end -# load recipes -Dir["#{Bently::BENTLY_REPOSITORY}/lib/bently/recipe/*.rb"].map{ |f| File.basename f, '.rb' }.each do |f| - require 'bently/recipe/' + f end