Class: Gjp::PomGetter

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/gjp/pom_getter.rb

Overview

attempts to get java projects’ pom file

Instance Method Summary collapse

Methods included from Logging

#log

Instance Method Details

#cleanup_name(path) ⇒ Object

get a heuristic name from a path



108
109
110
# File 'lib/gjp/pom_getter.rb', line 108

def cleanup_name(path)
  Pathname.new(path).basename.to_s.sub(/.jar$/, "")
end

#get_pom(filename) ⇒ Object

saves a jar poms in <jar_filename>.pom returns filename and status if found, else nil



18
19
20
21
22
23
24
25
# File 'lib/gjp/pom_getter.rb', line 18

def get_pom(filename)
  content, status = (get_pom_from_jar(filename) || get_pom_from_sha1(filename) || get_pom_from_heuristic(filename))
  if content
    pom_filename = filename.sub(/(\.jar)?$/, ".pom")
    File.open(pom_filename, "w") { |io| io.write(content) }
    [pom_filename, status]
  end
end

#get_pom_from_heuristic(filename) ⇒ Object

returns a pom from search.maven.org with a heuristic name search



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
102
103
104
105
# File 'lib/gjp/pom_getter.rb', line 69

def get_pom_from_heuristic(filename)
  begin
    log.debug("Attempting heuristic POM search for #{filename}")
    site = MavenWebsite.new
    filename = cleanup_name(filename)
    version_matcher = VersionMatcher.new
    my_artifact_id, my_version = version_matcher.split_version(filename)
    log.debug("Guessed artifact id: #{my_artifact_id}, version: #{my_version}")

    result = site.search_by_name(my_artifact_id).first
    log.debug("Artifact id search result: #{result}")
    unless result.nil?
      group_id, artifact_id, version = site.get_maven_id_from result
      results = site.search_by_group_id_and_artifact_id(group_id, artifact_id)
      log.debug("All versions: #{results}")
      their_versions = results.map { |doc| doc["v"] }
      best_matched_version = (
        if !my_version.nil?
          version_matcher.best_match(my_version, their_versions)
        else
          their_versions.max
        end
      )
      best_matched_result = (results.select { |result| result["v"] == best_matched_version }).first

      group_id, artifact_id, version = site.get_maven_id_from(best_matched_result)
      log.warn("pom.xml for #{filename} found on search.maven.org with heuristic search\
        (#{group_id}:#{artifact_id}:#{version})"
      )

      return site.download_pom(group_id, artifact_id, version), :found_via_heuristic
    end
  rescue RestClient::ResourceNotFound
    log.warn("Got a 404 error while looking for #{filename} heuristically in search.maven.org")
  end
  nil
end

#get_pom_from_jar(file) ⇒ Object

returns a pom embedded in a jar file



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/gjp/pom_getter.rb', line 28

def get_pom_from_jar(file)
  log.debug("Attempting unpack of #{file} to find a POM")
  begin
    Zip::File.foreach(file) do |entry|
      if entry.name =~ /\/pom.xml$/
        log.info("pom.xml found in #{file}##{entry.name}")
        return entry.get_input_stream.read, :found_in_jar
      end
    end
  rescue Zip::Error
    log.warn("#{file} does not seem to be a valid jar archive, skipping")
  rescue TypeError
    log.warn("#{file} seems to be a valid jar archive but is corrupt, skipping")
  end
  nil
end

#get_pom_from_sha1(file) ⇒ Object

returns a pom from search.maven.org with a jar sha1 search



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/gjp/pom_getter.rb', line 46

def get_pom_from_sha1(file)
  log.debug("Attempting SHA1 POM lookup for #{file}")
  begin
    if File.file?(file)
      site = MavenWebsite.new
      sha1 = Digest::SHA1.hexdigest File.read(file)
      results = site.search_by_sha1(sha1).select { |result| result["ec"].include?(".pom") }
      result = results.first
      unless result.nil?
        log.info("pom.xml for #{file} found on search.maven.org for sha1 #{sha1}\
          (#{result["g"]}:#{result["a"]}:#{result["v"]})"
        )
        group_id, artifact_id, version = site.get_maven_id_from result
        return site.download_pom(group_id, artifact_id, version), :found_via_sha1
      end
    end
  rescue RestClient::ResourceNotFound
    log.warn("Got a 404 error while looking for #{file}'s SHA1 in search.maven.org")
  end
  nil
end