1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Updated vendored thor to 0.11.3.

This commit is contained in:
José Valim 2009-07-23 14:30:49 +02:00
parent 178044dbd2
commit edd07b5a7a
36 changed files with 92 additions and 116 deletions

View file

@ -11,7 +11,7 @@ end
$:.unshift(File.dirname(__FILE__))
require 'vendor/thor-0.11.2/lib/thor'
require 'vendor/thor-0.11.3/lib/thor'
require 'generators/base'
require 'generators/named_base'

View file

@ -4,28 +4,6 @@ module Rails
module Generators
module Actions
# Loads an external file and execute it in the instance binding.
#
# ==== Parameters
# path<String>:: The path to the file to execute. Can be a web address or
# a relative path from the source root.
#
# ==== Examples
#
# apply "http://gist.github.com/103208"
#
# apply "recipes/jquery.rb"
#
def apply(path, options={})
verbose = options.fetch(:verbose, true)
path = find_in_source_paths(path) unless path =~ /^http\:\/\//
log :apply, path, verbose
shell.padding += 1 if verbose
instance_eval(open(path).read)
shell.padding -= 1 if verbose
end
# Install a plugin. You must provide either a Subversion url or Git url.
# For a Git-hosted plugin, you can specify if it should be added as a submodule instead of cloned.
#

View file

@ -166,16 +166,19 @@ class Thor
shell.say task.description
else
list = (options[:short] ? tasks : all_tasks).map do |_, task|
[ banner(task, options[:namespace]), task.short_description || '' ]
item = [ " " + banner(task, options[:namespace]) ]
item << if task.short_description
"\n # #{task.short_description}\n"
else
"\n"
end
end
if options[:short]
shell.print_table(list, :emphasize_last => true)
shell.print_table(list)
else
shell.say "Tasks:"
shell.print_table(list, :ident => 2, :emphasize_last => true)
shell.say
shell.print_table(list)
class_options_help(shell, "Class")
end
end
@ -196,13 +199,19 @@ class Thor
Thor
end
def valid_task?(meth) #:nodoc:
@usage && @desc
end
def create_task(meth) #:nodoc:
tasks[meth.to_s] = Thor::Task.new(meth, @desc, @usage, method_options)
@usage, @desc, @method_options = nil
if @usage && @desc
tasks[meth.to_s] = Thor::Task.new(meth, @desc, @usage, method_options)
@usage, @desc, @method_options = nil
true
elsif self.all_tasks[meth.to_s] || meth.to_sym == :method_missing
true
else
puts "[WARNING] Attempted to create task #{meth.inspect} without usage or description. " <<
"Call desc if you want this method to be available as task or declare it inside a " <<
"no_tasks{} block. Invoked from #{caller[1].inspect}."
false
end
end
def initialize_added #:nodoc:

View file

@ -158,11 +158,34 @@ class Thor
inside(@destination_stack.first) { yield }
end
# Loads an external file and execute it in the instance binding.
#
# ==== Parameters
# path<String>:: The path to the file to execute. Can be a web address or
# a relative path from the source root.
#
# ==== Examples
#
# apply "http://gist.github.com/103208"
#
# apply "recipes/jquery.rb"
#
def apply(path, config={})
verbose = config.fetch(:verbose, true)
path = find_in_source_paths(path) unless path =~ /^http\:\/\//
say_status :apply, path, verbose
shell.padding += 1 if verbose
instance_eval(open(path).read)
shell.padding -= 1 if verbose
end
# Executes a command.
#
# ==== Parameters
# command<String>:: the command to be executed.
# config<Hash>:: give :verbose => false to not log the status.
# config<Hash>:: give :verbose => false to not log the status. Specify :with
# to append an executable to command executation.
#
# ==== Example
#
@ -172,7 +195,16 @@ class Thor
#
def run(command, config={})
return unless behavior == :invoke
say_status :run, command, config.fetch(:verbose, true)
destination = relative_to_original_destination_root(destination_root, false)
desc = "#{command} from #{destination.inspect}"
if config[:with]
desc = "#{File.basename(config[:with].to_s)} #{desc}"
command = "#{config[:with]} #{command}"
end
say_status :run, desc, config.fetch(:verbose, true)
`#{command}` unless options[:pretend]
end
@ -184,8 +216,7 @@ class Thor
#
def run_ruby_script(command, config={})
return unless behavior == :invoke
say_status File.basename(Thor::Util.ruby_command), command, config.fetch(:verbose, true)
`#{Thor::Util.ruby_command} #{command}` unless options[:pretend]
run "#{command}", config.merge(:with => Thor::Util.ruby_command)
end
# Run a thor command. A hash of options can be given and it's converted to
@ -213,8 +244,7 @@ class Thor
args.push Thor::Options.to_switches(config)
command = args.join(' ').strip
say_status :thor, command, verbose
run "thor #{command}", :verbose => false
run command, :with => :thor, :verbose => verbose
end
protected

View file

@ -189,6 +189,7 @@ class Thor
say_status :remove, relative_to_original_destination_root(path), config.fetch(:verbose, true)
::FileUtils.rm_rf(path) if !options[:pretend] && File.exists?(path)
end
alias :remove_dir :remove_file
end
end

View file

