Class: Fastlane::Actions::TranslateGptReleaseNotesAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/translate_gpt_release_notes/actions/translate_gpt_release_notes_action.rb

Class Method Summary collapse

Class Method Details

.authorsObject



317
318
319
# File 'lib/fastlane/plugin/translate_gpt_release_notes/actions/translate_gpt_release_notes_action.rb', line 317

def self.authors
  ["antonkarliner"]
end

.available_optionsObject



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/fastlane/plugin/translate_gpt_release_notes/actions/translate_gpt_release_notes_action.rb', line 158

def self.available_options
  [
    FastlaneCore::ConfigItem.new(
      key: :provider,
      env_name: 'TRANSLATION_PROVIDER',
      description: "Translation provider to use (#{Helper::Providers::ProviderFactory.available_provider_names.join(', ')})",
      type: String,
      default_value: 'openai',
      verify_block: proc do |value|
        unless Helper::Providers::ProviderFactory.valid_provider?(value)
          available = Helper::Providers::ProviderFactory.available_provider_names.join(', ')
          UI.user_error!("Invalid provider '#{value}'. Available: #{available}")
        end
      end
    ),
    FastlaneCore::ConfigItem.new(
      key: :openai_api_key,
      env_name: 'OPENAI_API_KEY',
      description: 'OpenAI API key (alternative to environment variable)',
      sensitive: true,
      code_gen_sensitive: true,
      optional: true,
      default_value: nil
    ),
    FastlaneCore::ConfigItem.new(
      key: :anthropic_api_key,
      env_name: 'ANTHROPIC_API_KEY',
      description: 'Anthropic API key (alternative to environment variable)',
      sensitive: true,
      code_gen_sensitive: true,
      optional: true,
      default_value: nil
    ),
    FastlaneCore::ConfigItem.new(
      key: :gemini_api_key,
      env_name: 'GEMINI_API_KEY',
      description: 'Google Gemini API key (alternative to environment variable)',
      sensitive: true,
      code_gen_sensitive: true,
      optional: true,
      default_value: nil
    ),
    FastlaneCore::ConfigItem.new(
      key: :deepl_api_key,
      env_name: 'DEEPL_API_KEY',
      description: 'DeepL API key (alternative to environment variable)',
      sensitive: true,
      code_gen_sensitive: true,
      optional: true,
      default_value: nil
    ),
    FastlaneCore::ConfigItem.new(
      key: :api_token,
      env_name: "GPT_API_KEY",
      description: "API token for ChatGPT",
      sensitive: true,
      code_gen_sensitive: true,
      default_value: ""
    ),
    FastlaneCore::ConfigItem.new(
      key: :model_name,
      env_name: "GPT_MODEL_NAME",
      description: "Name of the AI model to use (provider-specific)",
      default_value: "gpt-5.2"
    ),
    FastlaneCore::ConfigItem.new(
      key: :service_tier,
      env_name: "GPT_SERVICE_TIER",
      description: "OpenAI service tier to use (auto, default, flex, or priority)",
      type: String,
      optional: true,
      verify_block: proc do |value|
        next if value.nil? || value.to_s.strip.empty?
        allowed_values = %w[auto default flex priority]
        unless allowed_values.include?(value)
          UI.user_error!("Invalid service_tier '#{value}'. Allowed values: #{allowed_values.join(', ')}")
        end
      end
    ),
    FastlaneCore::ConfigItem.new(
      key: :request_timeout,
      env_name: "GPT_REQUEST_TIMEOUT",
      description: "Timeout for the request in seconds (auto-bumped to 900s for flex if lower)",
      type: Integer,
      default_value: 30
    ),
    FastlaneCore::ConfigItem.new(
      key: :temperature,
      env_name: "GPT_TEMPERATURE",
      description: "What sampling temperature to use, between 0 and 2",
      type: Float,
      optional: true,
      default_value: 0.5
    ),
    FastlaneCore::ConfigItem.new(
      key: :master_locale,
      env_name: "MASTER_LOCALE",
      description: "Master language/locale for the source texts",
      type: String,
      default_value: "en-US"
    ),
    FastlaneCore::ConfigItem.new(
      key: :platform,
      env_name: "PLATFORM",
      description: "Platform for which to translate (ios or android)",
      is_string: true, 
      default_value: 'ios'
    ),
    FastlaneCore::ConfigItem.new(
      key: :context,
      env_name: "GPT_CONTEXT",
      description: "Context for translation to improve accuracy",
      optional: true,
      type: String
    ),
    FastlaneCore::ConfigItem.new(
      key: :glossary,
      env_name: "GLOSSARY_PATH",
      description: "Path to a JSON glossary file with term translations per locale",
      optional: true,
      type: String,
      verify_block: proc do |value|
        next if value.nil? || value.to_s.strip.empty?
        UI.user_error!("Glossary file not found: #{value}") unless File.exist?(value)
      end
    ),
    FastlaneCore::ConfigItem.new(
      key: :glossary_dir,
      env_name: "GLOSSARY_DIR",
      description: "Path to directory with localization files (ARB, .strings, .xml, .json, .xliff) for auto-extracting glossary",
      optional: true,
      type: String,
      verify_block: proc do |value|
        next if value.nil? || value.to_s.strip.empty?
        UI.user_error!("Glossary directory not found: #{value}") unless Dir.exist?(value)
      end
    ),
    FastlaneCore::ConfigItem.new(
      key: :dry_run,
      env_name: 'TRANSLATE_DRY_RUN',
      description: 'Preview translations without writing files',
      type: Boolean,
      optional: true,
      default_value: false
    )
  ]
