Class: MavenCentralRepo

Inherits:
Object
  • Object
show all
Defined in:
lib/mvn-get/mvn-get.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ MavenCentralRepo

Returns a new instance of MavenCentralRepo.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/mvn-get/mvn-get.rb', line 78

def initialize(id)

  @id = id
  d = MavenCentral.id2dict(@id)
  raise "not a correct id. please use 'groupId:artifactId' or 'groupId:artifactId:version'" if d.nil?

  # use lastest version by default
  if d["version"] == nil || d["version"].strip == ""
    d["version"] = MavenCentral.fetch_lastest_version(d["groupId"], d["artifactId"])
    @id = MavenCentral.dict2id(d)
  end

  @groupId, @artifactId, @version = d["groupId"], d["artifactId"], d["version"]

  pom_url = MavenCentral.extract_url_from_dict(d)
  body = MavenCentral.request(pom_url)
  @pom = Hash.from_xml( body )
end

Instance Attribute Details

#artifactIdObject (readonly)

Returns the value of attribute artifactId.



72
73
74
# File 'lib/mvn-get/mvn-get.rb', line 72

def artifactId
  @artifactId
end

#groupIdObject (readonly)

Returns the value of attribute groupId.



72
73
74
# File 'lib/mvn-get/mvn-get.rb', line 72

def groupId
  @groupId
end

#idObject (readonly)

Returns the value of attribute id.



72
73
74
# File 'lib/mvn-get/mvn-get.rb', line 72

def id
  @id
end

#pomObject (readonly)

Returns the value of attribute pom.



72
73
74
# File 'lib/mvn-get/mvn-get.rb', line 72

def pom
  @pom
end

#versionObject (readonly)

Returns the value of attribute version.



72
73
74
# File 'lib/mvn-get/mvn-get.rb', line 72

def version
  @version
end

Instance Method Details

#<=>(other) ⇒ Object



156
157
158
# File 'lib/mvn-get/mvn-get.rb', line 156

def <=>(other)
  id <=> other.id
end

#==(other) ⇒ Object



152
153
154
# File 'lib/mvn-get/mvn-get.rb', line 152

def ==(other)
  other.class == MavenCentralRepo and id == other.id
end

#dependencies(ignore_test_lib = true) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/mvn-get/mvn-get.rb', line 97

def dependencies(ignore_test_lib = true)
  return [] if @pom["project"]["dependencies"].nil? or
    (@pom["project"]["dependencies"].class == String and @pom["project"]["dependencies"].strip == "")
  [@pom["project"]["dependencies"]["dependency"]].flatten.map{|x| 
    next if ignore_test_lib and x["scope"] and x["scope"].strip == "test"
    if x["version"] =~ /\s*[$]{\s*project[.]version\s*}\s*/
      x["version"] = @version
    end
    "#{x["groupId"]}:#{x["artifactId"]}:#{x["version"]}" 
  }.compact.sort!
end

#download(path = ".") ⇒ Object



160
161
162
163
164
165
# File 'lib/mvn-get/mvn-get.rb', line 160

def download(path = ".")
  url = jar_url
  open(File.join(path, url.split("/").last), "w") { |f|
    f.write MavenCentral.request(url)
  }
end

#inspectObject



148
149
150
# File 'lib/mvn-get/mvn-get.rb', line 148

def inspect
  @id
end

#jar_urlObject



109
110
111
# File 'lib/mvn-get/mvn-get.rb', line 109

def jar_url
  MavenCentral.extract_url_from_id(@id, "jar")
end

#recursive_dependencies(ignore_test_lib = true, only_select_lastest_version = true) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/mvn-get/mvn-get.rb', line 113

def recursive_dependencies(ignore_test_lib = true, only_select_lastest_version = true)
  return [] if dependencies(ignore_test_lib) == []
  return @_recursive_dependencies[ignore_test_lib][only_select_lastest_version] if @_recursive_dependencies and @_recursive_dependencies[ignore_test_lib] and @_recursive_dependencies[ignore_test_lib][only_select_lastest_version]

  
  res = []
  deps = dependencies(ignore_test_lib)
  while not deps.empty?
    d = deps.shift
    if not res.map{|x| x.id }.include?(d)
      r = MavenCentralRepo.new(d)
      if not res.include?(r)
        if only_select_lastest_version == false or
          (only_select_lastest_version == true and 
            ( not res.any?{|x| r.groupId == x.groupId and r.artifactId == x.artifactId } or
             res.any?{|x| r.groupId == x.groupId and r.artifactId == x.artifactId and r.version < x.version }) # lexicographic order
          )
            deps += r.dependencies
            res << r
        end
      end
    end
  end

  if only_select_lastest_version
    res = res.group_by{|x| "#{x.groupId}:#{x.artifactId}" }.map{|k, v| v.reduce{|s, x| if s.version <=> x.version then x else s end } }.flatten
  end

  res.sort!
  @_recursive_dependencies ||= {}
  @_recursive_dependencies[ignore_test_lib] ||= {}
  @_recursive_dependencies[ignore_test_lib][only_select_lastest_version] = res
  return res
end