refactor, cleanup and tweak
This commit is contained in:
@@ -125,33 +125,18 @@ class Homesick < Thor
|
|||||||
|
|
||||||
# Are we already tracking this or anything inside it?
|
# Are we already tracking this or anything inside it?
|
||||||
target = Pathname.new(castle_path.join(file.basename))
|
target = Pathname.new(castle_path.join(file.basename))
|
||||||
|
|
||||||
if target.exist?
|
if target.exist?
|
||||||
|
|
||||||
if absolute_path.directory?
|
if absolute_path.directory?
|
||||||
child_files = absolute_path.children
|
move_dir_contents(target, absolute_path)
|
||||||
child_files.each do |child|
|
|
||||||
|
|
||||||
if target.join(child.basename).exist?
|
|
||||||
next
|
|
||||||
end
|
|
||||||
|
|
||||||
mv child, target
|
|
||||||
end
|
|
||||||
absolute_path.rmtree
|
absolute_path.rmtree
|
||||||
manifest = Pathname.new(repos_dir.join(castle, '.manifest'))
|
manifest_remove(castle, relative_dir + file.basename)
|
||||||
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
|
|
||||||
|
|
||||||
elsif absolute_path.mtime > target.mtime && !absolute_path.symlink?
|
elsif more_recent? absolute_path, target
|
||||||
target.delete
|
target.delete
|
||||||
mv absolute_path, castle_path
|
mv absolute_path, castle_path
|
||||||
else
|
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
|
end
|
||||||
|
|
||||||
else
|
else
|
||||||
mv absolute_path, castle_path
|
mv absolute_path, castle_path
|
||||||
end
|
end
|
||||||
@@ -166,14 +151,10 @@ class Homesick < Thor
|
|||||||
git_add absolute_path
|
git_add absolute_path
|
||||||
end
|
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('.'))
|
unless relative_dir.eql?(Pathname.new('.'))
|
||||||
manifest_path = Pathname.new(repos_dir.join(castle, '.manifest'))
|
manifest_add(castle, relative_dir)
|
||||||
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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
desc "list", "List cloned castles"
|
desc "list", "List cloned castles"
|
||||||
@@ -265,4 +246,54 @@ class Homesick < Thor
|
|||||||
git_push
|
git_push
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user