Class: Beet::Executor

Inherits:
Object
  • Object
show all
Includes:
Capistrano, CommandExecution, FileSystem, Interaction, Rails, SCM
Defined in:
lib/beet/executor.rb

Constant Summary collapse

BEET_DATA_FILE =
"~/.beet.yml"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SCM

#git

Methods included from Capistrano

#capify!

Methods included from Rails

#environment, #freeze!, #gem, #gem_group, #gemfile, #generate, #initializer, #plugin, #route, #vendor

Methods included from Interaction

#ask, #no?, #yes?

Methods included from FileSystem

#add_after, #add_after_or_append, #add_before, #append_file, #file, #gsub_file, #in_root, #inside, #lib, #rakefile

Methods included from CommandExecution

#rake, #run, #run_ruby_script, #sudo

Constructor Details

#initialize(project_name, options = {}) ⇒ Executor

:nodoc:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/beet/executor.rb', line 24

def initialize(project_name, options={}) # :nodoc:
  @root = calculate_project_root(project_name)
  @project_name = ((project_name == '.') ? File.basename(Dir.pwd) : project_name)
  @logger = Beet::Logger.new
  @gems = []
  @template = options[:template]
  @options = options
  @todo_items = ''
  @recipes = []
  @project_type = (options[:project_type] && options[:project_type].to_sym) || :rails3
  @generate = true unless options[:generate] == false
  @display = options[:display]
  extract_commands_from_options
end

Instance Attribute Details

#gemsObject

Returns the value of attribute gems.



22
23
24
# File 'lib/beet/executor.rb', line 22

def gems
  @gems
end

#loggerObject (readonly)

Returns the value of attribute logger.



21
22
23
# File 'lib/beet/executor.rb', line 21

def logger
  @logger
end

#optionsObject (readonly)

Returns the value of attribute options.



21
22
23
# File 'lib/beet/executor.rb', line 21

def options
  @options
end

#project_nameObject

Returns the value of attribute project_name.



22
23
24
# File 'lib/beet/executor.rb', line 22

def project_name
  @project_name
end

#recipesObject

Returns the value of attribute recipes.



22
23
24
# File 'lib/beet/executor.rb', line 22

def recipes
  @recipes
end

#rootObject (readonly)

Returns the value of attribute root.



21
22
23
# File 'lib/beet/executor.rb', line 21

def root
  @root
end

#templateObject (readonly)

Returns the value of attribute template.



21
22
23
# File 'lib/beet/executor.rb', line 21

def template
  @template
end

#todo_itemsObject

Returns the value of attribute todo_items.



22
23
24
# File 'lib/beet/executor.rb', line 22

def todo_items
  @todo_items
end

Instance Method Details

#log(*args) ⇒ Object



105
106
107
# File 'lib/beet/executor.rb', line 105

def log(*args)
  logger.log(*args)
end

#run_recipesObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/beet/executor.rb', line 90

def run_recipes
  @recipes.each do |recipe|
    begin
      code = open(recipe).read
      if @display
        puts code
      else
        in_root { instance_eval(code)}
      end
    rescue LoadError, Errno::ENOENT => e
      raise "The recipe [#{recipe}] could not be loaded. Error: #{e}"
    end
  end
end

#startObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/beet/executor.rb', line 39

def start
  if options[:use]
    puts "Loading saved configuration: #{@options[:use]}"
    data = load_saved_recipe_file
    if config = data[@options[:use]]
      @gems.concat(config[:gems]) if config[:gems]
      @template = config[:template] if config[:template]
      @recipes.concat(config[:recipes]) if config[:recipes]
    end
  end

  if @display && @template
    puts open(TEMPLATE_LOCATIONS[@template]).read
  else
    if @generate
      # TODO maybe let people specify path to rails 3 script rather than just assume its 'rails'
      if @project_type == :rails3
        puts "Generating rails 3 project #{project_name}..."
        if @template
          system("rails new #{project_name} -m #{TEMPLATE_LOCATIONS[@template]}")
        else
          system("rails new #{project_name}")
        end
      else
        unless rails2_version = search_for_rails_2
          puts "Please create a rails2 command which points to rails 2.x executable."
          exit
        else
          puts "Generating rails project #{project_name}..."
          if @template
            system("rails _#{rails2_version}_ #{project_name} -m #{TEMPLATE_LOCATIONS[@template]}")
          else
            system("rails _#{rails2_version}_ #{project_name}")
          end
        end
      end
    end

    add_gems

    print_todo

    if options[:save]
      save_run
    end
  end

  run_recipes

end

#todo(string = nil, &block) ⇒ Object



109
110
111
# File 'lib/beet/executor.rb', line 109

def todo(string=nil, &block)
  self.todo_items << (string || block.call)
end