class Buildr::IntellijIdea::IdeaFile

Abstract base class for IdeaModule and IdeaProject

Constants

DEFAULT_LOCAL_REPOSITORY_ENV_OVERRIDE
DEFAULT_SUFFIX

Attributes

buildr_project[R]
id[W]
local_repository_env_override[RW]
suffix[W]
template[RW]

Public Class Methods

new() click to toggle source
# File lib/buildr/ide/idea.rb, line 33
def initialize
  @local_repository_env_override = DEFAULT_LOCAL_REPOSITORY_ENV_OVERRIDE
end

Public Instance Methods

add_component(name, attrs = {}, &xml) click to toggle source
# File lib/buildr/ide/idea.rb, line 49
def add_component(name, attrs = {}, &xml)
  self.components << create_component(name, attrs, &xml)
end
filename() click to toggle source
# File lib/buildr/ide/idea.rb, line 41
def filename
  buildr_project.path_to("#{name}.#{extension}")
end
id() click to toggle source
# File lib/buildr/ide/idea.rb, line 45
def id
  @id ||= buildr_project.name.split(':').last
end
suffix() click to toggle source
# File lib/buildr/ide/idea.rb, line 37
def suffix
  @suffix ||= DEFAULT_SUFFIX
end
write(f) click to toggle source

IDEA can not handle text content with indents so need to removing indenting Can not pass true as third argument as the ruby library seems broken

# File lib/buildr/ide/idea.rb, line 55
def write(f)
  document.write(f, -1, false, true)
end

Protected Instance Methods

add_to_composite_component(components) { |xml_markup(:target => target, :indent => 2)| ... } click to toggle source
# File lib/buildr/ide/idea.rb, line 113
def add_to_composite_component(components)
  components << lambda do
    target = StringIO.new
    yield Builder::XmlMarkup.new(:target => target, :indent => 2)
    Buildr::IntellijIdea.new_document(target.string).root
  end
end
base_directory() click to toggle source
# File lib/buildr/ide/idea.rb, line 69
def base_directory
  buildr_project.path_to
end
components() click to toggle source
# File lib/buildr/ide/idea.rb, line 99
def components
  @components ||= []
end
create_component(name, attrs = {}) { |xml| ... } click to toggle source
# File lib/buildr/ide/idea.rb, line 91
def create_component(name, attrs = {})
  target = StringIO.new
  Builder::XmlMarkup.new(:target => target, :indent => 2).component({:name => name}.merge(attrs)) do |xml|
    yield xml if block_given?
  end
  Buildr::IntellijIdea.new_document(target.string).root
end
create_composite_component(name, attrs, components) click to toggle source
# File lib/buildr/ide/idea.rb, line 103
def create_composite_component(name, attrs, components)
  return nil if components.empty?
  component = self.create_component(name, attrs)
  components.each do |element|
    element = element.call if element.is_a?(Proc)
    component.add_element element
  end
  component
end
document() click to toggle source
# File lib/buildr/ide/idea.rb, line 125
def document
  if File.exist?(self.filename)
    doc = load_document(self.filename)
  else
    doc = base_document
    inject_components(doc, self.initial_components)
  end
  if self.template
    template_doc = load_document(self.template)
    REXML::XPath.each(template_doc, "//component") do |element|
      inject_component(doc, element)
    end
  end
  inject_components(doc, self.default_components.compact + self.components)

  # Sort the components in the same order the idea sorts them
  sorted = doc.root.get_elements('//component').sort { |s1, s2| s1.attribute('name').value <=> s2.attribute('name').value }
  doc = base_document
  sorted.each do |element|
    doc.root.add_element element
  end

  doc
end
file_path(path) click to toggle source
# File lib/buildr/ide/idea.rb, line 87
def file_path(path)
  "file://#{resolve_path(path)}"
end
inject_component(doc, component) click to toggle source

replace overridden component (if any) with specified component

# File lib/buildr/ide/idea.rb, line 159
def inject_component(doc, component)
  doc.root.delete_element("//component[@name='#{component.attributes['name']}']")
  doc.root.add_element component
end
inject_components(doc, components) click to toggle source
# File lib/buildr/ide/idea.rb, line 150
def inject_components(doc, components)
  components.each do |component|
    # execute deferred components
    component = component.call if Proc === component
    inject_component(doc, component) if component
  end
end
load_document(filename) click to toggle source
# File lib/buildr/ide/idea.rb, line 121
def load_document(filename)
  Buildr::IntellijIdea.new_document(File.read(filename))
end
name() click to toggle source
# File lib/buildr/ide/idea.rb, line 61
def name
  "#{self.id}#{suffix}"
end
relative(path) click to toggle source
# File lib/buildr/ide/idea.rb, line 65
def relative(path)
  ::Buildr::Util.relative_path(File.expand_path(path.to_s), self.base_directory)
end
resolve_path_from_base(path, base_variable) click to toggle source
# File lib/buildr/ide/idea.rb, line 73
def resolve_path_from_base(path, base_variable)
  m2repo = Buildr::Repositories.instance.local
  if path.to_s.index(m2repo) == 0 && !self.local_repository_env_override.nil?
    return path.sub(m2repo, "$#{self.local_repository_env_override}$")
  else
    begin
      return "#{base_variable}/#{relative(path)}"
    rescue ArgumentError
      # ArgumentError happens on windows when self.base_directory and path are on different drives
      return path
    end
  end
end