Module: Fastlane::Sunny

Defined in:
lib/fastlane/plugin/sunny_project/helper/sunny_project_helper.rb

Class Method Summary collapse

Class Method Details

.blank(str) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/fastlane/plugin/sunny_project/helper/sunny_project_helper.rb', line 17

def self.blank(str)
  if str
    str.strip.empty?
  else
    true
  end
end

.build_ios(build_ver, build_num, **options) ⇒ Object



256
257
258
259
260
# File 'lib/fastlane/plugin/sunny_project/helper/sunny_project_helper.rb', line 256

def self.build_ios(build_ver, build_num, **options)
  flutter = get_flutter(options[:flutter])

  self.exec_cmd("build flutter ios release #{build_ver} #{build_num}", "#{flutter} build ios --release --no-tree-shake-icons --no-codesign")
end

.build_numberObject



271
272
273
# File 'lib/fastlane/plugin/sunny_project/helper/sunny_project_helper.rb', line 271

def self.build_number
  self.current_semver.build
end

.build_runner(options) ⇒ Object



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
# File 'lib/fastlane/plugin/sunny_project/helper/sunny_project_helper.rb', line 208

def self.build_runner(options)
  flutter = get_flutter(options[:flutter])
  opt_hash = config_to_hash(options)
  if options[:clean]
    exec_cmd("flutter clean", "#{flutter} clean", **opt_hash)
  end

  if options[:clean] || (!options[:skip_pub])
    exec_cmd("flutter pub get", "#{flutter} pub get", **opt_hash)
  end

  if options[:clean] || (!options[:skip_gen])
    dc = if options[:clean]
           " --delete-conflicting-outputs"
         else
           ""
         end
    vb = if options[:verbose]
           " -v"
         else
           ""
         end
    exec_cmd("flutter pub run build_runner build#{dc}#{vb}", "#{flutter} pub run build_runner build#{dc}#{vb}", **opt_hash)
  end
end

.config(available_options, options) ⇒ Object



39
40
41
# File 'lib/fastlane/plugin/sunny_project/helper/sunny_project_helper.rb', line 39

def self.config(available_options, options)
  FastlaneCore::Configuration.create(available_options, options)
end

.config_to_hash(options) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/fastlane/plugin/sunny_project/helper/sunny_project_helper.rb', line 91

def self.config_to_hash(options)
  hash = Hash([])
  options.all_keys.each do |key|
    hash.store(key, options.fetch(key, ask: false))
  end
  return hash
end

.current_semverObject

Reads the latest version from pubspec.yaml



133
134
135
# File 'lib/fastlane/plugin/sunny_project/helper/sunny_project_helper.rb', line 133

def self.current_semver
  Semantic::Version.new(current_version_string)
end

.current_semver_pathObject

Reads the latest version from pubspec.yaml



263
264
265
266
267
268
269
# File 'lib/fastlane/plugin/sunny_project/helper/sunny_project_helper.rb', line 263

def self.current_semver_path
  version = nil
  Dir.chdir("..") do
    version = self.current_semver
  end
  version
end

.current_version_stringObject

Retrieves the current semver based on git tags



276
277
278
# File 'lib/fastlane/plugin/sunny_project/helper/sunny_project_helper.rb', line 276

def self.current_version_string
  self.exec_cmd("get version", "pubver get", quiet: true)
end

.do_increase_version(options) ⇒ Object



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
77
78
79
80
81
# File 'lib/fastlane/plugin/sunny_project/helper/sunny_project_helper.rb', line 51

def self.do_increase_version(options)
  curr = self.current_semver

  if curr.pre
    me = curr.pre
    pos = me.rindex(".")
    pre_id = me[0...pos]
    pre_num = me[pos + 1..-1]
    curr.pre = "#{pre_id}.#{Integer(pre_num) + 1}"
    self.exec_cmd("pubver set #{curr}", "pubver set #{curr}")
  else
    bump_type = options[:type]
    bump_type = "build" unless bump_type
    if bump_type.eql?('build')
    elsif bump_type.eql?('patch')
    elsif bump_type.eql?('minor')
    elsif bump_type.eql?('major')
    end

    command = "pubver bump #{bump_type}"
    unless bump_type.eql?('build')
      command += " -b"
    end
    self.exec_cmd(command.to_s, command)

    unless bump_type.eql?('build')
      self.exec_cmd("also bump build", "pubver bump build")
    end
  end
  self.current_semver
