class Buildr::IntellijIdea::IdeaModule

IdeaModule represents an .iml file

Constants

DEFAULT_TYPE

Attributes

facets[R]
group[RW]
jdk_version[W]
main_output_dir[W]
test_output_dir[W]
type[RW]

Public Class Methods

new() click to toggle source
Calls superclass method Buildr::IntellijIdea::IdeaFile.new
# File lib/buildr/ide/idea.rb, line 174
def initialize
  super()
  @type = DEFAULT_TYPE
end

Public Instance Methods

add_ejb_facet(options = {}) click to toggle source
# File lib/buildr/ide/idea.rb, line 402
def add_ejb_facet(options = {})
  name = options[:name] || "EJB"

  default_ejb_roots = [buildr_project.iml.main_source_directories, buildr_project.compile.sources, buildr_project.resources.sources].flatten.compact
  ejb_roots = options[:ejb_roots] || default_ejb_roots

  default_deployment_descriptors = []
  ['ejb-jar.xml', 'glassfish-ejb-jar.xml', 'ibm-ejb-jar-bnd.xml', 'ibm-ejb-jar-ext-pme.xml', 'ibm-ejb-jar-ext.xml',
   'jboss.xml', 'jbosscmp-jdbc.xml', 'openejb-jar.xml', 'sun-cmp-mapping.xml', 'sun-ejb-jar.xml',
   'weblogic-cmp-rdbms-jar.xml', 'weblogic-ejb-jar.xml'].
    each do |descriptor|
    ejb_roots.each do |path|
      d = "#{path}/WEB-INF/#{descriptor}"
      default_deployment_descriptors << d if File.exist?(d)
    end
  end
  deployment_descriptors = options[:deployment_descriptors] || default_deployment_descriptors

  add_facet(name, "ejb") do |facet|
    facet.configuration do |c|
      c.descriptors do |d|
        deployment_descriptors.each do |deployment_descriptor|
          d.deploymentDescriptor :name => File.basename(deployment_descriptor), :url => file_path(deployment_descriptor)
        end
      end
      c.ejbRoots do |e|
        ejb_roots.each do |ejb_root|
          e.root :url => file_path(ejb_root)
        end
      end
    end
  end
end
add_facet(name, type) { |xml| ... } click to toggle source
# File lib/buildr/ide/idea.rb, line 238
def add_facet(name, type)
  add_to_composite_component(self.facets) do |xml|
    xml.facet(:name => name, :type => type) do |xml|
      yield xml if block_given?
    end
  end
end
add_gwt_facet(modules = {}, options = {}) click to toggle source
# File lib/buildr/ide/idea.rb, line 254
def add_gwt_facet(modules = {}, options = {})
  name = options[:name] || "GWT"
  detected_gwt_version = nil
  if options[:gwt_dev_artifact]
    a = Buildr.artifact(options[:gwt_dev_artifact])
    a.invoke
    detected_gwt_version = a.to_s
  end

  settings =
    {
      :webFacet => "Web",
      :compilerMaxHeapSize => "512",
      :compilerParameters => "-draftCompile -localWorkers 2 -strict",
      :gwtScriptOutputStyle => "PRETTY"
    }.merge(options[:settings] || {})

  buildr_project.compile.dependencies.each do |d|
    if d.to_s =~ /\/com\/google\/gwt\/gwt-dev\/(.*)\//
      detected_gwt_version = d.to_s
      break
    end
  end unless detected_gwt_version

  if detected_gwt_version
    settings[:gwtSdkUrl] = resolve_path(File.dirname(detected_gwt_version))
    settings[:gwtSdkType] = "maven"
  else
    settings[:gwtSdkUrl] = "file://$GWT_TOOLS$"
  end

  add_facet(name, "gwt") do |f|
    f.configuration do |c|
      settings.each_pair do |k, v|
        c.setting :name => k.to_s, :value => v.to_s
      end
      c.packaging do |d|
        modules.each_pair do |k, v|
          d.module :name => k, :enabled => v
        end
      end
    end
  end
