copy_tuner_incompatible_search 0.1.1 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4bfaa3ca7e233984dde7c77ea02cd22817a7c9fc1dce97b2c14c926dc7636f86
4
- data.tar.gz: 54b63e786512d5854f8f28a5cfbb6efe8c2d7bb4aa8fc4b380dcfed19867df70
3
+ metadata.gz: ea523148710e0770c70a4777d23d84ae93b02c1d7e4d92e7b96080f0a2188a6f
4
+ data.tar.gz: c5f2a3729156ca2dfe2871e6f78776bd0996772c1e123209f1ecf46126a2fbc8
5
5
  SHA512:
6
- metadata.gz: 7ad386c722b6f559b4b23b1abe642421c7ccb1d0d7b5540f6b7e2d1fffed8a307ac18be450583395f87825819ee2e72d65abb005ddbe32efc24b55cba66f2ff7
7
- data.tar.gz: 6924441484d3a2a6e25572efab8eab590669fd39187cd092a8147722279c34510a70654b93c7c08e2befbe50108c03d6bb0285355296eaa626a8c2097f66e660
6
+ metadata.gz: d615646ece07c67dee19035058beffe7c57608477d247faf1eac486aaefcbe860fb25cbe1d2a4f686b52a3e81901501796742f7f8682408b268a8b379988a7f5
7
+ data.tar.gz: 63a79cdd22595f74d16faf6f4337ac9be4939539adf230cdd0ed7616d113e8a560b33e4c07a51d98a7cbd211a7434a59b7dd21d68ea4da1b2baf99cb16f881ef
data/.rubocop.yml ADDED
@@ -0,0 +1,16 @@
1
+ AllCops:
2
+ NewCops: enable
3
+ inherit_gem:
4
+ sgcop: ruby/rubocop.yml
5
+ require:
6
+ - rubocop-rake
7
+ - rubocop-rspec
8
+
9
+ Bundler/GemComment:
10
+ Enabled: false
11
+ RSpec/DescribedClass:
12
+ Enabled: false
13
+ RSpec/ReceiveMessages:
14
+ Enabled: false
15
+ RSpec/NestedGroups:
16
+ Enabled: false
data/Rakefile CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "bundler/gem_tasks"
4
- require "rspec/core/rake_task"
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
5
 
6
6
  RSpec::Core::RakeTask.new(:spec)
7
7
 
@@ -2,4 +2,9 @@
2
2
 
3
3
  require_relative '../lib/copy_tuner_incompatible_search/command'
4
4
 
5
- CopyTunerIncompatibleSearch::Command.run
5
+ project_name = File.basename(Dir.pwd)
6
+ timestamp = Time.now.strftime('%Y%m%d%H%M%S')
7
+ output_path = "tmp/usages-#{project_name}-#{timestamp}.xlsx"
8
+ CopyTunerIncompatibleSearch::Command.run(output_path)
9
+ puts "open #{output_path}"
10
+ `open #{output_path}`
@@ -1,56 +1,23 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'axlsx'
3
4
  require 'set'
4
5
 
5
6
  module CopyTunerIncompatibleSearch
6
7
  class Command
7
- def self.run
8
- self.new.run
8
+ def self.run(output_path)
9
+ self.new.run(output_path)
9
10
  end
10
11
 
11
- def run
12
- puts "Start"
13
-
14
- stdout = `rails copy_tuner:detect_html_incompatible_keys`
15
- keys = stdout.lines(chomp: true).map do |line|
16
- line.split(".", 2).last
17
- end.uniq.sort
18
- keys = Set[*keys]
19
-
20
- # 完全一致する翻訳キー
21
- puts "Searching #{keys.count} keys"
22
- count = 0
23
- results = {}
24
- keys.each do |key|
25
- count += 1
26
- puts "#{count} / #{keys.count}" if count % 100 == 0
27
-
28
- results[key] ||= Result.new(:static, key)
29
- result = `git grep -n "#{Regexp.escape(key)}"`.strip
30
- unless result.empty?
31
- results[key].add_usage(result)
32
- end
33
- end
34
-
35
- # .で始まる翻訳キー
36
- grep_result = `git grep -n -P "\\btt?[ \\(]['\\"]\\.\\w+"`
37
- results['lazy'] = Result.new(:lazy, "")
38
- results['lazy'].add_usage(grep_result)
39
-
40
- # 変数を含む翻訳キー
41
- grep_result = `git grep -n -P "\\btt?[ \\(]['\\"][\\w.-]*[#$]"`
42
- results['with vars'] = Result.new(:dynamic, "")
43
- results['with vars'].add_usage(grep_result)
44
-
45
- # Excelに出力
46
- project_name = File.basename(Dir.pwd)
47
- timestamp = Time.now.strftime("%Y%m%d%H%M%S")
48
- output_path = "tmp/usages-#{project_name}-#{timestamp}.xlsx"
49
- dump_to_xlsx(results, keys, output_path)
50
-
51
- puts "Finish"
52
- puts "open #{output_path}"
53
- `open #{output_path}`
12
+ def run(output_path)
13
+ puts 'Start'
14
+ incompatible_keys = search_incompatible_keys
15
+ results = search_static_usages(incompatible_keys)
16
+ results << search_lazy_usages
17
+ results << search_dynamic_usages
18
+ ignored_keys = Set[*eval(ignored_keys_text)] # rubocop:disable Security/Eval
19
+ XlsxWriter.dump_to_xlsx(results, incompatible_keys, ignored_keys, output_path)
20
+ puts 'Finish'
54
21
  end
