le1t0-capistrano 2.5.18.001

Sign up to get free protection for your applications and to get access to all the features.
Files changed (107) hide show
  1. data/.gitignore +9 -0
  2. data/CHANGELOG +843 -0
  3. data/README +102 -0
  4. data/Rakefile +36 -0
  5. data/VERSION +1 -0
  6. data/bin/cap +4 -0
  7. data/bin/capify +86 -0
  8. data/lib/capistrano.rb +2 -0
  9. data/lib/capistrano/callback.rb +45 -0
  10. data/lib/capistrano/cli.rb +47 -0
  11. data/lib/capistrano/cli/execute.rb +85 -0
  12. data/lib/capistrano/cli/help.rb +125 -0
  13. data/lib/capistrano/cli/help.txt +78 -0
  14. data/lib/capistrano/cli/options.rb +243 -0
  15. data/lib/capistrano/cli/ui.rb +40 -0
  16. data/lib/capistrano/command.rb +283 -0
  17. data/lib/capistrano/configuration.rb +44 -0
  18. data/lib/capistrano/configuration/actions/file_transfer.rb +52 -0
  19. data/lib/capistrano/configuration/actions/inspect.rb +46 -0
  20. data/lib/capistrano/configuration/actions/invocation.rb +295 -0
  21. data/lib/capistrano/configuration/callbacks.rb +148 -0
  22. data/lib/capistrano/configuration/connections.rb +204 -0
  23. data/lib/capistrano/configuration/execution.rb +143 -0
  24. data/lib/capistrano/configuration/loading.rb +197 -0
  25. data/lib/capistrano/configuration/namespaces.rb +197 -0
  26. data/lib/capistrano/configuration/roles.rb +73 -0
  27. data/lib/capistrano/configuration/servers.rb +98 -0
  28. data/lib/capistrano/configuration/variables.rb +127 -0
  29. data/lib/capistrano/errors.rb +19 -0
  30. data/lib/capistrano/extensions.rb +57 -0
  31. data/lib/capistrano/logger.rb +59 -0
  32. data/lib/capistrano/processable.rb +53 -0
  33. data/lib/capistrano/recipes/compat.rb +32 -0
  34. data/lib/capistrano/recipes/deploy.rb +589 -0
  35. data/lib/capistrano/recipes/deploy/dependencies.rb +44 -0
  36. data/lib/capistrano/recipes/deploy/local_dependency.rb +54 -0
  37. data/lib/capistrano/recipes/deploy/remote_dependency.rb +105 -0
  38. data/lib/capistrano/recipes/deploy/scm.rb +19 -0
  39. data/lib/capistrano/recipes/deploy/scm/accurev.rb +169 -0
  40. data/lib/capistrano/recipes/deploy/scm/base.rb +196 -0
  41. data/lib/capistrano/recipes/deploy/scm/bzr.rb +86 -0
  42. data/lib/capistrano/recipes/deploy/scm/cvs.rb +152 -0
  43. data/lib/capistrano/recipes/deploy/scm/darcs.rb +96 -0
  44. data/lib/capistrano/recipes/deploy/scm/git.rb +278 -0
  45. data/lib/capistrano/recipes/deploy/scm/mercurial.rb +137 -0
  46. data/lib/capistrano/recipes/deploy/scm/none.rb +44 -0
  47. data/lib/capistrano/recipes/deploy/scm/perforce.rb +138 -0
  48. data/lib/capistrano/recipes/deploy/scm/subversion.rb +121 -0
  49. data/lib/capistrano/recipes/deploy/strategy.rb +19 -0
  50. data/lib/capistrano/recipes/deploy/strategy/base.rb +79 -0
  51. data/lib/capistrano/recipes/deploy/strategy/checkout.rb +20 -0
  52. data/lib/capistrano/recipes/deploy/strategy/copy.rb +218 -0
  53. data/lib/capistrano/recipes/deploy/strategy/export.rb +20 -0
  54. data/lib/capistrano/recipes/deploy/strategy/remote.rb +52 -0
  55. data/lib/capistrano/recipes/deploy/strategy/remote_cache.rb +56 -0
  56. data/lib/capistrano/recipes/deploy/templates/maintenance.rhtml +53 -0
  57. data/lib/capistrano/recipes/standard.rb +37 -0
  58. data/lib/capistrano/recipes/templates/maintenance.rhtml +53 -0
  59. data/lib/capistrano/role.rb +102 -0
  60. data/lib/capistrano/server_definition.rb +56 -0
  61. data/lib/capistrano/shell.rb +260 -0
  62. data/lib/capistrano/ssh.rb +99 -0
  63. data/lib/capistrano/task_definition.rb +75 -0
  64. data/lib/capistrano/transfer.rb +216 -0
  65. data/lib/capistrano/version.rb +18 -0
  66. data/test/cli/execute_test.rb +132 -0
  67. data/test/cli/help_test.rb +165 -0
  68. data/test/cli/options_test.rb +329 -0
  69. data/test/cli/ui_test.rb +28 -0
  70. data/test/cli_test.rb +17 -0
  71. data/test/command_test.rb +286 -0
  72. data/test/configuration/actions/file_transfer_test.rb +61 -0
  73. data/test/configuration/actions/inspect_test.rb +65 -0
  74. data/test/configuration/actions/invocation_test.rb +225 -0
  75. data/test/configuration/callbacks_test.rb +220 -0
  76. data/test/configuration/connections_test.rb +349 -0
  77. data/test/configuration/execution_test.rb +175 -0
  78. data/test/configuration/loading_test.rb +132 -0
  79. data/test/configuration/namespace_dsl_test.rb +311 -0
  80. data/test/configuration/roles_test.rb +144 -0
  81. data/test/configuration/servers_test.rb +158 -0
  82. data/test/configuration/variables_test.rb +184 -0
  83. data/test/configuration_test.rb +88 -0
  84. data/test/deploy/local_dependency_test.rb +76 -0
  85. data/test/deploy/remote_dependency_test.rb +114 -0
  86. data/test/deploy/scm/accurev_test.rb +23 -0
  87. data/test/deploy/scm/base_test.rb +55 -0
  88. data/test/deploy/scm/bzr_test.rb +51 -0
  89. data/test/deploy/scm/darcs_test.rb +37 -0
  90. data/test/deploy/scm/git_test.rb +184 -0
  91. data/test/deploy/scm/mercurial_test.rb +134 -0
  92. data/test/deploy/scm/none_test.rb +35 -0
  93. data/test/deploy/scm/subversion_test.rb +32 -0
  94. data/test/deploy/strategy/copy_test.rb +302 -0
  95. data/test/extensions_test.rb +69 -0
  96. data/test/fixtures/cli_integration.rb +5 -0
  97. data/test/fixtures/config.rb +5 -0
  98. data/test/fixtures/custom.rb +3 -0
  99. data/test/logger_test.rb +123 -0
  100. data/test/role_test.rb +11 -0
  101. data/test/server_definition_test.rb +121 -0
  102. data/test/shell_test.rb +90 -0
  103. data/test/ssh_test.rb +104 -0
  104. data/test/task_definition_test.rb +116 -0
  105. data/test/transfer_test.rb +160 -0
  106. data/test/utils.rb +39 -0
  107. metadata +289 -0
