Added ability for methods to be overrode, through the pretend and quiet

options, skipping their default behaviour if so.
This commit is contained in:
Jeremy Cook
2014-04-20 10:52:30 -04:00
parent b5138bcdd1
commit d4f9633a0c
5 changed files with 67 additions and 51 deletions

View File

@@ -8,7 +8,7 @@ module Homesick
destination = Pathname.new(destination + source.basename) destination = Pathname.new(destination + source.basename)
if destination.exist? if destination.exist?
say_status :conflict, "#{destination} exists", :red unless options[:quiet] say_status :conflict, "#{destination} exists", :red
FileUtils.mv source, destination if (options[:force] || shell.file_collision(destination) { source }) && !options[:pretend] FileUtils.mv source, destination if (options[:force] || shell.file_collision(destination) { source }) && !options[:pretend]
else else
@@ -18,7 +18,7 @@ module Homesick
end end
def rm_rf(dir) def rm_rf(dir)
say_status "rm -rf #{dir}", '', :green unless options[:quiet] say_status "rm -rf #{dir}", '', :green
FileUtils.rm_r dir, force: true FileUtils.rm_r dir, force: true
end end
@@ -26,20 +26,20 @@ module Homesick
target = Pathname.new(target) target = Pathname.new(target)
if target.symlink? if target.symlink?
say_status :unlink, "#{target.expand_path}", :green unless options[:quiet] say_status :unlink, "#{target.expand_path}", :green
FileUtils.rm_rf target FileUtils.rm_rf target
else else
say_status :conflict, "#{target} is not a symlink", :red unless options[:quiet] say_status :conflict, "#{target} is not a symlink", :red
end end
end end
def rm(file) def rm(file)
say_status "rm #{file}", '', :green unless options[:quiet] say_status "rm #{file}", '', :green
FileUtils.rm file, force: true FileUtils.rm file, force: true
end end
def rm_r(dir) def rm_r(dir)
say_status "rm -r #{dir}", '', :green unless options[:quiet] say_status "rm -r #{dir}", '', :green
FileUtils.rm_r dir FileUtils.rm_r dir
end end
@@ -64,16 +64,16 @@ module Homesick
def handle_symlink_action(action, source, destination) def handle_symlink_action(action, source, destination)
case action case action
when :identical when :identical
say_status :identical, destination.expand_path, :blue unless options[:quiet] say_status :identical, destination.expand_path, :blue
when :symlink_conflict when :symlink_conflict
say_status :conflict, say_status :conflict,
"#{destination} exists and points to #{destination.readlink}", "#{destination} exists and points to #{destination.readlink}",
:red unless options[:quiet] :red
FileUtils.rm destination FileUtils.rm destination
FileUtils.ln_s source, destination, force: true unless options[:pretend] FileUtils.ln_s source, destination, force: true unless options[:pretend]
when :conflict when :conflict
say_status :conflict, "#{destination} exists", :red unless options[:quiet] say_status :conflict, "#{destination} exists", :red
if collision_accepted? if collision_accepted?
FileUtils.rm_r destination, force: true unless options[:pretend] FileUtils.rm_r destination, force: true unless options[:pretend]
@@ -82,7 +82,7 @@ module Homesick
else else
say_status :symlink, say_status :symlink,
"#{source.expand_path} to #{destination.expand_path}", "#{source.expand_path} to #{destination.expand_path}",
:green unless options[:quiet] :green
FileUtils.ln_s source, destination unless options[:pretend] FileUtils.ln_s source, destination unless options[:pretend]
end end
end end

View File

