lib/rake/funnel/support/specs_remover.rb in rake-funnel-0.5.0.pre vs lib/rake/funnel/support/specs_remover.rb in rake-funnel-0.6.0.pre

- old
+ new

@@ -1,79 +1,135 @@ +require 'pathname' require 'rexml/document' module Rake module Funnel module Support class SpecsRemover - class << self - def remove(args = {}) - remove_specs_from_projects(args) - delete_test_files(args) - remove_paket_references(args) - end + class ProjectFiles + class << self + def remove_specs_and_references(projects, references, specs) + deleted = remove(projects, references, specs) - private - def remove_specs_from_projects(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) + projects = deleted.map { |e| e[:project] }.flatten.uniq + packages = deleted.map { |e| e[:packages] }.flatten.uniq + specs = deleted.map { |e| e[:specs] }.flatten.uniq - write_xml(project, xml) if removed.flatten.any? + [projects, packages, specs] end - end - def remove_paket_references(args) - paket_references(args).each do |references| - text = File.read(references) - removed = remove_packages(text, args) + private + def remove(projects, references, specs) + Dir[*projects].map do |project| + xml = REXML::Document.new(File.read(project), attribute_quote: :quote) + references = remove_references(references, xml) + specs = remove_specs(specs, xml) - File.write(references, removed) if removed != text + save(project, xml) if (references + specs).any? + + { + project: project, + packages: resolve_package_names(project, references), + specs: resolve_paths(project, specs) + } + end end - end - def delete_test_files(args) - Dir[*list(args[:specs])].uniq.each do |spec| - RakeFileUtils.rm(spec) + def remove_references(references, xml) + deleted = references.map do |ref| + query = "/Project//Reference[starts-with(lower-case(@Include), '#{ref.downcase}')]" + xml.elements.delete_all(query) + end + + deleted.flatten.map { |d| + d.get_elements('/HintPath').map(&:text) + }.flatten end - end - def list(args) - ([] << args).flatten.compact - end + def remove_specs(specs, xml) + deleted = specs.map do |glob| + query = "/Project//Compile[matches(lower-case(@Include), '#{glob}')]" + xml.elements.delete_all(query) + end - def projects(args) - Dir[*list(args[:projects])] - end + deleted.flatten.map { |d| d.attributes['Include'] } + end - def write_xml(project, xml) - File.open(project, 'w+') do |file| - xml.write(output: file, ie_hack: true) + def save(project, xml) + File.open(project, 'w+') do |file| + xml.write(output: file, ie_hack: true) + end end - 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) + def resolve_package_names(project, references) + references.map { |r| package_for(project, r) }.compact end + + def package_for(project, reference) + path = File.expand_path(File.join(project, reference)) + + Pathname.new(path).ascend do |p| + break p.parent.basename.to_s if p.basename.to_s == 'lib' + end + end + + def resolve_paths(project, files) + files.map { |f| File.expand_path(File.join(File.dirname(project), f)) } + end 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) + class PaketReferences + class << self + def remove_packages(projects, packages) + projects.each do |project| + references = paket_references_for(project) + next unless references + + text = File.read(references) + removed = remove(text, packages) + + File.write(references, removed) if removed != text + end end + + private + def paket_references_for(project) + project_specific = project + '.paket.references' + global = File.join(File.dirname(project), 'paket.references') + + [project_specific, global].select { |f| File.exist?(f) }.first + end + + def remove(text, packages) + packages.each do |package| + text = text.gsub(/^#{package}.*\n?/i, '') + end + text + end end + end - def paket_references(args) - Dir[*list(args[:paket_references])] + class << self + def remove(args = {}) + projects, packages, specs = ProjectFiles.remove_specs_and_references(list(args[:projects]), + list(args[:references]), + list(args[:specs])) + + delete(specs) + + PaketReferences.remove_packages(projects, list(args[:packages]) + packages) end - def remove_packages(text, args) - list(args[:packages]).each do |package| - text = text.gsub(/^#{package}.*\n?/i, '') + private + def list(args) + ([] << args).flatten.compact + end + + def delete(files) + files.each do |file| + RakeFileUtils.rm(file) if File.exist?(file) end - text end end end end end