lib/capitate/plugins/base.rb in capitate-0.1.7 vs lib/capitate/plugins/base.rb in capitate-0.1.8
- old
+ new
@@ -2,20 +2,30 @@
require 'yaml'
# Capitate base capistrano plugin
module Capitate::Plugins::Base
- # Project root
+ # Project root. Fetch from :project_root, or fall back to RAILS_ROOT.
+ #
def root
if respond_to?(:fetch)
return fetch(:project_root)
else
RAILS_ROOT
end
end
- # Path relative to project root
+ # Path relative to project root.
+ # Project root is set via, set :project_root, "path/to/project" in Capfile.
+ #
+ # ==== Options
+ # +path+:: Relative path
+ # +check_exist+:: Whether to check its existence and throw error if not found
+ #
+ # ==== Examples
+ # relative_to_root("config/foo.yml") => "path/to/project/config/foo.yml"
+ #
def relative_to_root(path = nil, check_exist = false)
if path
root_path = File.join(root, path)
else
root_path = root
@@ -31,54 +41,90 @@
#{File.expand_path(File.dirname(__FILE__) + "/../doc/README")}
EOS
end
root_path
- end
+ end
- # Documentation (yaml) for current task (namespace).
- #
- # ==== Examples
- # capitate.current_task_docs => { "task_name" => { "variable" => "The usage docs" } }
+ # Usage for current task.
#
- def current_task_doc
- path = File.dirname(__FILE__) + "/../../doc/" + current_task.namespace.fully_qualified_name.to_s.gsub(":", "/") + ".yml"
- puts "Current task doc: #{path}"
- return YAML.load_file(path) if File.exist?(path)
- nil
- end
-
- # Usage for variable from current task documentation.
- #
# ==== Options
- # +var+:: Variable
- #
+ # +variable+:: Missing variable setting
+ #
# ==== Examples
- # usage(:gem_list) => "The usage for gem_list variable from the doc/the_namespace.yml file."
+ # usage(:gem_list) => "Description from task definition."
#
- def usage(var)
- task_doc = current_task_doc
- task_name = current_task.name.to_s
- var_name = var.to_s
- if task_doc and task_doc.has_key?(task_name)
- var_usage = task_doc[task_name][var_name]
- return <<-EOS
-
- Please set :#{var_name} variable in your Capfile, deploy.rb or profile.
-
- Usage:
-
-#{indent_doc(var_usage)}
-
+ def usage(variable = nil)
+ message = ""
+
+ if variable
+ message += <<-EOS
+
+ Error: :#{variable} not set.
EOS
end
+
+ message += <<-EOS
+
+ Usage:
+
+#{indent_doc(current_task.desc)}
+
+ EOS
end
-
- def indent_doc(s, amount = 8)
+ # Indent string block.
+ #
+ # ==== Options
+ # +s+:: String block
+ # +amount+:: Amount to indent
+ #
+ def indent_doc(s, amount = 4)
return nil if s.blank?
indentation = (0..amount).collect { |n| " " }.join
s.split("\n").collect { |sp| "#{indentation}#{sp}"}.join("\n")
+ end
+
+ # Unindent, lifted from capistrano bin/capify
+ def unindent(string)
+ return "" if string.blank?
+ if string =~ /\A(\s*)/
+ amount = $1.length
+ return string.strip.gsub(/^#{$1}/, "")
+ end
+ string
+ end
+
+ # Load all tasks
+ def load_all_tasks
+ tasks = []
+ top.namespaces.each do |namespace|
+ load_tasks(namespace, tasks)
+ end
+ tasks
+ end
+
+ def task_tree
+ top_node = Capitate::TaskNode.new("top")
+
+ load_all_tasks.each do |task|
+ Capitate::TaskNode.populate_with_task(top_node, task)
+ end
+ top_node
+ end
+
+protected
+
+ def load_tasks(namespace, tasks = [])
+ recipe = namespace.last
+
+ recipe.namespaces.each do |nested_namespace|
+ load_tasks(nested_namespace, tasks)
+ end
+
+ recipe.task_list.each do |task|
+ tasks << task
+ end
end
end
Capistrano.plugin :capitate, Capitate::Plugins::Base
\ No newline at end of file