Class: Automatiek::Gem

Inherits:
Object
  • Object
show all
Defined in:
lib/automatiek/gem.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gem_name, &block) ⇒ Gem

Returns a new instance of Gem.


5
6
7
8
9
# File 'lib/automatiek/gem.rb', line 5

def initialize(gem_name, &block)
  @gem_name = gem_name
  @dependencies = []
  block.call(self) if block
end

Instance Attribute Details

#gem_nameObject

Returns the value of attribute gem_name


45
46
47
# File 'lib/automatiek/gem.rb', line 45

def gem_name
  @gem_name
end

#namespaceObject

Returns the value of attribute namespace


46
47
48
# File 'lib/automatiek/gem.rb', line 46

def namespace
  @namespace
end

#prefixObject

Returns the value of attribute prefix


47
48
49
# File 'lib/automatiek/gem.rb', line 47

def prefix
  @prefix
end

#require_entrypointObject


56
57
58
# File 'lib/automatiek/gem.rb', line 56

def require_entrypoint
  @require_entrypoint ||= gem_name.tr("-", "/")
end

#vendor_libObject

Returns the value of attribute vendor_lib


48
49
50
# File 'lib/automatiek/gem.rb', line 48

def vendor_lib
  @vendor_lib
end

#versionObject

Returns the value of attribute version


49
50
51
# File 'lib/automatiek/gem.rb', line 49

def version
  @version
end

Instance Method Details

#cleanObject


70
71
72
73
# File 'lib/automatiek/gem.rb', line 70

def clean
  files = Dir.glob("#{vendor_lib}/*", File::FNM_DOTMATCH).reject {|f| %(. .. lib).include? f.split("/").last }
  FileUtils.rm_r files
end

#dependency(name, &block) ⇒ Object


40
41
42
43
# File 'lib/automatiek/gem.rb', line 40

def dependency(name, &block)
  dep = self.class.new(name, &block)
  @dependencies << dep
end

#download=(opts = {}, &block) ⇒ Object


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/automatiek/gem.rb', line 24

def download=(opts = {}, &block)
  if block
    @download = block
  elsif github = opts.delete(:github)
    @download = lambda do |version|
      Dir.chdir File.dirname(vendor_lib) do
        `curl -L #{github}/archive/#{version}.tar.gz | tar -xz`
        unless $?.success?
          raise "Downloading & untarring #{gem_name} (#{version}) failed"
        end
        FileUtils.mv "#{github.split("/").last}-#{version.sub(/^v/, "")}", gem_name
      end
    end
  end
end

#namespace_files(folder) ⇒ Object


62
63
64
65
66
67
68
# File 'lib/automatiek/gem.rb', line 62

def namespace_files(folder)
  files = Dir.glob("#{folder}/**/*.rb")
  require_target = vendor_lib.sub(%r{^(.+?/)?lib/}, "") << "/lib"
  process(files, namespace, "#{prefix}::#{namespace}")
  process(files, /require (["'])#{Regexp.escape require_entrypoint}/, "require \\1#{require_target}/#{require_entrypoint}")
  process(files, /(autoload\s+[:\w]+,\s+["'])(#{Regexp.escape require_entrypoint}[\w\/]+["'])/, "\\1#{require_target}/\\2")
end

#update(version) ⇒ Object


51
52
53
54
# File 'lib/automatiek/gem.rb', line 51

def update(version)
  FileUtils.rm_rf vendor_lib
  @download.call(version)
end

#vendor!(version = nil) ⇒ Object


11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/automatiek/gem.rb', line 11

def vendor!(version = nil)
  update(version || self.version)

  @dependencies.each do |dependency|
    dependency.vendor!
    dependency.namespace_files(vendor_lib)
  end

  namespace_files(vendor_lib)

  clean
end