stowaway 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2009 Emilio Cavazos
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,18 @@
1
+ # Stowaway
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:
4
+
5
+ * .jpg
6
+ * .gif
7
+ * .png
8
+ * .ico
9
+ * .js
10
+ * .css
11
+
12
+ ## Installing
13
+
14
+ sudo gem install ecavazos-stowaway
15
+
16
+ ## Note
17
+
18
+ I'll be adding support for matching on relative and absolute paths. 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. Comparing by path will preserve the uniqueness of the file.
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ require 'rake'
2
+
3
+ begin
4
+ require 'jeweler'
5
+ Jeweler::Tasks.new do |gem|
6
+ gem.name = "stowaway"
7
+ gem.summary = "Locate files in a web project that aren't being used."
8
+ gem.authors = ["Emilio Cavazos"]
9
+ gem.email = "ejcavazos@gmail.com"
10
+ gem.homepage = "http://www.iamneato.com"
11
+ end
12
+ Jeweler::GemcutterTasks.new
13
+ rescue LoadError
14
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
15
+ end
16
+
17
+ require 'spec/rake/spectask'
18
+ Spec::Rake::SpecTask.new(:spec) do |spec|
19
+ spec.libs << 'lib' << 'spec'
20
+ spec.spec_files = FileList['spec/**/*_spec.rb']
21
+ end
22
+
23
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
24
+ spec.libs << 'lib' << 'spec'
25
+ spec.pattern = 'spec/**/*_spec.rb'
26
+ spec.rcov = true
27
+ end
28
+
29
+ task :spec => :check_dependencies
30
+
31
+ task :default => :spec
32
+
33
+ require 'rake/rdoctask'
34
+ Rake::RDocTask.new do |rdoc|
35
+ if File.exist?('VERSION')
36
+ version = File.read('VERSION')
37
+ else
38
+ version = ""
39
+ end
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "test-jeweler #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/TODO.txt ADDED
@@ -0,0 +1,6 @@
1
+ == Future
2
+
3
+ * when locator returns duplicate filenames, the files should be matched on path+name in order to retain uniqueness
4
+ * currently, two files with the same name in different directories will be treated as the same file
5
+ * specify runner.rb
6
+ * clean up it's dependencies
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 0
4
+ :patch: 1
data/bin/stowaway ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'lib/stowaway/runner'
4
+ runner = Stowaway::Runner.new(ARGV)
5
+ runner.run
@@ -0,0 +1,21 @@
1
+ module Stowaway
2
+ class FileObj
3
+ attr :fullpath
4
+
5
+ def initialize(fullpath)
6
+ @fullpath = fullpath
7
+ end
8
+
9
+ def name
10
+ File.basename(@fullpath)
11
+ end
12
+
13
+ def path
14
+ File.split(@fullpath)[0]
15
+ end
16
+
17
+ def ==(fileObj)
18
+ self.fullpath == fileObj.fullpath
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,10 @@
1
+ module FSHelpyHelp
2
+ def ignore? file
3
+ @ignore.each do |s|
4
+ if file.match(/#{s}/i)
5
+ return true
6
+ end
7
+ end
8
+ false
9
+ end
10
+ end
@@ -0,0 +1,39 @@
1
+ require_relative 'fshelpyhelp'
2
+ require_relative 'file'
3
+
4
+ module Stowaway
5
+ class Locator
6
+ include FSHelpyHelp
7
+
8
+ def initialize(extensions)
9
+ @extensions = extensions
10
+ @ignore = ["^\\."]
11
+ end
12
+
13
+ def type?(file)
14
+ @extensions.each do |e|
15
+ return true if file.match(/#{e}$/)
16
+ end
17
+ false
18
+ end
19
+
20
+ def find_all(path, files = [])
21
+
22
+ dir = Dir.new(path)
23
+
24
+ dir.each do |f|
25
+ next if ignore?(f)
26
+
27
+ file = File.join(dir.path, f)
28
+
29
+ if File.directory?(file)
30
+ find_all file, files
31
+ elsif type?(f)
32
+ files << FileObj.new(file)
33
+ end
34
+ end
35
+ files
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,40 @@
1
+ require 'optparse'
2
+
3
+ module Stowaway
4
+ class Options
5
+ DEFAULT_FILE_TYPES = %w{.jpg .gif .png .ico .js .css}
6
+
7
+ attr_reader :path, :file_types
8
+
9
+ def initialize(argv)
10
+ @file_types = DEFAULT_FILE_TYPES
11
+ parse(argv)
12
+ @path = argv[0]
13
+ end
14
+
15
+ private
16
+ def parse(argv)
17
+ OptionParser.new do |opts|
18
+ opts.banner = "Usage: stowaway [ options ] path/to/site"
19
+
20
+ opts.on("-t", "--types <TYPES>", String, "File types to search for (ex: .jpg .gif)") do |ext|
21
+ @file_types = ext.split(' ')
22
+ end
23
+
24
+ opts.on("-h", "--help", "Show this message") do
25
+ puts opts
26
+ exit
27
+ end
28
+
29
+ begin
30
+ argv = ["-h"] if argv.empty?
31
+ opts.parse!(argv)
32
+ rescue OptionParser::ParseError => e
33
+ STDERR.puts e.message, "\n", opts
34
+ exit(-1)
35
+ end
36
+
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,32 @@
1
+ require_relative 'options'
2
+ require_relative 'locator'
3
+ require_relative 'sweeper'
4
+
5
+ module Stowaway
6
+ class Runner
7
+
8
+ def initialize(argv)
9
+ @options = Options.new(argv)
10
+ end
11
+
12
+ def run
13
+ locator = Stowaway::Locator.new(@options.file_types)
14
+ files = locator.find_all @options.path
15
+
16
+ fs = Stowaway::Sweeper.new files
17
+ respond fs.sweep @options.path
18
+ end
19
+
20
+ private
21
+ def respond(not_found)
22
+ if not_found.empty?
23
+ print "Zero stowaways found. You run a tight ship.\n\n"
24
+ else
25
+ print "\nYou have #{not_found.length} stowaway(s)\n"
26
+ print "--------------------------\n\n"
27
+ not_found.each_with_index { |f, i| print "#{i+1}: #{f.fullpath}\n" }
28
+ print "\n"
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,42 @@
1
+ require_relative 'fshelpyhelp'
2
+
3
+ module Stowaway
4
+ class Sweeper
5
+ include FSHelpyHelp
6
+
7
+ def initialize(files_to_find, ext_to_ignore = nil)
8
+ @files_to_find = files_to_find
9
+ @ignore = ext_to_ignore || ["^\\.", ".png", ".gif", ".jpg"]
10
+ end
11
+
12
+ def inspect_file(file)
13
+ File.open(file, 'r') do |i|
14
+ while line = i.gets
15
+ remove_matches(line)
16
+ end
17
+ end
18
+ end
19
+
20
+ def remove_matches(line)
21
+ @files_to_find.delete_if { |file| line.include?(file.name) }
22
+ end
23
+
24
+ def sweep(path)
25
+ dir = Dir.new(path)
26
+
27
+ dir.each do |f|
28
+ next if ignore?(f)
29
+
30
+ file = File.join(dir.path, f)
31
+
32
+ if File.directory?(file)
33
+ sweep(file)
34
+ else
35
+ inspect_file(file)
36
+ end
37
+ end
38
+ @files_to_find
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,4 @@
1
+ this is a line of test text.
2
+ this line has the name of a file: file1.jpg.
3
+ this line also has the name of a file (file2.gif) and it is so awesome.
4
+ the end.
@@ -0,0 +1,2 @@
1
+ Hello, I'm test data:
2
+ file3.js
@@ -0,0 +1,29 @@
1
+ require 'spec/spec_helper'
2
+ require 'lib/stowaway/file'
3
+
4
+ describe Stowaway::FileObj do
5
+
6
+ before(:all) do
7
+ @file = Stowaway::FileObj.new('/fake/path/test.rb')
8
+ end
9
+
10
+ it "should return full path" do
11
+ @file.fullpath.should == '/fake/path/test.rb'
12
+ end
13
+
14
+ it "should return name of file" do
15
+ @file.name.should == 'test.rb'
16
+ end
17
+
18
+ it "should return path to file" do
19
+ @file.path.should == '/fake/path'
20
+ end
21
+
22
+ it "should be == if paths are the same" do
23
+ @file.should == Stowaway::FileObj.new('/fake/path/test.rb')
24
+ end
25
+
26
+ it "should not be == if paths are differet" do
27
+ @file.should_not == Stowaway::FileObj.new('/blah/test.rb')
28
+ end
29
+ end
Binary file
@@ -0,0 +1,25 @@
1
+ require 'spec/spec_helper'
2
+ require 'lib/stowaway/options'
3
+
4
+ describe Stowaway::Options do
5
+
6
+ it "should use the the default file types when none are provided" do
7
+ opts = Stowaway::Options.new(['fake/path'])
8
+ Stowaway::Options::DEFAULT_FILE_TYPES.should == %w{.jpg .gif .png .ico .js .css}
9
+ end
10
+
11
+ it "should use the file types provided by user" do
12
+ opts = Stowaway::Options.new(['-t', '.png .jpg', 'fake/path'])
13
+ opts.file_types.should == %w{.png .jpg}
14
+ end
15
+
16
+ it "should parse path provided by user when file types are provided" do
17
+ opts = Stowaway::Options.new(['-t', '.png .jpg', 'fake/path'])
18
+ opts.path.should == 'fake/path'
19
+ end
20
+
21
+ it "should parse path provided by user when no file types are provided" do
22
+ opts = Stowaway::Options.new(['fake/path'])
23
+ opts.path.should == 'fake/path'
24
+ end
25
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec/spec_helper'
2
+ require 'lib/stowaway/file'
3
+ require 'lib/stowaway/sweeper'
4
+
5
+ describe Stowaway::Sweeper do
6
+
7
+ before(:each) do
8
+ @f1 = Stowaway::FileObj.new('/fake/file1.jpg')
9
+ @f2 = Stowaway::FileObj.new('/fake/file2.gif')
10
+ @f3 = Stowaway::FileObj.new('/fake/file3.js')
11
+ @files = [@f1, @f2, @f3]
12
+ @sweeper = Stowaway::Sweeper.new(@files)
13
+ end
14
+
15
+ it "should sweep through directory structure looking for matches" do
16
+ @sweeper.sweep('.')
17
+ @files.should be_empty
18
+ end
19
+
20
+ it "should not examine ignored file types" do
21
+ @sweeper = Stowaway::Sweeper.new(@files, ["^\\.", ".rb", "testfile1"])
22
+ @sweeper.sweep('.').length.should == 2
23
+ end
24
+
25
+ it "should read through the file looking for matches" do
26
+ @sweeper.inspect_file('spec/data/testfile1.txt')
27
+ @files.should == [@f3]
28
+ end
29
+
30
+ it "should remove matches from the list of files to find" do
31
+ line = 'file2.gif, file3.js'
32
+ @sweeper.remove_matches line
33
+ @files.should == [@f1]
34
+ end
35
+
36
+ end
@@ -0,0 +1,8 @@
1
+ require 'spec/spec_helper'
2
+ require 'lib/stowaway/runner'
3
+
4
+ describe Stowaway::Runner do
5
+ it "should be tested" do
6
+
7
+ end
8
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,4 @@
1
+ --colour
2
+ --format progress
3
+ --loadby mtime
4
+ --reverse
@@ -0,0 +1,3 @@
1
+ require 'spec'
2
+ require 'spec/autorun'
3
+ require 'spec/interop/test'
data/stowaway.gemspec ADDED
@@ -0,0 +1,67 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{stowaway}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Emilio Cavazos"]
12
+ s.date = %q{2009-09-30}
13
+ s.default_executable = %q{stowaway}
14
+ s.email = %q{ejcavazos@gmail.com}
15
+ s.executables = ["stowaway"]
16
+ s.extra_rdoc_files = [
17
+ "LICENSE",
18
+ "README.md"
19
+ ]
20
+ s.files = [
21
+ "LICENSE",
22
+ "README.md",
23
+ "Rakefile",
24
+ "TODO.txt",
25
+ "VERSION.yml",
26
+ "bin/stowaway",
27
+ "lib/stowaway/file.rb",
28
+ "lib/stowaway/fshelpyhelp.rb",
29
+ "lib/stowaway/locator.rb",
30
+ "lib/stowaway/options.rb",
31
+ "lib/stowaway/runner.rb",
32
+ "lib/stowaway/sweeper.rb",
33
+ "spec/data/testfile1.txt",
34
+ "spec/data/testfile2.txt",
35
+ "spec/lib/file_spec.rb",
36
+ "spec/lib/locator_spec.rb",
37
+ "spec/lib/options_spec.rb",
38
+ "spec/lib/sweeper_spec.rb",
39
+ "spec/runner_spec.rb",
40
+ "spec/spec.opts",
41
+ "spec/spec_helper.rb",
42
+ "stowaway.gemspec"
43
+ ]
44
+ s.homepage = %q{http://www.iamneato.com}
45
+ s.rdoc_options = ["--charset=UTF-8"]
46
+ s.require_paths = ["lib"]
47
+ s.rubygems_version = %q{1.3.5}
48
+ s.summary = %q{Locate files in a web project that aren't being used.}
49
+ s.test_files = [
50
+ "spec/lib/file_spec.rb",
51
+ "spec/lib/locator_spec.rb",
52
+ "spec/lib/options_spec.rb",
53
+ "spec/lib/sweeper_spec.rb",
54
+ "spec/runner_spec.rb",
55
+ "spec/spec_helper.rb"
56
+ ]
57
+
58
+ if s.respond_to? :specification_version then
59
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
60
+ s.specification_version = 3
61
+
62
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
63
+ else
64
+ end
65
+ else
66
+ end
67
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stowaway
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Emilio Cavazos
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-30 00:00:00 -07:00
13
+ default_executable: stowaway
14
+ dependencies: []
15
+
16
+ description:
17
+ email: ejcavazos@gmail.com
18
+ executables:
19
+ - stowaway
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.md
25
+ files:
26
+ - LICENSE
27
+ - README.md
28
+ - Rakefile
29
+ - TODO.txt
30
+ - VERSION.yml
31
+ - bin/stowaway
32
+ - lib/stowaway/file.rb
33
+ - lib/stowaway/fshelpyhelp.rb
34
+ - lib/stowaway/locator.rb
35
+ - lib/stowaway/options.rb
36
+ - lib/stowaway/runner.rb
37
+ - lib/stowaway/sweeper.rb
38
+ - spec/data/testfile1.txt
39
+ - spec/data/testfile2.txt
40
+ - spec/lib/file_spec.rb
41
+ - spec/lib/locator_spec.rb
42
+ - spec/lib/options_spec.rb
43
+ - spec/lib/sweeper_spec.rb
44
+ - spec/runner_spec.rb
45
+ - spec/spec.opts
46
+ - spec/spec_helper.rb
47
+ - stowaway.gemspec
48
+ has_rdoc: true
49
+ homepage: http://www.iamneato.com
50
+ licenses: []
51
+
52
+ post_install_message:
53
+ rdoc_options:
54
+ - --charset=UTF-8
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
69
+ requirements: []
70
+
71
+ rubyforge_project:
72
+ rubygems_version: 1.3.5
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: Locate files in a web project that aren't being used.
76
+ test_files:
77
+ - spec/lib/file_spec.rb
78
+ - spec/lib/locator_spec.rb
79
+ - spec/lib/options_spec.rb
80
+ - spec/lib/sweeper_spec.rb
81
+ - spec/runner_spec.rb
82
+ - spec/spec_helper.rb