summaryrefslogtreecommitdiff
path: root/src/main/groovy/frege/gradle/FregeTask.groovy
blob: a85b4ccf6ecf90d5bb0ef266a48b6cd1e195f5a6 (plain)
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
package frege.gradle

import org.gradle.api.DefaultTask
import org.gradle.api.tasks.*
import org.gradle.process.internal.DefaultJavaExecAction
import org.gradle.process.internal.JavaExecAction
import org.gradle.api.internal.file.FileResolver
import org.gradle.tooling.BuildException

class FregeTask extends DefaultTask {

    static String DEFAULT_CLASSES_SUBDIR = "classes/main"       // TODO: should this come from a convention?
    static String DEFAULT_SRC_DIR        = "src/main/frege"     // TODO: should this come from a source set?

    @Optional @Input
    String xss = "4m"

    @Optional @Input
    boolean hints = false

    @Optional @Input
    boolean verbose = false

    @Optional @Input
    boolean inline = true

    @Optional @Input
    boolean make = true

    @Optional @Input
    boolean skipCompile = false

    @Optional @Input
    String extraArgs = ""

    @Optional @Input
    String allArgs = "" // this is an option to overrule all other settings

    @Optional @Input
    String module = ""

    @Optional @InputDirectory
    File sourceDir = new File(project.projectDir, DEFAULT_SRC_DIR).exists() ?  new File(project.projectDir, DEFAULT_SRC_DIR) : null

    @Optional @OutputDirectory
    File outputDir = new File(project.buildDir, DEFAULT_CLASSES_SUBDIR)

    @TaskAction
    void executeCompile() {
        if (! outputDir.exists() ) {
            logger.info "Creating output directory '${outputDir.absolutePath}'."
            outputDir.mkdirs()
        }

        // access extension configuration values as ${project.frege.key1}

        FileResolver fileResolver = getServices().get(FileResolver.class)
        JavaExecAction action = new DefaultJavaExecAction(fileResolver)
        action.setMain("frege.compiler.Main")
        action.setClasspath(project.files(project.configurations.compile))

        List jvmargs = []
        if (xss)
            jvmargs << "-Xss$xss"
        action.setJvmArgs(jvmargs)

        def args = allArgs ? allArgs.split().toList() : assembleArguments()

        logger.info("Calling Frege compiler with args: '$args'")
        action.args(args)
        action.execute()
    }

    protected List assembleArguments() {
        List args = []
        if (hints)
            args << "-hints"
        if (inline)
            args << "-inline"
        if (make)
            args << "-make"
        if (verbose)
            args << "-v"
        if (skipCompile)
            args << "-j"

        if (sourceDir != null) {
            args << "-sp"
            args << sourceDir.absolutePath
        }

        args << "-d"
        args << outputDir

        if (!module && !extraArgs) {
            logger.info "no module and no extra args given: compiling all of the sourceDir"
            if (sourceDir != null) {
                args << sourceDir.absolutePath
            }

        } else if (module) {
            logger.info "compiling module '$module'"
            args << module
        } else {
            args = args + extraArgs.split().toList()
        }
        args
    }
}