-
Notifications
You must be signed in to change notification settings - Fork 3
/
task-rules.gradle
153 lines (144 loc) · 5.21 KB
/
task-rules.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
import groovy.lang.Closure;
import org.codehaus.groovy.tools.GroovyClass;
import org.gradle.api.Project;
import org.gradle.api.Rule;
import org.gradle.api.tasks.wrapper.Wrapper;
Class<?> classForNameNoThrow(String s) {
// classloader bitches
try {
// not much in this one
return Class.forName(s)
} catch (Exception e) {
logger.debug "Failed: ${e}"
}
try {
// gradle-core
return Project.class.getClassLoader().loadClass(s)
} catch (Exception ee) {
logger.debug "Failed: ${ee}"
}
try {
// gradle-plugins
return Wrapper.class.getClassLoader().loadClass(s)
} catch (Exception eee) {
logger.debug "Failed: ${eee}"
}
}
Task createTask(String name, Map args = [:]) {
if (hasProperty('type')) {
String type = project.getProperty('type')
def found = tasks.find { (it.class.name - '_Decorated') == type }
found = found ?: tasks.find { it.name == type }
found = found ?: tasks.find { (it.class.simpleName - '_Decorated') == type }
found = found ?: tasks.find { (it.class.simpleName - '_Decorated').toLowerCase() == type.toLowerCase() }
found = found ?: classForNameNoThrow(type)
found = found ?: classForNameNoThrow("org.gradle.api.tasks.${type}")
found = found ?: classForNameNoThrow("org.gradle.api.tasks.${type.toLowerCase()}.${type}")
if (found) {
args.type = found instanceof Class ? found : found.class
logger.info "Inline task type: ${args.type}"
} else {
throw new GradleException("Failed to find requested task base type ${type}")
}
}
if (project.hasProperty('dependsOn')) {
args.dependsOn = getProperty('dependsOn')
}
Task dummyTask = project.task(args, name)
if (project.hasProperty('configure')) {
def script = 'return { ' + getProperty('configure') + ' }'
def del = Eval.me(script)
del.setResolveStrategy Closure.DELEGATE_FIRST
del.delegate = dummyTask
del()
}
Map<String, ?> props = project.getProperties()
dummyTask.metaClass.getProperties().each() {
def value = props[it.name]
if (value && !Project.class.metaClass.hasProperty(null, it.name)) {
if (it.type == String.class || it.type == Boolean.class || it.type == boolean.class) {
println "set ${it.name} = ${value}"
it.setProperty(dummyTask, value)
}
// TODO support other types (numbers)
}
}
dummyTask
}
Rule addRule(String description, Closure ruleAction)
{
if (!tasks.getRules().any { it.description == description} )
{
tasks.addRule(description, ruleAction)
}
}
// gradle "exec compileJava.classpath.files.each { println it }"
// gradle -Ptype=Wrapper -PgradleVersion=1.3 exec
addRule('Pattern: "exec <some groovy code>": Executes the given code within a dynamic task') { String taskName ->
final String prefix = "exec"
if (taskName.startsWith(prefix) && taskName.length() >= prefix.length() && !tasks.findByName(taskName)) {
Task dummyTask = createTask(taskName)
def script = 'return { ' + (taskName-prefix) + ' }'
def del = Eval.me(script)
dummyTask << {
del.setResolveStrategy Closure.DELEGATE_FIRST
del.delegate = project
del()
}
}
}
// gradle "println convention.plugins.base.distsDir"
// gradle "println configurations"
// gradle "println tasks.jar.outputs.files.files"
addRule('Pattern: "println <some groovy code>": Executes the given code within a dynamic task, and print the result') { String taskName ->
final String prefix = "println"
if (taskName.startsWith(prefix) && taskName.length() > prefix.length() && !tasks.findByName(taskName)) {
Task dummyTask = createTask(taskName)
def script = 'return { ' + (taskName-prefix) + ' }'
def del = Eval.me(script)
dummyTask << {
del.setResolveStrategy Closure.DELEGATE_FIRST
del.delegate = project
println del()
}
}
}
// gradle "customTest debug true"
// gradle -Pdebug=true customTest
addRule('Pattern: "custom<Task> <some groovy code>": Executes the given task, with the given code as extra configure-ation') { String taskName ->
final String prefix = "custom"
if (taskName.startsWith(prefix) && taskName.length() > prefix.length() && !tasks.findByName(taskName)) {
String orig = taskName-prefix
String extra
int i = orig.indexOf(' ')
if (i != -1)
{
extra = orig.substring(i+1)
orig = orig.substring(0, i)
}
orig = '' + orig.charAt(0).toLowerCase() + orig.substring(1)
def task = tasks.findByName(orig)
Map<String, ?> props = project.getProperties()
task.metaClass.getProperties().each() {
def value = props[it.name]
if (value && !Project.class.metaClass.hasProperty(null, it.name)) {
if (it.type == String.class || it.type == Boolean.class || it.type == boolean.class) {
println "set ${it.name} = ${value}"
it.setProperty(task, value)
}
// TODO support other types (numbers)
}
}
Map args = [dependsOn: task]
Task dummyTask = project.task(args, taskName)
if (extra)
{
def script = 'return { ' + extra + ' }'
def del = Eval.me(script)
del.setResolveStrategy Closure.DELEGATE_FIRST
del.delegate = task
del()
}
dummyTask
}
}