Module: Thor::Actions
- Included in:
- Bundler::CLI
- Defined in:
- lib/bundler/vendor/thor/actions.rb,
lib/bundler/vendor/thor/actions/directory.rb,
lib/bundler/vendor/thor/actions/create_file.rb,
lib/bundler/vendor/thor/actions/empty_directory.rb,
lib/bundler/vendor/thor/actions/inject_into_file.rb,
lib/bundler/vendor/thor/actions/file_manipulation.rb
Defined Under Namespace
Modules: ClassMethods Classes: CreateFile, Directory, EmptyDirectory, InjectIntoFile
Instance Attribute Summary collapse
-
#behavior ⇒ Object
Returns the value of attribute behavior.
Class Method Summary collapse
-
.included(base) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#action(instance) ⇒ Object
Wraps an action object and call it accordingly to the thor class behavior.
-
#append_file(path, *args, &block) ⇒ Object
Append text to a file.
-
#apply(path, config = {}) ⇒ Object
Loads an external file and execute it in the instance binding.
-
#chmod(path, mode, config = {}) ⇒ Object
Changes the mode of the given file or directory.
-
#copy_file(source, *args, &block) ⇒ Object
Copies the file from the relative source to the relative destination.
-
#create_file(destination, *args, &block) ⇒ Object
(also: #add_file)
Create a new file relative to the destination root with the given data, which is the return value of a block or a data string.
-
#destination_root ⇒ Object
Returns the root for this thor class (also aliased as destination root).
-
#destination_root=(root) ⇒ Object
Sets the root for this thor class.
-
#directory(source, *args, &block) ⇒ Object
Copies recursively the files from source directory to root directory.
-
#empty_directory(destination, config = {}) ⇒ Object
Creates an empty directory.
-
#find_in_source_paths(file) ⇒ Object
Receives a file or directory and search for it in the source paths.
-
#get(source, *args, &block) ⇒ Object
Gets the content at the given address and places it at the given relative destination.
-
#gsub_file(path, flag, *args, &block) ⇒ Object
Run a regular expression replacement on a file.
-
#in_root ⇒ Object
Goes to the root and execute the given block.
-
#initialize(args = [], options = {}, config = {}) ⇒ Object
Extends initializer to add more configuration options.
-
#inject_into_class(path, klass, *args, &block) ⇒ Object
Injects text right after the class definition.
-
#inject_into_file(destination, *args, &block) ⇒ Object
Injects the given content into a file.
-
#inside(dir = '', config = {}, &block) ⇒ Object
Do something in the root or on a provided subfolder.
-
#prepend_file(path, *args, &block) ⇒ Object
Prepend text to a file.
-
#relative_to_original_destination_root(path, remove_dot = true) ⇒ Object
Returns the given path relative to the absolute root (ie, root where the script started).
-
#remove_file(path, config = {}) ⇒ Object
(also: #remove_dir)
Removes a file at the given location.
-
#run(command, config = {}) ⇒ Object
Executes a command returning the contents of the command.
-
#run_ruby_script(command, config = {}) ⇒ Object
Executes a ruby script (taking into account WIN32 platform quirks).
-
#source_paths ⇒ Object
Holds source paths in instance so they can be manipulated.
-
#template(source, *args, &block) ⇒ Object
Gets an ERB template at the relative source, executes it and makes a copy at the relative destination.
-
#thor(task, *args) ⇒ Object
Run a thor command.
Instance Attribute Details
#behavior ⇒ Object
Returns the value of attribute behavior.
11 12 13 |
# File 'lib/bundler/vendor/thor/actions.rb', line 11 def behavior @behavior end |
Class Method Details
.included(base) ⇒ Object
:nodoc:
13 14 15 |
# File 'lib/bundler/vendor/thor/actions.rb', line 13 def self.included(base) #:nodoc: base.extend ClassMethods end |
Instance Method Details
#action(instance) ⇒ Object
Wraps an action object and call it accordingly to the thor class behavior.
89 90 91 92 93 94 95 |
# File 'lib/bundler/vendor/thor/actions.rb', line 89 def action(instance) #:nodoc: if behavior == :revoke instance.revoke! else instance.invoke! end end |
#append_file(path, *args, &block) ⇒ Object
Append text to a file. Since it depends on inject_into_file, it’s reversible.
Parameters
- path<String>
-
path of the file to be changed
- data<String>
-
the data to append to the file, can be also given as a block.
- config<Hash>
-
give :verbose => false to not log the status.
Example
append_file 'config/environments/test.rb', 'config.gem "rspec"'
append_file 'config/environments/test.rb' do
'config.gem "rspec"'
end
149 150 151 152 153 |
# File 'lib/bundler/vendor/thor/actions/file_manipulation.rb', line 149 def append_file(path, *args, &block) config = args.last.is_a?(Hash) ? args.pop : {} config.merge!(:before => /\z/) inject_into_file(path, *(args << config), &block) end |
#apply(path, config = {}) ⇒ Object
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"
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/bundler/vendor/thor/actions.rb', line 191 def apply(path, config={}) verbose = config.fetch(:verbose, true) is_uri = path =~ /^https?\:\/\// path = find_in_source_paths(path) unless is_uri say_status :apply, path, verbose shell.padding += 1 if verbose if is_uri contents = open(path, "Accept" => "application/x-thor-template") {|io| io.read } else contents = open(path) {|io| io.read } end instance_eval(contents, path) shell.padding -= 1 if verbose end |
#chmod(path, mode, config = {}) ⇒ Object
Changes the mode of the given file or directory.
Parameters
- mode<Integer>
-
the file mode
- path<String>
-
the name of the file to change mode
- config<Hash>
-
give :verbose => false to not log the status.
Example
chmod "script/*", 0755
106 107 108 109 110 111 |
# File 'lib/bundler/vendor/thor/actions/file_manipulation.rb', line 106 def chmod(path, mode, config={}) return unless behavior == :invoke path = File.(path, destination_root) say_status :chmod, relative_to_original_destination_root(path), config.fetch(:verbose, true) FileUtils.chmod_R(mode, path) unless [:pretend] end |
#copy_file(source, *args, &block) ⇒ Object
Copies the file from the relative source to the relative destination. If the destination is not given it’s assumed to be equal to the source.
Parameters
- source<String>
-
the relative path to the source root.
- destination<String>
-
the relative path to the destination root.
- config<Hash>
-
give :verbose => false to not log the status.
Examples
copy_file "README", "doc/README"
copy_file "doc/README"
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/bundler/vendor/thor/actions/file_manipulation.rb', line 21 def copy_file(source, *args, &block) config = args.last.is_a?(Hash) ? args.pop : {} destination = args.first || source source = File.(find_in_source_paths(source.to_s)) create_file destination, nil, config do content = File.binread(source) content = block.call(content) if block content end end |
#create_file(destination, *args, &block) ⇒ Object Also known as: add_file
Create a new file relative to the destination root with the given data, which is the return value of a block or a data string.
Parameters
- destination<String>
-
the relative path to the destination root.
- data<String|NilClass>
-
the data to append to the file.
- config<Hash>
-
give :verbose => false to not log the status.
Examples
create_file "lib/fun_party.rb" do
hostname = ask("What is the virtual hostname I should use?")
"vhost.name = #{hostname}"
end
create_file "config/apach.conf", "your apache config"
23 24 25 26 27 |
# File 'lib/bundler/vendor/thor/actions/create_file.rb', line 23 def create_file(destination, *args, &block) config = args.last.is_a?(Hash) ? args.pop : {} data = args.first action CreateFile.new(self, destination, block || data.to_s, config) end |
#destination_root ⇒ Object
Returns the root for this thor class (also aliased as destination root).
99 100 101 |
# File 'lib/bundler/vendor/thor/actions.rb', line 99 def destination_root @destination_stack.last end |
#destination_root=(root) ⇒ Object
Sets the root for this thor class. Relatives path are added to the directory where the script was invoked and expanded.
106 107 108 109 |
# File 'lib/bundler/vendor/thor/actions.rb', line 106 def destination_root=(root) @destination_stack ||= [] @destination_stack[0] = File.(root || '') end |
#directory(source, *args, &block) ⇒ Object
Copies recursively the files from source directory to root directory. If any of the files finishes with .tt, it’s considered to be a template and is placed in the destination without the extension .tt. If any empty directory is found, it’s copied and all .empty_directory files are ignored. Remember that file paths can also be encoded, let’s suppose a doc directory with the following files:
doc/
components/.empty_directory
README
rdoc.rb.tt
%app_name%.rb
When invoked as:
directory "doc"
It will create a doc directory in the destination with the following files (assuming that the app_name is “blog”):
doc/
components/
README
rdoc.rb
blog.rb
Parameters
- source<String>
-
the relative path to the source root.
- destination<String>
-
the relative path to the destination root.
- config<Hash>
-
give :verbose => false to not log the status. If :recursive => false, does not look for paths recursively.
Examples
directory "doc"
directory "doc", "docs", :recursive => false
43 44 45 46 47 |
# File 'lib/bundler/vendor/thor/actions/directory.rb', line 43 def directory(source, *args, &block) config = args.last.is_a?(Hash) ? args.pop : {} destination = args.first || source action Directory.new(self, source, destination || source, config, &block) end |
#empty_directory(destination, config = {}) ⇒ Object
Creates an empty directory.
Parameters
- destination<String>
-
the relative path to the destination root.
- config<Hash>
-
give :verbose => false to not log the status.
Examples
empty_directory "doc"
14 15 16 |
# File 'lib/bundler/vendor/thor/actions/empty_directory.rb', line 14 def empty_directory(destination, config={}) action EmptyDirectory.new(self, destination, config) end |
#find_in_source_paths(file) ⇒ Object
Receives a file or directory and search for it in the source paths.
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/bundler/vendor/thor/actions.rb', line 127 def find_in_source_paths(file) relative_root = relative_to_original_destination_root(destination_root, false) source_paths.each do |source| source_file = File.(file, File.join(source, relative_root)) return source_file if File.exists?(source_file) end = "Could not find #{file.inspect} in any of your source paths. " unless self.class.source_root << "Please invoke #{self.class.name}.source_root(PATH) with the PATH containing your templates. " end if source_paths.empty? << "Currently you have no source paths." else << "Your current source paths are: \n#{source_paths.join("\n")}" end raise Error, end |
#get(source, *args, &block) ⇒ Object
Gets the content at the given address and places it at the given relative destination. If a block is given instead of destination, the content of the url is yielded and used as location.
Parameters
- source<String>
-
the address of the given content.
- destination<String>
-
the relative path to the destination root.
- config<Hash>
-
give :verbose => false to not log the status.
Examples
get "http://gist.github.com/103208", "doc/README"
get "http://gist.github.com/103208" do |content|
content.split("\n").first
end
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/bundler/vendor/thor/actions/file_manipulation.rb', line 50 def get(source, *args, &block) config = args.last.is_a?(Hash) ? args.pop : {} destination = args.first source = File.(find_in_source_paths(source.to_s)) unless source =~ /^http\:\/\// render = open(source) {|input| input.binmode.read } destination ||= if block_given? block.arity == 1 ? block.call(render) : block.call else File.basename(source) end create_file destination, render, config end |
#gsub_file(path, flag, *args, &block) ⇒ Object
Run a regular expression replacement on a file.
Parameters
- path<String>
-
path of the file to be changed
- flag<Regexp|String>
-
the regexp or string to be replaced
- replacement<String>
-
the replacement, can be also given as a block
- config<Hash>
-
give :verbose => false to not log the status.
Example
gsub_file 'app/controllers/application_controller.rb', /#\s*(filter_parameter_logging :password)/, '\1'
gsub_file 'README', /rake/, :green do |match|
match << " no more. Use thor!"
end
194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/bundler/vendor/thor/actions/file_manipulation.rb', line 194 def gsub_file(path, flag, *args, &block) return unless behavior == :invoke config = args.last.is_a?(Hash) ? args.pop : {} path = File.(path, destination_root) say_status :gsub, relative_to_original_destination_root(path), config.fetch(:verbose, true) unless [:pretend] content = File.binread(path) content.gsub!(flag, *args, &block) File.open(path, 'wb') { |file| file.write(content) } end end |
#in_root ⇒ Object
Goes to the root and execute the given block.
175 176 177 |
# File 'lib/bundler/vendor/thor/actions.rb', line 175 def in_root inside(@destination_stack.first) { yield } end |
#initialize(args = [], options = {}, config = {}) ⇒ Object
Extends initializer to add more configuration options.
Configuration
- behavior<Symbol>
-
The actions default behavior. Can be :invoke or :revoke. It also accepts :force, :skip and :pretend to set the behavior and the respective option.
- destination_root<String>
-
The root directory needed for some actions.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/bundler/vendor/thor/actions.rb', line 72 def initialize(args=[], ={}, config={}) self.behavior = case config[:behavior].to_s when "force", "skip" (, config[:behavior]) :invoke when "revoke" :revoke else :invoke end super self.destination_root = config[:destination_root] end |
#inject_into_class(path, klass, *args, &block) ⇒ Object
Injects text right after the class definition. Since it depends on inject_into_file, it’s reversible.
Parameters
- path<String>
-
path of the file to be changed
- klass<String|Class>
-
the class to be manipulated
- data<String>
-
the data to append to the class, can be also given as a block.
- config<Hash>
-
give :verbose => false to not log the status.
Examples
inject_into_class "app/controllers/application_controller.rb", " filter_parameter :password\n"
inject_into_class "app/controllers/application_controller.rb", ApplicationController do
" filter_parameter :password\n"
end
172 173 174 175 176 |
# File 'lib/bundler/vendor/thor/actions/file_manipulation.rb', line 172 def inject_into_class(path, klass, *args, &block) config = args.last.is_a?(Hash) ? args.pop : {} config.merge!(:after => /class #{klass}\n|class #{klass} .*\n/) inject_into_file(path, *(args << config), &block) end |
#inject_into_file(destination, *args, &block) ⇒ Object
Injects the given content into a file. Different from gsub_file, this method is reversible.
Parameters
- destination<String>
-
Relative path to the destination root
- data<String>
-
Data to add to the file. Can be given as a block.
- config<Hash>
-
give :verbose => false to not log the status and the flag for injection (:after or :before) or :force => true for insert two or more times the same content.
Examples
inject_into_file "config/environment.rb", "config.gem :thor", :after => "Rails::Initializer.run do |config|\n"
inject_into_file "config/environment.rb", :after => "Rails::Initializer.run do |config|\n" do
gems = ask "Which gems would you like to add?"
gems.split(" ").map{ |gem| " config.gem :#{gem}" }.join("\n")
end
25 26 27 28 29 30 31 32 |
# File 'lib/bundler/vendor/thor/actions/inject_into_file.rb', line 25 def inject_into_file(destination, *args, &block) if block_given? data, config = block, args.shift else data, config = args.shift, args.shift end action InjectIntoFile.new(self, destination, data, config) end |
#inside(dir = '', config = {}, &block) ⇒ Object
Do something in the root or on a provided subfolder. If a relative path is given it’s referenced from the current root. The full path is yielded to the block you provide. The path is set back to the previous path when the method exits.
Parameters
- dir<String>
-
the directory to move to.
- config<Hash>
-
give :verbose => true to log and use padding.
159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/bundler/vendor/thor/actions.rb', line 159 def inside(dir='', config={}, &block) verbose = config.fetch(:verbose, false) say_status :inside, dir, verbose shell.padding += 1 if verbose @destination_stack.push File.(dir, destination_root) FileUtils.mkdir_p(destination_root) unless File.exist?(destination_root) FileUtils.cd(destination_root) { block.arity == 1 ? yield(destination_root) : yield } @destination_stack.pop shell.padding -= 1 if verbose end |
#prepend_file(path, *args, &block) ⇒ Object
Prepend text to a file. Since it depends on inject_into_file, it’s reversible.
Parameters
- path<String>
-
path of the file to be changed
- data<String>
-
the data to prepend to the file, can be also given as a block.
- config<Hash>
-
give :verbose => false to not log the status.
Example
prepend_file 'config/environments/test.rb', 'config.gem "rspec"'
prepend_file 'config/environments/test.rb' do
'config.gem "rspec"'
end
128 129 130 131 132 |
# File 'lib/bundler/vendor/thor/actions/file_manipulation.rb', line 128 def prepend_file(path, *args, &block) config = args.last.is_a?(Hash) ? args.pop : {} config.merge!(:after => /\A/) inject_into_file(path, *(args << config), &block) end |
#relative_to_original_destination_root(path, remove_dot = true) ⇒ Object
Returns the given path relative to the absolute root (ie, root where the script started).
114 115 116 117 |
# File 'lib/bundler/vendor/thor/actions.rb', line 114 def relative_to_original_destination_root(path, remove_dot=true) path = path.gsub(@destination_stack[0], '.') remove_dot ? (path[2..-1] || '') : path end |
#remove_file(path, config = {}) ⇒ Object Also known as: remove_dir
Removes a file at the given location.
Parameters
- path<String>
-
path of the file to be changed
- config<Hash>
-
give :verbose => false to not log the status.
Example
remove_file 'README'
remove_file 'app/controllers/application_controller.rb'
219 220 221 222 223 224 225 |
# File 'lib/bundler/vendor/thor/actions/file_manipulation.rb', line 219 def remove_file(path, config={}) return unless behavior == :invoke path = File.(path, destination_root) say_status :remove, relative_to_original_destination_root(path), config.fetch(:verbose, true) ::FileUtils.rm_rf(path) if ![:pretend] && File.exists?(path) end |
#run(command, config = {}) ⇒ Object
Executes a command returning the contents of the command.
Parameters
- command<String>
-
the command to be executed.
- config<Hash>
-
give :verbose => false to not log the status. Specify :with to append an executable to command executation.
Example
inside('vendor') do
run('ln -s ~/edge rails')
end
222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/bundler/vendor/thor/actions.rb', line 222 def run(command, config={}) return unless behavior == :invoke 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 [:pretend] end |
#run_ruby_script(command, config = {}) ⇒ Object
Executes a ruby script (taking into account WIN32 platform quirks).
Parameters
- command<String>
-
the command to be executed.
- config<Hash>
-
give :verbose => false to not log the status.
243 244 245 246 |
# File 'lib/bundler/vendor/thor/actions.rb', line 243 def run_ruby_script(command, config={}) return unless behavior == :invoke run command, config.merge(:with => Thor::Util.ruby_command) end |
#source_paths ⇒ Object
Holds source paths in instance so they can be manipulated.
121 122 123 |
# File 'lib/bundler/vendor/thor/actions.rb', line 121 def source_paths @source_paths ||= self.class.source_paths_for_search end |
#template(source, *args, &block) ⇒ Object
Gets an ERB template at the relative source, executes it and makes a copy at the relative destination. If the destination is not given it’s assumed to be equal to the source removing .tt from the filename.
Parameters
- source<String>
-
the relative path to the source root.
- destination<String>
-
the relative path to the destination root.
- config<Hash>
-
give :verbose => false to not log the status.
Examples
template "README", "doc/README"
template "doc/README"
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/bundler/vendor/thor/actions/file_manipulation.rb', line 81 def template(source, *args, &block) config = args.last.is_a?(Hash) ? args.pop : {} destination = args.first || source source = File.(find_in_source_paths(source.to_s)) context = instance_eval('binding') create_file destination, nil, config do content = ERB.new(::File.binread(source), nil, '-').result(context) content = block.call(content) if block content end end |
#thor(task, *args) ⇒ Object
Run a thor command. A hash of options can be given and it’s converted to switches.
Parameters
- task<String>
-
the task to be invoked
- args<Array>
-
arguments to the task
- config<Hash>
-
give :verbose => false to not log the status. Other options are given as parameter to Thor.
Examples
thor :install, "http://gist.github.com/103208"
#=> thor install http://gist.github.com/103208
thor :list, :all => true, :substring => 'rails'
#=> thor list --all --substring=rails
265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/bundler/vendor/thor/actions.rb', line 265 def thor(task, *args) config = args.last.is_a?(Hash) ? args.pop : {} verbose = config.key?(:verbose) ? config.delete(:verbose) : true pretend = config.key?(:pretend) ? config.delete(:pretend) : false args.unshift task args.push Thor::Options.to_switches(config) command = args.join(' ').strip run command, :with => :thor, :verbose => verbose, :pretend => pretend end |