class TwitterCldr::Resources::Requirements::PomManager

Constants

BLANK_POM
DEPENDENCY_TEMPLATE

Attributes

path[R]
pom_file[R]

Public Class Methods

new(pom_file) click to toggle source
# 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

Public Instance Methods

add_dependency(group_id, artifact_id, version) click to toggle source
# 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
classpath() click to toggle source
# 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) click to toggle source
# 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
install() click to toggle source
# File lib/twitter_cldr/resources/requirements/pom_manager.rb, line 88
def install
  save
  mvn('install')
end
require_jar(group_id, artifact_id) click to toggle source
# 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

Private Instance Methods

contents() click to toggle source
# File lib/twitter_cldr/resources/requirements/pom_manager.rb, line 116
def contents
  @contents ||= if File.exist?(pom_file)
    Nokogiri::XML(File.read(pom_file)) do |config|
      config.options = Nokogiri::XML::ParseOptions::NOBLANKS
    end
  else
    Nokogiri::XML(BLANK_POM) do |config|
      config.options = Nokogiri::XML::ParseOptions::NOBLANKS
    end
  end
end
formatter() click to toggle source
# File lib/twitter_cldr/resources/requirements/pom_manager.rb, line 136
def formatter
  @formatter ||= begin
    REXML::Formatters::Pretty.new(2).tap do |fmt|
      fmt.compact = true
    end
  end
end
mvn(cmd) click to toggle source
# File lib/twitter_cldr/resources/requirements/pom_manager.rb, line 144
def mvn(cmd)
  Dir.chdir(path) { `sudo mvn #{cmd}` }
end
save() click to toggle source
# File lib/twitter_cldr/resources/requirements/pom_manager.rb, line 128
def save
  FileUtils.mkdir_p(File.dirname(pom_file))

  File.open(pom_file, 'w+') do |f|
    formatter.write(REXML::Document.new(contents.to_xml), f)
  end
end