/
JSXTask.groovy
77 lines (58 loc) · 2.1 KB
/
JSXTask.groovy
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
package net.eikehirsch.gradle.react
import com.moowork.gradle.node.task.NodeTask
import org.gradle.api.GradleException
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.OutputDirectory
import org.gradle.util.ConfigureUtil
class JSXTask extends NodeTask {
public static final String NAME = 'jsx'
public static final String DEFAULT_SOURCES_DIR = 'src/main/react'
public static final String DEFAULT_DEST_DIR = 'build/react'
private final static String JSX_SCRIPT = 'node_modules/react-tools/bin/jsx'
@InputDirectory sourcesDir
@OutputDirectory destDir
JSXOptions jsxOptions = new JSXOptions()
JSXTask() {
this.group = 'React'
this.description = 'Compiles jsx sources into javascript. Configure via jsx extension.'
this.dependsOn 'installReact'
}
// updating will happen when the project was evaluated.
def updateSettings() {
JSXExtension config = project.extensions.getByName(JSXExtension.NAME) as JSXExtension
sourcesDir = config.sourcesDir ?: DEFAULT_SOURCES_DIR
destDir = config.destDir ?: DEFAULT_DEST_DIR
jsxOptions = config.getOptions() ?: jsxOptions
}
@Override
void exec() {
def jsx = this.project.file(JSX_SCRIPT)
if (!jsx.isFile()) {
throw new GradleException(
"React-Tools not installed in node_modules, please run 'gradle " +
"${ReactInstallTask.NAME}' first.")
}
setScript( jsx )
// make sure the output folder exists
def out = getDestDir()
if( out.exists() ) {
out.mkdirs()
}
def args = jsxOptions.toArgsArray()
args << getSourcesDir().absolutePath
args << out.absolutePath
setArgs args
super.exec()
}
def getSourcesDir() {
return project.file(sourcesDir)
}
def getDestDir() {
return project.file(destDir)
}
@SuppressWarnings("GroovyUnusedDeclaration")
jsxOptions(Closure<JSXOptions> optionsClosure) {
ConfigureUtil.configure(optionsClosure, jsxOptions)
return this
}
}