diff --git a/lib/homesick.rb b/lib/homesick.rb index 64d6d1e..3f90558 100644 --- a/lib/homesick.rb +++ b/lib/homesick.rb @@ -75,6 +75,32 @@ class Homesick < Thor end end + desc "track FILE CASTLE", "add a file to a castle" + def track(file, castle) + castle = Pathname.new(castle) + file = Pathname.new(file) + check_castle_existance(castle, 'track') + + github_user = `git config github.user`.chomp + github_user = nil if github_user == "" + github_repo = castle.basename + + absolute_path = file.expand_path + castle_path = castle_dir(castle) + mv absolute_path, castle_path + + inside castle_path do + system "git add #{file.basename}" + system "git commit -m \"Added #{file.basename}\"" + end + + inside home_dir do + absolute_path = castle_dir(castle) + file.basename + home_path = home_dir + file + ln_s absolute_path, home_path + end + end + desc "list", "List cloned castles" def list #require 'ruby-debug'; breakpoint @@ -94,7 +120,6 @@ class Homesick < Thor github_user = nil if github_user == "" github_repo = castle.basename - empty_directory castle inside castle do git_init @@ -107,6 +132,7 @@ class Homesick < Thor end end + protected def home_dir diff --git a/lib/homesick/actions.rb b/lib/homesick/actions.rb index c94f6be..f42bcf4 100644 --- a/lib/homesick/actions.rb +++ b/lib/homesick/actions.rb @@ -59,6 +59,22 @@ class Homesick system "git pull --quiet" unless options[:pretend] end + def mv(source, destination, config = {}) + source = Pathname.new(source) + destination = Pathname.new(destination + source.basename) + + if destination.exist? + say_status :conflict, "#{destination} exists", :red unless options[:quiet] + + if options[:force] || shell.file_collision(destination) { source } + system "mv #{source} #{destination}" unless options[:pretend] + end + else + # this needs some sort of message here. + system "mv #{source} #{destination}" unless options[:pretend] + end + 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 25b7859..e8007a0 100644 --- a/spec/homesick_spec.rb +++ b/spec/homesick_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require 'spec_helper' describe Homesick do before do @@ -83,13 +83,30 @@ describe Homesick do system "git remote add origin git://github.com/technicalpickles/zomg.git >/dev/null 2>&1" end end - end - + end @homesick.should_receive(:say_status).with("zomg", "git://github.com/technicalpickles/zomg.git", :cyan) @homesick.should_receive(:say_status).with("wtf/zomg", "git://github.com/technicalpickles/zomg.git", :cyan) @homesick.list end + end + describe "track" do + it "should move the tracked file into the castle" do + some_rc_file = @user_dir.file '.some_rc_file' + homesickrepo = @user_dir.directory('.homesick').directory('repos').directory('castle_repo') + castle_path = homesickrepo.directory 'home' + + # There is some hideous thing going on with construct; rming the file I'm moving works on this test. + # Otherwise when track ln_s's it back out, it sees a conflict. Its as if file operations don't + # actually effect this thing, or something. + system "rm #{some_rc_file.to_s}" + Dir.chdir homesickrepo do + system "git init >/dev/null 2>&1" + end + + @homesick.should_receive(:mv).with(some_rc_file, castle_path) + @homesick.track(some_rc_file.to_s, 'castle_repo') + end end end