-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsetup.gradle
129 lines (122 loc) · 5.35 KB
/
setup.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
import java.nio.file.Paths
import java.nio.file.Path
import org.gradle.internal.os.OperatingSystem
// write operating system platform name to project property
switch (OperatingSystem.current()) {
case OperatingSystem.LINUX:
project.ext.osName = "Linux"
break
case OperatingSystem.MAC_OS:
project.ext.osName = "macOS"
break
case OperatingSystem.WINDOWS:
project.ext.osName = "Windows"
break
}
/**
* Register a project property of a given name as a {@code boolean} value.
*
* @param name name of the property to register.
* @param defaultValue value to register with if property is {@code null}.
* @see Boolean#parseBoolean(String)
*/
void registerBooleanProjectProperty(String name, boolean defaultValue) {
def property = project.findProperty(name)
if (property == null)
{
// seach for environment variable with same name before assigning default value
def envVariable = providers.environmentVariable(name).forUseAtConfigurationTime()
if (envVariable.present) {
property = envVariable.get()
}
else property = defaultValue
}
project.ext.setProperty(name, Boolean.parseBoolean(property as String))
}
// is the build being run on a CI server?
registerBooleanProjectProperty('CI', false)
// should gradle run spotless tasks?
registerBooleanProjectProperty('makeSpotless', false)
// directory containing a copy of Project Zomboid classes
project.ext.zomboidClassesDir = file("$buildDir/classes/zomboid").absoluteFile
Properties localProjectProperties = new Properties()
project.ext.localProperties = localProjectProperties
/*
* handle locating properties file when configuring buildSrc project
* as the path to project/root directory will be different
*/
def rootDir = projectDir.name != 'buildSrc' ? project.rootDir : project.rootDir.parentFile
def propertiesFile = rootDir.toPath().resolve('local.properties').toFile()
if (propertiesFile.exists()) {
localProjectProperties.load(propertiesFile.newDataInputStream())
}
/**
* Initialize and get project property from {@code local.properties} file with designated type.
*
* @param name property name to search for in local properties.
* @param type class to initialize the property as.
* @param env environment variable to use if property not found.
* @param required whether to throw an exception if property is not found.
* @param defaultValue value to be used if property was not found.
* @return found project property of type {@code T} or {@code null} if no property found.
*/
def <T> T getLocalProjectProperty(String name, Class<T> type, String env, boolean required, T defaultValue) {
Properties localProperties = project.ext.localProperties
String property = localProperties.getProperty(name, '')
if (property.isEmpty())
{
if (!System.hasProperty(name))
{
// when env parameter is not defined search for env variable with property name
def sEnv = env != null && !env.isEmpty() ? env : name
def envVariable = providers.environmentVariable(sEnv).forUseAtConfigurationTime()
if (envVariable.present) {
property = envVariable.get()
}
else if (required && defaultValue == null) {
throw new InvalidUserDataException("Unable to find local project property ${name}")
}
else return defaultValue
}
else property = System.getProperty(name)
}
if (type == Path) {
return Paths.get(property) as T
}
else return property as T
}
/**
* Initialize and register project property from {@code local.properties} file.
*
* @param name property name to search for in local properties.
* @param type class to initialize the property as.
* @param env environment variable to use if property not found.
* @param required whether to throw an exception if property is not found.
* @return found project property or {@code null} if no property found.
*/
def registerLocalProjectProperty(String name, Class<Object> type, String env, boolean required) {
project.ext.setProperty(name, getLocalProjectProperty(name, type, env, required, null))
}
/**
* Initialize and register project property from {@code local.properties} file.
*
* @param name property name to search for in local properties.
* @param type class to initialize the property as.
* @param env environment variable to use if property not found.
* @param required whether to throw an exception if property is not found.
* @param defaultValue value to be used if property was not found.
* @return found project property or {@code null} if no property found.
*/
def <T> void registerLocalProjectProperty(String name, Class<T> type, String env, boolean required, T defaultValue) {
project.ext.setProperty(name, getLocalProjectProperty(name, type, env, required, defaultValue))
}
// initialize local project properties here
// some local properties (env variables) will not be available on CI server
if (!CI) {
// path to Project Zomboid installation directory
registerLocalProjectProperty('gameDir', Path.class, 'PZ_DIR_PATH', true)
// path to IntelliJ IDEA installation directory
registerLocalProjectProperty('ideaHome', Path.class, 'IDEA_HOME', true)
}
// Github repository token used to generate changelog
registerLocalProjectProperty('cg.token', String.class, 'CHANGELOG_GITHUB_TOKEN', false)