end
add_jpa_facet(options = {}) click to toggle source
# File lib/buildr/ide/idea.rb, line 355
def add_jpa_facet(options = {})
  name = options[:name] || "JPA"

  source_roots = [buildr_project.iml.main_source_directories, buildr_project.compile.sources, buildr_project.resources.sources].flatten.compact
  default_deployment_descriptors = []
  ['orm.xml', 'persistence.xml'].
    each do |descriptor|
    source_roots.each do |path|
      d = "#{path}/META-INF/#{descriptor}"
      default_deployment_descriptors << d if File.exist?(d)
    end
  end
  deployment_descriptors = options[:deployment_descriptors] || default_deployment_descriptors

  factory_entry = options[:factory_entry] || buildr_project.name.to_s
  validation_enabled = options[:validation_enabled].nil? ? true : options[:validation_enabled]
  if options[:provider_enabled]
    provider = options[:provider_enabled]
  else
    provider = nil
    {'org.hibernate.ejb.HibernatePersistence' => 'Hibernate',
     'org.eclipse.persistence.jpa.PersistenceProvider' => 'EclipseLink'}.
      each_pair do |match, candidate_provider|
      deployment_descriptors.each do |descriptor|
        if File.exist?(descriptor) && /#{Regexp.escape(match)}/ =~ IO.read(descriptor)
          provider = candidate_provider
        end
      end
    end
  end

  add_facet(name, "jpa") do |f|
    f.configuration do |c|
      if provider
        c.setting :name => "validation-enabled", :value => validation_enabled
        c.setting :name => "provider-name", :value => provider
      end
      c.tag!('datasource-mapping') do |ds|
        ds.tag!('factory-entry', :name => factory_entry)
      end
      deployment_descriptors.each do |descriptor|
        c.deploymentDescriptor :name => File.basename(descriptor), :url => file_path(descriptor)
      end
    end
  end
end
add_jruby_facet(options = {}) click to toggle source
# File lib/buildr/ide/idea.rb, line 340
def add_jruby_facet(options = {})
  name = options[:name] || "JRuby"

  ruby_version_file = buildr_project._('.ruby-version')
  default_jruby_version = File.exist?(ruby_version_file) ? "rbenv: #{IO.read(ruby_version_file).strip}" : 'jruby-1.6.7.2'
  jruby_version = options[:jruby_version] || default_jruby_version
  add_facet(name, "JRUBY") do |f|
    f.configuration do |c|
      c.JRUBY_FACET_CONFIG_ID :NAME => "JRUBY_SDK_NAME", :VALUE => jruby_version
      c.LOAD_PATH :number => "0"
      c.I18N_FOLDERS :number => "0"
    end
  end
end
add_web_facet(options = {}) click to toggle source
# File lib/buildr/ide/idea.rb, line 299
def add_web_facet(options = {})
  name = options[:name] || "Web"
  default_webroots = {}
  buildr_project.assets.paths.each {|p| default_webroots[p] = "/" }
  webroots = options[:webroots] || default_webroots
  default_deployment_descriptors = []
  ['web.xml', 'sun-web.xml', 'glassfish-web.xml', 'jetty-web.xml', 'geronimo-web.xml',
   'context.xml', 'weblogic.xml',
   'jboss-deployment-structure.xml', 'jboss-web.xml',
   'ibm-web-bnd.xml', 'ibm-web-ext.xml', 'ibm-web-ext-pme.xml'].
    each do |descriptor|
    webroots.each_pair do |path, relative_url|
      next unless relative_url == "/"
      d = "#{path}/WEB-INF/#{descriptor}"
      default_deployment_descriptors << d if File.exist?(d)
    end
  end
  deployment_descriptors = options[:deployment_descriptors] || default_deployment_descriptors

  add_facet(name, "web") do |f|
    f.configuration do |c|
      c.descriptors do |d|
        deployment_descriptors.each do |deployment_descriptor|
          d.deploymentDescriptor :name => File.basename(deployment_descriptor), :url => file_path(deployment_descriptor)
        end
      end
      c.webroots do |w|
        webroots.each_pair do |webroot, relative_url|
          w.root :url => file_path(webroot), :relative => relative_url
        end
      end
    end
    default_enable_jsf = webroots.select{|webroot| File.exist?("#{webroot}/WEB-INF/faces-config.xml")}
    enable_jsf = options[:enable_jsf].nil? ? default_enable_jsf : options[:enable_jsf]
    enable_jsf = false if root_project.ipr? && root_project.ipr.version >= '13'
    f.facet(:type => 'jsf', :name => 'JSF') do |jsf|
      jsf.configuration
    end if enable_jsf
  end
end
buildr_project=(buildr_project) click to toggle source
# File lib/buildr/ide/idea.rb, line 179
def buildr_project=(buildr_project)
  @id = nil
  @facets = []
  @skip_content = false
  @buildr_project = buildr_project
