-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtaskutils.gradle
More file actions
93 lines (83 loc) · 2.84 KB
/
taskutils.gradle
File metadata and controls
93 lines (83 loc) · 2.84 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
import javax.inject.Inject
interface InjectedExecOps {
@Inject ExecOperations getExecOps()
}
def runCommand(String commandName, boolean ignoreExitCode, File workDir, String... commands) {
def osName = System.getProperty("os.name").toLowerCase()
def args = []
if (osName.contains("windows")) {
args.add('cmd')
args.add('/c')
} else {
args.add('bash')
args.add('-c')
}
args.addAll(commands)
if (workDir != null) {
printGreen("Executing '{}' in {}", commandName, workDir)
} else {
printGreen("Executing '{}'", commandName)
}
def execOps = project.objects.newInstance(InjectedExecOps).execOps
def result = execOps.exec {
if (workDir != null) {
workingDir = workDir
}
commandLine args
ignoreExitValue ignoreExitCode
}
if (result.exitValue == 0) {
printGreen("Successfully executed '{}'", commandName)
} else if (ignoreExitCode) {
logger.error("Task '{}' failed with exit code {} - Ignoring", commandName, result.exitValue)
}
return result
}
def runGradleTasks(List<String> mandatoryTasks, List<String> optionalTasks) {
def errored = false
def results = []
for (String task : mandatoryTasks) {
if (errored) {
results.add([task, null])
continue
}
def result = runGradleTask(task, true)
if (result.exitValue != 0) {
errored = true
}
results.add([task, result])
}
for (String task : optionalTasks) {
if (errored) {
results.add([task, null])
continue
}
def result = runGradleTask(task, true)
results.add([task, result])
}
logger.lifecycle('')
printGreen('Execution results:')
results.forEach {
def result = it[1]
printGreen('{} -> {}', it[0], result == null ? 'SKIPPED' : (result.exitValue == 0 ? 'SUCCESS' : 'FAILURE'))
}
}
def runGradleTask(String taskName, boolean ignoreExitCode, File workDir=null) {
def osName = System.getProperty("os.name").toLowerCase()
def gradlewCmd = osName.contains("windows") ? "gradlew.bat" : "./gradlew"
return runCommand(taskName, ignoreExitCode, workDir, "${gradlewCmd} ${taskName}")
}
def printGreen(String template, Object... args) {
def green = '\u001B[32m'
def reset = '\u001B[0m'
logger.lifecycle("${green}${template}${reset}", args)
}
ext.runGradleTasks = { List<String> mandatoryTasks, List<String> optionalTasks ->
runGradleTasks(mandatoryTasks, optionalTasks)
}
ext.runGradleTask = { String taskName, boolean ignoreExitCode, File workDir=null ->
runGradleTask(taskName, ignoreExitCode, workDir)
}
ext.runCommand = { String commandName, boolean ignoreExitCode, File workDir, String... commands ->
runCommand(commandName, ignoreExitCode, workDir, commands)
}