Class: Fastlane::Actions::FlutterGenerateAction

Inherits:
Action
  • Object
show all
Extended by:
FlutterActionBase
Defined in:
lib/fastlane/plugin/flutter/actions/flutter_generate_action.rb

Constant Summary collapse

ALL_IMPORTS_TEST_FILE =

Although this file is autogenerated, we should not call it “.g.dart”, because it is common for coverage configuration to exclude such files. Note that it’s also common to configure Dart analyser to exclude these files from checks; this won’t match, and we have to use ignore_for_file for all known lint rules that we might be breaking.

'test/all_imports_for_coverage_test.dart'

Class Method Summary collapse

Methods included from FlutterActionBase

authors, is_supported?

Class Method Details

.available_optionsObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/fastlane/plugin/flutter/actions/flutter_generate_action.rb', line 93

def self.available_options
  # https://docs.fastlane.tools/advanced/actions/#configuration-files
  [
    FastlaneCore::ConfigItem.new(
      key: :intl_strings_file,
      env_name: 'FL_FLUTTER_INTL_STRINGS_FILE',
      description: 'Path to source .dart file with Intl.message calls',
      verify_block: proc do |value|
        UI.user_error!("File `#{value}' does not exist") if generate_translation? && !File.exist?(value)
      end,
      default_value: 'lib/intl/intl.dart'
    ),
    FastlaneCore::ConfigItem.new(
      key: :intl_strings_locale,
      env_name: 'FL_FLUTTER_INTL_STRINGS_LOCALE',
      description: 'Locale of the default data in the strings_file',
      optional: true
    ),
    FastlaneCore::ConfigItem.new(
      key: :coverage_all_imports,
      env_name: 'FL_FLUTTER_COVERAGE_ALL_IMPORTS',
      description: <<-DESCRIPTION,
      Set to true to generate an empty test importing all .dart files in
      lib/, which would allow calculating correct coverage numbers for the
      whole project. NOTE: Don't forget to add
        /#{ALL_IMPORTS_TEST_FILE}
      to .gitignore!
      DESCRIPTION
      optional: true,
      type: Boolean
    )
  ]
end

.descriptionObject



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/fastlane/plugin/flutter/actions/flutter_generate_action.rb', line 81

def self.description
  '(1) Run `flutter packages get`;  ' \
  '(2) Run `build_runner build` if build_runner is in dev_dependencies;' \
  '   ' \
  '(3) According to `package:intl`, take `$strings_file` and generate ' \
  '`${strings_file.dirname}/arb/intl_messages.arb`, then take all ' \
  'files matching `${strings_file.dirname}/intl_*.arb`, fix them and ' \
  'generate .dart files from them.  ' \
  '(4) Generate an empty test importing all files, which would be used ' \
  'to calculate correct full coverage numbers.'
end

.generate_translation?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/fastlane/plugin/flutter/actions/flutter_generate_action.rb', line 77

def self.generate_translation?
  Helper::FlutterHelper.dev_dependency?('intl_translation')
end

.run(params) ⇒ Object



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
# File 'lib/fastlane/plugin/flutter/actions/flutter_generate_action.rb', line 21

def self.run(params)
  Helper::FlutterHelper.flutter(*%w[packages get]) {}

  if params[:coverage_all_imports] && File.exist?(ALL_IMPORTS_TEST_FILE)
    # This file may somehow confuse codegeneration (e.g. built_value).
    File.delete(ALL_IMPORTS_TEST_FILE)
  end

  # In an ideal world, this should be a part of build_runner:
  # https://github.com/dart-lang/intl_translation/issues/32
  # Generate Intl messages before others, since these are static and
  # others may be not.
  if generate_translation?
    Helper::FlutterGenerateIntlHelper.generate(
      params[:intl_strings_file], params[:intl_strings_locale]
    )
  end

  if Helper::FlutterHelper.dev_dependency?('build_runner')
    UI.message('Found build_runner dependency, running build...')
    Helper::FlutterGenerateBuildRunnerHelper.build
  end

  return unless params[:coverage_all_imports]

  UI.message("Generating #{ALL_IMPORTS_TEST_FILE} for coverage...")

  dart_file_literals = Dir['lib/**/*.dart'].reject do |file_name|
    # ".g.dart" files often are "part of" files and can not be imported
    # directly. Commonly coverage for generated files is not that useful
    file_name.end_with?('.g.dart')
  end.map do |file_name|
    Helper::FlutterHelper
      .import_path_for_test(file_name, '..')
      .gsub("'", "\\\\'")
  end

  File.write(
    ALL_IMPORTS_TEST_FILE,
    <<~DART
      // This file is autogenerated by fastlane flutter_generate action.
      // It imports all files in lib/ so that test coverage in percentage
      // of overall project is calculated correctly. Do not modify this
      // file manually!

      // ignore_for_file: unused_import, directives_ordering
      // ignore_for_file: avoid_relative_lib_imports
      // ignore_for_file: lines_longer_than_80_chars

      #{dart_file_literals.map { |fn| "import '#{fn}';" }.sort.join("\n")}

      void main() {}
    DART
  )
end