Module Thor::Actions
In: lib/bundler/vendor/thor/actions/empty_directory.rb
lib/bundler/vendor/thor/actions/create_file.rb
lib/bundler/vendor/thor/actions/create_link.rb
lib/bundler/vendor/thor/actions/file_manipulation.rb
lib/bundler/vendor/thor/actions/directory.rb
lib/bundler/vendor/thor/actions/inject_into_file.rb
lib/bundler/vendor/thor/actions.rb

Methods

Classes and Modules

Module Thor::Actions::ClassMethods

Attributes

behavior  [RW] 
output_buffer  [RW] 

Public Class methods

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.

Public Instance methods

add_file(destination, *args, &block)

Alias for create_file

add_link(destination, *args, &block)

Alias for create_link

append_file(path, *args, &block)

Alias for append_to_file

Append text to a file. Since it depends on insert_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_to_file 'config/environments/test.rb', 'config.gem "rspec"'

  append_to_file 'config/environments/test.rb' do
    'config.gem "rspec"'
  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"

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

Comment all lines matching a given regex. It will leave the space which existed before the beginning of the line in tact and will insert a single space after the comment hash.

Parameters

path<String>:path of the file to be changed
flag<Regexp|String>:the regexp or string used to decide which lines to comment
config<Hash>:give :verbose => false to not log the status.

Example

  comment_lines 'config/initializers/session_store.rb', /cookie_store/

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"

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/apache.conf", "your apache config"

Create a new file relative to the destination root from the given source.

Parameters

destination<String>:the relative path to the destination root.
source<String|NilClass>:the relative path to the source root.
config<Hash>:give :verbose => false to not log the status.
  :: give :symbolic => false for hard link.

Examples

  create_link "config/apache.conf", "/etc/apache.conf"

Returns the root for this thor class (also aliased as destination root).

Sets the root for this thor class. Relatives path are added to the directory where the script was invoked and expanded.

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. If any file name is wrapped within % signs, the text within the % signs will be executed as a method and replaced with the returned value. 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` method returns the value "blog"):

  doc/
    components/
    README
    rdoc.rb
    blog.rb

Encoded path note: Since Thor internals use Object#respond_to? to check if it can expand %something%, this `something` should be a public method in the class calling directory. If a method is private, Thor stack raises PrivateMethodEncodedError.

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

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"

Receives a file or directory and search for it in the source paths.

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

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

Goes to the root and execute the given block.

Injects text right after the class definition. Since it depends on insert_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", ApplicationController, "  filter_parameter :password\n"

  inject_into_class "app/controllers/application_controller.rb", ApplicationController do
    "  filter_parameter :password\n"
  end
inject_into_file(destination, *args, &block)

Alias for insert_into_file

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

  insert_into_file "config/environment.rb", "config.gem :thor", :after => "Rails::Initializer.run do |config|\n"

  insert_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

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.

Links 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

  link_file "README", "doc/README"

  link_file "doc/README"
prepend_file(path, *args, &block)

Alias for prepend_to_file

Prepend text to a file. Since it depends on insert_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_to_file 'config/environments/test.rb', 'config.gem "rspec"'

  prepend_to_file 'config/environments/test.rb' do
    'config.gem "rspec"'
  end

Returns the given path relative to the absolute root (ie, root where the script started).

remove_dir(path, config={})

Alias for remove_file

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'

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, :capture => true to hide to output. Specify :with to append an executable to command executation.

Example

  inside('vendor') do
    run('ln -s ~/edge rails')
  end

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.

Holds source paths in instance so they can be manipulated.

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"

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, :capture => true to hide to output. 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

Uncomment all lines matching a given regex. It will leave the space which existed before the comment hash in tact but will remove any spacing between the comment hash and the beginning of the line.

Parameters

path<String>:path of the file to be changed
flag<Regexp|String>:the regexp or string used to decide which lines to uncomment
config<Hash>:give :verbose => false to not log the status.

Example

  uncomment_lines 'config/initializers/session_store.rb', /active_record/

[Validate]