gjp 0.29.0 → 0.30.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -43,7 +43,7 @@ module Gjp
43
43
 
44
44
  # determines if a user without Internet access exists
45
45
  def set_up?
46
- user_exists? and firewall_rule_exists?
46
+ user_exists? && firewall_rule_exists?
47
47
  end
48
48
 
49
49
  # checks user existence
@@ -0,0 +1,47 @@
1
+ # encoding: UTF-8
2
+
3
+ module Gjp
4
+ # runs Maven with gjp-specific options
5
+ class MavenRunner < KitRunner
6
+ include Logger
7
+
8
+ # runs Maven in a subprocess
9
+ def mvn(options)
10
+ run_executable("#{get_maven_commandline(@project.full_path)} #{options.join(' ')}")
11
+ end
12
+
13
+ # runs Maven to attempt getting a source jar
14
+ # returns true if successful
15
+ def get_source_jar(group_id, artifact_id, version)
16
+ mvn(["dependency:get", "-Dartifact=#{group_id}:#{artifact_id}:#{version}:jar:sources", "-Dtransitive=false"])
17
+ end
18
+
19
+ # runs Maven to get the effective POM from an existing POM
20
+ # returns effective pom path or nil if not found
21
+ def get_effective_pom(pom_path)
22
+ effective_pom_path = "#{pom_path}.effective"
23
+ success = mvn(["help:effective-pom", "-f#{pom_path}", "-Doutput=#{File.split(effective_pom_path)[1]}"])
24
+ if success
25
+ effective_pom_path
26
+ else
27
+ nil
28
+ end
29
+ end
30
+
31
+ # returns a command line for running Maven from the specified
32
+ # prefix
33
+ def get_maven_commandline(prefix)
34
+ executable = find_executable("mvn")
35
+
36
+ if executable != nil
37
+ mvn_path = File.join(prefix, executable)
38
+ repo_path = File.join(prefix, "kit", "m2")
39
+ config_path = File.join(prefix, "kit", "m2", "settings.xml")
40
+
41
+ "#{mvn_path} -Dmaven.repo.local=#{repo_path} -s#{config_path}"
42
+ else
43
+ raise ExecutableNotFoundError.new("mvn")
44
+ end
45
+ end
46
+ end
47
+ end
data/lib/gjp/pom.rb CHANGED
@@ -10,14 +10,7 @@ module Gjp
10
10
  @doc = Nokogiri::XML(open(filename).read)
11
11
  @doc.remove_namespaces!
12
12
  end
13
-
14
- def connection_address
15
- connection_nodes = @doc.xpath("//scm/connection/text()")
16
- if connection_nodes.any?
17
- connection_nodes.first.to_s.sub(/^scm:/, "")
18
- end
19
- end
20
-
13
+
21
14
  def group_id
22
15
  @doc.xpath("project/groupId").text
23
16
  end
@@ -26,6 +19,10 @@ module Gjp
26
19
  @doc.xpath("project/artifactId").text
27
20
  end
28
21
 
22
+ def name
23
+ @doc.xpath("project/name").text
24
+ end
25
+
29
26
  def version
30
27
  @doc.xpath("project/version").text
31
28
  end
@@ -48,16 +45,12 @@ module Gjp
48
45
  end
49
46
  end
50
47
 
51
- def parent_group_id
52
- @doc.xpath("project/parent/groupId").text
53
- end
54
-
55
- def parent_artifact_id
56
- @doc.xpath("project/parent/artifactId").text
48
+ def scm_connection
49
+ @doc.xpath("project/scm/connection").text
57
50
  end
58
-
59
- def parent_version
60
- @doc.xpath("project/parent/version").text
51
+
52
+ def scm_url
53
+ @doc.xpath("project/scm/url").text
61
54
  end
62
55
  end
63
56
  end
@@ -26,6 +26,7 @@ module Gjp
26
26
 
27
27
  # returns a pom embedded in a jar file
28
28
  def get_pom_from_jar(file)
29
+ log.debug("Attempting unpack of #{file} to find a POM")
29
30
  begin
30
31
  Zip::ZipFile.foreach(file) do |entry|
31
32
  if entry.name =~ /\/pom.xml$/
