<commit_before>module Buildr
  module IntellijIdea
    # Abstract base class for IdeaModule and IdeaProject
    class IdeaFile
      DEFAULT_SUFFIX = ""

      attr_reader :buildr_project
      attr_writer :suffix
      attr_writer :id
      attr_accessor :template

      def suffix
        @suffix ||= DEFAULT_SUFFIX
      end

      def filename
        buildr_project.path_to("#{name}.#{extension}")
      end

      def id
        @id ||= buildr_project.name.split(':').last
      end

      def add_component(name, attrs = {}, &xml)
        self.components << create_component(name, attrs, &xml)
      end

      def write(f)
        document.write f
      end

      protected

      def name
        "#{self.id}#{suffix}"
      end

      def create_component(name, attrs = {})
        target = StringIO.new
        Builder::XmlMarkup.new(:target => target, :indent => 2).component(attrs.merge({ :name => name })) do |xml|
          yield xml if block_given?
        end
        REXML::Document.new(target.string).root
      end

      def components
        @components ||= self.default_components
      end

      def load_document(filename)
        REXML::Document.new(File.read(filename))
      end

      def document
        doc = nil
        doc = load_document(self.template) if self.template
        doc = load_document(self.filename) if (doc.nil? && File.exist?(self.filename))
        doc = base_document if doc.nil?
        # replace overridden components, if any
        self.components.each do |comp_elt|
          # execute deferred components
          comp_elt = comp_elt.call if Proc === comp_elt
          if comp_elt
            doc.root.delete_element("//component[@name='#{comp_elt.attributes['name']}']")
            doc.root.add_element comp_elt
          end
        end
        doc
      end
    end
  end
end<commit_msg>Change templates so that component sections are populated using the following mechanisms; * generated * from template file (if specified) * from existing file (if present)<commit_after>module Buildr
  module IntellijIdea
    # Abstract base class for IdeaModule and IdeaProject
    class IdeaFile
      DEFAULT_SUFFIX = ""

      attr_reader :buildr_project
      attr_writer :suffix
      attr_writer :id
      attr_accessor :template

      def suffix
        @suffix ||= DEFAULT_SUFFIX
      end

      def filename
        buildr_project.path_to("#{name}.#{extension}")
      end

      def id
        @id ||= buildr_project.name.split(':').last
      end

      def add_component(name, attrs = {}, &xml)
        self.components << create_component(name, attrs, &xml)
      end

      def write(f)
        document.write f
      end

      protected

      def name
        "#{self.id}#{suffix}"
      end

      def create_component(name, attrs = {})
        target = StringIO.new
        Builder::XmlMarkup.new(:target => target, :indent => 2).component(attrs.merge({ :name => name })) do |xml|
          yield xml if block_given?
        end
        REXML::Document.new(target.string).root
      end

      def components
        @components ||= self.default_components
      end

      def load_document(filename)
        REXML::Document.new(File.read(filename))
      end

      def document
        doc = (File.exist?(self.filename)) ? load_document(self.filename) : base_document 
        if self.template
          template_doc = load_document(self.template)
          REXML::XPath.each(template_doc, "//component") do |element|
            inject_component(doc, element)
          end
        end
        self.components.each do |comp_elt|
          # execute deferred components
          comp_elt = comp_elt.call if Proc === comp_elt
          inject_component(doc, comp_elt) if comp_elt
        end
        doc
      end

      # replace overridden component (if any) with specified component
      def inject_component(doc, component)
        doc.root.delete_element("//component[@name='#{component.attributes['name']}']")
        doc.root.add_element component
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.09603545924649101, 'fraction_numerical': 0.0004924895345973898, 'mean_word_length': 2.1858823529411766, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6524100', 'n_tokens_mistral': 1172, 'n_tokens_neox': 1140, 'n_words': 288}