Skip to content

Commit

Permalink
Merge branch '55minutes-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed Apr 6, 2012
2 parents cc06bdf + 39d03e9 commit a698ad1
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 9 deletions.
46 changes: 37 additions & 9 deletions lib/vendorer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,31 @@ def parse(content)
eval(content, nil, 'Vendorfile', 1)
end

def file(path, url)
def file(path, url=nil)
path = complete_path(path)
update_or_not path do
run "mkdir -p #{File.dirname(path)}"
run "curl '#{url}' -L -o #{path}"
raise "Downloaded empty file" unless File.exist?(path)
if @from_git
copy_from_git(path, url)
else
run "curl '#{url}' -L -o #{path}"
raise "Downloaded empty file" unless File.exist?(path)
end
yield path if block_given?
end
end

def folder(path, url=nil, options={})
if url
if @from_git || url
path = complete_path(path)
update_or_not path do
run "rm -rf #{path}"
run "mkdir -p #{File.dirname(path)}"
run "git clone '#{url}' #{path}"
if commit = (options[:ref] || options[:tag] || options[:branch])
run "cd #{path} && git checkout '#{commit}'"
if @from_git
copy_from_git(path, url)
else
git_clone(path, url, options)
end
run("cd #{path} && git submodule update --init --recursive")
run "rm -rf #{path}/.git"
yield path if block_given?
end
else
Expand Down Expand Up @@ -59,6 +62,15 @@ def init
File.open('Vendorfile', 'w') { |f| f.write(examples.strip) }
end

def from(url, options={})
Dir.mktmpdir do |tmpdir|
git_clone tmpdir, url, options
@from_git, @from_path = url, tmpdir
yield
@from_git = @from_path = nil
end
end

private

def update_or_not(path)
Expand All @@ -84,4 +96,20 @@ def run(cmd)
def complete_path(path)
File.join(@sub_path + [path])
end

def git_clone(path, url, options)
run "git clone '#{url}' #{path}"
if commit = (options[:ref] || options[:tag] || options[:branch])
run "cd #{path} && git checkout '#{commit}'"
end
run("cd #{path} && git submodule update --init --recursive")
run "rm -rf #{path}/.git"
end

def copy_from_git(dest_path, src_path)
src_path = dest_path if src_path.nil?
copy_from = File.join(@from_path, src_path)
raise "'#{src_path}' not found in #{@from_git}" unless File.exist?(copy_from)
run "cp -Rp #{copy_from} #{dest_path}"
end
end
36 changes: 36 additions & 0 deletions spec/vendorer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -441,4 +441,40 @@ def v.puts(x);end # silence
end
end
end

describe "#from" do
def valid_vendorfile
write "Vendorfile", "
from '../../.git', :tag => 'b1e6460' do
file 'Readme.md'
file 'Rakefile.renamed', 'Rakefile'
folder 'spec'
folder 'spec-renamed', 'spec'
end
"
end

def bogus_vendorfile
write "Vendorfile", "
from '../../.git', :tag => 'b1e6460' do
file 'bogus'
end
"
end

it "copies appropriate files and folders" do
valid_vendorfile
vendorer
ls(".").sort.should == ['Rakefile.renamed', 'Readme.md', 'Vendorfile', 'spec', 'spec-renamed']
%w(spec spec-renamed).each do |spec|
ls(spec).should == ['spec_helper.rb', 'vendorer_spec.rb']
end
end

it "gives 'not found' error for non-existent file" do
bogus_vendorfile
output = vendorer '', :raise => true
output.should include("'bogus' not found in ../../.git")
end
end
end

0 comments on commit a698ad1

Please sign in to comment.