@@ -12,12 +12,12 @@ module Homesick
FileUtils.mkdir_p destination.dirname FileUtils.mkdir_p destination.dirname
if destination.directory? if destination.directory?
say_status :exist, destination.expand_path, :blue unless options[:quiet] say_status :exist, destination.expand_path, :blue
else else
say_status 'git clone', say_status 'git clone',
"#{repo} to #{destination.expand_path}", "#{repo} to #{destination.expand_path}",
:green unless options[:quiet] :green
system "git clone -q --config push.default=upstream --recursive #{repo} #{destination}" unless options[:pretend] system "git clone -q --config push.default=upstream --recursive #{repo} #{destination}"
end end
end end
@@ -26,10 +26,10 @@ module Homesick
inside path do inside path do
if path.join('.git').exist? if path.join('.git').exist?
say_status 'git init', 'already initialized', :blue unless options[:quiet] say_status 'git init', 'already initialized', :blue
else else
say_status 'git init', '' unless options[:quiet] say_status 'git init', ''
system 'git init >/dev/null' unless options[:pretend] system 'git init >/dev/null'
end end
end end
end end
@@ -39,55 +39,55 @@ module Homesick
existing_remote = nil if existing_remote == '' existing_remote = nil if existing_remote == ''
if existing_remote if existing_remote
say_status 'git remote', "#{name} already exists", :blue unless options[:quiet] say_status 'git remote', "#{name} already exists", :blue
else else
say_status 'git remote', "add #{name} #{url}" unless options[:quiet] say_status 'git remote', "add #{name} #{url}"
system "git remote add #{name} #{url}" unless options[:pretend] system "git remote add #{name} #{url}"
end end
end end
def git_submodule_init(config = {}) def git_submodule_init(config = {})
say_status 'git submodule', 'init', :green unless options[:quiet] say_status 'git submodule', 'init', :green
system 'git submodule --quiet init' unless options[:pretend] system 'git submodule --quiet init'
end end
def git_submodule_update(config = {}) def git_submodule_update(config = {})
say_status 'git submodule', 'update', :green unless options[:quiet] say_status 'git submodule', 'update', :green
system 'git submodule --quiet update --init --recursive >/dev/null 2>&1' unless options[:pretend] system 'git submodule --quiet update --init --recursive >/dev/null 2>&1'
end end
def git_pull(config = {}) def git_pull(config = {})
say_status 'git pull', '', :green unless options[:quiet] say_status 'git pull', '', :green
system 'git pull --quiet' unless options[:pretend] system 'git pull --quiet'
end end
def git_push(config = {}) def git_push(config = {})
say_status 'git push', '', :green unless options[:quiet] say_status 'git push', '', :green
system 'git push' unless options[:pretend] system 'git push'
end end
def git_commit_all(config = {}) def git_commit_all(config = {})
say_status 'git commit all', '', :green unless options[:quiet] say_status 'git commit all', '', :green
if config[:message] if config[:message]
system "git commit -a -m '#{config[:message]}'" unless options[:pretend] system "git commit -a -m '#{config[:message]}'"
else else
system 'git commit -v -a' unless options[:pretend] system 'git commit -v -a'
end end
end end
def git_add(file, config = {}) def git_add(file, config = {})
say_status 'git add file', '', :green unless options[:quiet] say_status 'git add file', '', :green
system "git add '#{file}'" unless options[:pretend] system "git add '#{file}'"
end end
def git_status(config = {}) def git_status(config = {})
say_status 'git status', '', :green unless options[:quiet] say_status 'git status', '', :green
system 'git status' unless options[:pretend] system 'git status'
end end
def git_diff(config = {}) def git_diff(config = {})
say_status 'git diff', '', :green unless options[:quiet] say_status 'git diff', '', :green
system 'git diff' unless options[:pretend] system 'git diff'
end end
end end
end end

View File

