Refactoring how stuff gets cloned.

This commit is contained in:
Joshua Nichols
2010-03-10 09:07:10 -05:00
parent 3fb5f5b0d9
commit 265131cfa9
2 changed files with 37 additions and 28 deletions

View File

@@ -3,19 +3,18 @@ require 'thor'
class Homesick < Thor class Homesick < Thor
include Thor::Actions 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" desc "clone URI", "Clone home's +uri+ for use with homesick"
def clone(uri) def clone(uri)
empty_directory homes_dir empty_directory repos_dir
inside homes_dir do inside repos_dir do
if uri =~ /^git:\/\// if uri =~ GIT_URI_PATTERN
unless File.directory? "#{homes_dir}/#{uri[/[A-Za-z_-]+\/[A-Za-z_-]+$/]}" git_clone uri
run "git clone #{uri}" elsif uri =~ GITHUB_NAME_REPO_PATTERN
end
elsif uri =~ /^[A-Za-z_-]+\/[A-Za-z_-]+$/
match = uri.match(/([A-Za-z_-]+)\/([A-Za-z_-]+)/) match = uri.match(/([A-Za-z_-]+)\/([A-Za-z_-]+)/)
unless File.directory? "#{homes_dir}/#{match[1]}_#{match[2]}" git_clone "git://github.com/#{match[0]}.git", "#{match[1]}_#{match[2]}"
run "git clone git://github.com/#{match[0]} #{match[1]}_#{match[2]}"
end
end end
end end
end end
@@ -37,30 +36,41 @@ class Homesick < Thor
desc "list", "List installed widgets" desc "list", "List installed widgets"
def list def list
inside homes_dir do inside repos_dir do
Pathname.glob('*') do |home| Pathname.glob('*') do |home|
puts home puts home
end end
end end
end end
protected
# class method, so it's convenient to stub out during tests # class method, so it's convenient to stub out during tests
def self.user_dir def self.user_dir
@user_dir ||= Pathname.new('~').expand_path @user_dir ||= Pathname.new('~').expand_path
end 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 def user_dir
self.class.user_dir self.class.user_dir
end end
def homes_dir
@homes_dir ||= Pathname.new('~/.homesick/repos').expand_path
end
def home_dir(name) def home_dir(name)
homes_dir.join(name, 'home') repos_dir.join(name, 'home')
end end
end end

View File

@@ -1,20 +1,19 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe "Homesick" do describe "Homesick" do
it "should clone any git repo" do before do
homesick = Homesick.new @homesick = Homesick.new
repos_dir = Pathname.new('~/.homesick/repos').expand_path end
homesick.clone "git://github.com/technicalpickles/pickled-vim.git"
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 end
it "should clone a github repo" do it "should clone a github repo" do
homesick = Homesick.new @homesick.should_receive(:git_clone).with('git://github.com/wfarr/dotfiles.git', 'wfarr_dotfiles')
repos_dir = Pathname.new('~/.homesick/repos').expand_path
homesick.clone "wfarr/dotfiles" @homesick.clone "wfarr/dotfiles"
File.directory?("#{repos_dir}/wfarr_dotfiles").should == true
end end
end end