@ -9,7 +9,8 @@ require 'thor/util'
class Thor
HELP_MAPPINGS = %w(-h -? --help -D)
THOR_RESERVED_WORDS = %w(invoke shell options behavior root destination_root relative_root)
THOR_RESERVED_WORDS = %w(invoke shell options behavior root destination_root relative_root
action add_file create_file in_root inside run run_ruby_script)
module Base
attr_accessor :options
@ -375,8 +376,16 @@ class Thor
padding = options.collect{ |o| o.aliases.size }.max.to_i * 4
options.each do |option|
list << [ option.usage(padding), option.description || "" ]
list << [ "", "Default: #{option.default}" ] if option.show_default?
item = [ option.usage(padding) ]
item << if option.description
"# #{option.description}"
else
""
end
list << item
list << [ "", "# Default: #{option.default}" ] if option.show_default?
end
unless list.empty?
@ -386,7 +395,7 @@ class Thor
shell.say "Options:"
end
shell.print_table(list, :emphasize_last => true, :ident => 2)
shell.print_table(list, :ident => 2)
shell.say ""
end
end
@ -405,7 +414,7 @@ class Thor
#
def is_thor_reserved_word?(word, type)
return false unless THOR_RESERVED_WORDS.include?(word.to_s)
raise "'#{word}' is a Thor reserved word and cannot be defined as #{type}"
raise "#{word.inspect} is a Thor reserved word and cannot be defined as #{type}"
end
# Build an option and adds it to the given scope.
@ -470,12 +479,10 @@ class Thor
return unless public_instance_methods.include?(meth) ||
public_instance_methods.include?(meth.to_sym)
# Return if @no_tasks is enabled or it's not a valid task
return if @no_tasks || !valid_task?(meth)
return if @no_tasks || !create_task(meth)
is_thor_reserved_word?(meth, :task)
Thor::Base.register_klass_file(self)
create_task(meth)
end
# Retrieves a value from superclass. If it reaches the baseclass,
@ -495,12 +502,6 @@ class Thor
def baseclass #:nodoc:
end
# SIGNATURE: Defines if a given method is a valid_task?. This method is
# called before a new method is added to the class.
def valid_task?(meth) #:nodoc:
true # unless otherwise given
end
# SIGNATURE: Creates a new task if valid_task? is true. This method is
# called when a new method is added to the class.
def create_task(meth) #:nodoc:

View file

@ -233,6 +233,7 @@ class Thor::Group
def create_task(meth) #:nodoc:
tasks[meth.to_s] = Thor::Task.new(meth, nil, nil, nil)
true
end
end

View file

@ -281,15 +281,11 @@ class Thor::Runner < Thor
unless klass.tasks.empty?
base = klass.namespace
if base == "default"
say "\033[1;35m#{base}\033[0m"
else
say "\033[1;34m#{base}\033[0m"
end
color = base == "default" ? :magenta : :blue
say shell.set_color(base, color, true)
say "-" * base.length
klass.help(shell, :short => true, :namespace => true)
say
end
end
end

View file

@ -117,13 +117,6 @@ class Thor
formats[0] = formats[0].insert(0, " " * options[:ident]) if options[:ident]
formats << "%s"
if options[:emphasize_last]
table.each do |row|
next if row[-1].empty?
row[-1] = "# #{row[-1]}"
end
end
table.each do |row|
row.each_with_index do |column, i|
$stdout.print formats[i] % column.to_s
@ -175,11 +168,13 @@ class Thor
$stderr.puts statement
end
protected
# Apply color to the given string with optional bold.
#
def set_color(string, color, bold=false)
string
end
def set_color(string, color, bold=false)
string
end
protected
def is?(value)
value = value.to_s

View file

@ -44,17 +44,17 @@ class Thor
# Set the terminal's background ANSI color to white.
ON_WHITE = "\e[47m"
protected
# Set color by using a string or one of the defined constants. Based
# on Highline implementation. CLEAR is automatically be embedded to
# the end of the returned String.
#
def set_color(string, color, bold=false)
color = self.class.const_get(color.to_s.upcase) if color.is_a?(Symbol)
bold = bold ? BOLD : ""
"#{bold}#{color}#{string}#{CLEAR}"
end
# Set color by using a string or one of the defined constants. Based
# on Highline implementation. CLEAR is automatically be embedded to
# the end of the returned String.
#
def set_color(string, color, bold=false)
color = self.class.const_get(color.to_s.upcase) if color.is_a?(Symbol)
bold = bold ? BOLD : ""
"#{bold}#{color}#{string}#{CLEAR}"
end
protected
# Overwrite show_diff to show diff with colors if Diff::LCS is
# available.

View file

@ -9,41 +9,6 @@ class ActionsTest < GeneratorsTestCase
@svn_plugin_uri = 'svn://svnhub.com/technoweenie/restful-authentication/trunk'
end
def test_apply_loads_and_evaluates_a_template
template = <<-TEMPLATE
@foo = "FOO"
TEMPLATE
template.instance_eval "def read; self; end" # Make the string respond to read
generator.expects(:open).with("http://gist.github.com/103208.txt").returns(template)
action :apply, "http://gist.github.com/103208.txt"
assert_equal generator.instance_variable_get("@foo"), "FOO"
end
def test_apply_uses_padding_in_the_applied_template
template = <<-TEMPLATE
say_status :cool, :padding
TEMPLATE
template.instance_eval "def read; self; end"
generator.expects(:open).with("http://gist.github.com/103208.txt").returns(template)
content = action(:apply, "http://gist.github.com/103208.txt")
assert_match /cool padding/, content
end
def test_apply_does_not_log_status_if_required
template = <<-TEMPLATE
say_status :cool, :padding
TEMPLATE
template.instance_eval "def read; self; end"
generator.expects(:open).with("http://gist.github.com/103208.txt").returns(template)
content = action(:apply, "http://gist.github.com/103208.txt", :verbose => false)
assert_match /cool padding/, content
assert_no_match /apply http/, content
end
def test_create_file_should_write_data_to_file_path
action :create_file, 'lib/test_file.rb', 'heres test data'
assert_file 'lib/test_file.rb', 'heres test data'