-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathconstants.gradle
More file actions
190 lines (175 loc) · 5.89 KB
/
constants.gradle
File metadata and controls
190 lines (175 loc) · 5.89 KB
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
/*
* This file is part of CodeOps Studio.
* CodeOps Studio - Code anywhere anytime
* https://github.com/euptron/CodeOps-Studio
* Copyright (C) 2024-2026 Etido Peter
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses/
*
* If you have more questions, feel free to message Etido Peter if you have any
* questions or need additional information. Email: etido.up@gmail.com
*/
/**
* Build configuration and versioning utilities for CodeOps Studio.
*
* <p>
* This script centralizes SDK versions, Java compatibility, application
* identifiers, and deterministic app versioning derived from the Git repository.
* It is intentionally designed to be declarative and predictable across local,
* CI, debug, and release builds.
* </p>
*
* <h3>Versioning Strategy</h3>
* <ul>
* <li>
* {@code baseVersion} is the canonical semantic version and <b>must</b> follow
* {@code MAJOR.MINOR.PATCH} format (e.g. {@code 1.0.5}).
* </li>
* <li>
* Non-release builds append a release-type suffix and the current Git commit
* hash for traceability.
* </li>
* <li>
* Release builds always use {@code baseVersion} only, with no suffixes or hashes.
* </li>
* <li>
* {@code versionCode} is derived from the Git commit count and gracefully
* falls back to a monotonic time-based value when Git metadata is unavailable.
* </li>
* </ul>
*
* <h3>Git Integration</h3>
* <p>
* The methods {@link #getGitCommitHash(Project)} and
* {@link #getGitCommitCount(Project)} are deliberately defensive and
* must not be simplified or refactored. They are written to:
* </p>
* <ul>
* <li>Work in local, CI, and detached source environments</li>
* <li>Fail safely when Git is unavailable</li>
* <li>Never block or break the build pipeline</li>
* </ul>
*
* <h3>Allowed Modifications</h3>
* <p>
* Only the following values are expected to be modified when required:
* </p>
* <ul>
* <li>{@code compileSdkVersion}</li>
* <li>{@code buildToolsVersion}</li>
* <li>{@code targetSdkVersion}</li>
* <li>{@code minSdkVersion}</li>
* <li>{@code javaSourceVersion} / {@code javaTargetVersion}</li>
* <li>{@code baseVersion} (semantic version only)</li>
* <li>{@code suffix} (release type only, without punctuation)</li>
* </ul>
*
* <h3>Restrictions</h3>
* <ul>
* <li>Do not add punctuation, separators, or symbols to {@code suffix}</li>
* <li>Do not insert extra spaces or formatting into {@code baseVersion}</li>
* <li>Do not manually construct {@code versionName} or {@code versionCode}</li>
* </ul>
*
* <p>
* For detailed semantic versioning rules and release conventions, see
* {@code CONTRIBUTING.md}.
* </p>
*
* @author Etido Peter
* @since 2024
*/
static def getGitCommitHash(proj) {
try {
def process = ["git", "rev-parse", "--short", "HEAD"].execute(null, proj.rootDir)
process.waitFor()
def output = process.text.trim()
if (process.exitValue() == 0 && output) {
return "(${output})"
} else {
return ""
}
} catch (Exception ignored) {
return ""
}
}
static def getGitCommitCount(proj) {
def pseudoVersionCode = (System.currentTimeMillis() / 1000).intValue()
try {
def process = ["git", "rev-list", "--count", "HEAD"].execute(null, proj.rootDir)
process.waitFor()
def output = process.text.trim()
if (process.exitValue() == 0) {
try {
return Math.max(1, output.toInteger())
} catch (NumberFormatException e) {
return pseudoVersionCode
}
} else {
return pseudoVersionCode
}
} catch (Exception ignored) {
return pseudoVersionCode
}
}
project.ext {
// Versions
compileSdkVersion = 35
buildToolsVersion = '34.0.0'
targetSdkVersion = 35
minSdkVersion = 26
javaSourceVersion = JavaVersion.VERSION_17
javaTargetVersion = JavaVersion.VERSION_17
// App Constants
appNameSpace = "com.eup.codeopsstudio"
applicationId = "com.eup.codeopsstudio"
def gitHash = getGitCommitHash(project)
def gitCommitCount = getGitCommitCount(project)
def isReleaseBuild = false
gradle.startParameter.taskNames.each { taskName ->
def lowerTask = taskName.toLowerCase()
if (lowerTask.contains('release')) {
isReleaseBuild = true
return
}
if (lowerTask == 'assemble' || lowerTask.endsWith(':assemble')) {
def anyTaskHasDebug = false
gradle.startParameter.taskNames.each { t ->
if (t.toLowerCase().contains('debug')) {
anyTaskHasDebug = true
return
}
}
if (!anyTaskHasDebug) {
isReleaseBuild = true
return
}
}
}
def baseVersion = "1.1.1"
def suffix = ""
if (isReleaseBuild) {
appVersionName = suffix ? "${baseVersion} ${suffix}" : baseVersion
} else {
def parts = [baseVersion]
if (suffix && !suffix.isEmpty()) {
parts << suffix
}
if (gitHash && !gitHash.isEmpty()) {
parts << gitHash
}
appVersionName = parts.join(' ').trim()
}
appVersionCode = gitCommitCount
}