data/README ADDED
@@ -0,0 +1,102 @@
1
+ = Capistrano
2
+
3
+ Capistrano is a utility and framework for executing commands in parallel on multiple remote machines, via SSH. It uses a simple DSL (borrowed in part from Rake, http://rake.rubyforge.org/) that allows you to define _tasks_, which may be applied to machines in certain roles. It also supports tunneling connections via some gateway machine to allow operations to be performed behind VPN's and firewalls.
4
+
5
+ Capistrano was originally designed to simplify and automate deployment of web applications to distributed environments, and originally came bundled with a set of tasks designed for deploying Rails applications. The deployment tasks are now (as of Capistrano 2.0) opt-in and require clients to explicitly put
6
+ "load 'deploy'" in their recipes.
7
+
8
+ == Documentation
9
+
10
+ We know that documentation is something that really lets us down, that's why there is a repository for a handbook below, please open an issue on it if you would like something documented:
11
+
12
+ * http://github.com/leehambley/capistrano-handbook
13
+
14
+ If you prefer the wiki style of documentation, then please see our wiki
15
+
16
+ * http://wiki.capify.org
17
+
18
+ Due to a failure of MySQL with PHP, searches shorter than three characters are all but ignored, we're going to rectify this, but in the meantime, please do what you can, tickets opened on the handbook for the wiki will be answered too, so please let us know if you don't find something you needed.
19
+
20
+ We take bug reports via lighthouse app, you can find that page here:
21
+
22
+ * http://capistrano.lighthouseapp.com
23
+
24
+ More documentation is on the way, if in doubt try opening the recipes that ship with capistrano.
25
+
26
+ == DEPENDENCIES
27
+
28
+ * Net::SSH v2 (http://net-ssh.rubyforge.org)
29
+ * Net::SFTP v2 (http://net-ssh.rubyforge.org)
30
+ * Net::SCP v1 (http://net-ssh.rubyforge.org)
31
+ * Net::SSH::Gateway v1 (http://net-ssh.rubyforge.org)
32
+ * HighLine (http://highline.rubyforge.org)
33
+
34
+ If you want to run the tests, you'll also need to have the following dependencies installed:
35
+
36
+ * Echoe (for the Rakefile)
37
+ * Mocha (http://mocha.rubyforge.org)
38
+
39
+ == ASSUMPTIONS
40
+
41
+ Capistrano is "opinionated software", which means it has very firm ideas about how things ought to be done, and tries to force those ideas on you. Some of the assumptions behind these opinions are:
42
+
43
+ * You are using SSH to access the remote servers.
44
+ * You either have the same password to all target machines, or you have public keys in place to allow passwordless access to them.
45
+
46
+ Do not expect these assumptions to change.
47
+
48
+ == USAGE
49
+
50
+ In general, you'll use Capistrano as follows:
51
+
52
+ * Create a recipe file ("capfile" or "Capfile").
53
+ * Use the +cap+ script to execute your recipe.
54
+
55
+ Use the +cap+ script as follows:
56
+
57
+ cap sometask
58
+
59
+ By default, the script will look for a file called one of +capfile+ or +Capfile+. The +someaction+ text indicates which task to execute. You can do "cap -h" to see all the available options and "cap -T" to see all the available tasks.
60
+
61
+ == Capistrano Edge
62
+
63
+ If you want to try Capistrano code that hasn't been formerly released yet, this repository now includes a gemspec that should build what you need, here's how to get a copy:
64
+
65
+ git clone git://github.com/capistrano/capistrano.git capistrano-capistrano
66
+ cd capistrano-capsitrano
67
+ rake package
68
+ sudo gem install pkg/capistrano-*.gem
69
+
70
+ This will install the most recent version of capistrano and make it available for both cap, and capify.
71
+
72
+ We recommend that you capify a new test application, as the resulting files are different to previous versions.
73
+
74
+ If you have multiple versions of capistrano (or indeed any gem with a binary) installed, you can call `cap` like so to specify which version to use:
75
+
76
+ cap _2.5.5_ deploy
77
+ cap _2.5.6_ deploy:setup
78
+
79
+ == LICENSE:
80
+
81
+ (The MIT License)
82
+
83
+ Copyright (c) 2005-2008 Jamis Buck <jamis@37signals.com>
84
+
85
+ Permission is hereby granted, free of charge, to any person obtaining
86
+ a copy of this software and associated documentation files (the
87
+ 'Software'), to deal in the Software without restriction, including
88
+ without limitation the rights to use, copy, modify, merge, publish,
89
+ distribute, sublicense, and/or sell copies of the Software, and to
90
+ permit persons to whom the Software is furnished to do so, subject to
91
+ the following conditions:
92
+
93
+ The above copyright notice and this permission notice shall be
94
+ included in all copies or substantial portions of the Software.
95
+
96
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
97
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
98
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
99
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
100
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
101
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
102
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,36 @@
1
+ require "./lib/capistrano/version"
2
+
3
+ begin
4
+ require 'jeweler'
5
+ Jeweler::Tasks.new do |gem|
6
+ gem.version
7
+ gem.name = "le1t0-capistrano"
8
+ gem.executables = %W(capify cap)
9
+ gem.summary = %Q{Capistrano – Welcome to easy deployment with Ruby over SSH}
10
+ gem.description = %Q{Capistrano is a utility and framework for executing commands in parallel on multiple remote machines, via SSH.}
11
+ gem.homepage = "http://github.com/le1t0/capistrano"
12
+ gem.email = [ "dev@ewout.to" ]
13
+ gem.authors = [ "Le1t0" ]
14
+ gem.add_dependency "net-ssh", ">=2.0.14"
15
+ gem.add_dependency "net-sftp", ">=2.0.0"
16
+ gem.add_dependency "net-scp", ">=1.0.0"
17
+ gem.add_dependency "net-ssh-gateway", ">=1.0.0"
18
+ gem.add_dependency "highline"
19
+ gem.add_development_dependency "mocha", ">= 0"
20
+ end
21
+ rescue LoadError
22
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
23
+ end
24
+
25
+ Jeweler::GemcutterTasks.new
26
+
27
+ require 'rake/testtask'
28
+ Rake::TestTask.new(:test) do |test|
29
+ test.libs << 'lib' << 'test'
30
+ test.pattern = 'test/**/*_test.rb'
31
+ test.verbose = true
32
+ end
33
+
34
+ task :test => :check_dependencies
35
+ task :default => :test
36
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 2.5.18.001
data/bin/cap ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'capistrano/cli'
4
+ Capistrano::CLI.execute
data/bin/capify ADDED
@@ -0,0 +1,86 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'fileutils'
5
+
6
+ OptionParser.new do |opts|
7
+ opts.banner = "Usage: #{File.basename($0)} [path]"
8
+
9
+ opts.on("-h", "--help", "Displays this help info") do
10
+ puts opts
11
+ exit 0
12
+ end
13
+
14
+ begin
15
+ opts.parse!(ARGV)
16
+ rescue OptionParser::ParseError => e
17
+ warn e.message
18
+ puts opts
19
+ exit 1
20
+ end
21
+ end
22
+
23
+ if ARGV.empty?
24
+ abort "Please specify the directory to capify, e.g. `#{File.basename($0)} .'"
25
+ elsif !File.exists?(ARGV.first)
26
+ abort "`#{ARGV.first}' does not exist."
27
+ elsif !File.directory?(ARGV.first)
28
+ abort "`#{ARGV.first}' is not a directory."
29
+ elsif ARGV.length > 1
30
+ abort "Too many arguments; please specify only the directory to capify."
31
+ end
32
+
33
+ def unindent(string)
34
+ indentation = string[/\A\s*/]
35
+ string.strip.gsub(/^#{indentation}/, "")
36
+ end
37
+
38
+ files = {
39
+ "Capfile" => unindent(<<-FILE),
40
+ load 'deploy' if respond_to?(:namespace) # cap2 differentiator
41
+ Dir['vendor/plugins/*/recipes/*.rb'].each { |plugin| load(plugin) }
42
+
43
+ load 'config/deploy' # remove this line to skip loading any of the default tasks
44
+ FILE
45
+
46
+ "config/deploy.rb" => 'set :application, "set your application name here"
47
+ set :repository, "set your repository location here"
48
+
49
+ set :scm, :subversion
50
+ # Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none`
51
+
52
+ role :web, "your web-server here" # Your HTTP server, Apache/etc
53
+ role :app, "your app-server here" # This may be the same as your `Web` server
54
+ role :db, "your primary db-server here", :primary => true # This is where Rails migrations will run
55
+ role :db, "your slave db-server here"
56
+
57
+ # If you are using Passenger mod_rails uncomment this:
58
+ # if you\'re still using the script/reapear helper you will need
59
+ # these http://github.com/rails/irs_process_scripts
60
+
61
+ # namespace :deploy do
62
+ # task :start do ; end
63
+ # task :stop do ; end
64
+ # task :restart, :roles => :app, :except => { :no_release => true } do
65
+ # run "#{try_sudo} touch #{File.join(current_path,\'tmp\',\'restart.txt\')}"
66
+ # end
67
+ # end'}
68
+
69
+ base = ARGV.shift
70
+ files.each do |file, content|
71
+ file = File.join(base, file)
72
+ if File.exists?(file)
73
+ warn "[skip] '#{file}' already exists"
74
+ elsif File.exists?(file.downcase)
75
+ warn "[skip] '#{file.downcase}' exists, which could conflict with `#{file}'"
76
+ else
77
+ unless File.exists?(File.dirname(file))
78
+ puts "[add] making directory '#{File.dirname(file)}'"
79
+ FileUtils.mkdir(File.dirname(file))
80
+ end
81
+ puts "[add] writing '#{file}'"
82
+ File.open(file, "w") { |f| f.write(content) }
83
+ end
84
+ end
85
+
86
+ puts "[done] capified!"
data/lib/capistrano.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'capistrano/configuration'
2
+ require 'capistrano/extensions'
@@ -0,0 +1,45 @@
1
+ module Capistrano
2
+ class Callback
3
+ attr_reader :source, :options, :only, :except
4
+
5
+ def initialize(source, options={})
6
+ @source = source
7
+ @options = options
8
+ @only = Array(options[:only]).map { |v| v.to_s }
9
+ @except = Array(options[:except]).map { |v| v.to_s }
10
+ end
11
+
12
+ def applies_to?(task)
13
+ if task && only.any?
14
+ return only.include?(task.fully_qualified_name)
15
+ elsif task && except.any?
16
+ return !except.include?(task.fully_qualified_name)
17
+ else
18
+ return true
19
+ end
20
+ end
21
+ end
22
+
23
+ class ProcCallback < Callback
24
+ def call
25
+ source.call
26
+ end
27
+ end
28
+
29
+ class TaskCallback < Callback
30
+ attr_reader :config
31
+
32
+ def initialize(config, source, options={})
33
+ super(source, options)
34
+ @config = config
35
+ end
36
+
37
+ def call
38
+ config.find_and_execute_task(source)
39
+ end
40
+
41
+ def applies_to?(task)
42
+ super && (task.nil? || task.fully_qualified_name != source.to_s)
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,47 @@
1
+ require 'capistrano'
2
+ require 'capistrano/cli/execute'
3
+ require 'capistrano/cli/help'
4
+ require 'capistrano/cli/options'
5
+ require 'capistrano/cli/ui'
6
+
7
+ module Capistrano
8
+ # The CLI class encapsulates the behavior of capistrano when it is invoked
9
+ # as a command-line utility. This allows other programs to embed Capistrano
10
+ # and preserve its command-line semantics.
11
+ class CLI
12
+ # The array of (unparsed) command-line options
13
+ attr_reader :args
14
+
15
+ # Create a new CLI instance using the given array of command-line parameters
16
+ # to initialize it. By default, +ARGV+ is used, but you can specify a
17
+ # different set of parameters (such as when embedded cap in a program):
18
+ #
19
+ # require 'capistrano/cli'
20
+ # Capistrano::CLI.parse(%W(-vvvv -f config/deploy update_code)).execute!
21
+ #
22
+ # Note that you can also embed cap directly by creating a new Configuration
23
+ # instance and setting it up, The above snippet, redone using the
24
+ # Configuration class directly, would look like:
25
+ #
26
+ # require 'capistrano'
27
+ # require 'capistrano/cli'
28
+ # config = Capistrano::Configuration.new
29
+ # config.logger.level = Capistrano::Logger::TRACE
30
+ # config.set(:password) { Capistrano::CLI.password_prompt }
31
+ # config.load "config/deploy"
32
+ # config.update_code
33
+ #
34
+ # There may be times that you want/need the additional control offered by
35
+ # manipulating the Configuration directly, but generally interfacing with
36
+ # the CLI class is recommended.
37
+ def initialize(args)
38
+ @args = args.dup
39
+ $stdout.sync = true # so that Net::SSH prompts show up
40
+ end
41
+
42
+ # Mix-in the actual behavior
43
+ include Execute, Options, UI
44
+ include Help # needs to be included last, because it overrides some methods
45
+
46
+ end
47
+ end
@@ -0,0 +1,85 @@
1
+ require 'capistrano/configuration'
2
+
3
+ module Capistrano
4
+ class CLI
5
+ module Execute
6
+ def self.included(base) #:nodoc:
7
+ base.extend(ClassMethods)
8
+ end
9
+
10
+ module ClassMethods
11
+ # Invoke capistrano using the ARGV array as the option parameters. This
12
+ # is what the command-line capistrano utility does.
13
+ def execute
14
+ parse(ARGV).execute!
15
+ end
16
+ end
17
+
18
+ # Using the options build when the command-line was parsed, instantiate
19
+ # a new Capistrano configuration, initialize it, and execute the
20
+ # requested actions.
21
+ #
22
+ # Returns the Configuration instance used, if successful.
23
+ def execute!
24
+ config = instantiate_configuration(options)
25
+ config.debug = options[:debug]
26
+ config.dry_run = options[:dry_run]
27
+ config.preserve_roles = options[:preserve_roles]
28
+ config.logger.level = options[:verbose]
29
+
30
+ set_pre_vars(config)
31
+ load_recipes(config)
32
+
33
+ config.trigger(:load)
34
+ execute_requested_actions(config)
35
+ config.trigger(:exit)
36
+
37
+ config
38
+ rescue Exception => error
39
+ handle_error(error)
40
+ end
41
+
42
+ def execute_requested_actions(config)
43
+ Array(options[:vars]).each { |name, value| config.set(name, value) }
44
+
45
+ Array(options[:actions]).each do |action|
46
+ config.find_and_execute_task(action, :before => :start, :after => :finish)
47
+ end
48
+ end
49
+
50
+ def set_pre_vars(config) #:nodoc:
51
+ config.set :password, options[:password]
52
+ Array(options[:pre_vars]).each { |name, value| config.set(name, value) }
53
+ end
54
+
55
+ def load_recipes(config) #:nodoc:
56
+ # load the standard recipe definition
57
+ config.load "standard"
58
+
59
+ # load systemwide config/recipe definition
60
+ config.load(options[:sysconf]) if options[:sysconf] && File.file?(options[:sysconf])
61
+
62
+ # load user config/recipe definition
63
+ config.load(options[:dotfile]) if options[:dotfile] && File.file?(options[:dotfile])
64
+
65
+ Array(options[:recipes]).each { |recipe| config.load(recipe) }
66
+ end
67
+
68
+ # Primarily useful for testing, but subclasses of CLI could conceivably
69
+ # override this method to return a Configuration subclass or replacement.
70
+ def instantiate_configuration(options={}) #:nodoc:
71
+ Capistrano::Configuration.new(options)
72
+ end
73
+
74
+ def handle_error(error) #:nodoc:
75
+ case error
76
+ when Net::SSH::AuthenticationFailed
77
+ abort "authentication failed for `#{error.message}'"
78
+ when Capistrano::Error
79
+ abort(error.message)
80
+ else raise error
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,125 @@
1
+ module Capistrano
2
+ class CLI
3
+ module Help
4
+ LINE_PADDING = 7
5
+ MIN_MAX_LEN = 30
6
+ HEADER_LEN = 60
7
+
8
+ def self.included(base) #:nodoc:
9
+ base.send :alias_method, :execute_requested_actions_without_help, :execute_requested_actions
10
+ base.send :alias_method, :execute_requested_actions, :execute_requested_actions_with_help
11
+ end
12
+
13
+ def execute_requested_actions_with_help(config)
14
+ if options[:tasks]
15
+ task_list(config, options[:tasks])
16
+ elsif options[:explain]
17
+ explain_task(config, options[:explain])
18
+ else
19
+ execute_requested_actions_without_help(config)
20
+ end
21
+ end
22
+
23
+ def task_list(config, pattern = true) #:nodoc:
24
+ tool_output = options[:tool]
25
+
26
+ if pattern.is_a?(String)
27
+ tasks = config.task_list(:all).select {|t| t.fully_qualified_name =~ /#{pattern}/}
28
+ end
29
+ if tasks.nil? || tasks.length == 0
30
+ warn "Pattern '#{pattern}' not found. Listing all tasks.\n\n" if !tool_output && !pattern.is_a?(TrueClass)
31
+ tasks = config.task_list(:all)
32
+ end
33
+
34
+ if tasks.empty?
35
+ warn "There are no tasks available. Please specify a recipe file to load." unless tool_output
36
+ else
37
+ all_tasks_length = tasks.length
38
+ if options[:verbose].to_i < 1
39
+ tasks = tasks.reject { |t| t.description.empty? || t.description =~ /^\[internal\]/ }
40
+ end
41
+
42
+ tasks = tasks.sort_by { |task| task.fully_qualified_name }
43
+
44
+ longest = tasks.map { |task| task.fully_qualified_name.length }.max
45
+ max_length = output_columns - longest - LINE_PADDING
46
+ max_length = MIN_MAX_LEN if max_length < MIN_MAX_LEN
47
+
48
+ tasks.each do |task|
49
+ if tool_output
50
+ puts "cap #{task.fully_qualified_name}"
51
+ else
52
+ puts "cap %-#{longest}s # %s" % [task.fully_qualified_name, task.brief_description(max_length)]
53
+ end
54
+ end
55
+
56
+ unless tool_output
57
+ if all_tasks_length > tasks.length
58
+ puts
59
+ puts "Some tasks were not listed, either because they have no description,"
60
+ puts "or because they are only used internally by other tasks. To see all"
61
+ puts "tasks, type `#{File.basename($0)} -vT'."
62
+ end
63
+
64
+ puts
65
+ puts "Extended help may be available for these tasks."
66
+ puts "Type `#{File.basename($0)} -e taskname' to view it."
67
+ end
68
+ end
69
+ end
70
+
71
+ def explain_task(config, name) #:nodoc:
72
+ task = config.find_task(name)
73
+ if task.nil?
74
+ warn "The task `#{name}' does not exist."
75
+ else
76
+ puts "-" * HEADER_LEN
77
+ puts "cap #{name}"
78
+ puts "-" * HEADER_LEN
79
+
80
+ if task.description.empty?
81
+ puts "There is no description for this task."
82
+ else
83
+ puts format_text(task.description)
84
+ end
85
+
86
+ puts
87
+ end
88
+ end
89
+
90
+ def long_help #:nodoc:
91
+ help_text = File.read(File.join(File.dirname(__FILE__), "help.txt"))
92
+ self.class.ui.page_at = self.class.ui.output_rows - 2
93
+ self.class.ui.say format_text(help_text)
94
+ end
95
+
96
+ def format_text(text) #:nodoc:
97
+ formatted = ""
98
+ text.each_line do |line|
99
+ indentation = line[/^\s+/] || ""
100
+ indentation_size = indentation.split(//).inject(0) { |c,s| c + (s[0] == ?\t ? 8 : 1) }
101
+ line_length = output_columns - indentation_size
102
+ line_length = MIN_MAX_LEN if line_length < MIN_MAX_LEN
103
+ lines = line.strip.gsub(/(.{1,#{line_length}})(?:\s+|\Z)/, "\\1\n").split(/\n/)
104
+ if lines.empty?
105
+ formatted << "\n"
106
+ else
107
+ formatted << lines.map { |l| "#{indentation}#{l}\n" }.join
108
+ end
109
+ end
110
+ formatted
111
+ end
112
+
113
+ def output_columns #:nodoc:
114
+ if ( @output_columns.nil? )
115
+ if ( self.class.ui.output_cols.nil? || self.class.ui.output_cols > 80 )
116
+ @output_columns = 80
117
+ else
118
+ @output_columns = self.class.ui.output_cols
119
+ end
120
+ end
121
+ @output_columns
122
+ end
123
+ end
124
+ end
125
+ end