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?, #say, #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, #rvm, #sudo

Constructor Details

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

:nodoc:



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

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.



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

def gems
  @gems
end

#loggerObject (readonly)

Returns the value of attribute logger.



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

def logger
  @logger
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#project_nameObject

Returns the value of attribute project_name.



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

def project_name
  @project_name
end

#recipesObject

Returns the value of attribute recipes.



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

def recipes
  @recipes
end

#rootObject (readonly)

Returns the value of attribute root.



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

def root
  @root
end

#ruby_versionObject

Returns the value of attribute ruby_version.



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

def ruby_version
  @ruby_version
end

#templateObject (readonly)

Returns the value of attribute template.



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

def template
  @template
end

#todo_itemsObject

Returns the value of attribute todo_items.



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

def todo_items
  @todo_items
end

Instance Method Details

#log(*args) ⇒ Object



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

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

#run_recipesObject



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

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



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
89
# File 'lib/beet/executor.rb', line 40

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



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

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