diff --git a/lib/homesick.rb b/lib/homesick.rb index 796da09..0616354 100644 --- a/lib/homesick.rb +++ b/lib/homesick.rb @@ -1,3 +1,4 @@ +# -*- encoding : utf-8 -*- require 'thor' class Homesick < Thor @@ -227,6 +228,16 @@ class Homesick < Thor end end + desc "destroy CASTLE", "Delete all symlinks and remove the cloned repository" + def destroy(name) + check_castle_existance name, "destroy" + + if shell.yes?("This will destroy your castle irreversible! Are you sure?") + unlink(name) + rm_rf repos_dir.join(name) + end + + end protected diff --git a/lib/homesick/actions.rb b/lib/homesick/actions.rb index 1499cbf..fe5719f 100644 --- a/lib/homesick/actions.rb +++ b/lib/homesick/actions.rb @@ -1,3 +1,4 @@ +# -*- encoding : utf-8 -*- class Homesick module Actions # TODO move this to be more like thor's template, empty_directory, etc @@ -108,6 +109,37 @@ class Homesick end end + def rm(file) + say_status "rm #{file}", '', :green unless options[:quiet] + system "rm #{file}" if File.exists?(file) + end + + def rm_rf(dir) + say_status "rm -rf #{dir}", '', :green unless options[:quiet] + system "rm -rf #{dir}" + end + + def rm_link(target) + target = Pathname.new(target) + + if target.symlink? + say_status :unlink, "#{target.expand_path}", :green unless options[:quiet] + FileUtils.rm_rf target + else + say_status :conflict, "#{target} is not a symlink", :red unless options[:quiet] + end + end + + def rm(file) + say_status "rm #{file}", '', :green unless options[:quiet] + system "rm #{file}" + end + + def rm_r(dir) + say_status "rm -r #{dir}", '', :green unless options[:quiet] + system "rm -r #{dir}" + end + def ln_s(source, destination, config = {}) source = Pathname.new(source) destination = Pathname.new(destination) diff --git a/spec/homesick_spec.rb b/spec/homesick_spec.rb index 1d60226..d05d735 100644 --- a/spec/homesick_spec.rb +++ b/spec/homesick_spec.rb @@ -1,3 +1,4 @@ +# -*- encoding : utf-8 -*- require 'spec_helper' describe 'homesick' do @@ -490,4 +491,26 @@ describe 'homesick' do end end end + + describe "destroy" do + it "removes the symlink files" do + expect_any_instance_of(Thor::Shell::Basic).to receive(:yes?).and_return('y') + given_castle("stronghold") + some_rc_file = home.file '.some_rc_file' + homesick.track(some_rc_file.to_s, "stronghold") + homesick.destroy('stronghold') + + some_rc_file.should_not be_exist + end + + it "deletes the cloned repository" do + expect_any_instance_of(Thor::Shell::Basic).to receive(:yes?).and_return('y') + castle = given_castle("stronghold") + some_rc_file = home.file '.some_rc_file' + homesick.track(some_rc_file.to_s, "stronghold") + homesick.destroy('stronghold') + + castle.should_not be_exist + end + end end