Skip to content

Commit e0e26e5

Browse files
committed
Add code to ensure locales declaration consistency
By comparing the locales limited by the `resourcesConfiguration` field in a product flavor with the ones we hardcoded in our Fastlane scripts. This way if we decide to update either place in the future (e.g. add new locales to download from GlotPress for Jetpack by updating the `JP_APP_LOCALES` constant in `fastlane/lanes/localization.rb`) we'll be reminded to ensure we updated the other as well (i.e. the `resourceConfigurations` in the `jetpack` flavor of the `WordPress/build.gradle` file)
1 parent 409b776 commit e0e26e5

2 files changed

Lines changed: 42 additions & 4 deletions

File tree

WordPress/build.gradle

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,16 @@ tasks.register("printAllVersions") {
603603
}
604604
}
605605

606+
tasks.register("printResourceConfigurations") {
607+
doLast {
608+
android.productFlavors.each { flavor ->
609+
if (flavor.dimension == "app") {
610+
println "${flavor.name}: ${flavor.resourceConfigurations}"
611+
}
612+
}
613+
}
614+
}
615+
606616
def checkGradlePropertiesFile() {
607617
def inputFile = file("${rootDir}/gradle.properties")
608618
if (!inputFile.exists()) {

fastlane/lanes/localization.rb

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -390,17 +390,17 @@
390390
#####################################################################################
391391
lane :download_translations do
392392
# WordPress strings
393-
wordpress_res_dir = File.join('WordPress', 'src', 'main', 'res')
393+
check_declared_locales_consistency(app_flavor: 'wordpress', locales_list: WP_APP_LOCALES)
394394
android_download_translations(
395-
res_dir: wordpress_res_dir,
395+
res_dir: File.join('WordPress', 'src', 'main', 'res'),
396396
glotpress_url: APP_SPECIFIC_VALUES[:wordpress][:glotpress_appstrings_project],
397397
locales: WP_APP_LOCALES
398398
)
399399

400400
# Jetpack strings
401-
jetpack_res_dir = File.join('WordPress', 'src', 'jetpack', 'res')
401+
check_declared_locales_consistency(app_flavor: 'jetpack', locales_list: JP_APP_LOCALES)
402402
android_download_translations(
403-
res_dir: jetpack_res_dir,
403+
res_dir: File.join('WordPress', 'src', 'jetpack', 'res'),
404404
glotpress_url: APP_SPECIFIC_VALUES[:jetpack][:glotpress_appstrings_project],
405405
locales: JP_APP_LOCALES
406406
)
@@ -421,4 +421,32 @@ def update_po_file_for_metadata_localization(po_path:, sources:, release_version
421421
git_add(path: po_path)
422422
git_commit(path: po_path, message: commit_message, allow_nothing_to_commit: true)
423423
end
424+
425+
# Compares the list of locales declared in the `resourceConfigurations` field of `build.gradle` for a given flavor
426+
# with the hardcoded list of locales we use in our Fastlane lanes, to ensure they match and we are consistent.
427+
#
428+
# @param [String] app_flavor `"wordpress"` or `"jetpack"` — The `productFlavor` to read from in the build.gradle
429+
# @param [Array<Hash>] locales_list The list of Hash defining the locales to compare that list to.
430+
# Typically one of the `WP_APP_LOCALES` or `JP_APP_LOCALES` constants
431+
def check_declared_locales_consistency(app_flavor:, locales_list:)
432+
output = gradle(task: 'printResourceConfigurations')
433+
resource_configs = output.match(/^#{app_flavor}: \[(.*)\]$/)&.captures&.first&.gsub(' ','')&.split(',')&.sort
434+
if resource_configs.nil? || resource_configs.empty?
435+
UI.message("No `resourceConfigurations` field set in `build.gradle` for the `#{app_flavor}` flavor. Nothing to check.")
436+
return
437+
end
438+
439+
expected_locales = locales_list.map { |l| l[:android] }.sort
440+
if resource_configs == expected_locales
441+
UI.message("The `resourceConfigurations` field set in `build.gradle` for the `#{app_flavor}` flavor matches what is set in our Fastfile. All is good!")
442+
else
443+
UI.user_error! <<~ERROR
444+
The list of `resourceConfigurations` declared in your `build.gradle` for the `#{app_flavor}` flavor
445+
does not match the list of locales we hardcoded in the `fastlane/lanes/localization.rb` for this app.
446+
447+
If you recently updated the hardcoded list of locales to include for this app, be sure to apply those
448+
changes in both places, to keep the Fastlane scripts consistent with the gradle configuration of your app.
449+
ERROR
450+
end
451+
end
424452
end

0 commit comments

Comments
 (0)