end

.exec_cmd(name, *command, **args) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/fastlane/plugin/sunny_project/helper/sunny_project_helper.rb', line 99

def self.exec_cmd(name, *command, **args)
  if command.count > 1
    command = command.map { |item| Shellwords.escape(item) }
  end
  joined = command.join(" ")
  if args[:verbose]
    begin
      return Fastlane::Actions.sh(*command, log: true, error_callback: ->(str) { UI.user_error!(">> #{name} failed << \n #{str}") })
    rescue StandardError => e
      UI.user_error!(">> #{name} failed << \n  #{e}")
    end
  else
    if args[:cmd_out]
      UI.command_output(name)
    elsif args[:quiet]
    else
      UI.command name
    end

    stdout, err, status = Open3.capture3(joined)
    UI.user_error!(">> #{name} failed << \n  command: #{joined}\n  error: #{err}") unless status == 0
    stdout
  end
end

.exec_cmd_options(name, command, options) ⇒ Object



124
125
126
# File 'lib/fastlane/plugin/sunny_project/helper/sunny_project_helper.rb', line 124

def self.exec_cmd_options(name, command, options)
  return exec_cmd(name, command, **config_to_hash(options))
end

.finalize_version(options) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/fastlane/plugin/sunny_project/helper/sunny_project_helper.rb', line 137

def self.finalize_version(options)
  version = self.current_semver
  # If we got this far, let's commit the build number and update the git tags.  If the rest of the pro
  # process fails, we should revert this because it will mess up our commit logs
  self.run_action(Fastlane::Actions::GitAddAction, path: %w[./pubspec.yaml ./pubspec.lock ./CHANGELOG.md])
  self.run_action(Fastlane::Actions::GitCommitAction, path: %w[./pubspec.yaml ./pubspec.lock ./CHANGELOG.md],
                  allow_nothing_to_commit: false,

                  message: "Version bump to: #{version.major}.#{version.minor}.#{version.patch}#800#{version.build}")
  self.run_action(Fastlane::Actions::AddGitTagAction,
                  tag: "sunny/builds/v#{version.build}",
                  force: true,
                  sign: false,
  )
  self.run_action(Fastlane::Actions::PushGitTagsAction, force: true)
  if File.exist?(self.release_notes_file)
    File.delete(self.release_notes_file)
  end
end

.get_flutter(provided = nil) ⇒ Object



204
205
206
# File 'lib/fastlane/plugin/sunny_project/helper/sunny_project_helper.rb', line 204

def self.get_flutter(provided = nil)
  provided || ".fvm/flutter_sdk/bin/flutter"
end

.is_branch(branch_name) ⇒ Object



32
33
34
35
36
37
# File 'lib/fastlane/plugin/sunny_project/helper/sunny_project_helper.rb', line 32

def self.is_branch(branch_name)
  self.run_action(Fastlane::Actions::EnsureGitBranchAction, branch: branch_name)
  true
rescue
  false
end

.is_cleanObject



25
26
27
28
29
30
# File 'lib/fastlane/plugin/sunny_project/helper/sunny_project_helper.rb', line 25

def self.is_clean
  self.run_action(Fastlane::Actions::EnsureGitStatusCleanAction)
  true
rescue
  false
end

.mmp(semver) ⇒ Object



47
48
49
# File 'lib/fastlane/plugin/sunny_project/helper/sunny_project_helper.rb', line 47

def self.mmp(semver)
  "#{semver.major}.#{semver.minor}.#{semver.patch}"
end

.override_version(**options) ⇒ Object



234
235
236
237
238
239
240
241
242
# File 'lib/fastlane/plugin/sunny_project/helper/sunny_project_helper.rb', line 234

def self.override_version(**options)
  semver = options[:version]
  unless semver
    UI.user_error!("No version parameter found")
    return
  end
  self.exec_cmd("set_version", "pubver set #{semver}", quiet: true)
  self.sync_version_number(semver)
end

.release_notes(options) ⇒ Object



157
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
# File 'lib/fastlane/plugin/sunny_project/helper/sunny_project_helper.rb', line 157

