Created
November 3, 2009 11:58
-
-
Save rhyskeepence/224971 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'fileutils' | |
require 'rexml/document' | |
class IntellijIml | |
def initialize(iml_file) | |
@iml_file = iml_file | |
@doc = REXML::Document.new(File.new(@iml_file)) | |
end | |
def write | |
@doc.write(File.new(@iml_file, 'w+')) | |
end | |
def add_source_folder_if_it_exists(module_dir, dir) | |
if File.exists?("#{module_dir}/#{dir}") && !REXML::XPath.first(@doc, "//module/component/content/sourceFolder[@url = 'file://$MODULE_DIR$/#{dir}']") | |
module_xml = REXML::Element.new('sourceFolder', REXML::XPath.first(@doc, "//module/component/content")) | |
module_xml.add_attribute('url', "file://$MODULE_DIR$/#{dir}") | |
module_xml.add_attribute('isTestSource', "true") | |
puts " . Added source folder #{dir}" | |
end | |
end | |
def exclude_directory(module_dir, dir) | |
if !REXML::XPath.first(@doc, "//module/component/content/excludeFolder[@url = 'file://$MODULE_DIR$/#{dir}']") | |
module_xml = REXML::Element.new('excludeFolder', REXML::XPath.first(@doc, "//module/component/content")) | |
module_xml.add_attribute('url', "file://$MODULE_DIR$/#{dir}") | |
puts " . Excluded #{dir}" | |
end | |
end | |
def kill_all_facets | |
@doc.delete_element "//module/component[@name = 'FacetManager']" | |
end | |
end | |
if ARGV.empty? | |
puts "usage: wol-intellij-project-fixer.rb intellij-project-directory" | |
exit 999 | |
end | |
project_dir = ARGV[0] | |
Dir["#{project_dir}/**/*.iml"].each do |iml_file| | |
puts " - Fudging #{iml_file}" | |
module_dir = File.dirname(iml_file) | |
iml = IntellijIml.new(iml_file) | |
iml.add_source_folder_if_it_exists(module_dir, "src/integration/java") | |
iml.add_source_folder_if_it_exists(module_dir, "src/integration/resources") | |
iml.add_source_folder_if_it_exists(module_dir, "src/acceptance/java") | |
iml.add_source_folder_if_it_exists(module_dir, "src/acceptance/resources") | |
iml.exclude_directory(module_dir, "/bin") | |
iml.kill_all_facets | |
iml.write | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment