stowaway 0.1.6 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,10 +1,22 @@
1
1
  # Stowaway
2
2
 
3
- Stowaway is a gem that searches a web project for files that aren't being used. By default it will search in the directory of your choice for files that have the following extension:
3
+ Stowaway will search through the source code of a web project and find all
4
+ of the images, scripts and stylesheets that aren't being used. To use,
5
+ simply provide the path to the directory you wish to search.
4
6
 
5
- * .jpg
6
- * .gif
7
- * .png
7
+ ***Please read the _warning_ section at the end before using***
8
+
9
+ ## Example
10
+ stowaway ~/repos/rails/my_rails_app/
11
+
12
+ Stowaway, by default, will search for the most common file types. Using the
13
+ **-t** flag, you can tell stowaway to use custom file types.
14
+
15
+ ## Default Types
16
+
17
+ * .jpg
18
+ * .gif
19
+ * .png
8
20
  * .ico
9
21
  * .js
10
22
  * .css
@@ -13,6 +25,35 @@ Stowaway is a gem that searches a web project for files that aren't being used.
13
25
 
14
26
  sudo gem install stowaway
15
27
 
28
+ ## Matching
29
+
30
+ Stowaway assumes the path you provide is the root for your web application
31
+ and will have no problem matching root-relative links to the actual files.
32
+
33
+ The example below will result in a positive match:
34
+
35
+ # path to file: /my_app/images/foo.jpg
36
+ # image tag: <img src="/images/foo.jpg" />
37
+
38
+ Stowaway currently recognizes the following as valid references:
39
+
40
+ src="/file.jpg"
41
+ href="/file.jpg"
42
+ :src => "file.jpg"
43
+ :href => "file.jpg"
44
+ = javascript_include_tag "file"
45
+ = javascript_include_tag "file.js"
46
+ = javascript_include_tag "file.js", "foo/bar.js"
47
+ = stylesheet_link_tag "file"
48
+ = stylesheet_link_tag "file.css"
49
+ = stylesheet_link_tag "file.css", "foo/bar.css"
50
+ url("/file.jpg")
51
+
52
+ ## Partial Matching
53
+
54
+ As a fall back, stowaway will match on file name. It will
55
+ report the number of files that were matched by name only at the end of a run.
56
+
16
57
  ## Usages
17
58
 
18
59
  # basic usage
@@ -24,7 +65,11 @@ Stowaway is a gem that searches a web project for files that aren't being used.
24
65
  # with file types
25
66
  stowaway -t .js .css path/to/site
26
67
 
27
- ## Note
68
+ ## Warning
28
69
 
29
- I'll be adding support for matching on relative and absolute paths (Comparing by path will preserve the uniqueness of the file). Currently, only file names are being checked for matches. If you have two files with the same name but they reside in different directories, they will be treated as one. In other words, if you have multiple files with the *same name*, stowaway won't work right.
70
+ Stowaway is useful for maintaining a tidy source tree, but it's not perfect.
71
+ I probably don't need to say this, but a good VCS is mandatory. So before you
72
+ start deleting files, make sure all your awesome changes have been committed and
73
+ that you double-check stowaway's result against your own manual process.
30
74
 
75
+ This gem only works on ruby 1.9 ATM
data/VERSION.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  ---
2
2
  :major: 0
3
3
  :minor: 1
4
- :patch: 6
4
+ :patch: 8
5
5
  :build:
@@ -38,7 +38,7 @@ module Stowaway
38
38
 
39
39
  def rails_helper_ref?(helper_name, directory, extension)
