discover-unused-partials 0.2.0 → 0.3.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3a28d1e85118c3214475e30a0fa2124e2124ff0e
4
+ data.tar.gz: ce4ac735dc68cc482c5fd43ba5590f8e555d22be
5
+ SHA512:
6
+ metadata.gz: 8177575c1c4c9cc9e66eb8085bc903c814fb7caea6a917dfae57ac6bc209573f83764fef6d0bf7bd73925b6e516d1c2fba8f062b6fdd4aad8e322b602e02ca7c
7
+ data.tar.gz: ce3cb5a10dc4416de7d3fb7211b45ecc9bed987120df8d44316ce62bd1c240224ff2f520ad90d2d4a1cc06b1ff7c6e06673ca3bc7d95641710a73faebf0f2026
@@ -3,13 +3,35 @@
3
3
  This utility script (which someday will be a Rails plug-in) was made in order to
4
4
  help developers to find out unused partials.
5
5
 
6
-
7
6
  == Usage
8
7
 
9
- To use this script, simply run it in your RAILS_ROOT. It will return a list of
10
- unmentioned partials. It supports detection of Haml and Erb (both .erb and
11
- .rhtml) templates.
8
+ === Install the gem
9
+ gem install discover-unused-partials
10
+
11
+ === Run the script
12
+ To use this script, simply run
13
+
14
+ discover-unused-partials path/to/your/rails_root
15
+
16
+ If path is not provided, script is executed in the current directory
17
+
18
+ It will return a list of unmentioned partials.
19
+ It supports detection of Haml and Erb (both .erb and .rhtml) templates.
20
+
21
+ === Configuration
22
+ To fine-tune script behavior, you can add discover-unused-partials.yml to your app config directory.
23
+ Content of such file:
24
+
25
+ # List of files that are not reported
26
+ keep:
27
+ - app/views/my/partial/_to_keep.html.erb
12
28
 
13
- === Thanks
29
+ # List of dynamically generated partials and their sources
30
+ dynamic:
31
+ app/controllers/user_controller.rb:
32
+ - app/views/user/_first_dynamic_partial.html.erb
33
+ - app/views/user/_second_dynamic_partial.text.haml
34
+
35
+ == Thanks
14
36
  Special thanks goes to Willian Molinari (PotHix) and Mateus Linhares
15
37
  (mateuslinhares) that helped me writing this little script.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.3.0
@@ -4,7 +4,7 @@
4
4
  require 'discover-unused-partials'
5
5
  require 'optparse'
6
6
 
7
- options = Hash.new
7
+ options = {}
8
8
  OptionParser.new do |opts|
9
9
  opts.banner = "Usage: discover-unused-partials [options]"
10
10
 
@@ -15,5 +15,9 @@ OptionParser.new do |opts|
15
15
 
16
16
  opts.parse!
17
17
  end
18
+ options[:root] = ARGV.first || '.'
18
19
 
19
- DiscoverUnusedPartials::find_in options
20
+ config = File.join(options[:root], 'config', 'discover-unused-partials.yml')
21
+ options.merge!(YAML.load_file(config)) if File.exist?(config)
22
+
23
+ DiscoverUnusedPartials::find(options)
@@ -1,10 +1,9 @@
1
1
  # -*- coding: UTF-8 -*-
2
2
  module DiscoverUnusedPartials
3
3
 
4
- #TODO: Prepare to give directory by argument
5
- def self.find_in directory
6
- worker = PartialWorker.new
7
- tree, dynamic = worker.used_partials("app")
4
+ def self.find options={}
5
+ worker = PartialWorker.new options
6
+ tree, dynamic = Dir.chdir(options[:root]){ worker.used_partials("app") }
8
7
 
9
8
  tree.each do |idx, level|
10
9
  indent = " " * idx*2
@@ -35,6 +34,10 @@ module DiscoverUnusedPartials
35
34
  @@extension = /\.\w+/
36
35
  @@partial = /:partial\s*=>\s*|partial:\s*/