@@ -56,12 +56,12 @@ module Homesick
if homesickrc.exist? if homesickrc.exist?
proceed = shell.yes?("#{name} has a .homesickrc. Proceed with evaling it? (This could be destructive)") proceed = shell.yes?("#{name} has a .homesickrc. Proceed with evaling it? (This could be destructive)")
if proceed if proceed
shell.say_status 'eval', homesickrc say_status 'eval', homesickrc
inside destination do inside destination do
eval homesickrc.read, binding, homesickrc.expand_path.to_s eval homesickrc.read, binding, homesickrc.expand_path.to_s
end end
else else
shell.say_status 'eval skip', say_status 'eval skip',
"not evaling #{homesickrc}, #{destination} may need manual configuration", "not evaling #{homesickrc}, #{destination} may need manual configuration",
:blue :blue
end end
@@ -78,7 +78,7 @@ module Homesick
def pull(name = DEFAULT_CASTLE_NAME) def pull(name = DEFAULT_CASTLE_NAME)
if options[:all] if options[:all]
inside_each_castle do |castle| inside_each_castle do |castle|
shell.say castle.to_s.gsub(repos_dir.to_s + '/', '') + ':' say castle.to_s.gsub(repos_dir.to_s + '/', '') + ':'
update_castle castle update_castle castle
end end
else else
@@ -156,9 +156,9 @@ module Homesick
target.delete target.delete
mv absolute_path, castle_path mv absolute_path, castle_path
else else
shell.say_status(:track, say_status(:track,
"#{target} already exists, and is more recent than #{file}. Run 'homesick SYMLINK CASTLE' to create symlinks.", "#{target} already exists, and is more recent than #{file}. Run 'homesick SYMLINK CASTLE' to create symlinks.",
:blue) unless options[:quiet] :blue)
end end
else else
mv absolute_path, castle_path mv absolute_path, castle_path
@@ -294,9 +294,9 @@ module Homesick
full_command = args.join(' ') full_command = args.join(' ')
say_status "exec '#{full_command}'", say_status "exec '#{full_command}'",
"#{options[:pretend] ? 'Would execute' : 'Executing command'} '#{full_command}' in castle '#{castle}'", "#{options[:pretend] ? 'Would execute' : 'Executing command'} '#{full_command}' in castle '#{castle}'",
:green unless options[:quiet] :green
inside repos_dir.join(castle) do inside repos_dir.join(castle) do
system(full_command) unless options[:pretend] system(full_command)
end end
end end
@@ -323,8 +323,8 @@ module Homesick
inside_each_castle do |castle| inside_each_castle do |castle|
say_status "exec '#{full_command}'", say_status "exec '#{full_command}'",
"#{options[:pretend] ? 'Would execute' : 'Executing command'} '#{full_command}' in castle '#{castle}'", "#{options[:pretend] ? 'Would execute' : 'Executing command'} '#{full_command}' in castle '#{castle}'",
:green unless options[:quiet] :green
system(full_command) unless options[:pretend] system(full_command)
end end
end end

View File

@@ -2,6 +2,22 @@
module Homesick module Homesick
# Various utility methods that are used by Homesick # Various utility methods that are used by Homesick
module Utils module Utils
QUIETABLE = ['say_status']
PRETENDABLE = ['system']
QUIETABLE.each do |method_name|
define_method(method_name) do |*args|
super(*args) unless options[:quiet]
end
end
PRETENDABLE.each do |method_name|
define_method(method_name) do |*args|
super(*args) unless options[:pretend]
end
end
protected protected
def home_dir def home_dir

View File

@@ -24,7 +24,7 @@ describe Homesick::CLI do
end end
expect_any_instance_of(Thor::Shell::Basic).to receive(:yes?).with(be_a(String)).and_return(true) expect_any_instance_of(Thor::Shell::Basic).to receive(:yes?).with(be_a(String)).and_return(true)
expect_any_instance_of(Thor::Shell::Basic).to receive(:say_status).with('eval', kind_of(Pathname)) expect(homesick).to receive(:say_status).with('eval', kind_of(Pathname))
homesick.clone local_repo homesick.clone local_repo
expect(castles.join('some_repo').join('testing')).to exist expect(castles.join('some_repo').join('testing')).to exist
@@ -129,7 +129,7 @@ describe Homesick::CLI do
end" end"
end end
expect_any_instance_of(Thor::Shell::Basic).to receive(:say_status).with('eval', kind_of(Pathname)) expect(homesick).to receive(:say_status).with('eval', kind_of(Pathname))
homesick.rc castle homesick.rc castle
expect(castle.join('testing')).to exist expect(castle.join('testing')).to exist
@@ -148,7 +148,7 @@ describe Homesick::CLI do
end" end"
end end
expect_any_instance_of(Thor::Shell::Basic).to receive(:say_status).with('eval skip', /not evaling.+/, :blue) expect(homesick).to receive(:say_status).with('eval skip', /not evaling.+/, :blue)
homesick.rc castle homesick.rc castle
expect(castle.join('testing')).not_to exist expect(castle.join('testing')).not_to exist