Class: CopyTunerIncompatibleSearch::ReplaceCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/copy_tuner_incompatible_search/replace_command.rb

Overview

rubocop:disable Metrics/ClassLength

Defined Under Namespace

Classes: Result, Usage

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(usage_path, blurbs_path) ⇒ ReplaceCommand

Returns a new instance of ReplaceCommand.



14
15
16
17
# File 'lib/copy_tuner_incompatible_search/replace_command.rb', line 14

def initialize(usage_path, blurbs_path)
  @usage_path = usage_path
  @blurbs_path = blurbs_path
end

Class Method Details

.run(usage_path, blurbs_path, output_path) ⇒ Object



10
11
12
# File 'lib/copy_tuner_incompatible_search/replace_command.rb', line 10

def self.run(usage_path, blurbs_path, output_path)
  self.new(usage_path, blurbs_path).run(output_path)
end

Instance Method Details

#run(output_path) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/copy_tuner_incompatible_search/replace_command.rb', line 19

def run(output_path) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  # usages.xlsxから生成したUsageオブジェクトの配列
  usages = usage_sheet.each(type: 'Type', key: 'Key', file: 'File', line: 'Line').filter_map.with_index do |hash, i|
    next if i.zero? # skip header

    Usage.new(type: hash[:type], key: hash[:key], file: hash[:file], line: hash[:line])
  end
  # プロジェクト内で後方互換性のないキーの集合
  incompatible_keys = Set[*usages.select(&:static?).map(&:key)]
  # 使用箇所が判明しているUsageオブジェクトの配列(ただし、dynamicなキーを除く)
  used_usages = usages.select(&:used?)
  # _html付きのキーに変換する必要があるキーの集合
  keys_to_convert = Set[*used_usages.map(&:key)]
  # staticかつ使用箇所が判明しているUsageオブジェクトの配列を翻訳キーでグループ化
  static_usages_by_key = used_usages.select(&:static?).group_by(&:key)
  # lazyかつ使用箇所が判明しているUsageオブジェクトの配列を翻訳キーでグループ化
  lazy_usages_by_key = used_usages.select(&:lazy?).group_by(&:key)

  # blurbs.csvの全キーの集合と、特殊文字を含むキーの配列
  all_blurb_keys, keys_with_special_chars = parse_blurbs_csv
  # すでに_htmlで終わるキーが存在する後方互換性のないキーの集合
  underscore_converted_keys = Set[*incompatible_keys.select { |k| all_blurb_keys.include?("#{k}_html") }]
  # すでに.htmlで終わるキーが存在する後方互換性のないキーの集合
  dot_converted_keys = Set[*incompatible_keys.select { |k| all_blurb_keys.include?("#{k}.html") }]

  # コード上のキーを置換する必要があるキーの配列
  keys_for_code_replace = []
  # _html付きのキーを新たに登録しなければならないキーの配列
  newly_replaced_keys = []
  # 使用箇所が見つからなかった後方互換性のないキーの配列
  not_used_incompatible_keys = []
  # _html付きのキーを定義したCSVを保存する。このファイルはCopyTunerにアップロードするために使う
  CSV.open(output_path, 'wb') do |csv_out|
    CSV.parse(blurbs_csv_text, headers: true, quote_char: '"').each_with_index do |row, i|
      if i.zero?
        csv_out << row.headers
      end
      key = row['key']
      converted_key = "#{key}_html"
      if keys_to_convert.include?(key) && !dot_converted_keys.include?(key) && !underscore_converted_keys.include?(key)
        csv_out << [converted_key, *row[1..]]
        newly_replaced_keys << key
      end
      if keys_to_convert.include?(key)
        keys_for_code_replace << key
      elsif incompatible_keys.include?(key)
        not_used_incompatible_keys << key
      end
    end
  end

  replace_code_for_static_usages(static_usages_by_key, keys_for_code_replace, underscore_converted_keys, dot_converted_keys)
  replace_code_for_lazy_usages(lazy_usages_by_key, keys_for_code_replace, underscore_converted_keys, dot_converted_keys)

  existing_keys = keys_for_code_replace - newly_replaced_keys
  already_ignored_keys = search_ignored_keys.uniq
  keys_to_ignore = usages.reject(&:dynamic?).map(&:key).uniq - already_ignored_keys
  dynamic_count = usages.count(&:dynamic?)
  Result.new(newly_replaced_keys, existing_keys, not_used_incompatible_keys, keys_to_ignore, already_ignored_keys, keys_with_special_chars, dynamic_count)
end