maven_pom 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in maven_pom.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 FINN.no AS
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # MavenPom
2
+
3
+ [![Build Status](https://secure.travis-ci.org/finn-no/maven_pom.png)](http://travis-ci.org/finn-no/maven_pom)
4
+
5
+ Tiny library to work with Maven's pom.xml files from Ruby.
6
+
7
+ The code was extracted and gemified from an internal project. The API and functionality is based on that project's relatively simple needs. For instance, it does not care about artifact versions and won't check the validity of the poms.
8
+
9
+ The project is also in "patches only" mode.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ gem 'maven_pom'
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install maven_pom
24
+
25
+ ## Usage
26
+
27
+ pom = MavenPom.from("path/to/some/pom.xml")
28
+
29
+ pom.packaging #=> "pom"
30
+ pom.parent #=> "com.example:parent"
31
+ pom.key #=> "com.example:this"
32
+ pom.dependencies #=> ["com.example:foo", "com.example:bar"]
33
+
34
+ # topological sort - should roughly match e.g. reactor order
35
+ MavenPom.sort [pom1, pom2, pom3]
36
+
37
+
38
+ ## Contributing
39
+
40
+ 1. Fork it
41
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
42
+ 3. Make your changes and add tests for it. This is important so I don't break it in a future version unintentionally.
43
+ 4. Commit your changes (`git commit -am 'Added some feature'`)
44
+ 5. Push to the branch (`git push origin my-new-feature`)
45
+ 6. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new
6
+
7
+ task :default => :spec
@@ -0,0 +1,7 @@
1
+ module MavenPom
2
+ class MissingGroupIdError < StandardError
3
+ end
4
+
5
+ class CyclicDependencyError < StandardError
6
+ end
7
+ end
@@ -0,0 +1,83 @@
1
+ module MavenPom
2
+ class Pom
3
+ attr_reader :uri
4
+
5
+ def initialize(str, uri)
6
+ @pom = Nokogiri.XML(str)
7
+ @uri = uri
8
+ end
9
+
10
+ def as_json(opts = nil)
11
+ {
12
+ :key => key,
13
+ :name => name,
14
+ :parent => parent,
15
+ :packaging => packaging
16
+ }
17
+ end
18
+
19
+ def to_json(*args)
20
+ as_json.to_json(*args)
21
+ end
22
+
23
+ def packaging
24
+ @pom.css("project > packaging").text
25
+ end
26
+
27
+ def name
28
+ @pom.css("project > name").text.strip
29
+ end
30
+
31
+ def build_plugins
32
+ @pom.css("plugins > plugin").map do |node|
33
+ dependency_name_from(node)
34
+ end
35
+ end
36
+
37
+ def parent
38
+ parent = @pom.css("project > parent").first
39
+
40
+ if parent
41
+ gid = parent.css("groupId").text
42
+ aid = parent.css("artifactId").text
43
+
44
+ "#{gid}:#{aid}"
45
+ end
46
+ end
47
+
48
+ def group_id
49
+ gid = @pom.css("project > groupId").text
50
+ gid = @pom.css("project > parent > groupId").text if gid.empty?
51
+
52
+ if gid.empty?
53
+ raise MissingGroupIdError, "could not find groupId in pom (uri=#{uri.inspect})"
54
+ end
55
+
56
+ gid
57
+ end
58
+
59
+ def artifact_id
60
+ @pom.css("project > artifactId").text
61
+ end
62
+
63
+ def key
64
+ @key ||= "#{group_id}:#{artifact_id}"
65
+ end
66
+
67
+ def dependencies
68
+ @pom.css("dependencies > dependency").map do |node|
69
+ dependency_name_from(node)
70
+ end
71
+ end
72
+
73
+ def dependency_name_from(node)
74
+ gid = node.css("groupId").text
75
+ aid = node.css("artifactId").text
76
+
77
+ gid = group_id if gid == "${project.groupId}" # ugh
78
+
79
+ "#{gid}:#{aid}"
80
+ end
81
+
82
+ end # Pom
83
+ end # Maven
@@ -0,0 +1,51 @@
1
+ require 'rgl/adjacency'
2
+ require 'rgl/topsort'
3
+ require 'rgl/dot'
4
+
5
+ module MavenPom
6
+ class Sorter
7
+ attr_reader :dag
8
+
9
+ def initialize(poms)
10
+ @poms = poms
11
+ build_graph
12
+ end
13
+
14
+ def sort
15
+ sorted_keys = @dag.topsort_iterator.to_a.reverse
16
+ sorted_keys.map { |key| @map[key] }.compact
17
+ end
18
+
19
+ private
20
+
21
+ def build_graph
22
+ @dag = RGL::DirectedAdjacencyGraph.new
23
+ @map = {}
24
+
25
+ @poms.each do |pom|
26
+ @map[pom.key] = pom
27
+ @dag.add_vertex(pom.key)
28
+ end
29
+
30
+ @dag.vertices.each do |key|
31
+ pom = @map.fetch(key)
32
+ pom.dependencies.each do |dep|
33
+ @dag.add_edge key, dep
34
+ end
35
+
36
+ if parent = pom.parent
37
+ @dag.add_edge key, parent
38
+ end
39
+
40
+ pom.build_plugins.each do |plugin|
41
+ @dag.add_edge key, plugin
42
+ end
43
+ end
44
+
45
+ unless @dag.acyclic?
46
+ raise CyclicDependencyError, "dependency graph has cycles"
47
+ end
48
+ end
49
+ end
50
+ end
51
+
@@ -0,0 +1,3 @@
1
+ module MavenPom
2
+ VERSION = "0.0.1"
3
+ end
data/lib/maven_pom.rb ADDED
@@ -0,0 +1,16 @@
1
+ require 'nokogiri'
2
+
3
+ require "maven_pom/version"
4
+ require "maven_pom/error"
5
+ require "maven_pom/pom"
6
+ require "maven_pom/sorter"
7
+
8
+ module MavenPom
9
+ def self.from(path)
10
+ Pom.new File.read(path), path
11
+ end
12
+
13
+ def self.sort(poms)
14
+ Sorter.new(poms).sort
15
+ end
16
+ end
data/maven_pom.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/maven_pom/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Jari Bakken"]
6
+ gem.email = ["jari.bakken@finn.no"]
7
+ gem.description = %q{Tiny library to work with Maven's pom.xml files from Ruby.}
8
+ gem.summary = %q{Tiny library to work with Maven's pom.xml files from Ruby.}
9
+ gem.homepage = "http://github.com/finn-no/maven_pom"
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "maven_pom"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = MavenPom::VERSION
17
+
18
+ gem.add_dependency "nokogiri", "~> 1.5.0"
19
+ gem.add_dependency "rgl", "~> 0.4.0"
20
+ gem.add_development_dependency "rspec", "~> 2.0"
21
+ end
@@ -0,0 +1,87 @@
1
+ <project xmlns="http://maven.apache.org/POM/4.0.0"
2
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
4
+ http://maven.apache.org/xsd/maven-4.0.0.xsd">
5
+ <modelVersion>4.0.0</modelVersion>
6
+
7
+ <parent>
8
+ <groupId>com.example</groupId>
9
+ <artifactId>parent</artifactId>
10
+ </parent>
11
+
12
+ <groupId>com.example</groupId>
13
+ <artifactId>child</artifactId>
14
+ <version>1.0.0</version>
15
+ <packaging>pom</packaging>
16
+
17
+ <dependencies>
18
+ <dependency>
19
+ <groupId>com.example</groupId>
20
+ <artifactId>example-dep-1</artifactId>
21
+ <version>4.0</version>
22
+ <type>jar</type>
23
+ <scope>test</scope>
24
+ <optional>true</optional>
25
+ </dependency>
26
+ <dependency>
27
+ <groupId>com.example</groupId>
28
+ <artifactId>example-dep-2</artifactId>
29
+ <version>4.0</version>
30
+ <type>jar</type>
31
+ <scope>test</scope>
32
+ <optional>true</optional>
33
+ </dependency>
34
+ </dependencies>
35
+
36
+ <profiles>
37
+ <profile>
38
+ <id>release-profile</id>
39
+
40
+ <activation>
41
+ <property>
42
+ <name>performRelease</name>
43
+ <value>true</value>
44
+ </property>
45
+ </activation>
46
+
47
+ <build>
48
+ <plugins>
49
+ <plugin>
50
+ <inherited>true</inherited>
51
+ <groupId>org.apache.maven.plugins</groupId>
52
+ <artifactId>maven-source-plugin</artifactId>
53
+ <executions>
54
+ <execution>
55
+ <id>attach-sources</id>
56
+ <goals>
57
+ <goal>jar</goal>
58
+ </goals>
59
+ </execution>
60
+ </executions>
61
+ </plugin>
62
+ <plugin>
63
+ <inherited>true</inherited>
64
+ <groupId>org.apache.maven.plugins</groupId>
65
+ <artifactId>maven-javadoc-plugin</artifactId>
66
+ <executions>
67
+ <execution>
68
+ <id>attach-javadocs</id>
69
+ <goals>
70
+ <goal>jar</goal>
71
+ </goals>
72
+ </execution>
73
+ </executions>
74
+ </plugin>
75
+ <plugin>
76
+ <inherited>true</inherited>
77
+ <groupId>org.apache.maven.plugins</groupId>
78
+ <artifactId>maven-deploy-plugin</artifactId>
79
+ <configuration>
80
+ <updateReleaseInfo>true</updateReleaseInfo>
81
+ </configuration>
82
+ </plugin>
83
+ </plugins>
84
+ </build>
85
+ </profile>
86
+ </profiles>
87
+ </project>
@@ -0,0 +1,33 @@
1
+ <project xmlns="http://maven.apache.org/POM/4.0.0"
2
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
4
+ http://maven.apache.org/xsd/maven-4.0.0.xsd">
5
+ <modelVersion>4.0.0</modelVersion>
6
+
7
+ <groupId>com.example</groupId>
8
+ <artifactId>example</artifactId>
9
+ <version>1.0.0</version>
10
+ <packaging>pom</packaging>
11
+
12
+ <dependencies>
13
+
14
+ <dependency>
15
+ <groupId>com.example</groupId>
16
+ <artifactId>example-dep-1</artifactId>
17
+ <version>4.0</version>
18
+ <type>jar</type>
19
+ <scope>test</scope>
20
+ <optional>true</optional>
21
+ </dependency>
22
+
23
+ <dependency>
24
+ <groupId>${project.groupId}</groupId>
25
+ <artifactId>example-dep-2</artifactId>
26
+ <version>4.0</version>
27
+ <type>jar</type>
28
+ <scope>test</scope>
29
+ <optional>true</optional>
30
+ </dependency>
31
+
32
+ </dependencies>
33
+ </project>
@@ -0,0 +1,12 @@
1
+ <project xmlns="http://maven.apache.org/POM/4.0.0"
2
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
4
+ http://maven.apache.org/xsd/maven-4.0.0.xsd">
5
+ <modelVersion>4.0.0</modelVersion>
6
+
7
+ <groupId>com.example</groupId>
8
+ <artifactId>example-war</artifactId>
9
+ <version>1.0.0</version>
10
+ <packaging>war</packaging>
11
+
12
+ </project>
data/spec/pom_spec.rb ADDED
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+
3
+ module MavenPom
4
+ describe Pom do
5
+ let(:pom_module) { Pom.new fixture("pom.xml"), "some/pom/path/pom.xml" }
6
+ let(:war_module) { Pom.new fixture("war.xml"), "some/war/path/pom.xml" }
7
+ let(:child_module) { Pom.new fixture("child.xml"), "some/child/path/pom.xml" }
8
+
9
+ it "knows the module's packaging" do
10
+ pom_module.packaging.should == 'pom'
11
+ war_module.packaging.should == 'war'
12
+ end
13
+
14
+ it "knows the module's dependencies" do
15
+ pom_module.dependencies.should == [
16
+ "com.example:example-dep-1",
17
+ "com.example:example-dep-2" # uses ${project.groupId}
18
+ ]
19
+ end
20
+
21
+ it "knows the module's parent" do
22
+ child_module.parent.should == "com.example:parent"
23
+ end
24
+
25
+ it "knows the module's build plugins" do
26
+ child_module.build_plugins.should == [
27
+ "org.apache.maven.plugins:maven-source-plugin",
28
+ "org.apache.maven.plugins:maven-javadoc-plugin",
29
+ "org.apache.maven.plugins:maven-deploy-plugin"
30
+ ]
31
+ end
32
+
33
+ it "knows the group_id and artifact_id" do
34
+ war_module.group_id.should == "com.example"
35
+ war_module.artifact_id.should == "example-war"
36
+ end
37
+
38
+ it "finds the parent's group_id if none is specified" do
39
+ child_module.group_id.should == "com.example"
40
+ child_module.artifact_id.should == "child"
41
+ end
42
+
43
+ it "has a key" do
44
+ war_module.key.should == "com.example:example-war"
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+
3
+ module MavenPom
4
+ describe Sorter do
5
+ let(:poms) {
6
+ [
7
+ mock(Pom,
8
+ :key => "no.finntech:control",
9
+ :dependencies => ["no.finntech:kernel", "no.finntech:base"],
10
+ :parent => nil
11
+ ),
12
+ mock(Pom,
13
+ :key => "no.finntech:kernel",
14
+ :dependencies => [],
15
+ :parent => "no.finntech:iad"
16
+ ),
17
+ mock(Pom,
18
+ :key => "no.finntech:base",
19
+ :dependencies => ["no.finntech:kernel"],
20
+ :parent => "no.finntech:iad"
21
+ ),
22
+ mock(Pom,
23
+ :key => "no.finntech:iad",
24
+ :dependencies => [],
25
+ :parent => nil
26
+ )
27
+ ].each { |e| e.stub(:build_plugins => []) }
28
+ }
29
+
30
+ it "does a topological sort of the given maven modules" do
31
+ sorted = Sorter.new(poms).sort.map { |e| e.key }
32
+
33
+ sorted.should == [
34
+ "no.finntech:iad",
35
+ "no.finntech:kernel",
36
+ "no.finntech:base",
37
+ "no.finntech:control"
38
+ ]
39
+ end
40
+
41
+ it "has a shortcut on the module" do
42
+ MavenPom.sort(poms).should be_kind_of(Array)
43
+ end
44
+
45
+ end
46
+ end
47
+
48
+
@@ -0,0 +1,11 @@
1
+ require 'maven_pom'
2
+
3
+ module SpecHelper
4
+ def fixture(name)
5
+ File.read File.expand_path("../fixtures/#{name}", __FILE__)
6
+ end
7
+ end
8
+
9
+ RSpec.configure do |c|
10
+ c.include(SpecHelper)
11
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: maven_pom
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Jari Bakken
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-01-06 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ type: :runtime
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ hash: 3
28
+ segments:
29
+ - 1
30
+ - 5
31
+ - 0
32
+ version: 1.5.0
33
+ prerelease: false
34
+ name: nokogiri
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ type: :runtime
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ hash: 15
44
+ segments:
45
+ - 0
46
+ - 4
47
+ - 0
48
+ version: 0.4.0
49
+ prerelease: false
50
+ name: rgl
51
+ version_requirements: *id002
52
+ - !ruby/object:Gem::Dependency
53
+ type: :development
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ~>
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 2
62
+ - 0
63
+ version: "2.0"
64
+ prerelease: false
65
+ name: rspec
66
+ version_requirements: *id003
67
+ description: Tiny library to work with Maven's pom.xml files from Ruby.
68
+ email:
69
+ - jari.bakken@finn.no
70
+ executables: []
71
+
72
+ extensions: []
73
+
74
+ extra_rdoc_files: []
75
+
76
+ files:
77
+ - .gitignore
78
+ - .travis.yml
79
+ - Gemfile
80
+ - LICENSE
81
+ - README.md
82
+ - Rakefile
83
+ - lib/maven_pom.rb
84
+ - lib/maven_pom/error.rb
85
+ - lib/maven_pom/pom.rb
86
+ - lib/maven_pom/sorter.rb
87
+ - lib/maven_pom/version.rb
88
+ - maven_pom.gemspec
89
+ - spec/fixtures/child.xml
90
+ - spec/fixtures/pom.xml
91
+ - spec/fixtures/war.xml
92
+ - spec/pom_spec.rb
93
+ - spec/sorter_spec.rb
94
+ - spec/spec_helper.rb
95
+ homepage: http://github.com/finn-no/maven_pom
96
+ licenses: []
97
+
98
+ post_install_message:
99
+ rdoc_options: []
100
+
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ hash: 3
109
+ segments:
110
+ - 0
111
+ version: "0"
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ hash: 3
118
+ segments:
119
+ - 0
120
+ version: "0"
121
+ requirements: []
122
+
123
+ rubyforge_project:
124
+ rubygems_version: 1.8.10
125
+ signing_key:
126
+ specification_version: 3
127
+ summary: Tiny library to work with Maven's pom.xml files from Ruby.
128
+ test_files:
129
+ - spec/fixtures/child.xml
130
+ - spec/fixtures/pom.xml
131
+ - spec/fixtures/war.xml
132
+ - spec/pom_spec.rb
133
+ - spec/sorter_spec.rb
134
+ - spec/spec_helper.rb