40
40
  expression = Regexp.new(/=\s?(%s_tag)?\s(["|'])(.+)(\2)/.to_s % helper_name)
41
- return false unless @line =~ expression
41
+ return false if @line !~ expression
42
42
  params = $3.gsub(/[\s|"|']/, "").split(",")
43
43
  params.each do |f|
44
44
  return true if "/public/#{directory}/#{f}" == @file.root_path ||
@@ -22,19 +22,19 @@ module Stowaway
22
22
 
23
23
  private
24
24
 
25
- def respond(results)
26
- if results[:files_to_find].empty?
25
+ def respond(result)
26
+ if result.files.empty?
27
27
  print "Zero stowaways found. You run a tight ship.\n\n"
28
28
  else
29
- print "\nYou have #{results[:files_to_find].length} stowaway(s) ... shameful\n\n"
29
+ print "\nYou have #{result.files.length} stowaway(s) ... shameful\n\n"
30
30
 
31
- unless results[:name_only].empty?
32
- p "Warning: #{results[:name_only].length} file(s) partially matched on name only"
31
+ unless result.name_only_matches.empty?
32
+ p "Warning: #{result.name_only_matches.length} file(s) partially matched on name only"
33
33
  end
34
34
 
35
35
  60.times { print "-" }
36
36
  print "\n\n"
37
- results[:files_to_find].each_with_index { |f, i| print "#{i+1}: #{f.root_path}\n" }
37
+ result.files.each_with_index { |f, i| print "#{i+1}: #{f.root_path}\n" }
38
38
  print "\n"
39
39
  end
40
40
  end
@@ -2,16 +2,18 @@ require_relative "fshelpyhelp"
2
2
  require_relative "status"
3
3
  require_relative "matcher"
4
4
  require_relative "file"
5
+ require "ostruct"
5
6
 
6
7
  module Stowaway
7
8
  class Sweeper
8
9
  include FSHelpyHelp
9
10
 
10
- def initialize(files_to_find, status = Status.new, ext_to_ignore = nil)
11
- @results = { :files_to_find => files_to_find, :name_only => []}
12
- @ignore = ext_to_ignore || [/^\.|\.jpg$|\.gif$|.png$|.ico$/i]
11
+ def initialize(files, status = Status.new, matcher = Matcher.new, ext_to_ignore = [])
12
+ @result = OpenStruct.new({ :files => files, :name_only_matches => []})
13
13
  @status = status
14
- @matcher = Matcher.new
14
+ @matcher = matcher
15
+ @ignore = ext_to_ignore || []
16
+ @ignore += [/^\.|\.jpg$|\.jpeg$|\.gif$|\.png$|\.ico$|\.bmp$/i]
15
17
  end
16
18
 
17
19
  def sweep(path)
@@ -28,7 +30,7 @@ module Stowaway
28
30
  inspect_file(file)
29
31
  end
30
32
  end
31
- @results
33
+ @result
32
34
  end
33
35
 
34
36
  private
@@ -44,11 +46,11 @@ module Stowaway
44
46
  end
45
47
 
46
48
  def remove_match(line)
47
- @results[:files_to_find].delete_if do |file|
49
+ @result.files.delete_if do |file|
48
50
  if @matcher.match?(line, file)
49
51
  true
50
52
  elsif line.include?(file.name)
51
- @results[:name_only] << file
53
+ @result.name_only_matches << file
52
54
  true
53
55
  end
54
56
  end
@@ -1,12 +1,12 @@
1
1
  require "spec/spec_helper"
2
2
  require "lib/stowaway/file"
3
3
  require "lib/stowaway/sweeper"
4
+ require "lib/stowaway/matcher"
4
5
 
5
6
  describe Stowaway::Sweeper do
6
7
 
7
8
  def sweeper(ignore = nil)
8
- ig = ignore || [/^\./]
9
- @sweeper ||= Stowaway::Sweeper.new(@files, @status_mock, ig)
9
+ @sweeper ||= Stowaway::Sweeper.new(@files, @status_mock, Stowaway::Matcher.new, ignore)
10
10
  end
11
11
 
12
12
  before(:each) do
@@ -14,83 +14,22 @@ describe Stowaway::Sweeper do
14
14
  @status_mock = mock("status_mock", :null_object => true)
15
15
  end
16
16
 
17
- it "should match images referenced in a src attribute" do
17
+ it "should remove files when a reference to the file is found in source" do
18
18
  @files << Stowaway::FileObj.new("/fake/path1/button.jpg")
19
19
  sweeper.sweep("spec/data")
20
20
  @files.should be_empty
21
21
  end
22
-
23
- it "should match images referenced in an href attribute" do
24
- @files << Stowaway::FileObj.new("/fake/path/photo.jpg")
25
- sweeper.sweep("spec/data")
26
- @files.should be_empty
27
- end
28
-
29
- it "should match images referenced in a src attribute when files are stored in public folder" do
30
- @files << Stowaway::FileObj.new("/public/fake/path1/button.jpg")
31
- sweeper.sweep("spec/data")
32
- @files.should be_empty
33
- end
34
-
35
- it "should match images referenced in an haml href attribute" do
36
- @files << Stowaway::FileObj.new("/images/haml.jpg")
37
- sweeper.sweep("spec/data")
38
- @files.should be_empty
39
- end
40
-
41
- it "should match images referenced in a haml src attribute" do
42
- @files << Stowaway::FileObj.new("/images/file.jpg")
43
- sweeper.sweep("spec/data")
44
- @files.should be_empty
45
- end
46
-
47
- it "should match images referenced in a haml src attribute when files are stored in public folder" do
48
- @files << Stowaway::FileObj.new("/public/images/file.jpg")
49
- sweeper.sweep("spec/data")
50
- @files.should be_empty
51
- end
52
22
 
53
- it "should match scripts referenced in a src attribute" do
54
- @files << Stowaway::FileObj.new("/fake/path/file.js")
55
- sweeper.sweep("spec/data")
56
- @files.should be_empty
57
- end
58
-
59
- it "should match scripts referenced in a rails javascript_include_tag helper" do
60
- @files << Stowaway::FileObj.new("/public/javascripts/file.js")
61
- sweeper([/^\.|\.rb$|testfile2/]).sweep("spec/data")
62
- @files.should be_empty
63
- end
64
-
65
- it "should match scripts referenced in a rails javascript_include_tag helper when no extension is given" do
66
- @files << Stowaway::FileObj.new("/public/javascripts/application.js")
67
- sweeper([/^\.|\.rb$|testfile2/]).sweep("spec/data")
68
- @files.should be_empty
69
- end
70
-
71
- it "should match multiple scripts referenced in a rails javascript_include_tag helper" do
72
- @files << Stowaway::FileObj.new("/public/javascripts/jquery.js")
73
- @files << Stowaway::FileObj.new("/public/javascripts/home/index.js")
74
- sweeper([/^\.|\.rb$|testfile2/]).sweep("spec/data")
75
- @files.should be_empty
76
- end
77
-
78
- it "should match css files referenced in a rails stylesheet_link_tag helper" do
79
- @files << Stowaway::FileObj.new("/public/stylesheets/file.css")
80
- sweeper([/^\.|\.rb$|testfile2/]).sweep("spec/data")
81
- @files.should be_empty
82
- end
83
-
84
- it "should match multiple css files referenced in a rails stylesheet_link_tag helper" do
85
- @files << Stowaway::FileObj.new("/public/stylesheets/reset.css")
86
- @files << Stowaway::FileObj.new("/public/stylesheets/common.css")
87
- sweeper([/^\.|\.rb$|testfile2/]).sweep("spec/data")
88
- @files.should be_empty
23
+ it "should return an OpenStruct with the result of the sweeping" do
24
+ result = sweeper.sweep("spec/data")
25
+ result.files.should be_an_instance_of Array
26
+ result.name_only_matches.should be_an_instance_of Array
89
27
  end
90
28
 
91
29
  it "should not sweep through ignored file types" do
92
- # @files << Stowaway::FileObj.new("/public/stylesheets/reset.css")
93
- # sweeper([/^\.|\.rb$|testfile1/]).sweep("spec/data").length.should == 1
30
+ @files << Stowaway::FileObj.new("/fake/path1/button.jpg")
31
+ sweeper([/^\.|\.rb$|\.txt$/]).sweep("spec/data")
32
+ @files.length.should == 1
94
33
  end
95
34
 
96
35
  it "should output a message when sweeping through a file" do
@@ -103,18 +42,23 @@ describe Stowaway::Sweeper do
103
42
  sweeper([/^\.|\.rb$|testfile2/]).sweep("spec/data")
104
43
  end
105
44
 
106
- it "should find images of the same name but with different paths" do
45
+ it "should files of the same name but with different paths as last resort" do
107
46
  @files << Stowaway::FileObj.new("/fake/path1/button.jpg")
108
47
  @files << Stowaway::FileObj.new("/fake/path2/button.jpg")
109
48
  sweeper([/^\.|\.rb$/]).sweep("spec/data")
110
49
  @files.should be_empty
111
50
  end
112
51
 
113
- # it "should remove matches and leave files that were not found" do
114
- # @files << Stowaway::FileObj.new("/a/stowaway/file.txt")
115
- # sweeper([/^\.|\.rb$/]).sweep("spec/data")
116
- # @files.should_not be_empty
117
- # @files.first.fullpath.should == "/a/stowaway/file.txt"
118
- # end
52
+ it "should add a file matched on name only to an array of partially matched files" do
53
+ @files << Stowaway::FileObj.new("/missing/button.jpg")
54
+ sweeper([/^\.|\.rb$/]).sweep("spec/data").should have(1).name_only_matches
55
+ end
56
+
57
+ it "should not remove files that were not found" do
58
+ @files << Stowaway::FileObj.new("/a/stowaway.txt")
59
+ sweeper([/^\.|\.rb$/]).sweep("spec/data")
60
+ @files.should_not be_empty
61
+ @files.first.fullpath.should == "/a/stowaway.txt"
62
+ end
119
63
 
120
64
  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.1.6"
8
+ s.version = "0.1.8"
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-20}
12
+ s.date = %q{2010-02-21}
13
13
  s.default_executable = %q{stowaway}
14
14
  s.email = %q{ejcavazos@gmail.com}
15
15
  s.executables = ["stowaway"]
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.1.6
4
+ version: 0.1.8
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-20 00:00:00 -08:00
12
+ date: 2010-02-21 00:00:00 -08:00
13
13
  default_executable: stowaway
14
14
  dependencies: []
15
15