end

.descriptionObject



154
155
156
# File 'lib/fastlane/plugin/translate_gpt_release_notes/actions/translate_gpt_release_notes_action.rb', line 154

def self.description
  "Translate release notes using AI providers: OpenAI, Claude, Gemini, or DeepL"
end

.fetch_master_texts(base_directory, master_locale, is_ios) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/fastlane/plugin/translate_gpt_release_notes/actions/translate_gpt_release_notes_action.rb', line 82

def self.fetch_master_texts(base_directory, master_locale, is_ios)
  master_path = is_ios ? File.join(base_directory, master_locale) : File.join(base_directory, master_locale, 'changelogs')

  # Check if the master path exists
  unless Dir.exist?(master_path)
    UI.error("Master path does not exist: #{master_path}")
    return [nil, nil]
  end

  filename = is_ios ? 'release_notes.txt' : highest_numbered_file(master_path)
  file_path = File.join(master_path, filename)

  # Check if the file exists before reading
  unless File.exist?(file_path)
    UI.error("File does not exist: #{file_path}")
    return [nil, nil]
  end

  [File.read(file_path), file_path]
end

.highest_numbered_file(directory) ⇒ Object



104
105
106
# File 'lib/fastlane/plugin/translate_gpt_release_notes/actions/translate_gpt_release_notes_action.rb', line 104

def self.highest_numbered_file(directory)
  Dir[File.join(directory, '*.txt')].max_by { |f| File.basename(f, '.txt').to_i }.split('/').last
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


321
322
323
# File 'lib/fastlane/plugin/translate_gpt_release_notes/actions/translate_gpt_release_notes_action.rb', line 321

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

.list_locales(base_directory) ⇒ Object



78
79
80
# File 'lib/fastlane/plugin/translate_gpt_release_notes/actions/translate_gpt_release_notes_action.rb', line 78

def self.list_locales(base_directory)
  Dir.children(base_directory).select { |entry| File.directory?(File.join(base_directory, entry)) }
end

.outputObject



306
307
308
309
310
311
# File 'lib/fastlane/plugin/translate_gpt_release_notes/actions/translate_gpt_release_notes_action.rb', line 306

def self.output
  [
    ['LOCALES_TRANSLATED', 'List of locales to which translations were applied'],
    ['MASTER_LOCALE', 'The master language/locale used as the source for translations']
  ]
end


112
113
114
115
116
117
118
119
120
# File 'lib/fastlane/plugin/translate_gpt_release_notes/actions/translate_gpt_release_notes_action.rb', line 112

def self.print_dry_run_preview(translated_texts, params)
  android_limit = 500 # Google Play hard limit; see BaseProvider::ANDROID_CHAR_LIMIT
  UI.important('DRY RUN: no files will be written.')
  translated_texts.each do |locale, text|
    next if locale == params[:master_locale]

    print_locale_preview(locale, text, android_limit, params[:platform])
  end
end


122
123
124
125
126
127
128
129
130
131
# File 'lib/fastlane/plugin/translate_gpt_release_notes/actions/translate_gpt_release_notes_action.rb', line 122

