Module: I18nContextGenerator::FileClassifier

Defined in:
lib/i18n_context_generator/file_classifier.rb

Overview

Owns source and translation file-type classification. Header files remain searchable for iOS runs but are deliberately not platform evidence because generic C headers also occur in Android projects.

Constant Summary collapse

IOS_SOURCE_EXTENSIONS =
%w[.swift .m .mm].freeze
IOS_SEARCH_EXTENSIONS =
(IOS_SOURCE_EXTENSIONS + ['.h']).freeze
ANDROID_SOURCE_EXTENSIONS =
%w[.kt .java].freeze
SEARCH_EXTENSIONS =
{
  ios: IOS_SEARCH_EXTENSIONS,
  android: (ANDROID_SOURCE_EXTENSIONS + ['.xml']).freeze,
  unknown: (IOS_SEARCH_EXTENSIONS + ANDROID_SOURCE_EXTENSIONS + ['.xml']).freeze
}.freeze

Class Method Summary collapse

Class Method Details

.android_resource_xml?(path) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/i18n_context_generator/file_classifier.rb', line 49

def android_resource_xml?(path)
  File.extname(path).downcase == '.xml' && path_components(path).include?('res')
end

.android_source_xml?(path) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/i18n_context_generator/file_classifier.rb', line 53

def android_source_xml?(path)
  android_resource_xml?(path) || File.basename(path).casecmp('AndroidManifest.xml').zero?
end

.android_translation_file?(path) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
# File 'lib/i18n_context_generator/file_classifier.rb', line 57

def android_translation_file?(path)
  return false unless File.extname(path).downcase == '.xml'
  return true if File.basename(path).casecmp('strings.xml').zero?

  path_components(path).each_cons(2).any? { |first, second| first == 'res' && second.start_with?('values') }
end

.path_components(path) ⇒ Object



68
69
70
# File 'lib/i18n_context_generator/file_classifier.rb', line 68

def path_components(path)
  path.to_s.tr('\\', '/').split('/').reject(&:empty?)
end

.searchable_platform(path, platform_hint: nil) ⇒ Object



27
28
29
30
31
32
# File 'lib/i18n_context_generator/file_classifier.rb', line 27

def searchable_platform(path, platform_hint: nil)
  return :ios if File.extname(path).downcase == '.h'
  return :android if File.extname(path).downcase == '.xml' && platform_hint&.to_sym == :android

  source_platform(path)
end

.searchable_source?(path, platform: :unknown) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
# File 'lib/i18n_context_generator/file_classifier.rb', line 34

def searchable_source?(path, platform: :unknown)
  extensions = SEARCH_EXTENSIONS.fetch(platform.to_sym, SEARCH_EXTENSIONS[:unknown])
  return false unless extensions.include?(File.extname(path).downcase)
  return platform.to_sym == :android || android_source_xml?(path) if File.extname(path).downcase == '.xml'

  true
end

.source_platform(path) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/i18n_context_generator/file_classifier.rb', line 19

def source_platform(path)
  extension = File.extname(path).downcase
  return :ios if IOS_SOURCE_EXTENSIONS.include?(extension)
  return :android if ANDROID_SOURCE_EXTENSIONS.include?(extension)

  :android if android_source_xml?(path)
end

.swift_source?(path) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/i18n_context_generator/file_classifier.rb', line 64

def swift_source?(path)
  File.extname(path).downcase == '.swift'
end

.translation_platform(path) ⇒ Object



42
43
44
45
46
47
# File 'lib/i18n_context_generator/file_classifier.rb', line 42

def translation_platform(path)
  case File.extname(path).downcase
  when '.strings', '.xcstrings' then :ios
  when '.xml' then :android if android_translation_file?(path)
  end
end