Class: Fastlane::Helper::FlutterHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/flutter/helper/flutter_helper.rb

Class Method Summary collapse

Class Method Details

.android_sdk_root!Object



133
134
135
136
137
138
139
140
# File 'lib/fastlane/plugin/flutter/helper/flutter_helper.rb', line 133

def self.android_sdk_root!
  (ENV['ANDROID_HOME'] || ENV['ANDROID_SDK_ROOT']).tap do |path|
    unless path
      raise 'Android SDK directory environment variables are not set. ' \
        'See https://developer.android.com/studio/command-line/variables'
    end
  end
end

.bootstrap_android(licenses = {}) ⇒ Object

Bootstrap Flutter application for build with Android SDK: github.com/flutter/flutter/issues/28076



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
126
127
128
129
130
131
# File 'lib/fastlane/plugin/flutter/helper/flutter_helper.rb', line 101

def self.bootstrap_android(licenses = {})
  # Gradle will refuse to install Android SDK unless the SDK directory
  # exists and licenses are accepted.
  licenses_directory = File.join(android_sdk_root!, 'licenses')
  FileUtils.mkdir_p(licenses_directory)
  if licenses.any?
    licenses.each_pair do |license, hash|
      license_file = File.join(licenses_directory, license)
      unless File.exist?(license_file) &&
             File.readlines(license_file).include?(hash)
        File.open(license_file, 'a') { |f| f.puts('', hash) }
      end
    end
  end

  android_properties_file = 'android/local.properties'
  flutter_sdk_par = 'flutter.sdk'
  unless File.exist?(android_properties_file) &&
         File.read(android_properties_file).include?(flutter_sdk_par)
    flutter_sdk_path = File.dirname(File.dirname(which('flutter')))
    File.open(android_properties_file, 'a') do |properties|
      properties.write("\n      ### Generated by \#{$0}\#{__method__} ###\n      \#{flutter_sdk_par}=\#{flutter_sdk_path}\n      CONFIG\n    end\n  end\n\n  download_android_dependencies\nend\n".gsub(/^\s+/, ''))

.build_name(schema) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/fastlane/plugin/flutter/helper/flutter_helper.rb', line 90

def self.build_name(schema)
  if schema && schema.start_with?('vcs')
    dirty_mark = schema['vcs'.size..-1]
    Actions.sh(*%W(git describe --tags --dirty=#{dirty_mark})).strip
  else
    schema
  end
end

.build_number(schema) ⇒ Object



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
# File 'lib/fastlane/plugin/flutter/helper/flutter_helper.rb', line 63

def self.build_number(schema)
  schema and Integer(schema)
rescue ArgumentError
  build_number_source, build_number_base = schema.split('+', 2)

  case build_number_source
  when 'vcs'
    build_number = Integer(
      Actions.sh(*%w(git rev-list --count HEAD)).strip
    )
  when 'ci'
    begin
      build_number = Integer(ENV['TRAVIS_BUILD_NUMBER'] ||
                             ENV['CIRCLE_BUILD_NUM'])
    rescue ArgumentError, TypeError
      raise if ENV.key?('CI')
      raise ArgumentError, 'CI version requested, but not running on a CI'
    end
  end

  if build_number_base
    build_number + Integer(build_number_base)
  else
    build_number
  end
end

.compare_arb(origin, sample) ⇒ Object



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
# File 'lib/fastlane/plugin/flutter/helper/flutter_helper.rb', line 36

def self.compare_arb(origin, sample)
  is_significant_key = ->(key) { !key.start_with?('@') }

  origin_keys = Set.new(JSON.parse(File.read(origin))
    .keys
    .select(&is_significant_key))
  sample_keys = Set.new(JSON.parse(File.read(sample))
    .keys
    .select(&is_significant_key))

  keys_not_in_sample = origin_keys - sample_keys
  keys_not_in_origin = sample_keys - origin_keys

  differencies = []
  if keys_not_in_sample.any?
    differencies.push("Translation string(s): " \
                      "#{keys_not_in_sample.to_a.join(', ')}; " \
                      "are missing")
  end
  if keys_not_in_origin.any?
    differencies.push("Translation string(s): " \
                      "#{keys_not_in_origin.to_a.join(', ')}; " \
                      "are unused")
  end
  differencies
end

.download_android_dependenciesObject



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/fastlane/plugin/flutter/helper/flutter_helper.rb', line 142

def self.download_android_dependencies
  Tempfile.create('resolveDependencies.gradle') do |init_script|
    init_script.puts("    allprojects {\n        task resolveDependencies {\n            doLast {\n                project.configurations.each { configuration ->\n                    if (configuration.isCanBeResolved()) {\n                        configuration.resolve()\n                    }\n                }\n                project.buildscript.configurations.each { configuration ->\n                    if (configuration.isCanBeResolved()) {\n                        configuration.resolve()\n                    }\n                }\n            }\n        }\n    }\n    GRADLE\n    init_script.flush\n\n    Dir.chdir('android') do\n      Actions.sh('./gradlew', '--init-script', init_script.path,\n                 '--', ':resolveDependencies')\n    end\n  end\nend\n")

.reformat_arb(file_name) ⇒ Object



30
31
32
33
34
# File 'lib/fastlane/plugin/flutter/helper/flutter_helper.rb', line 30

def self.reformat_arb(file_name)
  pretty_content = JSON.pretty_generate(JSON.parse(File.read(file_name)))

  File.write(file_name, pretty_content + "\n")
end

.restore_l10n_timestamp(file_name, old_content) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/fastlane/plugin/flutter/helper/flutter_helper.rb', line 12

def self.restore_l10n_timestamp(file_name, old_content)
  new_content_tree = JSON.parse(File.read(file_name))
  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(file_name, old_content)
    return true
  end

  false
end

.which(cmd) ⇒ Object



172
173
174
175
176
177
178
179
180
181
# File 'lib/fastlane/plugin/flutter/helper/flutter_helper.rb', line 172

def self.which(cmd)
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exts.each do |ext|
      exe = File.join(path, "#{cmd}#{ext}")
      return exe if File.executable?(exe) && !File.directory?(exe)
    end
  end
  nil
end