diff --git a/lib/homesick.rb b/lib/homesick.rb index b90de36..6b28ef5 100644 --- a/lib/homesick.rb +++ b/lib/homesick.rb @@ -153,33 +153,18 @@ class Homesick < Thor # Are we already tracking this or anything inside it? target = Pathname.new(castle_path.join(file.basename)) - if target.exist? - if absolute_path.directory? - child_files = absolute_path.children - child_files.each do |child| - - if target.join(child.basename).exist? - next - end - - mv child, target - end + move_dir_contents(target, absolute_path) absolute_path.rmtree - manifest = Pathname.new(repos_dir.join(castle, '.manifest')) - if manifest.exist? - lines = IO.readlines(manifest).delete_if { |line| line == "#{relative_dir + file.basename}\n" } - File.open(manifest, 'w') { |manfile| manfile.puts lines } - end + manifest_remove(castle, relative_dir + file.basename) - elsif absolute_path.mtime > target.mtime && !absolute_path.symlink? + elsif more_recent? absolute_path, target target.delete mv absolute_path, castle_path else - shell.say_status(:track, "#{target} already exists, and is more recent than #{file}. Run 'homesick SYMLINK CASTLE' to create symlinks.") + shell.say_status(:track, "#{target} already exists, and is more recent than #{file}. Run 'homesick SYMLINK CASTLE' to create symlinks.", :blue) unless options[:quiet] end - else mv absolute_path, castle_path end @@ -194,14 +179,10 @@ class Homesick < Thor git_add absolute_path end - # are we tracking something nested? Add the parent dir to the manifest unless its already listed + # are we tracking something nested? Add the parent dir to the manifest unless relative_dir.eql?(Pathname.new('.')) - manifest_path = Pathname.new(repos_dir.join(castle, '.manifest')) - File.open(manifest_path, 'a+') do |manifest| - manifest.puts relative_dir unless manifest.readlines.inject(false) { |memo, line| line.eql?("#{relative_dir.to_s}\n") || memo } - end + manifest_add(castle, relative_dir) end - end desc "list", "List cloned castles" @@ -310,4 +291,54 @@ class Homesick < Thor git_push end end + + def manifest(castle) + Pathname.new(repos_dir.join(castle, '.manifest')) + end + + def manifest_add(castle, path) + manifest_path = manifest(castle) + File.open(manifest_path, 'a+') do |manifest| + manifest.puts path unless manifest.readlines.inject(false) { |memo, line| line.eql?("#{path.to_s}\n") || memo } + end + + inside castle_dir(castle) do + git_add manifest_path + end + end + + def manifest_remove(castle, path) + manifest_file = manifest(castle) + if manifest_file.exist? + lines = IO.readlines(manifest_file).delete_if { |line| line == "#{path}\n" } + File.open(manifest_file, 'w') { |manfile| manfile.puts lines } + end + + inside castle_dir(castle) do + git_add manifest_file + end + end + + def move_dir_contents(target, dir_path) + child_files = dir_path.children + child_files.each do |child| + + target_path = target.join(child.basename) + if target_path.exist? + if more_recent?(child, target_path) && target.file? + target_path.delete + mv child, target + end + next + end + + mv child, target + end + end + + def more_recent?(first, second) + first_p = Pathname.new(first) + second_p = Pathname.new(second) + first_p.mtime > second_p.mtime && !first_p.symlink? + end end