Class: Vendor::VendorFile::Library::Remote

Inherits:
Base
  • Object
show all
Defined in:
lib/vendor/vendor_file/library/remote.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#name, #parent, #require, #targets

Instance Method Summary collapse

Methods inherited from Base

#<=>, #build_settings, #dependencies, #files, #frameworks, #initialize, #per_file_flag, #resources

Constructor Details

This class inherits a constructor from Vendor::VendorFile::Library::Base

Instance Attribute Details

#equalityObject

Returns the value of attribute equality.



9
10
11
# File 'lib/vendor/vendor_file/library/remote.rb', line 9

def equality
  @equality
end

#sourcesObject

Returns the value of attribute sources.



10
11
12
# File 'lib/vendor/vendor_file/library/remote.rb', line 10

def sources
  @sources
end

Instance Method Details

#==(other) ⇒ Object



118
119
120
# File 'lib/vendor/vendor_file/library/remote.rb', line 118

def ==(other)
  other.name == @name && other.version == @version && other.equality == @equality
end

#cache_pathObject



114
115
116
# File 'lib/vendor/vendor_file/library/remote.rb', line 114

def cache_path
  File.join(Vendor.library_path, "remote", name, matched_version.to_s)
end

#descriptionObject



126
127
128
# File 'lib/vendor/vendor_file/library/remote.rb', line 126

def description
  [ @name, @equality, @version ].compact.join(" ")
end

#display_nameObject



122
123
124
# File 'lib/vendor/vendor_file/library/remote.rb', line 122

def display_name
  [ name, matched_version ].compact.join(" ")
end

#downloadObject



103
104
105
106
107
108
109
110
111
112
# File 'lib/vendor/vendor_file/library/remote.rb', line 103

def download
  # If we haven't already downloaded the vendor
  unless File.exist?(cache_path)
    # Download it
    file = Vendor::API.download(name, matched_version)

    # Unzip the download
    unzip_file file.path, cache_path
  end
end

#matched_versionObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/vendor/vendor_file/library/remote.rb', line 33

def matched_version
  # If we just have a version, and no equality matcher
  if version && !equality
    return version
  end

  # If we don't have a version, get the latest version
  # from the remote sources
  unless version
    return meta['release']
  end

  # Try and find a version that matches the lib
  other_versions = meta['versions'].map { |v| v[0] }
  found = version_matches_any?(other_versions)

  # Did we actually find something?
  unless found
    Vendor.ui.error "Could not find a valid version '#{equality} #{version}' in '#{other_versions}'"
    exit 1
  else
    found
  end
end

#versionObject



12
13
14
# File 'lib/vendor/vendor_file/library/remote.rb', line 12

def version
  @version
end

#version=(value) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/vendor/vendor_file/library/remote.rb', line 16

def version=(value)
  # Matches (some equality matcher, followed by some spaces, then a version)
  if value
    if value.match(/^(\<\=|\>\=|\~\>|\>|\<)?\s*([a-zA-Z0-9\.]+)$/)
      @equality = $1
      @version = $2
    else
      Vendor.ui.error "Invalid version format '#{value}' for '#{name}'"
      exit 1
    end
  else
    @equality = nil
    @version = nil
  end
  value
end

#version_matches_any?(other_versions) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/vendor/vendor_file/library/remote.rb', line 58

def version_matches_any?(other_versions)
  # If we have an equality matcher, we need sort through
  # the versions, and try and find the best match
  wants = Vendor::Version.new(version)

  # Sort them from the latest versions first
  versions = other_versions.map{|v| Vendor::Version.create(v) }.sort.reverse

  # We don't want to include pre-releases if the wants version
  # isn't a pre-release itself. If we're after "2.5.alpha", then
  # we should be able to include that, however if we're asking for
  # "2.5", then pre-releases shouldn't be included.
  unless wants.prerelease?
    versions = versions.reject { |v| v.prerelease? }
  end

  # If this a spermy search, we have a slightly different search mechanism. We use the
  # version segemets to determine if the version is valid. For example, lets say we're
  # testing to see if "0.1" is a spermy match to "0.1.5.2", we start by getting the
  # two segments for each:
  #
  # [ 0, 1 ]
  # [ 0, 1, 5, 2 ]
  #
  # We chop the second set of segments to be the same lenth as the first one:
  #
  # [ 0, 1, 5, 2 ].slice(0, 2) #=. [ 0, 1 ]
  #
  # No we just test to see if they are the same! I think this works...
  if equality == "~>"
    segments = wants.segments
    versions.find do |has|
      segments == has.segments.slice(0, segments.length)
    end
  elsif equality
    versions.find do |has|
      wants.send(:"#{equality}", has)
    end
  else
    versions.find do |has|
      wants == has
    end
  end
end