Skip to content

Instantly share code, notes, and snippets.

@gregoriokusowski
Created April 27, 2012 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gregoriokusowski/2508888 to your computer and use it in GitHub Desktop.
Save gregoriokusowski/2508888 to your computer and use it in GitHub Desktop.
Script to compare apps. ( Used when creating a engine to avoid duplicate code on two projects. )
class Comparer
DEFAULT_CONFIG = {
folders: [:app, :lib],
formats: [:erb, :rb, :js, :css, :coffee]
}
end
class Application
def initialize app_path
Comparer::DEFAULT_CONFIG[:folders].each do |folder|
add_files_from "#{app_path}/#{folder}"
end
end
def add_files_from dir
entries = Dir.entries dir
entries.each do |entry|
if entry != "." and entry != ".."
file_path = "#{dir}/#{entry}"
if File.directory? file_path
add_files_from file_path
elsif Comparer::DEFAULT_CONFIG[:formats].any? { |format| file_path.end_with? ".#{format}" }
add_single_file file_path
end
end
end
end
def add_single_file file_path
files[file_path] = File.read(file_path).chomp.gsub(" ", "").gsub("\n", "").gsub("\t", "")
end
def files
@files ||= {}
end
end
app_one = Application.new("path to app 1")
app_two = Application.new("path to app 2")
app_one.files.each do |path, content|
app_two.files.each do |tpath, tcontent|
if content == tcontent
puts "Match: #{path} && #{tpath}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment