From bf66d91b1faec3ca6de9a6e01638d72b122c7e06 Mon Sep 17 00:00:00 2001 From: Joshua Nichols Date: Thu, 18 Mar 2010 23:28:25 -0400 Subject: [PATCH] Shuffled some stuff out of main homesick.rb --- lib/homesick.rb | 57 +++-------------------------------------- lib/homesick/actions.rb | 40 +++++++++++++++++++++++++++++ lib/homesick/shell.rb | 17 ++++++++++++ 3 files changed, 61 insertions(+), 53 deletions(-) create mode 100644 lib/homesick/actions.rb create mode 100644 lib/homesick/shell.rb diff --git a/lib/homesick.rb b/lib/homesick.rb index 43b1fea..d3cac95 100644 --- a/lib/homesick.rb +++ b/lib/homesick.rb @@ -1,23 +1,11 @@ require 'thor' class Homesick < Thor + autoload :Shell, 'homesick/shell' + autoload :Actions, 'homesick/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 "- #{destination.readlink}", :red, true - say "+ #{content.expand_path}", :green, true - else - super - end - end - - end + include Homesick::Actions def initialize(args=[], options={}, config={}) super @@ -91,43 +79,6 @@ class Homesick < Thor protected - # TODO move this to be more like thor's template, empty_directory, etc - def git_clone(repo, config = {}) - 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 - def user_dir self.class.user_dir end diff --git a/lib/homesick/actions.rb b/lib/homesick/actions.rb new file mode 100644 index 0000000..7931b53 --- /dev/null +++ b/lib/homesick/actions.rb @@ -0,0 +1,40 @@ +class Homesick + module Actions + # TODO move this to be more like thor's template, empty_directory, etc + def git_clone(repo, config = {}) + 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 +end diff --git a/lib/homesick/shell.rb b/lib/homesick/shell.rb new file mode 100644 index 0000000..880801c --- /dev/null +++ b/lib/homesick/shell.rb @@ -0,0 +1,17 @@ +class Homesick + # Hack in support for diffing symlinks + class Shell < Thor::Shell::Color + + def show_diff(destination, content) + destination = Pathname.new(destination) + + if destination.symlink? + say "- #{destination.readlink}", :red, true + say "+ #{content.expand_path}", :green, true + else + super + end + end + + end +end