def self.print_locale_preview(locale, text, android_limit, platform)
  if text.nil? || text.to_s.strip.empty?
    UI.warning("  #{locale}: translation FAILED (would be skipped)")
    return
  end
  length = text.length
  over = platform == 'android' && length > android_limit
  suffix = over ? " — EXCEEDS #{android_limit}-char Android limit" : ''
  UI.message("  #{locale}: #{length} chars#{suffix}")
end

.return_valueObject



313
314
315
# File 'lib/fastlane/plugin/translate_gpt_release_notes/actions/translate_gpt_release_notes_action.rb', line 313

def self.return_value
  nil
end

.run(params) ⇒ Object



10
11
12
13
14
15
16
17
18
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
# File 'lib/fastlane/plugin/translate_gpt_release_notes/actions/translate_gpt_release_notes_action.rb', line 10

def self.run(params)
  provider_name = params[:provider] || 'openai'

  unless Helper::CredentialResolver.credentials_exist?(provider_name, params)
    available = Helper::CredentialResolver.available_providers(params)
    if available.empty?
      UI.user_error!("No translation provider credentials configured. Set one of: OPENAI_API_KEY, ANTHROPIC_API_KEY, GEMINI_API_KEY, or DEEPL_API_KEY")
    else
      UI.user_error!("Provider '#{provider_name}' has no credentials. Available providers: #{available.join(', ')}")
    end
  end

  # Define the path for the last run time file
  last_run_file = "last_successful_run.txt"

  # Determine if iOS or Android based on the platform
  is_ios = params[:platform] == 'ios'
  base_directory = is_ios ? 'fastlane/metadata' : 'fastlane/metadata/android'

  # Check if the base directory exists before proceeding
  unless Dir.exist?(base_directory)
    UI.error("Directory does not exist: #{base_directory}")
    return
  end

  locales = list_locales(base_directory)
  master_texts, master_file_path = fetch_master_texts(base_directory, params[:master_locale], is_ios)

  # Skip translation if master texts are not found
  unless master_texts && master_file_path
    UI.message("Master file not found, skipping translation.")
    return
  end
  
  # Compare last modification time with the last run time
  if File.exist?(last_run_file) && File.exist?(master_file_path)
    last_run_time = File.read(last_run_file).to_i
    file_mod_time = File.mtime(master_file_path).to_i
    if file_mod_time <= last_run_time
      UI.message("No changes in source file detected, translation skipped.")
      return
    end
  end

  helper = Helper::TranslateGptReleaseNotesHelper.new(params)
  translated_texts = locales.each_with_object({}) do |locale, translations|
    next if locale == params[:master_locale]  # Skip master locale
    translations[locale] = helper.translate_text(master_texts, locale, params[:platform])
  end

  if params[:dry_run]
    print_dry_run_preview(translated_texts, params)
  else
    update_translated_texts(base_directory, translated_texts, is_ios, params)

    # Only mark the run successful if at least one translation actually succeeded;
    # otherwise a transient failure would suppress the next run's retry.
    any_success = translated_texts.any? do |locale, text|
      locale != params[:master_locale] && !translation_empty?(text)
    end
    if any_success
      File.write(last_run_file, Time.now.to_i)
    else
      UI.error("No translations succeeded; not updating #{last_run_file} so the next run will retry.")
    end
  end
end

.translation_empty?(text) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/fastlane/plugin/translate_gpt_release_notes/actions/translate_gpt_release_notes_action.rb', line 108

def self.translation_empty?(text)
  text.nil? || text.to_s.strip.empty?
end

.update_translated_texts(base_directory, translated_texts, is_ios, params) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/fastlane/plugin/translate_gpt_release_notes/actions/translate_gpt_release_notes_action.rb', line 133

def self.update_translated_texts(base_directory, translated_texts, is_ios, params)
  translated_texts.each do |locale, text|
    next if locale == params[:master_locale]  # Skip master locale

    if translation_empty?(text)
      UI.warning("Skipping #{locale}: translation failed or returned empty; existing file left unchanged.")
      next
    end

    target_path = is_ios ? File.join(base_directory, locale) : File.join(base_directory, locale, 'changelogs')

    # Ensure target path exists or create it
    FileUtils.mkdir_p(target_path) unless Dir.exist?(target_path)

    filename = is_ios ? 'release_notes.txt' : highest_numbered_file(File.join(base_directory, params[:master_locale], 'changelogs'))

    # Write the translated text to the file
    File.write(File.join(target_path, filename), text)
  end
end