Class: Fastlane::Helper::FlutterGenerateIntlHelper
- Inherits:
-
Object
- Object
- Fastlane::Helper::FlutterGenerateIntlHelper
- Defined in:
- lib/fastlane/plugin/flutter/helper/flutter_generate_intl_helper.rb
Class Method Summary collapse
- .amend_arb_files(arb_files_dirname, full_arb_filename) ⇒ Object
- .generate(messages_filename, messages_locale = nil) ⇒ Object
- .generate_arb_from_dart(dart_filename, dart_locale, arb_dirname) ⇒ Object
- .restore_last_modified(filename, old_content) ⇒ Object
Class Method Details
.amend_arb_files(arb_files_dirname, full_arb_filename) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/fastlane/plugin/flutter/helper/flutter_generate_intl_helper.rb', line 41 def self.amend_arb_files(arb_files_dirname, full_arb_filename) full_arb_json = JSON.parse(File.read(full_arb_filename)) # Sort files for consistency, because generated messages_all.dart will # have imports ordered as in the command line below. arb_filenames = Dir.glob(File.join(arb_files_dirname, 'intl_*.arb')).sort arb_filenames.each do |arb_filename| arb_json = JSON.parse(File.read(arb_filename)) if arb_filename != full_arb_filename Fastlane::UI.("Amending #{arb_filename}...") full_arb_json.each_pair do |k, v| # Ignore @@keys. We don't want to copy @@locale over all files, and # it's often unspecified to be inferred from file name. arb_json[k] ||= v unless k.start_with?('@@') end arb_json.keep_if { |k| full_arb_json.key?(k) } end File.write(arb_filename, "#{JSON.pretty_generate(arb_json)}\n") end end |
.generate(messages_filename, messages_locale = nil) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/fastlane/plugin/flutter/helper/flutter_generate_intl_helper.rb', line 17 def self.generate(, = nil) dart_files_dirname = File.dirname() arb_files_dirname = File.join(dart_files_dirname, 'arb') full_arb_filename = generate_arb_from_dart( , , arb_files_dirname ) arb_filenames = amend_arb_files(arb_files_dirname, full_arb_filename) unless # Don't generate .dart for the ARB generated from original, unless it # has its own locale. arb_filenames.delete(full_arb_filename) end Fastlane::UI.('Generating .dart files from .arb...') Fastlane::Helper::FlutterHelper.flutter( *%W[packages pub run intl_translation:generate_from_arb --output-dir=#{dart_files_dirname} --no-use-deferred-loading #{}] + arb_filenames ) end |
.generate_arb_from_dart(dart_filename, dart_locale, arb_dirname) ⇒ Object
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 |
# File 'lib/fastlane/plugin/flutter/helper/flutter_generate_intl_helper.rb', line 62 def self.generate_arb_from_dart(dart_filename, dart_locale, arb_dirname) arb_filename = File.join(arb_dirname, 'intl_messages.arb') Fastlane::UI.("Generating #{arb_filename} from #{dart_filename}...") if File.exist?(arb_filename) arb_file_was = File.read(arb_filename) else # The file may not exist on the first run. Then it's also probable that # the output directory does not exist yet. FileUtils.mkdir_p(arb_dirname) end = ["--output-dir=#{arb_dirname}"] .push("--locale=#{dart_locale}") if dart_locale Fastlane::Helper::FlutterHelper.flutter( *%w[packages pub run intl_translation:extract_to_arb], *, dart_filename ) # intl will update @@last_modified even if there are no updates; this # leaves Git directory unnecessary dirty. If that's the only change, # just restore the previous contents. if arb_file_was && restore_last_modified(arb_filename, arb_file_was) Fastlane::UI.( "@@last_modified has been restored in #{arb_filename}" ) end arb_filename end |
.restore_last_modified(filename, old_content) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/fastlane/plugin/flutter/helper/flutter_generate_intl_helper.rb', line 94 def self.restore_last_modified(filename, old_content) new_content_tree = JSON.parse(File.read(filename)) old_content_tree = JSON.parse(old_content) new_content_tree['@@last_modified'] = old_content_tree['@@last_modified'] # Use to_json to compare the objects deep and in consistent format. if new_content_tree.to_json == old_content_tree.to_json # Except for the @@last_modified attribute that we replaced # above, the objects are identical. Restore previous timestamp. File.write(filename, old_content) return true end false end |