-
Notifications
You must be signed in to change notification settings - Fork 552
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
373 additions
and
232 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
class Thor | ||
module Actions | ||
# Executes a command. | ||
# | ||
# ==== Parameters | ||
# command<String>:: the command to be executed. | ||
# log_status<Boolean>:: if false, does not log the status. True by default. | ||
# If a symbol is given, uses it as the output color. | ||
# | ||
# ==== Example | ||
# | ||
# inside('vendor') do | ||
# run('ln -s ~/edge rails') | ||
# end | ||
# | ||
def run(command, log_status=true) | ||
say_status_if_log :run, "#{command} from #{relative_to_absolute_root(root, false)}", log_status | ||
`#{command}` unless options[:pretend] | ||
end | ||
|
||
# Executes a ruby script (taking into account WIN32 platform quirks). | ||
# | ||
# ==== Parameters | ||
# command<String>:: the command to be executed. | ||
# log_status<Boolean>:: if false, does not log the status. True by default. | ||
# If a symbol is given, uses it as the output color. | ||
# | ||
def run_ruby_script(command, log_status=true) | ||
run("ruby #{command}", log_status) | ||
end | ||
|
||
# Run a command in git. | ||
# | ||
# ==== Parameters | ||
# command<String>:: the command to be executed. | ||
# log_status<Boolean>:: if false, does not log the status. True by default. | ||
# If a symbol is given, uses it as the output color. | ||
# | ||
# ==== Examples | ||
# | ||
# git :init | ||
# git :add => "this.file that.rb" | ||
# git :add => "onefile.rb", :rm => "badfile.cxx" | ||
# | ||
def git(command, log_status=true) | ||
in_root do | ||
if command.is_a?(Symbol) | ||
run "git #{command}", log_status | ||
else | ||
command.each do |command, options| | ||
run "git #{command} #{options}", log_status | ||
end | ||
end | ||
end | ||
end | ||
|
||
# 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 | ||
# options<Hash>:: a hash with options used on invocation | ||
# log_status<Boolean>:: if false, does not log the status. True by default. | ||
# If a symbol is given, uses it as the output color. | ||
# | ||
# ==== 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 | ||
# | ||
def thor(task, *args) | ||
log_status = [true, false].include?(args.last) ? args.pop : true | ||
options = args.last.is_a?(Hash) ? args.pop : {} | ||
|
||
in_root do | ||
args.unshift "thor #{task}" | ||
args.push Thor::Options.to_switches(options) | ||
run args.join(' ').strip, log_status | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
class Thor | ||
module Actions | ||
|
||
# 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 | ||
# log_status<Boolean>:: if false, does not log the status. True by default. | ||
# If a symbol is given, uses it as the output color. | ||
# | ||
# ==== 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 | ||
# | ||
def gsub_file(path, flag, *args, &block) | ||
log_status = args.last.is_a?(Symbol) || [ true, false ].include?(args.last) ? args.pop : true | ||
|
||
path = File.expand_path(path, root) | ||
say_status_if_log :gsub, relative_to_absolute_root(path), log_status | ||
|
||
unless options[:pretend] | ||
content = File.read(path) | ||
content.gsub!(flag, *args, &block) | ||
File.open(path, 'wb') { |file| file.write(content) } | ||
end | ||
end | ||
|
||
# Append text to a file. | ||
# | ||
# ==== Parameters | ||
# path<String>:: path of the file to be changed | ||
# data<String>:: the data to append to the file | ||
# log_status<Boolean>:: if false, does not log the status. True by default. | ||
# If a symbol is given, uses it as the output color. | ||
# | ||
# ==== Example | ||
# | ||
# append_file 'config/environments/test.rb', 'config.gem "rspec"' | ||
# | ||
def append_file(path, data, log_status=true) | ||
path = File.expand_path(path, root) | ||
say_status_if_log :append, relative_to_absolute_root(path), log_status | ||
|
||
File.open(path, 'ab') { |file| file.write(data) } unless options[:pretend] | ||
end | ||
|
||
# Prepend text to a file. | ||
# | ||
# ==== Parameters | ||
# path<String>:: path of the file to be changed | ||
# data<String>:: the data to prepend to the file | ||
# log_status<Boolean>:: if false, does not log the status. True by default. | ||
# If a symbol is given, uses it as the output color. | ||
# | ||
# ==== Example | ||
# | ||
# prepend_file 'config/environments/test.rb', 'config.gem "rspec"' | ||
# | ||
def prepend_file(path, data, log_status=true) | ||
path = File.expand_path(path, root) | ||
say_status_if_log :prepend, relative_to_absolute_root(path), log_status | ||
|
||
File.open(path, 'r+b') { |file| file.write(data) } unless options[:pretend] | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.