Skip to content

Commit 5a8bad3

Browse files
committed
fastlane: implement lanes to handle/bump versions
Signed-off-by: Álvaro Brey <alvaro.brey@nextcloud.com>
1 parent 96c1393 commit 5a8bad3

2 files changed

Lines changed: 141 additions & 18 deletions

File tree

fastlane/Fastfile

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ default_platform(:android)
2828

2929
BUNDLE_PATH = "app/build/outputs/bundle/playRelease/app-play-release.aab"
3030

31+
import("./utils.Fastfile")
32+
3133
platform :android do
3234
desc "Build app bundle"
3335

@@ -63,31 +65,14 @@ desc "Build app bundle"
6365
uploadToPlayStore()
6466
end
6567

66-
desc "Read versions from gradle file"
67-
private_lane :getVersionInfo do
68-
File.open("../app/build.gradle","r") do |file|
69-
text = file.read
70-
versionName = text.match(/versionName "([0-9\.]*)"$/)[1]
71-
versionCode = text.match(/versionCode ([0-9]*)$/)[1]
72-
73-
{ "versionCode" => versionCode, "versionName" => versionName }
74-
end
75-
end
76-
7768
desc "Show versions and prompt for confirmation"
7869
private_lane :promptVersion do |versionInfo|
7970
currentBranch = git_branch()
8071
print "Version code: #{versionInfo["versionCode"]}\n"
8172
print "Version name: #{versionInfo["versionName"]}\n"
8273
print "Current branch: #{currentBranch}\n"
8374
print "Tag (to be created): #{versionInfo["versionName"]}\n"
84-
85-
86-
answer = prompt(text: "is this okay?", boolean: true)
87-
88-
if !answer
89-
exit
90-
end
75+
promptYesNo()
9176
end
9277

9378
desc "Check if release artifacts exist"