37
36
  @@render = /\brender\s*(?:\(\s*)?/
37
+
38
+ def initialize options
39
+ @options = options
40
+ end
38
41
 
39
42
  def existent_partials root
40
43
  partials = []
@@ -48,6 +51,7 @@ module DiscoverUnusedPartials
48
51
  end
49
52
 
50
53
  def used_partials root
54
+ raise "#{Dir.pwd} does not have '#{root}' directory" unless File.directory? root
51
55
  files = []
52
56
  each_file(root) do |file|
53
57
  files << file
@@ -76,10 +80,9 @@ module DiscoverUnusedPartials
76
80
  end
77
81
 
78
82
  def process_partials(files)
79
- partials = []
83
+ partials = @options['keep'] || []
80
84
  dynamic = {}
81
85
  files.each do |file|
82
- #next unless FileTest.exists?(file)
83
86
  File.open(file) do |f|
84
87
  f.each do |line|
85
88
  line.strip!
@@ -100,8 +103,12 @@ module DiscoverUnusedPartials
100
103
  end
101
104
  partials << check_extension_path(full_path)
102
105
  elsif line =~ /#@@partial|#@@render["']/
103
- dynamic[file] ||= []
104
- dynamic[file] << line
106
+ if @options["dynamic"] && @options["dynamic"][file]
107
+ partials += @options["dynamic"][file]
108
+ else
109
+ dynamic[file] ||= []
110
+ dynamic[file] << line
111
+ end
105
112
  end
106
113
  end
107
114
  end
@@ -109,16 +116,10 @@ module DiscoverUnusedPartials
109
116
  partials.uniq!
110
117
  [partials, dynamic]
111
118
  end
112
-
119
+
120
+ EXT = %w(.html.erb .text.erb .html.haml .text.haml .rhtml)
113
121
  def check_extension_path(file)
114
- if File.exists? file + ".html.erb"
115
- file += ".html.erb"
116
- elsif File.exists? file + ".html.haml"
117
- file += ".html.haml"
118
- else
119
- file += ".rhtml"
120
- end
121
- file
122
+ "#{file}#{EXT.find{ |e| File.exists? file + e }}"
122
123
  end
123
124
 
124
125
  def each_file(root, &block)
@@ -128,7 +129,6 @@ module DiscoverUnusedPartials
128
129
  next if file =~ %r[^app/assets]
129
130
  each_file(file) {|file| yield file}
130
131
  else
131
- next if file =~ /\.js\b/
132
132
  yield file
133
133
  end
134
134
  end
@@ -1,3 +1,3 @@
1
1
  module DiscoverUnusedPartials
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: discover-unused-partials
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
5
- prerelease:
4
+ version: 0.3.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Willian Molinari (a.k.a PotHix)
@@ -10,7 +9,7 @@ authors:
10
9
  autorequire:
11
10
  bindir: bin
12
11
  cert_chain: []
13
- date: 2012-08-28 00:00:00.000000000 Z
12
+ date: 2013-08-12 00:00:00.000000000 Z
14
13
  dependencies: []
15
14
  description: A script to help you finding out unused partials. Good for big projects
16
15
  or projects under heavy refactoring
@@ -37,27 +36,26 @@ files:
37
36
  - spec/spec_helper.rb
38
37
  homepage: https://github.com/vinibaggio/discover-unused-partials
39
38
  licenses: []
39
+ metadata: {}
40
40
  post_install_message:
41
41
  rdoc_options: []
42
42
  require_paths:
43
43
  - lib
44
44
  required_ruby_version: !ruby/object:Gem::Requirement
45
- none: false
46
45
  requirements:
47
- - - ! '>='
46
+ - - '>='
48
47
  - !ruby/object:Gem::Version
49
48
  version: '0'
50
49
  required_rubygems_version: !ruby/object:Gem::Requirement
51
- none: false
52
50
  requirements:
53
- - - ! '>='
51
+ - - '>='
54
52
  - !ruby/object:Gem::Version
55
53
  version: '0'
56
54
  requirements: []
57
55
  rubyforge_project:
58
- rubygems_version: 1.8.23
56
+ rubygems_version: 2.0.3
59
57
  signing_key:
60
- specification_version: 3
58
+ specification_version: 4
61
59
  summary: A script to help you finding out unused partials. Good for big projects or
62
60
  projects under heavy refactoring
63
61
  test_files: