module ImlRobolectricity

Constants

VERSION

Public Class Methods

robolectrify(iml_path) click to toggle source
# File lib/ImlRobolectricity.rb, line 5
def self.robolectrify(iml_path)
  if iml_path.nil?
    puts 'No .iml file given.'
    return false
  end

  file_path = File.expand_path iml_path
  iml_input_file = File.open file_path

  iml_xml = Nokogiri::XML.parse(iml_input_file)
  iml_input_file.close

  jdk = iml_xml.search("orderEntry[jdkType='Android SDK']").first
  if jdk.nil?
    puts 'Not an Android module.'
    return false
  end

  puts 'Android dependency found.'

  jdk.remove
  iml_xml.search('orderEntry').last.after jdk
  puts 'Put Android dependency last.'

  output = iml_xml.search('output').first
  if output.nil?
    puts 'No output directory defined for the module.'
    return false
  end

  test_output = iml_xml.search('output-test').first
  if test_output.nil?
    test_output = Nokogiri::XML::Node.new('output-test', iml_xml)
    test_output['url'] = output['url'].gsub(/classes\/.*$/, 'test-classes')
    output.after test_output
    puts 'Test output directory set.'
  else
    puts "Test output set as: #{test_output['url']}"
  end

  iml_output_file = File.open file_path, 'w'
  iml_output_file.print iml_xml.to_xml
  iml_output_file.close

  return true
end