@@ -43,6 +44,7 @@ module Gjp
43
44
 
44
45
  # returns a pom from search.maven.org with a jar sha1 search
45
46
  def get_pom_from_sha1(file)
47
+ log.debug("Attempting SHA1 POM lookup for #{file}")
46
48
  begin
47
49
  if File.file?(file)
48
50
  site = MavenWebsite.new
@@ -55,24 +57,28 @@ module Gjp
55
57
  return site.download_pom(group_id, artifact_id, version), :found_via_sha1
56
58
  end
57
59
  end
58
- return nil
59
60
  rescue RestClient::ResourceNotFound
60
- log.error("Got an error while looking for #{file}'s SHA1 in search.maven.org")
61
+ log.warn("Got a 404 error while looking for #{file}'s SHA1 in search.maven.org")
61
62
  end
63
+ nil
62
64
  end
63
65
 
64
66
  # returns a pom from search.maven.org with a heuristic name search
65
67
  def get_pom_from_heuristic(filename)
66
68
  begin
69
+ log.debug("Attempting heuristic POM search for #{filename}")
67
70
  site = MavenWebsite.new
68
71
  filename = cleanup_name(filename)
69
72
  version_matcher = VersionMatcher.new
70
73
  my_artifact_id, my_version = version_matcher.split_version(filename)
74
+ log.debug("Guessed artifact id: #{my_artifact_id}, version: #{my_version}")
71
75
 
72
76
  result = site.search_by_name(my_artifact_id).first
77
+ log.debug("Artifact id search result: #{result}")
73
78
  if result != nil
74
79
  group_id, artifact_id, version = site.get_maven_id_from result
75
80
  results = site.search_by_group_id_and_artifact_id(group_id, artifact_id)
81
+ log.debug("All versions: #{results}")
76
82
  their_versions = results.map {|doc| doc["v"]}
77
83
  best_matched_version = if my_version != nil then version_matcher.best_match(my_version, their_versions) else their_versions.max end
78
84
  best_matched_result = (results.select{|result| result["v"] == best_matched_version}).first
@@ -83,8 +89,9 @@ module Gjp
83
89
  return site.download_pom(group_id, artifact_id, version), :found_via_heuristic
84
90
  end
85
91
  rescue RestClient::ResourceNotFound
86
- log.error("Got an error while looking for #{filename}'s SHA1 in search.maven.org")
92
+ log.warn("Got a 404 error while looking for #{filename} heuristically in search.maven.org")
87
93
  end
94
+ nil
88
95
  end
89
96
 
90
97
  # get a heuristic name from a path
data/lib/gjp/project.rb CHANGED
@@ -26,7 +26,7 @@ module Gjp
26
26
  # finds the project directory up in the tree, like git does
27
27
  def self.find_project_dir(starting_dir)
28
28
  result = starting_dir
29
- while is_project(result) == false and result != "/"
29
+ while is_project(result) == false && result != "/"
30
30
  result = File.expand_path("..", result)
31
31
  end
32
32
 
@@ -37,8 +37,8 @@ module Gjp
37
37
 
38
38
  # returns true if the specified directory is a valid gjp project
39
39
  def self.is_project(dir)
40
- File.directory?(File.join(dir, "src")) and
41
- File.directory?(File.join(dir, "kit")) and
40
+ File.directory?(File.join(dir, "src")) &&
41
+ File.directory?(File.join(dir, "kit")) &&
42
42
  File.directory?(File.join(dir, ".git"))
43
43
  end
44
44
 
@@ -49,7 +49,7 @@ module Gjp
49
49
  dir_path = Pathname.new(File.expand_path(dir)).relative_path_from(Pathname.new(@full_path))
50
50
  components = dir_path.to_s.split(File::SEPARATOR)
51
51
 
52
- if components.count >= 2 and components.first == "src" and Dir.exist?(File.join(@full_path, components[0], components[1]))
52
+ if components.count >= 2 && components.first == "src" && Dir.exist?(File.join(@full_path, components[0], components[1]))
53
53
  components[1]
54
54
  else
55
55
  raise NoPackageDirectoryError
@@ -210,7 +210,7 @@ module Gjp
210
210
  from_directory do
211
211
  result = []
212
212
  Find.find("src") do |file|
