stowaway 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/TODO.txt CHANGED
@@ -2,7 +2,6 @@
2
2
 
3
3
  * add verbose option to output file names of the files that weren't found and
4
4
  the files that were only partially matched on name.
5
- * ignore vendor folder
6
- * ignore spec folder
5
+ * centralize ignore patterns
7
6
  * DRY options.rb
8
7
  * specify runner.rb
data/VERSION.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  ---
2
2
  :major: 0
3
3
  :minor: 2
4
- :patch: 0
4
+ :patch: 2
5
5
  :build:
data/lib/stowaway/file.rb CHANGED
@@ -1,16 +1,16 @@
1
1
  module Stowaway
2
2
  class FileObj
3
3
  attr :fullpath
4
-
4
+
5
5
  def initialize(fullpath, root = nil)
6
6
  @fullpath = fullpath
7
7
  @root = root || Dir.pwd
8
8
  end
9
-
9
+
10
10
  def name
11
11
  File.basename(@fullpath)
12
12
  end
13
-
13
+
14
14
  def path
15
15
  File.split(@fullpath)[0]
16
16
  end
@@ -19,7 +19,7 @@ module Stowaway
19
19
  root = @root.split("/").last
20
20
  @fullpath.sub(/^.+\/#{root}/, "")
21
21
  end
22
-
22
+
23
23
  def ==(fileObj)
24
24
  self.fullpath == fileObj.fullpath
25
25
  end
@@ -1,10 +1,33 @@
1
- module FSHelpyHelp
2
- def ignore?(file)
3
- @ignore.each do |exp|
4
- if file.match(exp)
5
- return true
1
+ module Stowaway
2
+ module FSHelpyHelp
3
+ def ignore?(file)
4
+ @ignore.each do |exp|
5
+ if file.match(exp)
6
+ return true
7
+ end
8
+ end
9
+ false
10
+ end
11
+
12
+ def ignore_special_directories(root)
13
+ @ignore << "/#{root}\/test$|spec$|vendor$|features$"
14
+ end
15
+
16
+ def recursively(path, &block)
17
+ dir = Dir.new(path)
18
+
19
+ dir.each do |f|
20
+ next if ignore?(dir.path)
21
+ next if ignore?(f)
22
+
23
+ file_p = File.join(dir.path, f)
24
+
25
+ if File.directory?(file_p)
26
+ recursively(file_p, &block)
27
+ else
28
+ yield(file_p)
29
+ end
6
30
  end
7
31
  end
8
- false
9
32
  end
10
33
  end
@@ -10,23 +10,18 @@ module Stowaway
10
10
  @ignore = [/^\./]
11
11
  end
12
12
 
13
- def find_all(path, files = [])
14
- @root = path if @root.nil?
15
-
16
- dir = Dir.new(path)
17
-
18
- dir.each do |f|
19
- next if ignore?(f)
20
-
21
- file = File.join(dir.path, f)
22
-
23
- if File.directory?(file)
24
- find_all file, files
25
- elsif type?(f)
26
- files << FileObj.new(file, @root)
27
- end
13
+ def find_all(path)
14
+ @root = path
15
+ @files = []
16
+ ignore_special_directories(@root)
17
+ recursively(path) do |file_p|
18
+ push_if_ext_match(file_p)
28
19
  end
29
- files
20
+ @files
21
+ end
22
+
23
+ def push_if_ext_match(file_p)
24
+ @files << FileObj.new(file_p, @root) if type?(file_p)
30
25
  end
31
26
 
32
27
  def type?(file)
@@ -20,40 +20,32 @@ module Stowaway
20
20
  @ignore << /^\.|\.jpg$|\.jpeg$|\.gif$|\.png$|\.ico$|\.bmp$/i
21
21
  end
22
22
 
23
- def sweep(path, files=nil)
24
- @root ||= path
25
- @result ||= OpenStruct.new({ :files => files, :name_only_matches => []})
26
-
27
- dir = Dir.new(path)
28
-
29
- dir.each do |f|
30
- next if ignore?(f)
31
-
32
- file = File.join(dir.path, f)
33
-
34
- if File.directory?(file)
35
- sweep(file)
36
- else
37
- inspect_file(file)
38
- end
39
- end
23
+ def sweep(path, files)
24
+ @root = path
25
+ @result = OpenStruct.new({ :files => files, :name_only_matches => []})
26
+ ignore_special_directories(@root)
27
+ recursively(path) { |fp| inspect_file(fp) }
40
28
  @result
41
29
  end
42
30
 
43
31
  private
44
32
 
45
- def inspect_file(file)
46
- root = @root.split("/").last
47
- file_name = file.sub(/^.+\/(#{root})/, "")
48
- clr_print(" => #{file_name}")
49
- File.open(file, "r") do |i|
33
+ def inspect_file(file_p)
34
+ clr_print(" => #{path_relative_to_root(file_p)}")
35
+ File.open(file_p, "r") do |i|
50
36
  while line = i.gets
37
+ next unless line.valid_encoding?
51
38
  remove_match(line) #rescue nil
52
39
  end
53
40
  end
54
41
  flush
55
42
  end
56
43
 
44
+ def path_relative_to_root(file_p)
45
+ root = @root.split("/").last
46
+ file_p.sub(/^.+\/(#{root})/, "")
47
+ end
48
+
57
49
  def remove_match(line)
58
50
  @result.files.delete_if do |file|
59
51
  if @matcher.match?(line, file)
@@ -0,0 +1,10 @@
1
+ class IOMock
2
+ def initialize
3
+ @counter = 0
4
+ end
5
+ def gets
6
+ return nil if @counter > 0
7
+ @counter += 1
8
+ "\xA9".force_encoding("utf-8")
9
+ end
10
+ end
Binary file
@@ -2,6 +2,7 @@ require "spec/spec_helper"
2
2
  require "lib/stowaway/file"
3
3
  require "lib/stowaway/sweeper"
4
4
  require "lib/stowaway/matcher"
5
+ require "spec/lib/io_mock.rb"
5
6
 
6
7
  describe Stowaway::Sweeper do
7
8
 
@@ -34,7 +35,7 @@ describe Stowaway::Sweeper do
34
35
  files.length.should == 1
35
36
  end
36
37
 
37
- it "should output a message when sweeping through a file" do
38
+ it "should print the path to the file (relative to root) being swept through" do
38
39
  @ignore << /testfile2/
39
40
  sweeper.should_receive(:clr_print).once.with(" => /testfile1.txt")
40
41
  sweeper.sweep("spec/data", [])
@@ -64,5 +65,12 @@ describe Stowaway::Sweeper do
64
65
  files.should_not be_empty
65
66
  files.first.fullpath.should == "/a/stowaway.txt"
66
67
  end
67
-
68
+
69
+ it "should ignore lines with invalid encoding" do
70
+ files = [Stowaway::FileObj.new("/fake/path1/button.jpg")]
71
+ File.stub!(:open).and_yield(IOMock.new)
72
+ sweeper.should_not_receive(:remove_match)
73
+ sweeper.sweep("spec/data", files)
74
+ end
75
+
68
76
  end
data/stowaway.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{stowaway}
8
- s.version = "0.2.0"
8
+ s.version = "0.2.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Emilio Cavazos"]
12
- s.date = %q{2010-02-26}
12
+ s.date = %q{2010-03-04}
13
13
  s.default_executable = %q{stowaway}
14
14
  s.email = %q{ejcavazos@gmail.com}
15
15
  s.executables = ["stowaway"]
@@ -36,6 +36,7 @@ Gem::Specification.new do |s|
36
36
  "spec/data/testfile1.txt",
37
37
  "spec/data/testfile2.txt",
38
38
  "spec/lib/file_spec.rb",
39
+ "spec/lib/io_mock.rb",
39
40
  "spec/lib/locator_spec.rb",
40
41
  "spec/lib/matcher_spec.rb",
41
42
  "spec/lib/options_spec.rb",
@@ -53,6 +54,7 @@ Gem::Specification.new do |s|
53
54
  s.summary = %q{Locate files in a web project that aren't being used.}
54
55
  s.test_files = [
55
56
  "spec/lib/file_spec.rb",
57
+ "spec/lib/io_mock.rb",
56
58
  "spec/lib/locator_spec.rb",
57
59
  "spec/lib/matcher_spec.rb",
58
60
  "spec/lib/options_spec.rb",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stowaway
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emilio Cavazos
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-26 00:00:00 -08:00
12
+ date: 2010-03-04 00:00:00 -08:00
13
13
  default_executable: stowaway
14
14
  dependencies: []
15
15
 
@@ -41,6 +41,7 @@ files:
41
41
  - spec/data/testfile1.txt
42
42
  - spec/data/testfile2.txt
43
43
  - spec/lib/file_spec.rb
44
+ - spec/lib/io_mock.rb
44
45
  - spec/lib/locator_spec.rb
45
46
  - spec/lib/matcher_spec.rb
46
47
  - spec/lib/options_spec.rb
@@ -80,6 +81,7 @@ specification_version: 3
80
81
  summary: Locate files in a web project that aren't being used.
81
82
  test_files:
82
83
  - spec/lib/file_spec.rb
84
+ - spec/lib/io_mock.rb
83
85
  - spec/lib/locator_spec.rb
84
86
  - spec/lib/matcher_spec.rb
85
87
  - spec/lib/options_spec.rb