fastlane/utils.Fastfile

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
BUILD_NUMBER_RC_START = 51
2+
BUILD_NUMBER_FINAL_START = 90
3+
4+
MAJOR_MULTIPLIER = 10000000
5+
MINOR_MULTIPLIER = 10000
6+
PATCH_MULTIPLIER = 100
7+
8+
platform :android do
9+
desc "Print version info"
10+
lane :printVersionInfo do
11+
versionInfo = getVersionInfo()
12+
versionComponents = parseVersionCode(versionInfo)
13+
print "Version code: #{versionInfo["versionCode"]}\n"
14+
print "Version name: #{versionInfo["versionName"]}\n"
15+
print "Major: #{versionComponents["major"]}\n"
16+
print "Minor: #{versionComponents["minor"]}\n"
17+
print "Patch: #{versionComponents["patch"]}\n"
18+
print "Build: #{versionComponents["build"]}\n"
19+
end
20+
21+
# Usage: fastlane incrementVersion [type:major|minor|patch|rc|final]
22+
# For major, minor, and patch: will increment that version number by 1 and set the smaller ones to 0
23+
# For rc, final: will set build number to first rc/first final or increment it by 1
24+
desc "Increment version code and version name"
25+
lane :incrementVersion do |options|
26+
versionInfo = getVersionInfo()
27+
versionComponents = parseVersionCode(versionInfo)
28+
newVersionComponents = incrementVersionComponents(versionComponents: versionComponents, type: options[:type])
29+
versionNameGenerated = generateVersionName(newVersionComponents)
30+
versionCodeGenerated = generateVersionCode(newVersionComponents)
31+
32+
print "Version code: #{versionInfo["versionCode"]} -> #{versionCodeGenerated}\n"
33+
print "Version name: #{versionInfo["versionName"]} -> #{versionNameGenerated}\n"
34+
promptYesNo()
35+
writeVersions(versionCode: versionCodeGenerated, versionName: versionNameGenerated)
36+
end
37+
38+
39+
desc "Parse major, minor, patch and build from versionCode"
40+
private_lane :parseVersionCode do |versionInfo|
41+
versionCode = versionInfo["versionCode"]
42+
build = versionCode % 100
43+
patch = (versionCode / PATCH_MULTIPLIER) % 100
44+
minor = (versionCode / MINOR_MULTIPLIER) % 100
45+
major = (versionCode / MAJOR_MULTIPLIER) % 100
46+
47+
{ "major" => major, "minor" => minor, "patch" => patch, "build" => build }
48+
end
49+
50+
desc "Generate versionCode from version components"
51+
private_lane :generateVersionCode do |versionComponents|
52+
print "Generating version code from #{versionComponents}\n"
53+
major = versionComponents["major"]
54+
minor = versionComponents["minor"]
55+
patch = versionComponents["patch"]
56+
build = versionComponents["build"]
57+
test = major * MAJOR_MULTIPLIER + minor * MINOR_MULTIPLIER + patch * PATCH_MULTIPLIER + build
58+
test
59+
end
60+
61+
desc "Compute version name from version code"
62+
private_lane :generateVersionName do |versionComponents|
63+
suffix = ""
64+
buildNumber = versionComponents["build"]
65+
case
66+
when buildNumber >= BUILD_NUMBER_RC_START && buildNumber < BUILD_NUMBER_FINAL_START
67+
rcNumber = (buildNumber - BUILD_NUMBER_RC_START) + 1
68+
suffix = " RC #{rcNumber}"
69+
when buildNumber < BUILD_NUMBER_RC_START
70+
suffix = " Alpha #{buildNumber + 1}"
71+
end
72+
"#{versionComponents["major"]}.#{versionComponents["minor"]}.#{versionComponents["patch"]}#{suffix}"
73+
end
74+
75+
desc "Read versions from gradle file"
76+
private_lane :getVersionInfo do
77+
File.open("../app/build.gradle","r") do |file|
78+
text = file.read
79+
versionName = text.match(/versionName "(.*)"$/)[1]
80+
versionCode = text.match(/versionCode ([0-9]*)$/)[1].to_i
81+
82+
{ "versionCode" => versionCode, "versionName" => versionName }
83+
end
84+
end
85+
86+
desc "Write versions to gradle file"
87+
private_lane :writeVersions do |options|
88+
File.open("../app/build.gradle","r+") do |file|
89+
text = file.read
90+
text.gsub!(/versionName "(.*)"$/, "versionName \"#{options[:versionName]}\"")
91+
text.gsub!(/versionCode ([0-9]*)$/, "versionCode #{options[:versionCode]}")
92+
file.rewind
93+
file.write(text)
94+
file.truncate(file.pos)
95+
end
96+
end
97+
98+
private_lane :incrementVersionComponents do |options|
99+
versionComponents = options[:versionComponents]
100+
case options[:type]
101+
when "major"
102+
versionComponents["major"] = versionComponents["major"] + 1
103+
versionComponents["minor"] = 0
104+
versionComponents["patch"] = 0
105+
versionComponents["build"] = 0
106+
when "minor"
107+
versionComponents["minor"] = versionComponents["minor"] + 1
108+
versionComponents["patch"] = 0
109+
versionComponents["build"] = 0
110+
when "patch"
111+
versionComponents["patch"] = versionComponents["patch"] + 1
112+
versionComponents["build"] = 0
113+
when "rc"
114+
if versionComponents["build"] < BUILD_NUMBER_RC_START || versionComponents["build"] >= BUILD_NUMBER_FINAL_START
115+
versionComponents["build"] = BUILD_NUMBER_RC_START
116+
else
117+
versionComponents["build"] = versionComponents["build"] + 1
118+
end
119+
when "final"
120+
if versionComponents["build"] < BUILD_NUMBER_FINAL_START
121+
versionComponents["build"] = BUILD_NUMBER_FINAL_START
122+
else
123+
versionComponents["build"] = versionComponents["build"] + 1
124+
end
125+
else
126+
print "Unknown or missing version type: #{options[:type]}\n"
127+
exit
128+
end
129+
versionComponents
130+
end
131+
132+
private_lane :promptYesNo do
133+
answer = prompt(text: "is this okay?", boolean: true)
134+
if !answer
135+
exit
136+
end
137+
end
138+
end

0 commit comments

Comments
 (0)