213
- if file =~ /.jar$/ and not File.symlink?(file)
213
+ if file =~ /.jar$/ && !File.symlink?(file)
214
214
  new_location = File.join("kit", "jars", Pathname.new(file).split[1])
215
215
  FileUtils.mv(file, new_location)
216
216
 
@@ -7,7 +7,8 @@ module Gjp
7
7
 
8
8
  def initialize(project, history_path)
9
9
  @project = project
10
- @kit_runner = Gjp::KitRunner.new(project)
10
+ @ant_runner = Gjp::AntRunner.new(project)
11
+ @maven_runner = Gjp::MavenRunner.new(project)
11
12
  @history_path = history_path
12
13
  end
13
14
 
@@ -29,9 +30,9 @@ module Gjp
29
30
  ] +
30
31
  relevant_lines.map do |line|
31
32
  if line =~ /gjp +mvn/
32
- line.gsub(/gjp +mvn/, "#{@kit_runner.get_maven_commandline("$PROJECT_PREFIX")}")
33
+ line.gsub(/gjp +mvn/, "#{@maven_runner.get_maven_commandline("$PROJECT_PREFIX")}")
33
34
  elsif line =~ /gjp +ant/
34
- line.gsub(/gjp +ant/, "#{@kit_runner.get_ant_commandline("$PROJECT_PREFIX")}")
35
+ line.gsub(/gjp +ant/, "#{@ant_runner.get_ant_commandline("$PROJECT_PREFIX")}")
35
36
  else
36
37
  line
37
38
  end
@@ -7,107 +7,38 @@ module Gjp
7
7
  class SourceGetter
8
8
  include Logger
9
9
 
10
+ # attempts to download a project's sources
11
+ def get_maven_source_jar(project, pom_path)
12
+ maven_runner = Gjp::MavenRunner.new(project)
13
+ pom = Pom.new(pom_path)
14
+ maven_runner.get_source_jar(pom.group_id, pom.artifact_id, pom.version)
15
+ end
16
+
10
17
  # looks for jars in maven's local repo and downloads corresponding
11
18
  # source jars
12
19
  def get_maven_source_jars(project)
13
- kit_runner = Gjp::KitRunner.new(project)
20
+ maven_runner = Gjp::MavenRunner.new(project)
14
21
 
15
22
  project.from_directory do
16
23
  paths = Find.find(".").reject {|path| artifact_from_path(path) == nil}.sort
17
24
 
18
25
  succeded_paths = paths.select.with_index do |path, i|
19
- artifact = artifact_from_path(path)
20
- log.info("attempting source download for #{path} (#{artifact})")
21
- status = kit_runner.mvn(["dependency:get", "-Dartifact=#{artifact}", "-Dtransitive=false"])
22
- status.exitstatus == 0
26
+ group_id, artifact_id, version = artifact_from_path(path)
27
+ log.info("attempting source download for #{path} (#{group_id}:#{artifact_id}:#{version})")
28
+ maven_runner.get_source_jar(group_id, artifact_id, version)
23
29
  end
24
30
 
25
31
  [succeded_paths, (paths - succeded_paths)]
26
32
  end
27
33
  end
28
34
 
