Class: TwitterCldr::Resources::Requirements::PomManager

Inherits:
Object
  • Object
show all
Defined in:
lib/twitter_cldr/resources/requirements/pom_manager.rb

Defined Under Namespace

Classes: Dep

Constant Summary collapse

BLANK_POM =
<<~END.freeze
  <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.mycompany.app</groupId>
    <artifactId>my-app</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
      <maven.compiler.source>1.9</maven.compiler.source>
      <maven.compiler.target>1.9</maven.compiler.target>
    </properties>

    <dependencies>
    </dependencies>
  </project>
END
DEPENDENCY_TEMPLATE =
<<~END.freeze
  <dependency>
    <groupId>%{group_id}</groupId>
    <artifactId>%{artifact_id}</artifactId>
    <version>%{version}</version>
  </dependency>
END

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pom_file) ⇒ PomManager

Returns a new instance of PomManager.



65
66
67
68
# File 'lib/twitter_cldr/resources/requirements/pom_manager.rb', line 65

def initialize(pom_file)
  @pom_file = pom_file
  @path = File.dirname(pom_file)
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



63
64
65
# File 'lib/twitter_cldr/resources/requirements/pom_manager.rb', line 63

def path
  @path
end

#pom_fileObject (readonly)

Returns the value of attribute pom_file.



63
64
65
# File 'lib/twitter_cldr/resources/requirements/pom_manager.rb', line 63

def pom_file
  @pom_file
end

Instance Method Details

#add_dependency(group_id, artifact_id, version) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/twitter_cldr/resources/requirements/pom_manager.rb', line 70

def add_dependency(group_id, artifact_id, version)
  existing_dep = (contents / 'dependencies' / 'dependency').find do |dep|
    (dep / 'groupId').text == group_id && (dep / 'artifactId').text == artifact_id
  end

  existing_dep.remove if existing_dep

  dep = DEPENDENCY_TEMPLATE % {
    group_id: group_id,
    artifact_id: artifact_id,
    version: version
  }

  (contents / 'dependencies').first.add_child(
    Nokogiri::XML(dep) / 'dependency'
  )
end

#classpathObject



101
102
103
104
105
106
107
108
# File 'lib/twitter_cldr/resources/requirements/pom_manager.rb', line 101

def classpath
  @classpath ||= mvn('dependency:build-classpath')
    .split("\n")
    .map(&:strip)
    .reject { |line| line =~ /\[[^\]]+\]/ }
    .first
    .split(':')
end

#get(group_id, artifact_id) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/twitter_cldr/resources/requirements/pom_manager.rb', line 93

def get(group_id, artifact_id)
  dep = (contents / 'dependencies' / 'dependency').find do |dep|
    (dep / 'groupId').text == group_id && (dep / 'artifactId').text == artifact_id
  end

  Dep.new(self, group_id, artifact_id, (dep / 'version').text)
end

#installObject



88
89
90
91
# File 'lib/twitter_cldr/resources/requirements/pom_manager.rb', line 88

def install
  save
  mvn('install')
end

#require_jar(group_id, artifact_id) ⇒ Object



110
111
112
# File 'lib/twitter_cldr/resources/requirements/pom_manager.rb', line 110

def require_jar(group_id, artifact_id)
  require get(group_id, artifact_id).path
end