-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.gradle
141 lines (120 loc) · 4.76 KB
/
build.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
import com.sun.tools.javac.util.Pair
import java.text.SimpleDateFormat
import com.android.build.gradle.internal.dsl.BuildType
/**
* Top-level build dependencies and configuration options common to all sub-projects/modules.
*/
buildscript {
repositories {
apply from: "dependencies.gradle"
applyRepositories(repositories)
}
dependencies {
classpath deps.build.android_gradle_plugin
classpath deps.build.kotlin_gradle_plugin
//TODO classpath deps.build.google_services_plugin
//TODO classpath deps.build.fabric
}
}
plugins {
id "io.gitlab.arturbosch.detekt" version "1.0.0"
}
/**
* Dependencies for all projects
*/
allprojects {
repositories {
applyRepositories(repositories)
}
apply from: "$rootDir/scripts/detekt.gradle"
apply from: "$rootDir/scripts/ktlint.gradle"
apply from: "$rootDir/scripts/findbugs.gradle"
apply from: "$rootDir/scripts/pmd.gradle"
apply from: "$rootDir/scripts/translations.gradle"
apply from: "$rootDir/dependencies.gradle"
}
/**
* Clean task
*/
task clean(type: Delete) {
delete rootProject.buildDir
}
/**
* Project configuration
*/
project.ext.setProperty("projectName", "REST Countries")
project.ext.setProperty("betaEmails", "[email protected]")
// Versioning. Default code number is 1.
@SuppressWarnings("GroovyUnusedDeclaration")
static def getAutoVersionName() {
def (major, minor, patch, build, sha) = getLastMasterGitTagVersion()
def code = getAutoVersionCode()
def date = getBuildDate()
return (getCurrentBranch() == "master") ? "${major}.${minor}.${patch}" : "${sha}-${date} ($code)"
}
@SuppressWarnings("GroovyUnusedDeclaration")
static def getDevelopVersionCode() {
def count = getGitCommitsCount()
return (count == null || count.empty) ? 1 : count.toInteger()
}
@SuppressWarnings("GroovyUnusedDeclaration")
static def getProductionVersionCode() {
def (major, minor, patch, build, sha) = getLastMasterGitTagVersion()
return major.toInteger() * 1_000_000 + minor.toInteger() * 1_000 + patch.toInteger()
}
@SuppressWarnings("GroovyUnusedDeclaration")
static def getAutoVersionCode() {
def branch = getCurrentBranch()
if (branch == "master") {
return getProductionVersionCode()
}
return getDevelopVersionCode()
}
static def getCurrentBranch() {
return "git rev-parse --abbrev-ref HEAD".execute().text.trim()
}
static def getBuildDate() {
def df = new SimpleDateFormat("dd.MM.yyyy")
df.setTimeZone(TimeZone.getDefault())
return df.format(new Date())
}
static def getGitCommitsCount() {
return ("git rev-list ${getCurrentBranch()} --count").execute().text.trim()
}
static def getLastMasterGitTagVersion() {
def name = "git describe --tags ${getCurrentBranch()} --long".execute().text.replace("v", "").trim()
def (tag, build, sha) = name.tokenize('-')
def (major, minor, patch) = (tag != null) ? tag.tokenize('.') : [1, 1, 1]
return [major, minor, patch, build, sha]
}
// Functions
static def setBuildProperty(Project project, BuildType it, String name, String defaultValue) {
String key = name.toUpperCase()
Pair<String, Boolean> property = getPropertyOrDefault(project, key, defaultValue)
it.buildConfigField "String", "$key", property.snd ? property.fst : "\"${property.fst}\""
}
static def setBuildProperty(Project project, BuildType it, String name, Integer defaultValue) {
String key = name.toUpperCase()
Pair<Integer, Boolean> property = getPropertyOrDefault(project, key, defaultValue)
it.buildConfigField "Integer", "$key", property.snd ? property.fst : "${property.fst}"
}
static def setBuildProperty(Project project, BuildType it, String name, Long defaultValue) {
String key = name.toUpperCase()
Pair<Long, Boolean> property = getPropertyOrDefault(project, key, defaultValue)
it.buildConfigField "Long", "$key", property.snd ? property.fst : "${property.fst}" + 'L'
}
static def setBuildProperty(Project project, BuildType it, String name, Boolean defaultValue) {
String key = name.toUpperCase()
Pair<Boolean, Boolean> property = getPropertyOrDefault(project, key, defaultValue)
it.buildConfigField "boolean", "$key", property.snd ? property.fst : "${property.fst}"
}
static def setBuildProperty(Project project, BuildType it, String name, Float defaultValue) {
String key = name.toUpperCase()
Pair<Float, Boolean> property = getPropertyOrDefault(project, key, defaultValue)
it.buildConfigField "float", "$key", property.snd ? property.fst : "${property.fst}" + 'f'
}
static <T> Pair<T, Boolean> getPropertyOrDefault(Project project, String key, T defaultValue) {
boolean isFromProperty = project.hasProperty(key)
T value = (isFromProperty) ? project.property(key) : defaultValue
return new Pair(value, isFromProperty)
}