Class: Vendorer

Inherits:
Object
  • Object
show all
Defined in:
lib/vendorer.rb,
lib/vendorer/version.rb

Constant Summary collapse

VERSION =
'0.2.0'

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Vendorer

Returns a new instance of Vendorer.



6
7
8
9
# File 'lib/vendorer.rb', line 6

def initialize(options={})
  @options = options
  @sub_path = []
end

Instance Method Details

#file(path, url = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/vendorer.rb', line 15

def file(path, url=nil)
  target_path = complete_path(path)
  update_or_not target_path do
    run "mkdir", "-p", File.dirname(target_path)
    if @copy_from_url
      copy_from_path(target_path, url || path)
    else
      run "curl", url, "--fail", "-L", "--compressed", "-o", target_path
      raise "Downloaded empty file" unless File.exist?(target_path)
    end
    yield target_path if block_given?
  end
end

#folder(path, url = nil, options = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/vendorer.rb', line 29

def folder(path, url=nil, options={})
  if @copy_from_path or url
    target_path = complete_path(path)
    update_or_not target_path do
      run "rm", "-rf", target_path
      run "mkdir", "-p", File.dirname(target_path)
      if @copy_from_path
        copy_from_path(target_path, url || path)
      else
        download_repository(url, target_path, options)
      end
      yield target_path if block_given?
    end
  else
    @sub_path << path
    yield
    @sub_path.pop
  end
end

#from(url, options = {}) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/vendorer.rb', line 67

def from(url, options={})
  Dir.mktmpdir do |tmpdir|
    download_repository url, tmpdir, options
    @copy_from_url, @copy_from_path = url, tmpdir
    yield(@copy_from_path)
    @copy_from_url = @copy_from_path = nil
  end
end

#initObject

Creates Vendorfile with examples



56
57
58
59
60
61
62
63
64
65
# File 'lib/vendorer.rb', line 56

def init
  separator = "<!-- extracted by vendorer init -->"
  readme = File.read(File.expand_path('../../Readme.md', __FILE__))
  examples = readme.split(separator)[1]
  examples.gsub!(/```.*/,'') # remove ``` from readme
  examples = examples.split("\n").map do |l|
    (l.start_with? '#' or l.empty?) ? l : "# #{l}"
  end.join("\n")
  File.open('Vendorfile', 'w') { |f| f.write(examples.strip) }
end

#parse(content) ⇒ Object



11
12
13
# File 'lib/vendorer.rb', line 11

def parse(content)
  eval(content, nil, 'Vendorfile', 1)
end

#rewrite(path) ⇒ Object



49
50
51
52
53
# File 'lib/vendorer.rb', line 49

def rewrite(path)
  content = File.read(path)
  result = yield content
  File.open(path,'w'){|f| f.write(result) }
end