Class: Fastlane::Actions::IosGenerateStringsFileFromCodeAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/wpmreleasetoolkit/actions/ios/ios_generate_strings_file_from_code.rb

Documentation collapse

Class Method Summary collapse

Class Method Details

.authorsObject



106
107
108
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/ios/ios_generate_strings_file_from_code.rb', line 106

def self.authors
  ['Automattic']
end

.available_optionsObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/ios/ios_generate_strings_file_from_code.rb', line 57

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :paths,
                                 env_name: 'FL_IOS_GENERATE_STRINGS_FILE_FROM_CODE_PATHS',
                                 description: 'Array of paths to scan for `.m` and `.swift` files. The entries can also contain glob patterns',
                                 type: Array,
                                 default_value: ['.']),
    FastlaneCore::ConfigItem.new(key: :exclude,
                                 env_name: 'FL_IOS_GENERATE_STRINGS_FILE_FROM_CODE_EXCLUDE',
                                 description: 'Array of paths or glob patterns to exclude from scanning',
                                 type: Array,
                                 default_value: []),
    FastlaneCore::ConfigItem.new(key: :routines,
                                 env_name: 'FL_IOS_GENERATE_STRINGS_FILE_FROM_CODE_ROUTINES',
                                 description: 'Base name of the alternate methods to be parsed in addition to the standard `NSLocalizedString()` one. See the `-s` option in `man genstrings`',
                                 type: Array,
                                 default_value: []),
    FastlaneCore::ConfigItem.new(key: :quiet,
                                 env_name: 'FL_IOS_GENERATE_STRINGS_FILE_FROM_CODE_QUIET',
                                 description: 'In quiet mode, `genstrings` will log warnings about duplicate values, but not about duplicate comments',
                                 type: Boolean,
                                 default_value: true),
    FastlaneCore::ConfigItem.new(key: :swiftui,
                                 env_name: 'FL_IOS_GENERATE_STRINGS_FILE_FROM_CODE_SWIFTUI',
                                 description: "Should we include SwiftUI's `Text()` when parsing code with `genstrings`",
                                 type: Boolean,
                                 default_value: false),
    FastlaneCore::ConfigItem.new(key: :output_dir,
                                 env_name: 'FL_IOS_GENERATE_STRINGS_FILE_FROM_CODE_OUTPUT_DIR',
                                 description: 'The path to the directory where the generated `.strings` files should be created',
                                 type: String),
    FastlaneCore::ConfigItem.new(key: :fail_on_error,
                                 env_name: 'FL_IOS_GENERATE_STRINGS_FILE_FROM_CODE_FAIL_ON_ERROR',
                                 description: 'If true, will fail with user_error! if `genstrings` printed any error while parsing',
                                 type: Boolean,
                                 default_value: true),
  ]
end

.descriptionObject



38
39
40
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/ios/ios_generate_strings_file_from_code.rb', line 38

def self.description
  'Generate the `.strings` files from your Objective-C and Swift code'
end

.detailsObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/ios/ios_generate_strings_file_from_code.rb', line 42

def self.details
  <<~DETAILS
    Uses `genstrings` to generate the `.strings` files from your Objective-C and Swift code.
    (especially `Localizable.strings` but it could generate more if the code uses custom tables).

    You can provide a list of paths to scan but also paths to exclude. Both supports glob patterns.
    You can also optionally provide a list of custom "routines" (aka macros or functions) that
    `genstrings` should parse in addition to the usual `NSLocalizedString`. (see `-s` option of `genstrings`).

    Tip: support for custom routines is useful if some of your targets define a helper function e.g.
    `PodLocalizedString` to wrap calls to `Bundle.localizedString(forKey: key, value: value, table: nil)`,
    just like the build-in `NSLocalizedString` does, but providing a custom bundle to look up the strings from.
  DETAILS
end

.files_matching(paths:, exclude:) ⇒ Object



27
28
29
30
31
32
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/ios/ios_generate_strings_file_from_code.rb', line 27

def self.files_matching(paths:, exclude:)
  globbed_paths = paths.map { |p| glob_pattern(p) }
  Dir.glob(globbed_paths).reject do |file|
    exclude&.any? { |ex| File.fnmatch?(ex, file) }
  end
end

.glob_pattern(path) ⇒ Object

Adds the proper ‘*/.m,swift` to the list of paths



17
18
19
20
21
22
23
24
25
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/ios/ios_generate_strings_file_from_code.rb', line 17

def self.glob_pattern(path)
  if path.end_with?('**') || path.end_with?('**/')
    File.join(path, '*.{m,swift}')
  elsif File.directory?(path) || path.end_with?('/')
    File.join(path, '**', '*.{m,swift}')
  else
    path
  end
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/ios/ios_generate_strings_file_from_code.rb', line 110

def self.is_supported?(platform)
  [:ios, :mac].include?(platform)
end

.return_typeObject



96
97
98
99
100
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/ios/ios_generate_strings_file_from_code.rb', line 96

def self.return_type
  # Describes what type of data is expected to be returned
  # see RETURN_TYPES in https://github.com/fastlane/fastlane/blob/master/fastlane/lib/fastlane/action.rb
  :array_of_strings
end

.return_valueObject



102
103
104
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/ios/ios_generate_strings_file_from_code.rb', line 102

def self.return_value
  'List of warning lines generated by genstrings on stdout'
end

.run(params) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/ios/ios_generate_strings_file_from_code.rb', line 4

def self.run(params)
  files = files_matching(paths: params[:paths], exclude: params[:exclude])
  flags = [('-q' if params[:quiet]), ('-SwiftUI' if params[:swiftui])].compact
  flags += Array(params[:routines]).flat_map { |routine| ['-s', routine] }
  cmd = ['genstrings', '-o', params[:output_dir], *flags, *files]
  out = Actions.sh_control_output(*cmd, print_command: FastlaneCore::Globals.verbose?, print_command_output: true)
  out = out.scrub.strip.split("\n")
  errors = out.select { |line| line.include?('genstrings: error: ') }
  UI.user_error!(errors.join("\n")) unless !params[:fail_on_error] || errors.empty?
  out
end