From 265131cfa9675c2c1844a70b849649dabb3ebc0d Mon Sep 17 00:00:00 2001 From: Joshua Nichols Date: Wed, 10 Mar 2010 09:07:10 -0500 Subject: [PATCH] Refactoring how stuff gets cloned. --- lib/homesick.rb | 46 +++++++++++++++++++++------------- spec/homesick/homesick_spec.rb | 19 +++++++------- 2 files changed, 37 insertions(+), 28 deletions(-) diff --git a/lib/homesick.rb b/lib/homesick.rb index 57225a7..9bbeca4 100644 --- a/lib/homesick.rb +++ b/lib/homesick.rb @@ -3,19 +3,18 @@ require 'thor' class Homesick < Thor include Thor::Actions + GIT_URI_PATTERN = /^git:\/\// + GITHUB_NAME_REPO_PATTERN = /^[A-Za-z_-]+\/[A-Za-z_-]+$/ + desc "clone URI", "Clone home's +uri+ for use with homesick" def clone(uri) - empty_directory homes_dir - inside homes_dir do - if uri =~ /^git:\/\// - unless File.directory? "#{homes_dir}/#{uri[/[A-Za-z_-]+\/[A-Za-z_-]+$/]}" - run "git clone #{uri}" - end - elsif uri =~ /^[A-Za-z_-]+\/[A-Za-z_-]+$/ + empty_directory repos_dir + inside repos_dir do + if uri =~ GIT_URI_PATTERN + git_clone uri + elsif uri =~ GITHUB_NAME_REPO_PATTERN match = uri.match(/([A-Za-z_-]+)\/([A-Za-z_-]+)/) - unless File.directory? "#{homes_dir}/#{match[1]}_#{match[2]}" - run "git clone git://github.com/#{match[0]} #{match[1]}_#{match[2]}" - end + git_clone "git://github.com/#{match[0]}.git", "#{match[1]}_#{match[2]}" end end end @@ -37,30 +36,41 @@ class Homesick < Thor desc "list", "List installed widgets" def list - inside homes_dir do + inside repos_dir do Pathname.glob('*') do |home| puts home end end end - protected - # class method, so it's convenient to stub out during tests def self.user_dir @user_dir ||= Pathname.new('~').expand_path end + def self.repos_dir + @repos_dir ||= Pathname.new('~/.homesick/repos').expand_path + end + + no_tasks do + def repos_dir + self.class.repos_dir + end + end + + protected + + # TODO move this to be more like thor's template, empty_directory, etc + def git_clone(repo, destination = nil) + run "git clone #{repo} #{destination}" + end + def user_dir self.class.user_dir end - def homes_dir - @homes_dir ||= Pathname.new('~/.homesick/repos').expand_path - end - def home_dir(name) - homes_dir.join(name, 'home') + repos_dir.join(name, 'home') end end diff --git a/spec/homesick/homesick_spec.rb b/spec/homesick/homesick_spec.rb index af4974f..e015724 100644 --- a/spec/homesick/homesick_spec.rb +++ b/spec/homesick/homesick_spec.rb @@ -1,20 +1,19 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe "Homesick" do - it "should clone any git repo" do - homesick = Homesick.new - repos_dir = Pathname.new('~/.homesick/repos').expand_path - homesick.clone "git://github.com/technicalpickles/pickled-vim.git" + before do + @homesick = Homesick.new + end - File.directory?("#{repos_dir}/pickled-vim").should == true + it "should clone any git repo" do + @homesick.should_receive(:git_clone).with('git://github.com/technicalpickles/pickled-vim.git') + + @homesick.clone "git://github.com/technicalpickles/pickled-vim.git" end it "should clone a github repo" do - homesick = Homesick.new - repos_dir = Pathname.new('~/.homesick/repos').expand_path + @homesick.should_receive(:git_clone).with('git://github.com/wfarr/dotfiles.git', 'wfarr_dotfiles') - homesick.clone "wfarr/dotfiles" - - File.directory?("#{repos_dir}/wfarr_dotfiles").should == true + @homesick.clone "wfarr/dotfiles" end end