29
- # downloads a project's source into a specified directory
30
- def get_source(address, pomfile, directory)
31
- log.info("downloading: #{address} in #{directory}, pomfile: #{pomfile}")
32
-
33
- dummy, prefix, scm_address = address.split(/^([^:]+):(.*)$/)
34
- log.info("prefix: #{prefix}, scm_address: #{scm_address}")
35
-
36
- get_source_from_scm(prefix, scm_address, pomfile, directory)
37
- end
38
-
39
- # checks code out from an scm
40
- def get_source_from_scm(prefix, scm_address, pomfile, directory)
41
- pom = Pom.new(pomfile)
42
- dir = File.join(directory, "#{pom.group_id}:#{pom.artifact_id}:#{pom.version}")
43
- begin
44
- Dir::mkdir(dir)
45
- rescue Errno::EEXIST
46
- log.warn("Source directory exists, leaving...")
47
- end
48
-
49
- if prefix == "git"
50
- get_source_from_git(scm_address, dir, pom.version)
51
- elsif prefix == "svn"
52
- get_source_from_svn(scm_address, dir, pom.version)
53
- end
54
- end
55
-
56
- # checks code out of git
57
- def get_source_from_git(scm_address, dir, version)
58
- `git clone #{scm_address} #{dir}`
59
-
60
- Dir.chdir(dir) do
61
- tags = `git tag`.split("\n")
62
-
63
- if tags.any?
64
- best_tag = get_best_tag(tags, version)
65
- log.info("checking out tag: #{best_tag}")
66
-
67
- `git checkout #{best_tag}`
68
- end
69
- end
70
- end
71
-
72
- # checks code out of svn
73
- def get_source_from_svn(scm_address, dir, version)
74
- `svn checkout #{scm_address} #{dir}`
75
-
76
- Dir.chdir(dir) do
77
- tags = `svn ls "^/tags"`.split("\n")
78
-
79
- if tags.any?
80
- best_tag = get_best_tag(tags, version)
81
- log.info("checking out tag: #{best_tag}")
82
-
83
- `svn checkout ^/tags/#{best_tag}`
84
- end
85
- end
86
- end
87
-
88
- # return the (heuristically) most similar tag to the specified version
89
- def get_best_tag(tags, version)
90
- version_matcher = VersionMatcher.new
91
-
92
- versions_to_tags = Hash[
93
- *tags.map do |tag|
94
- [version_matcher.split_version(tag)[1], tag]
95
- end.flatten
96
- ]
97
-
98
- log.info("found the following versions and tags: #{versions_to_tags}")
99
-
100
- best_version = version_matcher.best_match(version, versions_to_tags.keys)
101
- versions_to_tags[best_version]
102
- end
103
-
104
35
  private
105
36
 
106
37
  # if possible, turn path into a Maven artifact name, otherwise return nil
107
38
  def artifact_from_path(path)
108
39
  match = path.match(/\.\/kit\/m2\/(.+)\/(.+)\/(.+)\/\2-\3.*\.jar$/)
109
40
  if match != nil
110
- "#{match[1].gsub("/", ".")}:#{match[2]}:#{match[3]}:jar:sources"
41
+ [match[1].gsub("/", "."), match[2], match[3]]
111
42
  end
112
43
  end
113
44
  end
data/lib/gjp/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  module Gjp
4
- VERSION = "0.29.0"
4
+ VERSION = "0.30.0"
5
5
  end
@@ -13,7 +13,7 @@ module Gjp
13
13
  # returns a [name, version] pair
14
14
  def split_version(full_name)
15
15
  matches = full_name.match(/(.*?)(?:[\.\-\_ ~,]?([0-9].*))?$/)
16
- if matches != nil and matches.length > 1
16
+ if matches != nil && matches.length > 1
17
17
  [matches[1], matches[2]]
18
18
  else
19
19
  [full_string, nil]
@@ -82,7 +82,7 @@ module Gjp
82
82
  if their_chunk == nil
83
83
  their_chunk = "0"
84
84
  end
85
- if my_chunk.is_i? and their_chunk.is_i?
85
+ if my_chunk.is_i? && their_chunk.is_i?
86
86
  return [(my_chunk.to_i - their_chunk.to_i).abs, 99].min
87
87
  else
88
88
  return [Text::Levenshtein.distance(my_chunk.upcase, their_chunk.upcase), 99].min
