-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathutils.gradle
275 lines (226 loc) · 7.79 KB
/
utils.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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
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
203
204
205
206
207
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// region versioning
def logd(message) {
if (LOG_UTILS == true)
println(message)
}
def commitHash() {
description = "git log -n 1 --format='%h'"
logd(description)
final def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'log', '-n', '1', "--format='%h'"
standardOutput = stdout
}
logd("Git Hash" + stdout.toString())
return stdout.toString().trim().replaceAll('\'', '')
}
def commitCount() {
final def stdout = new ByteArrayOutputStream()
description = "git rev-list --full-history --all"
logd(description)
exec {
commandLine 'git', 'rev-list', '--full-history', '--all'
standardOutput = stdout
}
final String output = stdout.toString()
final List<String> commits = output.split("\n")
logd("Version Code: " + commits.size())
return commits.size()
}
def simpleReleaseVersionName() {
description = "Creating release version name. Format: major.minor.build"
logd(description)
def tag = System.getenv("BITRISE_GIT_TAG")
if (tag != null)
return tag
final def buildNumber = buildNumberByCI()
def releaseName = branchName()
// remove 'origin' in from branch name
if (releaseName != null && releaseName.contains("origin/"))
releaseName = releaseName.replace("origin/", "")
if (releaseName.contains("release"))
releaseName = releaseName.replaceFirst(".*release.*\\/", "")
else
releaseName = VERSION_NAME
if (releaseName.count(".") < 2) {
if (buildNumber != null)
releaseName += "." + buildNumber
else
releaseName += "." + 0
}
logd("Version Name: " + releaseName)
return releaseName
}
def canonicalReleaseVersionName() {
description = "Creating release version name. Format: branch/major.minor.build-commithash"
logd(description)
final def buildNumber = buildNumberByCI()
def releaseName = branchName()
// remove 'origin' in from branch name
if (releaseName != null && releaseName.contains("origin/"))
releaseName = releaseName.replace("origin/", "")
if (!releaseName.contains("release"))
releaseName += "/" + VERSION_NAME
if (releaseName.count(".") < 2) {
if (buildNumber != null) {
releaseName += "." + buildNumber
} else {
releaseName += ".0"
}
}
releaseName += "-" + commitHash()
logd('Version Name: ' + releaseName)
return releaseName
}
def buildNumberByCI() {
description = 'Getting build number from Jenkins, Travis or Bitrise.'
logd(description)
final def env = System.getenv()
if (env.BITRISE_BUILD_NUMBER != null)
return Integer.parseInt(env.BITRISE_BUILD_NUMBER)
if (env.BUILD_NUMBER != null)
return Integer.parseInt(env.BUILD_NUMBER)
if (env.TRAVIS_BUILD_NUMBER != null)
return Integer.parseInt(env.TRAVIS_BUILD_NUMBER)
return 0
}
def branchName() {
description = 'Getting branch name from CI or from git directly.'
logd(description)
def branchName = branchNameByCI()
if (branchName == null)
branchName = branchNameByGit()
return branchName
}
def branchNameByCI() {
description = 'Getting build number from Jenkins, Travis or Bitrise.'
logd(description)
final def env = System.getenv()
if (env.BITRISE_GIT_BRANCH != null)
return env.BITRISE_GIT_BRANCH
if (env.GIT_BRANCH != null)
return env.GIT_BRANCH
if (env.TRAVIS_BRANCH != null)
return env.TRAVIS_BRANCH
return null
}
def branchNameByGit() {
final def stdout = new ByteArrayOutputStream()
description = 'git rev-parse --abbrev-ref HEAD'
logd(description)
exec {
commandLine 'git', 'rev-parse', '--abbrev-ref', 'HEAD'
standardOutput = stdout
}
final String output = stdout.toString().trim()
logd('Branch Name: ' + output)
return output
}
def gitLog() {
description = "git log --pretty=format:'[[%h]] %cn %s'"
logd(description)
final def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'log', "--pretty=format:'[[%h]] %cn %s'"
standardOutput = stdout
}
return stdout.toString().trim()
}
def generateReleaseNotes() {
final String filePath = "${project.rootDir}/app/src/main/assets/RELEASE.md"
description = "Generate release notes. [" + filePath + "]"
logd(description)
final def env = System.getenv()
env.each { final key, final value -> println "$key = $value" }
def assetFolder = new File("${project.rootDir}/app/src/main/assets/")
if (!assetFolder.exists())
assetFolder.mkdirs()
final def output = new File(filePath)
def notes = 'Internal Release.'
output.write(notes)
logd new File(filePath).getText('UTF-8')
}
def generateEnvironmentLog() {
final String filePath = "${project.rootDir}/app/src/main/assets/ENVIRONMENT.md"
description = "Generate environemnt log. [" + filePath + "]"
logd(description)
def assetFolder = new File("${project.rootDir}/app/src/main/assets/")
if (!assetFolder.exists())
assetFolder.mkdirs()
final def output = new File(filePath)
def notes = System.getenv().toString()
output.write(notes)
logd new File(filePath).getText('UTF-8')
}
def generateChangelog() {
final String filePath = "${project.rootDir}/app/src/main/assets/CHANGELOG.md"
description = "Generate Changelog. [" + filePath + "]"
logd(description)
def log = gitLog()
StringBuilder builder = new StringBuilder()
Scanner scanner = new Scanner(log)
while (scanner.hasNextLine()) {
def line = scanner.nextLine()
def commit = line.subSequence(1, line.length() - 1)
def hash = commit.subSequence(commit.indexOf("[[") + 2, commit.indexOf("]]"))
def message = commit.replaceFirst("\\[\\[$hash\\]\\]", "<a href=\"${VSC_PATH}commits/$hash\">#$hash</a>")
builder.append(message).append("\r\n").append("\r\n")
}
def assetFolder = new File("${project.rootDir}/app/src/main/assets/")
if (!assetFolder.exists())
assetFolder.mkdirs()
final def output = new File(filePath)
output.write(builder.toString())
// logd new File(filePath).getText('UTF-8')
}
def runCommand(sh, cmd) {
logd "Running command $cmd"
def sout = new ByteArrayOutputStream()
project.exec {
commandLine = [sh, cmd]
standardOutput = sout
}
return sout.toString()
}
def connectDevice() {
Properties properties = new Properties()
try {
properties.load(project.rootProject.file('local.properties').newDataInputStream())
}
catch (ignored) {
return
}
def deviceIp = properties.getProperty('deviceIp')?.replace('\"', "")
if (deviceIp == null || deviceIp.length() < 0)
return
description = "adb connect " + deviceIp
logd(description)
def stdout = new ByteArrayOutputStream()
exec {
executable android.getAdbExe().toString()
args = ['connect', deviceIp]
standardOutput = stdout
}
def result = stdout.toString().trim()
if (result.contains("unable to connect"))
throw new GradleException(result)
else
println(result)
}
ext {
generateEnvironmentLog = this.&generateEnvironmentLog
printGitLog = this.&printGitLog
commitHash = this.&commitHash
commitCount = this.&commitCount
simpleReleaseVersionName = this.&simpleReleaseVersionName
canonicalReleaseVersionName = this.&canonicalReleaseVersionName
buildNumberByCI = this.&buildNumberByCI
branchName = this.&branchName
branchNameByCI = this.&branchNameByCI
branchNameByGit = this.&branchNameByGit
gitLog = this.&gitLog
generateReleaseNotes = this.&generateReleaseNotes
generateChangelog = this.&generateChangelog
runCommand = this.&runCommand
connectDevice = this.&connectDevice
}