discover-unused-partials 0.1.0 → 0.2.0

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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in discover-unused-partials1.gemspec
4
+ gemspec
data/README.rdoc CHANGED
@@ -1,9 +1,7 @@
1
1
  == Description
2
2
 
3
3
  This utility script (which someday will be a Rails plug-in) was made in order to
4
- help developers to find out unused partials. Beware that dynamic named partials
5
- (such as +render @post+ or render :partial => my_var) are NOT detected, so
6
- you need to be careful while checking that list.
4
+ help developers to find out unused partials.
7
5
 
8
6
 
9
7
  == Usage
data/Rakefile CHANGED
@@ -1,31 +1,2 @@
1
- require 'jeweler'
2
- require 'rubygems'
3
- require 'rake'
4
- require 'spec/rake/spectask'
5
- require 'rake/rdoctask'
6
-
7
- Spec::Rake::SpecTask.new(:spec) do |spec|
8
- spec.libs << 'lib' << 'spec'
9
- spec.spec_files = FileList['spec/**/*_spec.rb']
10
- end
11
-
12
- task :spec => :check_dependencies
13
- task :default => :spec
14
-
15
- Rake::RDocTask.new do |rdoc|
16
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
17
-
18
- rdoc.rdoc_dir = 'rdoc'
19
- rdoc.title = "discover-unused-partials #{version}"
20
- rdoc.rdoc_files.include('README*')
21
- rdoc.rdoc_files.include('lib/**/*.rb')
22
- end
23
-
24
- Jeweler::Tasks.new do |gemspec|
25
- gemspec.name = "discover-unused-partials"
26
- gemspec.summary = "Discover your unused partials"
27
- gemspec.description = "A script to help you finding out unused partials. Good for big projects or projects under heavy refactoring"
28
- gemspec.email = "vinibaggio@gmail.com"
29
- gemspec.homepage = "http://github.com/vinibaggio/discover-unused-partials"
30
- gemspec.authors = ["Vinicius Baggio", "Willian Molinari (a.k.a PotHix)"]
31
- end
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # -*- coding: UTF-8 -*-
2
3
 
3
4
  require 'discover-unused-partials'
4
5
  require 'optparse'
@@ -1,54 +1,17 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
1
  # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/discover-unused-partials/version', __FILE__)
5
3
 