end
excluded_directories() click to toggle source
# File lib/buildr/ide/idea.rb, line 208
def excluded_directories
  @excluded_directories ||= [
    buildr_project.resources.target,
    buildr_project.test.resources.target,
    buildr_project.path_to(:target, :main),
    buildr_project.path_to(:target, :test),
    buildr_project.path_to(:reports)
  ].flatten.compact
end
extension() click to toggle source
# File lib/buildr/ide/idea.rb, line 190
def extension
  "iml"
end
jdk_version() click to toggle source
# File lib/buildr/ide/idea.rb, line 186
def jdk_version
  @jdk_version || buildr_project.compile.options.source || "1.6"
end
main_dependencies() click to toggle source
# File lib/buildr/ide/idea.rb, line 230
def main_dependencies
  @main_dependencies ||=  buildr_project.compile.dependencies
end
main_output_dir() click to toggle source
# File lib/buildr/ide/idea.rb, line 220
def main_output_dir
  @main_output_dir ||= buildr_project._(:target, :main, :idea, :classes)
end
main_source_directories() click to toggle source
# File lib/buildr/ide/idea.rb, line 194
def main_source_directories
  @main_source_directories ||= [
    buildr_project.compile.sources,
    buildr_project.resources.sources
  ].flatten.compact
end
skip_content!() click to toggle source
# File lib/buildr/ide/idea.rb, line 250
def skip_content!
  @skip_content = true
end
skip_content?() click to toggle source
# File lib/buildr/ide/idea.rb, line 246
def skip_content?
  !!@skip_content
end
test_dependencies() click to toggle source
# File lib/buildr/ide/idea.rb, line 234
def test_dependencies
  @test_dependencies ||=  buildr_project.test.compile.dependencies
end
test_output_dir() click to toggle source
# File lib/buildr/ide/idea.rb, line 226
def test_output_dir
  @test_output_dir ||= buildr_project._(:target, :test, :idea, :classes)
end
test_source_directories() click to toggle source
# File lib/buildr/ide/idea.rb, line 201
def test_source_directories
  @test_source_directories ||= [
    buildr_project.test.compile.sources,
    buildr_project.test.resources.sources
  ].flatten.compact
end

Protected Instance Methods

base_document() click to toggle source
# File lib/buildr/ide/idea.rb, line 462
def base_document
  target = StringIO.new
  Builder::XmlMarkup.new(:target => target).module(:version => "4", :relativePaths => "true", :type => self.type)
  Buildr::IntellijIdea.new_document(target.string)
end
default_components() click to toggle source
# File lib/buildr/ide/idea.rb, line 472
def default_components
  [
    lambda { module_root_component },
    lambda { facet_component }
  ]
end
facet_component() click to toggle source
# File lib/buildr/ide/idea.rb, line 479
def facet_component
  create_composite_component("FacetManager", {}, self.facets)
end
generate_compile_output(xml) click to toggle source
# File lib/buildr/ide/idea.rb, line 538
def generate_compile_output(xml)
  xml.output(:url => file_path(self.main_output_dir.to_s))
  xml.tag!("output-test", :url => file_path(self.test_output_dir.to_s))
  xml.tag!("exclude-output")
end
generate_content(xml) click to toggle source
# File lib/buildr/ide/idea.rb, line 544
def generate_content(xml)
  xml.content(:url => "file://$MODULE_DIR$") do
    # Source folders
    {
      :main => self.main_source_directories,
      :test => self.test_source_directories
    }.each do |kind, directories|
      directories.map { |dir| dir.to_s }.compact.sort.uniq.each do |dir|
        xml.sourceFolder :url => file_path(dir), :isTestSource => (kind == :test ? 'true' : 'false')
      end
    end

    # Exclude target directories
    self.net_excluded_directories.
      collect { |dir| file_path(dir) }.
      select { |dir| relative_dir_inside_dir?(dir) }.
      sort.each do |dir|
      xml.excludeFolder :url => dir
    end
  end
end
generate_initial_order_entries(xml) click to toggle source
# File lib/buildr/ide/idea.rb, line 570
def generate_initial_order_entries(xml)
  xml.orderEntry :type => "sourceFolder", :forTests => "false"
  xml.orderEntry :type => "jdk", :jdkName => jdk_version, :jdkType => "JavaSDK"