def self.release_notes(options)
  changes = Sunny.string(options[:changelog])
  if Sunny.blank(changes)
    if File.file?(Sunny.release_notes_file)
      changes = Sunny.string(File.read(Sunny.release_notes_file))
      return changes
    end
    unless File.file?(Sunny.release_notes_file)
      changes = Sunny.string(Fastlane::Actions::ChangelogFromGitCommitsAction.run(
        path: "./",
        pretty: "%B",
        ancestry_path: false,
        match_lightweight_tag: true,
        quiet: false,
        merge_commit_filtering: ":exclude_merges"
      ))

      if Sunny.blank(changes)
        changes = Sunny.string(Fastlane::Actions::PromptAction.run(
          text: "Please Enter a description of what changed.\nWhen you are finished, type END\n Changelog: ",
          multi_line_end_keyword: 'END'))
      end
    end
    unless Sunny.blank(changes)
      File.open(Sunny.release_notes_file, 'w') { |file|
        file.write(changes)
      }
    end
    if File.file?(Sunny.release_notes_file)
      changes = Sunny.string(File.read(Sunny.release_notes_file))
    end
  end

  if File.file?("CHANGELOG.md")
    f = File.open("CHANGELOG.md", "r+")
    lines = f.readlines
    f.close
    v = Sunny.current_semver
    lines = ["## [#{v}]\n", " * #{changes}\n", "\n"] + lines

    output = File.new("CHANGELOG.md", "w")
    lines.each { |line| output.write(line) }
    output.close
  end
  changes
end

.release_notes_fileObject



128
129
130
# File 'lib/fastlane/plugin/sunny_project/helper/sunny_project_helper.rb', line 128

def self.release_notes_file
  ".release-notes"
end

.run_action(action, **options) ⇒ Object



43
44
45
# File 'lib/fastlane/plugin/sunny_project/helper/sunny_project_helper.rb', line 43

def self.run_action(action, **options)
  action.run(self.config(action.available_options, options))
end

.string(str) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/fastlane/plugin/sunny_project/helper/sunny_project_helper.rb', line 9

def self.string(str)
  if str
    str.strip
  else
    nil
  end
end

.sync_version_number(version) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
# File 'lib/fastlane/plugin/sunny_project/helper/sunny_project_helper.rb', line 244

def self.sync_version_number(version)
  if version
    self.run_action(Fastlane::Actions::IncrementVersionNumberAction,
                    version_number: "#{version.major}.#{version.minor}.#{version.patch}",
                    xcodeproj: "ios/Runner.xcodeproj"
    )
  else
    UI.user_error!("No version found")
  end

end

.underscore(str) ⇒ Object

lane :ximg do |options|

Dir.chdir("..") {
  cmd("dart asset_renamer.dart", "dart tools/asset_renamer.dart")
}

end

lane :icons do

download_icons
build_icon_fonts

end

lane :build_icon_fonts do

Dir.chdir("..") {
  cmd("Generate flutter icons", "icon_font_generator", "--from=iconsource/svg", "--class-name=AuthIcons",
      "--out-font=lib/fonts/AuthIcons.ttf", "--out-flutter=lib/auth_icon_font.dart", "--normalize")
}

end

lane :download_icons do

Dir.chdir("..") {
  cmd("Download icons", "dart", "tools/iconsource/downloader.dart")
}

end



304
305
306
307
308
309
310
# File 'lib/fastlane/plugin/sunny_project/helper/sunny_project_helper.rb', line 304

def self.underscore(str)
  str.gsub(/::/, '/').
    gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
    gsub(/([a-z\d])([A-Z])/, '\1_\2').
    tr("-", "_").
    downcase
end

.update_ios_project_version(new_version) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/fastlane/plugin/sunny_project/helper/sunny_project_helper.rb', line 83

def self.update_ios_project_version(new_version)
  Dir.chdir("ios") {
    puts("Updating XCode Project files: version:#{mmp(new_version)}, build: #{new_version.build}")
    self.run_action(Fastlane::Actions::IncrementVersionNumberAction, version_number: mmp(new_version))
    self.run_action(Fastlane::Actions::IncrementBuildNumberAction, build_number: new_version.build)
  }
end