6
- Gem::Specification.new do |s|
7
- s.name = %q{discover-unused-partials}
8
- s.version = "0.1.0"
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Willian Molinari (a.k.a PotHix)", "Vinicius Baggio"]
6
+ gem.email = ["pothix@pothix.com", "vinibaggio@gmail.com"]
7
+ gem.description = %q{A script to help you finding out unused partials. Good for big projects or projects under heavy refactoring}
8
+ gem.summary = gem.description
9
+ gem.homepage = "https://github.com/vinibaggio/discover-unused-partials"
9
10
 
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Vinicius Baggio", "Willian Molinari (a.k.a PotHix)"]
12
- s.date = %q{2009-12-17}
13
- s.default_executable = %q{discover-unused-partials}
14
- s.description = %q{A script to help you finding out unused partials. Good for big projects or projects under heavy refactoring}
15
- s.email = %q{vinibaggio@gmail.com}
16
- s.executables = ["discover-unused-partials"]
17
- s.extra_rdoc_files = [
18
- "LICENSE",
19
- "README.rdoc"
20
- ]
21
- s.files = [
22
- ".gitignore",
23
- "LICENSE",
24
- "README.rdoc",
25
- "Rakefile",
26
- "VERSION",
27
- "bin/discover-unused-partials",
28
- "discover-unused-partials.gemspec",
29
- "lib/discover-unused-partials.rb",
30
- "spec/discover-unused-partials_spec.rb",
31
- "spec/spec.opts",
32
- "spec/spec_helper.rb"
33
- ]
34
- s.homepage = %q{http://github.com/vinibaggio/discover-unused-partials}
35
- s.rdoc_options = ["--charset=UTF-8"]
36
- s.require_paths = ["lib"]
37
- s.rubygems_version = %q{1.3.5}
38
- s.summary = %q{Discover your unused partials}
39
- s.test_files = [
40
- "spec/spec_helper.rb",
41
- "spec/discover-unused-partials_spec.rb"
42
- ]
43
-
44
- if s.respond_to? :specification_version then
45
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
46
- s.specification_version = 3
47
-
48
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
49
- else
50
- end
51
- else
52
- end
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "discover-unused-partials"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = DiscoverUnusedPartials::VERSION
53
17
  end
54
-
@@ -1,18 +1,40 @@
1
+ # -*- coding: UTF-8 -*-
1
2
  module DiscoverUnusedPartials
2
3
 
4
+ #TODO: Prepare to give directory by argument
3
5
  def self.find_in directory
4
6
  worker = PartialWorker.new
7
+ tree, dynamic = worker.used_partials("app")
5
8
 
6
- existent = worker.existent_partials("app").sort
7
- used = worker.used_partials("app").sort
9
+ tree.each do |idx, level|
10
+ indent = " " * idx*2
11
+ h_indent = idx == 1 ? "" : "\n" + " "*(idx-1)*2
8
12
 
9
- unless (existent & used) == existent
10
- unused = (existent - used)
11
- puts unused * "\n"
13
+ if idx == 1
14
+ puts "#{h_indent}The following partials are not referenced directly by any code:"
15
+ else
16
+ puts "#{h_indent}The following partials are only referenced directly by the partials above:"
17
+ end
18
+ level[:unused].sort.each do |partial|
19
+ puts "#{indent}#{partial}"
20
+ end
21
+ end
22
+
23
+ unless dynamic.empty?
24
+ puts "\n\nSome of the partials above (at any level) might be referenced dynamically by the following lines of code:"
25
+ dynamic.sort.map do |file, lines|
26
+ lines.each do |line|
27
+ puts " #{file}:#{line}"
28
+ end
29
+ end
12
30
  end
13
31
  end
14
32
 
15
33
  class PartialWorker
34
+ @@filename = /[a-zA-Z\d_\/]+?/
35
+ @@extension = /\.\w+/
36
+ @@partial = /:partial\s*=>\s*|partial:\s*/
37
+ @@render = /\brender\s*(?:\(\s*)?/
16
38
 
17
39
  def existent_partials root
18
40
  partials = []
@@ -22,21 +44,47 @@ module DiscoverUnusedPartials
22
44
  end
23
45
  end
24
46
 
25
- partials.sort
47
+ partials
26
48
  end
27
49
 
28
50
  def used_partials root
29
- partials = []
51
+ files = []
30
52
  each_file(root) do |file|
53
+ files << file
54
+ end
55
+ tree = {}
56
+ level = 1
57
+ existent = existent_partials(root)
58
+ top_dynamic = nil
59
+ loop do
60
+ used, dynamic = process_partials(files)
61
+ break if level > 1 && used.size == tree[level-1][:used].size
62
+ tree[level] = {
63
+ used: used,
64
+ }
65
+ if level == 1
66
+ top_dynamic = dynamic
67
+ tree[level][:unused] = existent - used
68
+ else
69
+ tree[level][:unused] = tree[level-1][:used] - used
70
+ end
71
+ break unless (files - tree[level][:unused]).size < files.size
72
+ files -= tree[level][:unused]
73
+ level += 1
74
+ end
75
+ [tree, top_dynamic]
76
+ end
77
+
78
+ def process_partials(files)
79
+ partials = []
80
+ dynamic = {}
81
+ files.each do |file|
82
+ #next unless FileTest.exists?(file)
31
83
  File.open(file) do |f|
32
84
  f.each do |line|
33
85
  line.strip!
34
- if line =~ /:partial\s+=>\s+[\"\']([a-zA-Z_\/]+)[\"\']/
35
- match = $1
36
- if match[0] == ?/ or match[0] == '/'
37
- match = match[1..-1]
38
- end
39
-
86
+ if line =~ %r[(?:#@@partial|#@@render)(['"])/?(#@@filename)#@@extension*\1]
87
+ match = $2
40
88
  if match.index("/")
41
89
 
42
90
  path = match.split('/')[0...-1].join('/')
@@ -51,11 +99,15 @@ module DiscoverUnusedPartials
51
99
  end
52
100
  end
53
101
  partials << check_extension_path(full_path)
102
+ elsif line =~ /#@@partial|#@@render["']/
103
+ dynamic[file] ||= []
104
+ dynamic[file] << line
54
105
  end
55
106
  end
56
107
  end
57
108
  end
58
- partials.uniq.sort
109
+ partials.uniq!
110
+ [partials, dynamic]
59
111
  end
60
112
 
61
113
  def check_extension_path(file)
@@ -73,8 +125,10 @@ module DiscoverUnusedPartials
73
125
  files = Dir.glob("#{root}/*")
74
126
  files.each do |file|
75
127
  if File.directory? file
128
+ next if file =~ %r[^app/assets]
76
129
  each_file(file) {|file| yield file}
77
130
  else
131
+ next if file =~ /\.js\b/
78
132
  yield file
79
133
  end
80
134
  end
@@ -0,0 +1,3 @@
1
+ module DiscoverUnusedPartials
2
+ VERSION = "0.2.0"
3
+ end
@@ -1,3 +1,4 @@
1
+ # -*- coding: UTF-8 -*-
1
2
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
3
 
3
4
  describe "DiscoverUnusedPartials" do
data/spec/spec_helper.rb CHANGED
@@ -1,9 +1,8 @@
1
+ # -*- coding: UTF-8 -*-
1
2
  $LOAD_PATH.unshift(File.dirname(__FILE__))
2
3
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
4
  require 'discover-unused-partials'
4
- require 'spec'
5
- require 'spec/autorun'
5
+ require 'rspec'
6
6
 
7
- Spec::Runner.configure do |config|
8
-
7
+ RSpec.configure do |config|
9
8
  end
metadata CHANGED
@@ -1,30 +1,29 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: discover-unused-partials
3
- version: !ruby/object:Gem::Version
4
- version: 0.1.0
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ prerelease:
5
6
  platform: ruby
6
- authors:
7
- - Vinicius Baggio
7
+ authors:
8
8
  - Willian Molinari (a.k.a PotHix)
9
+ - Vinicius Baggio
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
-
13
- date: 2009-12-17 00:00:00 -02:00
14
- default_executable: discover-unused-partials
13
+ date: 2012-08-28 00:00:00.000000000 Z
15
14
  dependencies: []
16
-
17
- description: A script to help you finding out unused partials. Good for big projects or projects under heavy refactoring
18
- email: vinibaggio@gmail.com
19
- executables:
15
+ description: A script to help you finding out unused partials. Good for big projects
16
+ or projects under heavy refactoring
17
+ email:
18
+ - pothix@pothix.com
19
+ - vinibaggio@gmail.com
20
+ executables:
20
21
  - discover-unused-partials
21
22
  extensions: []
22
-
23
- extra_rdoc_files:
24
- - LICENSE
25
- - README.rdoc
26
- files:
23
+ extra_rdoc_files: []
24
+ files:
27
25
  - .gitignore
26
+ - Gemfile
28
27
  - LICENSE
29
28
  - README.rdoc
30
29
  - Rakefile
@@ -32,37 +31,36 @@ files:
32
31
  - bin/discover-unused-partials
33
32
  - discover-unused-partials.gemspec
34
33
  - lib/discover-unused-partials.rb
34
+ - lib/discover-unused-partials/version.rb
35
35
  - spec/discover-unused-partials_spec.rb
36
36
  - spec/spec.opts
37
37
  - spec/spec_helper.rb
38
- has_rdoc: true
39
- homepage: http://github.com/vinibaggio/discover-unused-partials
38
+ homepage: https://github.com/vinibaggio/discover-unused-partials
40
39
  licenses: []
41
-
42
40
  post_install_message:
43
- rdoc_options:
44
- - --charset=UTF-8
45
- require_paths:
41
+ rdoc_options: []
42
+ require_paths:
46
43
  - lib
47
- required_ruby_version: !ruby/object:Gem::Requirement
48
- requirements:
49
- - - ">="
50
- - !ruby/object:Gem::Version
51
- version: "0"
52
- version:
53
- required_rubygems_version: !ruby/object:Gem::Requirement
54
- requirements:
55
- - - ">="
56
- - !ruby/object:Gem::Version
57
- version: "0"
58
- version:
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
59
56
  requirements: []
60
-
61
57
  rubyforge_project:
62
- rubygems_version: 1.3.5
58
+ rubygems_version: 1.8.23
63
59
  signing_key:
64
60
  specification_version: 3
65
- summary: Discover your unused partials
66
- test_files:
67
- - spec/spec_helper.rb
61
+ summary: A script to help you finding out unused partials. Good for big projects or
62
+ projects under heavy refactoring
63
+ test_files:
68
64
  - spec/discover-unused-partials_spec.rb
65
+ - spec/spec.opts
66
+ - spec/spec_helper.rb