Handle symlink collisions much more gracefully.
This commit is contained in:
@@ -3,18 +3,38 @@ require 'thor'
|
|||||||
class Homesick < Thor
|
class Homesick < Thor
|
||||||
include Thor::Actions
|
include Thor::Actions
|
||||||
|
|
||||||
|
# Hack in support for diffing symlinks
|
||||||
|
class Shell < Thor::Shell::Color
|
||||||
|
|
||||||
|
def show_diff(destination, content)
|
||||||
|
destination = Pathname.new(destination)
|
||||||
|
|
||||||
|
if destination.symlink?
|
||||||
|
say "+ #{content.expand_path}", :green, true
|
||||||
|
say "- #{destination.readlink}", :red, true
|
||||||
|
else
|
||||||
|
super
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
def initialize(args=[], options={}, config={})
|
||||||
|
super
|
||||||
|
self.shell = Homesick::Shell.new
|
||||||
|
end
|
||||||
|
|
||||||
GIT_URI_PATTERN = /^git:\/\//
|
GIT_URI_PATTERN = /^git:\/\//
|
||||||
GITHUB_NAME_REPO_PATTERN = /^[A-Za-z_-]+\/[A-Za-z_-]+$/
|
GITHUB_NAME_REPO_PATTERN = /([A-Za-z_-]+)\/([A-Za-z_-]+)/
|
||||||
|
|
||||||
desc "clone URI", "Clone home's +uri+ for use with homesick"
|
desc "clone URI", "Clone home's +uri+ for use with homesick"
|
||||||
def clone(uri)
|
def clone(uri)
|
||||||
empty_directory repos_dir
|
empty_directory repos_dir, :verbose => false
|
||||||
inside repos_dir do
|
inside repos_dir do
|
||||||
if uri =~ GIT_URI_PATTERN
|
if uri =~ GIT_URI_PATTERN
|
||||||
git_clone uri
|
git_clone uri
|
||||||
elsif uri =~ GITHUB_NAME_REPO_PATTERN
|
elsif uri =~ GITHUB_NAME_REPO_PATTERN
|
||||||
match = uri.match(/([A-Za-z_-]+)\/([A-Za-z_-]+)/)
|
git_clone "git://github.com/#{$1}/#{$2}.git", "#{$1}_#{$2}"
|
||||||
git_clone "git://github.com/#{match[0]}.git", "#{match[1]}_#{match[2]}"
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -28,7 +48,8 @@ class Homesick < Thor
|
|||||||
|
|
||||||
inside user_dir do
|
inside user_dir do
|
||||||
adjusted_path = (user_dir + path).basename
|
adjusted_path = (user_dir + path).basename
|
||||||
run "ln -sf #{absolute_path} #{adjusted_path}"
|
|
||||||
|
symlink absolute_path, adjusted_path
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -61,8 +82,40 @@ class Homesick < Thor
|
|||||||
protected
|
protected
|
||||||
|
|
||||||
# TODO move this to be more like thor's template, empty_directory, etc
|
# TODO move this to be more like thor's template, empty_directory, etc
|
||||||
def git_clone(repo, destination = nil)
|
def git_clone(repo, config = {})
|
||||||
run "git clone #{repo} #{destination}"
|
config ||= {}
|
||||||
|
destination = config[:destination] || begin
|
||||||
|
repo =~ /([^\/]+)\.git$/
|
||||||
|
$1
|
||||||
|
end
|
||||||
|
|
||||||
|
destination = Pathname.new(destination) unless destination.kind_of?(Pathname)
|
||||||
|
|
||||||
|
if ! destination.directory?
|
||||||
|
say_status 'git clone', "#{repo} to #{destination.expand_path}", :green if config.fetch(:verbose, true)
|
||||||
|
system "git clone #{repo} #{destination}" unless options[:pretend]
|
||||||
|
else
|
||||||
|
say_status :exist, destination.expand_path, :blue
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def symlink(source, destination, config = {})
|
||||||
|
destination = Pathname.new(destination)
|
||||||
|
|
||||||
|
if destination.symlink?
|
||||||
|
if destination.readlink == source
|
||||||
|
say_status :identical, destination.expand_path, :blue
|
||||||
|
else
|
||||||
|
say_status :conflict, "#{destination} exists and points to #{destination.readlink}", :red
|
||||||
|
|
||||||
|
if shell.file_collision(destination) { source }
|
||||||
|
system "ln -sf #{source} #{destination}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
say_status :symlink, "#{source.expand_path} to #{destination.expand_path}", :green
|
||||||
|
system "ln -s #{source} #{destination}"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def user_dir
|
def user_dir
|
||||||
|
|||||||
Reference in New Issue
Block a user