55
22
 
56
23
  private
@@ -64,6 +31,10 @@ module CopyTunerIncompatibleSearch
64
31
  @usages = []
65
32
  end
66
33
 
34
+ def static?
35
+ @type == :static
36
+ end
37
+
67
38
  def lazy?
68
39
  @type == :lazy
69
40
  end
@@ -89,7 +60,7 @@ module CopyTunerIncompatibleSearch
89
60
  attr_reader :file, :line, :code, :lazy_key
90
61
 
91
62
  def initialize(grep_result, lazy_key = nil)
92
- file, line, code = grep_result.split(":", 3)
63
+ file, line, code = grep_result.split(':', 3)
93
64
  @file = file
94
65
  @line = line
95
66
  @code = code.strip
@@ -97,53 +68,62 @@ module CopyTunerIncompatibleSearch
97
68
  end
98
69
  end
99
70
 
100
- def ignored_keys
101
- @ignored_keys ||= begin
102
- text = `rails r "p CopyTunerClient::configuration.ignored_keys"`
103
- Set[*eval(text)]
104
- end
71
+ def search_incompatible_keys
72
+ stdout = detect_html_incompatible_keys
73
+ keys = stdout.lines(chomp: true).map do |line|
74
+ line.split('.', 2).last
75
+ end.uniq.sort
76
+ Set[*keys]
105
77
  end
106
78
 
107
- def dump_to_xlsx(results, keys, output_path)
108
- Axlsx::Package.new do |p|
109
- p.workbook.add_worksheet(name: "Data") do |sheet|
110
- monospace_style = sheet.styles.add_style(font_name: 'Courier New', sz: 14)
111
- sheet.add_row ["Type", "Key", "Ignored", "File", "Line", "Code"], style: monospace_style
112
- sheet.auto_filter = "A1:F1"
113
-
114
- # freeze pane
115
- sheet.sheet_view.pane do |pane|
116
- pane.top_left_cell = 'A2'
117
- pane.state = :frozen_split
118
- pane.y_split = 1
119
- pane.x_split = 0
120
- pane.active_pane = :bottom_right
121
- end
79
+ def search_static_usages(incompatible_keys)
80
+ puts "Searching #{incompatible_keys.count} keys"
81
+ count = 0
82
+ incompatible_keys.map do |key|
83
+ count += 1
84
+ puts "#{count} / #{incompatible_keys.count}" if (count % 100).zero?
122
85
 