end
generate_lib(xml, dependency_path, export, source_path, project_dependencies) click to toggle source
# File lib/buildr/ide/idea.rb, line 505
def generate_lib(xml, dependency_path, export, source_path, project_dependencies)
  project_for_dependency = Buildr.projects.detect do |project|
    [project.packages, project.compile.target, project.resources.target, project.test.compile.target, project.test.resources.target].flatten.
      detect { |artifact| artifact.to_s == dependency_path }
  end
  if project_for_dependency
    if project_for_dependency.iml? &&
      !project_dependencies.include?(project_for_dependency) &&
      project_for_dependency != self.buildr_project
      generate_project_dependency(xml, project_for_dependency.iml.name, export, !export)
    end
    project_dependencies << project_for_dependency
  else
    generate_module_lib(xml, url_for_path(dependency_path), export, (source_path ? url_for_path(source_path) : nil), !export)
  end
end
generate_module_lib(xml, path, export, source_path, test = false) click to toggle source
# File lib/buildr/ide/idea.rb, line 582
def generate_module_lib(xml, path, export, source_path, test = false)
  attribs = {:type => 'module-library'}
  attribs[:exported] = '' if export
  attribs[:scope] = 'TEST' if test
  xml.orderEntry attribs do
    xml.library do
      xml.CLASSES do
        xml.root :url => path
      end
      xml.JAVADOC
      xml.SOURCES do
        if source_path
          xml.root :url => source_path
        end
      end
    end
  end
end
generate_project_dependency(xml, other_project, export, test = false) click to toggle source
# File lib/buildr/ide/idea.rb, line 575
def generate_project_dependency(xml, other_project, export, test = false)
  attribs = {:type => 'module', "module-name" => other_project}
  attribs[:exported] = '' if export
  attribs[:scope] = 'TEST' if test
  xml.orderEntry attribs
end
initial_components() click to toggle source
# File lib/buildr/ide/idea.rb, line 468
def initial_components
  []
end
jar_path(path) click to toggle source
# File lib/buildr/ide/idea.rb, line 522
def jar_path(path)
  "jar://#{resolve_path(path)}!/"
end
module_root_component() click to toggle source
# File lib/buildr/ide/idea.rb, line 483
def module_root_component
  create_component("NewModuleRootManager", "inherit-compiler-output" => "false") do |xml|
    generate_compile_output(xml)
    generate_content(xml) unless skip_content?
    generate_initial_order_entries(xml)
    project_dependencies = []


    self.test_dependency_details.each do |dependency_path, export, source_path|
      next unless export
      generate_lib(xml, dependency_path, export, source_path, project_dependencies)
    end

    self.test_dependency_details.each do |dependency_path, export, source_path|
      next if export
      generate_lib(xml, dependency_path, export, source_path, project_dependencies)
    end

    xml.orderEntryProperties
  end
end
net_excluded_directories() click to toggle source

Don’t exclude things that are subdirectories of other excluded things

# File lib/buildr/ide/idea.rb, line 602
def net_excluded_directories
  net = []
  all = self.excluded_directories.map { |dir| buildr_project._(dir.to_s) }.sort_by { |d| d.size }
  all.each_with_index do |dir, i|
    unless all[0 ... i].find { |other| dir =~ /^#{other}/ }
      net << dir
    end
  end
  net
end
relative_dir_inside_dir?(dir) click to toggle source
# File lib/buildr/ide/idea.rb, line 566
def relative_dir_inside_dir?(dir)
  !dir.include?("../")
end
resolve_path(path) click to toggle source
# File lib/buildr/ide/idea.rb, line 534
def resolve_path(path)
  resolve_path_from_base(path, "$MODULE_DIR$")
end
root_project() click to toggle source
# File lib/buildr/ide/idea.rb, line 438
def root_project
  p = buildr_project
  while p.parent
    p = p.parent
  end
  p
end
test_dependency_details() click to toggle source
# File lib/buildr/ide/idea.rb, line 446
def test_dependency_details
  main_dependencies_paths = main_dependencies.map(&:to_s)
  target_dir = buildr_project.compile.target.to_s
  test_dependencies.select { |d| d.to_s != target_dir }.collect do |d|
    dependency_path = d.to_s
    export = main_dependencies_paths.include?(dependency_path)
    source_path = nil
    if d.respond_to?(:to_spec_hash)
      source_spec = d.to_spec_hash.merge(:classifier => 'sources')
      source_path = Buildr.artifact(source_spec).to_s
      source_path = nil unless File.exist?(source_path)
    end
    [dependency_path, export, source_path]
  end
end
url_for_path(path) click to toggle source
# File lib/buildr/ide/idea.rb, line 526
def url_for_path(path)
  if path =~ /jar$/
    jar_path(path)
  else
    file_path(path)
  end
end