@@ -0,0 +1,46 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'spec_helper'
4
+ require 'lib/kit_runner_spec'
5
+
6
+ describe Gjp::AntRunner do
7
+ it_behaves_like Gjp::KitRunner
8
+ include Gjp::Mockers
9
+
10
+ before(:each) do
11
+ create_mock_project
12
+ @kit_runner = Gjp::AntRunner.new(@project)
13
+ end
14
+
15
+ after(:each) do
16
+ delete_mock_project
17
+ end
18
+
19
+ describe "#get_ant_commandline" do
20
+ it "returns commandline options for running Ant" do
21
+ executable_path = create_mock_executable("ant")
22
+ @project.from_directory do
23
+ commandline = @kit_runner.get_ant_commandline(".")
24
+ commandline.should eq "./#{executable_path}"
25
+ end
26
+ end
27
+ it "doesn't return commandline options if Ant is not available" do
28
+ expect { @kit_runner.get_ant_commandline(".") }.to raise_error(Gjp::ExecutableNotFoundError)
29
+ end
30
+ end
31
+
32
+ describe "#ant" do
33
+ it "runs Ant" do
34
+ executable_path = create_mock_executable("ant")
35
+ @project.from_directory do
36
+ @kit_runner.ant(["extra-option"])
37
+ File.read("test_out").strip.should match /extra-option$/
38
+ end
39
+ end
40
+ it "doesn't run Ant if it is not available" do
41
+ @project.from_directory do
42
+ expect { @kit_runner.ant([]) }.to raise_error(Gjp::ExecutableNotFoundError)
43
+ end
44
+ end
45
+ end
46
+ end
@@ -2,87 +2,16 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- describe Gjp::KitRunner do
6
-
7
- before(:each) do
8
- @project_path = File.join("spec", "data", "test-project")
9
- Dir.mkdir(@project_path)
10
-
11
- Gjp::Project.init(@project_path)
12
- @project = Gjp::Project.new(@project_path)
13
- @kit_runner = Gjp::KitRunner.new(@project)
14
- end
15
-
16
- after(:each) do
17
- FileUtils.rm_rf(@project_path)
18
- end
5
+ shared_examples_for Gjp::KitRunner do
6
+ include Gjp::Mockers
19
7
 
20
8
  describe "#find_executable" do
21
9
  it "finds an executable in kit" do
22
- executable_path = mock_executable("mvn", @project_path)
23
- @kit_runner.find_executable("mvn").should eq executable_path
10
+ executable_path = create_mock_executable("any")
11
+ @kit_runner.find_executable("any").should eq executable_path
24
12
  end
25
13
  it "doesn't find a Maven executable in kit" do
26
- @kit_runner.find_executable("mvn").should be_nil
27
- end
28
- end
29
-
30
- describe "#get_maven_commandline" do
31
- it "returns commandline options for running maven" do
32
- executable_path = mock_executable("mvn", @project_path)
33
-
34
- @project.from_directory do
35
- commandline = @kit_runner.get_maven_commandline(".")
36
- commandline.should eq "./#{executable_path} -Dmaven.repo.local=./kit/m2 -s./kit/m2/settings.xml"
37
- end
38
- end
39
- it "doesn't return commandline options if Maven is not available" do
40
- expect { @kit_runner.get_maven_commandline(".") }.to raise_error(Gjp::ExecutableNotFoundError)
41
- end
42
- end
43
-
44
- describe "#mvn" do
45
- it "runs maven" do
46
- mock_executable("mvn", @project_path)
47
- @project.from_directory do
48
- @kit_runner.mvn(["extra-option"])
49
- File.read("test_out").strip.should match /extra-option$/
50
- end
51
- end
52
- it "doesn't run Maven if it is not available" do
53
- @project.from_directory do
54
- expect { @kit_runner.mvn([]) }.to raise_error(Gjp::ExecutableNotFoundError)
55
- end
56
- end
57
- end
58
-
59
-
60
- describe "#get_ant_commandline" do
61
- it "returns commandline options for running Ant" do
62
- executable_path = mock_executable("ant", @project_path)
63
-
64
- @project.from_directory do
65
- commandline = @kit_runner.get_ant_commandline(".")
66
- commandline.should eq "./#{executable_path}"
67
- end
68
- end
69
- it "doesn't return commandline options if Ant is not available" do
70
- expect { @kit_runner.get_ant_commandline(".") }.to raise_error(Gjp::ExecutableNotFoundError)
71
- end
72
- end
73
-
74
- describe "#ant" do
75
- it "runs Ant" do
76
- mock_executable("ant", @project_path)
77
- @project.from_directory do
78
- @kit_runner.ant(["extra-option"])
79
- File.read("test_out").strip.should match /extra-option$/
80
- end
81
- end
82
- it "doesn't run Ant if it is not available" do
83
- @project.from_directory do
84
- expect { @kit_runner.ant([]) }.to raise_error(Gjp::ExecutableNotFoundError)
85
- end
14
+ @kit_runner.find_executable("any").should be_nil
86
15
  end
87
16
  end
88
17
  end