Class: Rip::Package

Inherits:
Object
  • Object
show all
Includes:
Memoize, PackageAPI
Defined in:
lib/rip/package.rb

Constant Summary collapse

@@patterns =
{}
@@blocks =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PackageAPI

#cached?, #exists?, #fetch!, #meta_package?, #name, #unpack!, #version

Methods included from Memoize

included, #memoize, #method_added

Constructor Details

#initialize(source, version = nil, files = nil) ⇒ Package

Returns a new instance of Package.



14
15
16
17
18
# File 'lib/rip/package.rb', line 14

def initialize(source, version = nil, files = nil)
  @source = source.strip.chomp
  @version = version
  @files = files
end

Instance Attribute Details

#filesObject



93
94
95
# File 'lib/rip/package.rb', line 93

def files
  @files ||= files!
end

#sourceObject (readonly)

Returns the value of attribute source.



13
14
15
# File 'lib/rip/package.rb', line 13

def source
  @source
end

Class Method Details

.for(source, *args) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rip/package.rb', line 31

def self.for(source, *args)
  source = source.strip.chomp

  handler = @@patterns.detect do |pattern, klass|
    case pattern
    when String
      if pattern[0,1] == '.'
        pattern = Regexp.escape(pattern)
        source.match Regexp.new("#{pattern}$")
      else
        source.include? pattern
      end
    else
      source.match(pattern)
    end
  end

  return handler[1].new(source, *args) if handler

  handler = @@blocks.detect do |klass, block|
    block.call(source)
  end

  return handler[0].new(source, *args) if handler
end

.handles(*patterns, &block) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/rip/package.rb', line 23

def self.handles(*patterns, &block)
  patterns.each do |pattern|
    @@patterns[pattern] = self
  end

  @@blocks[self] = block if block
end

Instance Method Details

#cache_nameObject



62
63
64
# File 'lib/rip/package.rb', line 62

def cache_name
  name + '-' + Digest::MD5.hexdigest(@source)
end

#cache_pathObject



67
68
69
# File 'lib/rip/package.rb', line 67

def cache_path
  File.join(packages_path, cache_name)
end

#dependenciesObject



107
108
109
# File 'lib/rip/package.rb', line 107

def dependencies
  @dependencies ||= dependencies!
end

#dependencies!Object



111
112
113
114
115
116
117
118
119
120
# File 'lib/rip/package.rb', line 111

def dependencies!
  if File.exists? deps = File.join(cache_path, 'deps.rip')
    File.readlines(deps).map do |line|
      source, version, *extra = line.split(' ')
      Package.for(source, version)
    end
  else
    []
  end
end

#fetchObject



81
82
83
84
85
# File 'lib/rip/package.rb', line 81

def fetch
  return if @fetched
  fetch!
  @fetched = true
end

#files!Object



97
98
99
100
101
102
103
104
# File 'lib/rip/package.rb', line 97

def files!
  fetch
  unpack

  Dir.chdir cache_path do
    Dir['lib/**/*'] + Dir['bin/**/*']
  end
end

#installed?Boolean

Returns:

  • (Boolean)


76
77
78
79
# File 'lib/rip/package.rb', line 76

def installed?
  graph = PackageManager.new
  graph.installed?(name) && graph.package_version(name) == version
end

#packages_pathObject



72
73
74
# File 'lib/rip/package.rb', line 72

def packages_path
  File.join(Rip.dir, 'rip-packages')
end

#run_hook(hook, *args, &block) ⇒ Object



122
123
124
# File 'lib/rip/package.rb', line 122

def run_hook(hook, *args, &block)
  send(hook, *args, &block) if respond_to? hook
end

#to_sObject



57
58
59
# File 'lib/rip/package.rb', line 57

def to_s
  version ? "#{name} (#{version})" : name.to_s
end

#uiObject



126
127
128
# File 'lib/rip/package.rb', line 126

def ui
  Rip.ui
end

#unpackObject



87
88
89
90
91
# File 'lib/rip/package.rb', line 87

def unpack
  return if @unpacked
  unpack!
  @unpacked = true
end