Skip to content

Instantly share code, notes, and snippets.

@mntmn
Created June 14, 2012 15:45
Show Gist options
  • Save mntmn/2931073 to your computer and use it in GitHub Desktop.
Save mntmn/2931073 to your computer and use it in GitHub Desktop.
Script to convert a single-module IntelliJ IDEA Scala/SBT project and libraries into a module definition
require "nokogiri"
puts "launching gen-idea..."
puts `sbt gen-idea`
puts "finding module name..."
module_name=nil
basepath=".idea/libraries/"
Dir.new(".idea_modules/").each do |f|
if f.match(".iml") && !f.match("build.iml")
module_name=f
end
end
unless module_name
puts "error: could not find module name."
exit
end
puts "module: #{module_name}"
puts "converting module definition..."
output_path=module_name
module_source=".idea_modules/#{module_name}"
source_doc = Nokogiri::XML(File.read(module_source))
root_manager_node = nil
components = source_doc.xpath("//component")
components.each do |c|
puts c
root_manager_node = c if c["name"]=="NewModuleRootManager"
end
unless root_manager_node
puts "error: root manager node not found"
exit
end
removed_count = 0
# remove previous library entries
old_libraries = source_doc.xpath("//orderEntry")
old_libraries.each do |l|
if l["type"]=="library" || l["type"]=="module-library"
l.remove
removed_count+=1
end
end
added_count = 0
Dir.new(basepath).each do |xml_file|
if xml_file.match(".xml")
puts "reading: #{xml_file}"
doc = Nokogiri::XML(File.read(basepath+xml_file))
library_node = doc.xpath("//library")
order_entry_node = Nokogiri::XML::Node.new("orderEntry",doc)
order_entry_node["type"] = "module-library"
order_entry_node.add_child(library_node)
root_manager_node.add_child(order_entry_node)
added_count+=1
end
end
#puts source_doc
puts "Removed #{removed_count} old libraries, added #{added_count} new."
File.open(output_path,"w") do |f|
f.write(source_doc.to_s)
end
puts "#{output_path} written."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment