Refactoring how stuff gets cloned.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user