Sha256: 0703488f9061f450f234f31fa3353210bec93ef2158cd1ea7b05cb66f08406f7

Contents?: true

Size: 1.64 KB

Versions: 1

Compression:

Stored size: 1.64 KB

Contents

require 'rexml/document'

module Rake
  module Funnel
    module Support
      class SpecsRemover
        class << self
          DEFAULTS = {
            projects: [],
            references: [],
            specs: []
          }

          def remove(args = {})
            args = DEFAULTS.merge(args)

            projects(args).each do |project|
              xml = REXML::Document.new(File.read(project), { attribute_quote: :quote })

              removed = remove_references(args, xml) + remove_specs(args, xml)

              write_xml(project, xml) if removed.flatten.any?
            end

            delete_specs(args)
          end

          private
          def write_xml(project, xml)
            File.open(project, 'w+') do |file|
              xml.write(output: file, ie_hack: true)
            end
          end

          def delete_specs(args)
            Dir[*args[:specs]].uniq.each do |spec|
              RakeFileUtils.rm(spec)
            end
          end

          def projects(args)
            Dir[*args[:projects]]
          end

          def list(args)
            ([] << args).flatten
          end

          def remove_references(args, xml)
            list(args[:references]).map do |ref|
              query = "/Project//Reference[starts-with(lower-case(@Include), '#{ref.downcase}')]"
              xml.elements.delete_all(query)
            end
          end

          def remove_specs(args, xml)
            list(args[:specs]).map do |glob|
              query = "/Project//Compile[matches(lower-case(@Include), '#{glob}')]"
              xml.elements.delete_all(query)
            end
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rake-funnel-0.4.0.pre lib/rake/funnel/support/specs_remover.rb