Skip to content
Browse files

initial commit

  • Loading branch information...
0 parents commit aafca06e09067f0865dcb1f31632e7e9123a3f6a @technoweenie committed Jan 25, 2008
1 .gitignore
@@ -0,0 +1 @@
+.DS_Store
13 Rakefile
@@ -0,0 +1,13 @@
+#!/usr/bin/env ruby
+
+require 'rubygems'
+require 'rake'
+require 'spec/rake/spectask'
+
+task :default => 'spec'
+
+desc "Run specifications"
+Spec::Rake::SpecTask.new('spec') do |t|
+ t.spec_opts = ["--format", "specdoc", "--colour"]
+ t.spec_files = FileList[(ENV['FILES'] || 'spec/**/*_spec.rb')]
+end
54 lib/duplikate.rb
@@ -0,0 +1,54 @@
+require 'pathname'
+class Duplikate
+ attr_accessor :source, :destination
+ attr_reader :deleted_files, :deleted_directories, :added_files, :added_directories, :existing_files
+
+ def initialize(source, dest, is_inverse=false)
+ @source, @destination = Pathname.new(source), Pathname.new(dest)
+ @inverse = self.class.new(dest, source, true) unless is_inverse
+ end
+
+ def process(&block)
+ @deleted_files, @deleted_directories, @added_files, @added_directories, @existing_files = [], [], [], [], []
+ if @inverse
+ @inverse.process
+ @deleted_files = @inverse.added_files
+ @deleted_directories = @inverse.added_directories
+ end
+ process_path(&block)
+ end
+
+protected
+ def process_path(path = nil, &block)
+ unless path.nil?
+ dest_entry = @destination + path
+ unless dest_entry.directory?
+ @added_directories << path.to_s
+ return
+ end
+ end
+
+ (path.nil? ? @source : @source + path).each_entry do |entry|
+ next if entry.to_s =~ /^\.(git|svn)?\.?$/
+ full_entry = path.nil? ? entry : path + entry
+ source_entry = @source + full_entry
+ if source_entry.directory?
+ process_path(full_entry, &block)
+ elsif source_entry.file?
+ process_file(full_entry, &block)
+ end
+ end
+ end
+
+ def process_file(file, &block)
+ if (@destination + file).file?
+ if block
+ block.call file.to_s
+ else
+ @existing_files << file.to_s
+ end
+ else
+ @added_files << file.to_s
+ end
+ end
+end
0 spec/dest/deleteme.txt
No changes.
1 spec/dest/foo/deleteme/same.txt
@@ -0,0 +1 @@
+same
1 spec/dest/foo/same.txt
@@ -0,0 +1 @@
+same
1 spec/dest/foo/same/changed.txt
@@ -0,0 +1 @@
+changed
1 spec/dest/same.txt
@@ -0,0 +1 @@
+same
46 spec/duplikate_spec.rb
@@ -0,0 +1,46 @@
+require File.join(File.dirname(__FILE__), 'spec_helper')
+
+describe Duplikate do
+ before do
+ @source = File.join(File.dirname(__FILE__), 'source')
+ @dest = File.join(File.dirname(__FILE__), 'dest')
+ @duplikate = Duplikate.new @source, @dest
+ @duplikate.process
+ end
+
+ it "gets #source location as Pathname" do
+ @duplikate.source.should == Pathname.new(@source)
+ end
+
+ it "gets #destination location as Pathname" do
+ @duplikate.destination.should == Pathname.new(@dest)
+ end
+
+ it "processes added_directories" do
+ @duplikate.should have(1).added_directories
+ @duplikate.added_directories.should include("foo/addme")
+ end
+
+ it "processes added_files" do
+ @duplikate.should have(1).added_files
+ @duplikate.added_files.should include("addme.txt")
+ end
+
+ it "processes existing_files" do
+ @duplikate.should have(3).existing_files
+ @duplikate.existing_files.should include("same.txt")
+ @duplikate.existing_files.should include("foo/same.txt")
+ @duplikate.existing_files.should include("foo/same/changed.txt")
+ end
+
+ it "processes deleted_directories" do
+ @duplikate.should have(2).deleted_directories
+ @duplikate.deleted_directories.should include("deleteme")
+ @duplikate.deleted_directories.should include("foo/deleteme")
+ end
+
+ it "processes deleted_files" do
+ @duplikate.should have(1).deleted_files
+ @duplikate.deleted_files.should include("deleteme.txt")
+ end
+end
1 spec/source/addme.txt
@@ -0,0 +1 @@
+adding...
1 spec/source/foo/addme/same.txt
@@ -0,0 +1 @@
+same
1 spec/source/foo/same.txt
@@ -0,0 +1 @@
+same
1 spec/source/foo/same/changed.txt
@@ -0,0 +1 @@
+same
1 spec/source/same.txt
@@ -0,0 +1 @@
+same
12 spec/spec_helper.rb
@@ -0,0 +1,12 @@
+require 'rubygems'
+require 'spec'
+
+begin
+ require 'ruby-debug'
+ Debugger.start
+rescue LoadError
+ # no debugging
+end
+
+$LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
+require 'duplikate'

0 comments on commit aafca06

Please sign in to comment.
Something went wrong with that request. Please try again.