123
- results.each do |key, result|
124
- added = false
125
- result.usages.each do |usage|
126
- key = if result.lazy?
127
- path = usage.file.sub(/^app\/views\//, '').sub(/\..+$/, '').sub('/_', '/').gsub('/', '.')
128
- path + usage.lazy_key
129
- else
130
- result.key
131
- end
132
- already_migrated = usage.file == 'config/initializers/copy_tuner.rb' || usage.code.include?("#{usage.lazy_key || result.key}_html")
133
- if (!result.lazy? || keys.include?(key)) && !already_migrated
134
- ignored = unless result.dynamic? then ignored_keys.include?(key) ? "Y" : "N" end
135
- sheet.add_row [result.type, key, ignored.to_s, usage.file, usage.line, usage.code], style: monospace_style
136
- added = true
137
- end
138
- end
139
- if !added && !result.lazy?
140
- ignored = ignored_keys.include?(result.key) ? "Y" : "N"
141
- sheet.add_row [result.type, result.key, ignored.to_s, "", "", ""], style: monospace_style
142
- end
143
- end
86
+ result = Result.new(:static, key)
87
+ usage = grep_usage(key).strip
88
+ unless usage.empty?
89
+ result.add_usage(usage)
144
90
  end
145
- p.serialize(output_path)
91
+ result
146
92
  end
147
93
  end
94
+
95
+ def search_lazy_usages
96
+ grep_result = grep_lazy_keys
97
+ result = Result.new(:lazy, '')
98
+ result.add_usage(grep_result)
99
+ result
100
+ end
101
+
102
+ def search_dynamic_usages
103
+ grep_result = grep_dynamic_keys
104
+ result = Result.new(:dynamic, '')
105
+ result.add_usage(grep_result)
106
+ result
107
+ end
108
+
109
+ def detect_html_incompatible_keys
110
+ `rails copy_tuner:detect_html_incompatible_keys`
111
+ end
112
+
113
+ def grep_lazy_keys
114
+ `git grep -n -P "\\btt?[ \\(]['\\"]\\.\\w+"`
115
+ end
116
+
117
+ def grep_dynamic_keys
118
+ `git grep -n -P "\\btt?[ \\(]['\\"][\\w.-]*[#$]"`
119
+ end
120
+
121
+ def grep_usage(key)
122
+ `git grep -n "#{Regexp.escape(key)}"`
123
+ end
124
+
125
+ def ignored_keys_text
126
+ `rails r "p CopyTunerClient::configuration.ignored_keys"`
127
+ end
148
128
  end
149
129
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CopyTunerIncompatibleSearch
4
- VERSION = "0.1.1"
4
+ VERSION = '0.1.3'
5
5
  end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CopyTunerIncompatibleSearch
4
+ class XlsxWriter
5
+ def self.dump_to_xlsx(results, incompatible_keys, ignored_keys, output_path)
6
+ self.new(results, incompatible_keys, ignored_keys).dump_to_xlsx(output_path)
7
+ end
8
+
9
+ def initialize(results, incompatible_keys, ignored_keys)
10
+ @results = results
11
+ @incompatible_keys = incompatible_keys
12
+ @ignored_keys = ignored_keys
13
+ end
14
+
15
+ def dump_to_xlsx(output_path)
16
+ Axlsx::Package.new do |p|
17
+ p.workbook.add_worksheet(name: 'Data') do |sheet|
18
+ style = sheet.styles.add_style(font_name: 'Courier New', sz: 14)
19
+ sheet.add_row %w[Type Key Ignored File Line Code], style: style
20
+ sheet.auto_filter = 'A1:F1'
21
+ freeze_pane(sheet)
22
+
23
+ results.each do |result|
24
+ add_result_rows(sheet, result, style)
25
+ end
26
+ end
27
+ p.serialize(output_path)
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ attr_reader :results, :incompatible_keys, :ignored_keys
34
+
35
+ def freeze_pane(sheet)
36
+ sheet.sheet_view.pane do |pane|
37
+ pane.top_left_cell = 'A2'
38
+ pane.state = :frozen_split
39
+ pane.y_split = 1
40
+ pane.x_split = 0
41
+ pane.active_pane = :bottom_right
42
+ end
43
+ end
44
+
45
+ def add_result_rows(sheet, result, style)
46
+ added = false
47
+ result.usages.each do |usage|
48
+ key = build_key(result, usage)
49
+ next unless should_output?(usage, result, key)
50
+
51
+ ignored = ignored_flag(key) unless result.dynamic?
52
+ sheet.add_row [result.type, key, ignored.to_s, usage.file, usage.line, usage.code], style: style
53
+ added = true
54
+ end
55
+ if !added && result.static?
56
+ sheet.add_row [result.type, result.key, ignored_flag(result.key), '', '', ''], style: style
57
+ end
58
+ end
59
+
60
+ def build_key(result, usage)
61
+ if result.lazy?
62
+ path = usage.file.sub(%r{^app/views/}, '').sub(/\..+$/, '').sub('/_', '/').gsub('/', '.')
63
+ path + usage.lazy_key
64
+ else
65
+ result.key
66
+ end
67
+ end
68
+
69
+ def should_output?(usage, result, key)
70
+ already_migrated = usage.file == 'config/initializers/copy_tuner.rb' || usage.code.include?("#{usage.lazy_key || result.key}_html")
71
+ (!result.lazy? || incompatible_keys.include?(key)) && !already_migrated
72
+ end
73
+
74
+ def ignored_flag(key)
75
+ ignored_keys.include?(key) ? 'Y' : 'N'
76
+ end
77
+ end
78
+ end
@@ -1,7 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "copy_tuner_incompatible_search/version"
4
- require_relative "copy_tuner_incompatible_search/command"
3
+ require_relative 'copy_tuner_incompatible_search/version'
4
+ require_relative 'copy_tuner_incompatible_search/command'
5
+ require_relative 'copy_tuner_incompatible_search/xlsx_writer'
5
6
 
6
7
  module CopyTunerIncompatibleSearch
7
8
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: copy_tuner_incompatible_search
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Junichi Ito
@@ -33,6 +33,7 @@ extensions: []
33
33
  extra_rdoc_files: []
34
34
  files:
35
35
  - ".rspec"
36
+ - ".rubocop.yml"
36
37
  - CHANGELOG.md
37
38
  - CODE_OF_CONDUCT.md
38
39
  - LICENSE.txt
@@ -42,6 +43,7 @@ files:
42
43
  - lib/copy_tuner_incompatible_search.rb
43
44
  - lib/copy_tuner_incompatible_search/command.rb
44
45
  - lib/copy_tuner_incompatible_search/version.rb
46
+ - lib/copy_tuner_incompatible_search/xlsx_writer.rb
45
47
  - sig/copy_tuner_incompatible_search.rbs
46
48
  homepage: https://github.com/JunichiIto/copy_tuner_incompatible_search
47
49
  licenses:
@@ -50,6 +52,7 @@ metadata:
50
52
  allowed_push_host: https://rubygems.org
51
53
  homepage_uri: https://github.com/JunichiIto/copy_tuner_incompatible_search
52
54
  changelog_uri: https://github.com/JunichiIto/copy_tuner_incompatible_search/CHANGELOG.md
55
+ rubygems_mfa_required: 'true'
53
56
  post_install_message:
54
57
  rdoc_options: []
55
58
  require_paths: