lib/thor/actions/templater.rb in josevalim-thor-0.10.0 vs lib/thor/actions/templater.rb in josevalim-thor-0.10.1
- old
+ new
@@ -95,15 +95,30 @@
# Sets the destination value from a relative destination value. The
# relative destination is kept to be used in output messages.
#
def destination=(destination)
if destination
- @destination = ::File.expand_path(destination.to_s, base.destination_root)
+ @destination = ::File.expand_path(convert_encoded_instructions(destination.to_s), base.destination_root)
@relative_destination = base.relative_to_absolute_root(@destination)
end
end
+ # Filenames in the encoded form are converted. If you have a file:
+ #
+ # %class_name%.rb
+ #
+ # It gets the class name from the base and replace it:
+ #
+ # user.rb
+ #
+ def convert_encoded_instructions(filename)
+ filename.gsub(/%(.*?)%/) do |string|
+ instruction = $1.strip
+ base.respond_to?(instruction) ? base.send(instruction) : string
+ end
+ end
+
# Receives a hash of options and just execute the block if some
# conditions are met.
#
def invoke_with_options!(options, &block)
if exists?
@@ -116,10 +131,12 @@
end
else
say_status :create, :green
block.call unless pretend?
end
+
+ destination
end
# If force is true, run the action, otherwise check if it's not being
# skipped. If both are false, show the file_collision menu, if the menu
# returns true, force it, otherwise skip.
@@ -144,19 +161,9 @@
# Shortcut to say_status shell method.
#
def say_status(status, color)
base.shell.say_status status, relative_destination, color if @log_status
- end
-
- # TODO Add this behavior to all actions.
- #
- def after_invoke
- # Optionally change permissions.
- FileUtils.chmod(base.options[:chmod], destination) if base.options[:chmod]
-
- # Optionally add file to subversion or git
- system("git add -v #{relative_destination}") if options[:git]
end
end
end
end