if (release != 'rtm') {
    version += "-$release"
}

if(!npmName)
    npmName = name;

if(!["amd", "commonjs", "system", "umd", "es6", "esnext"].contains(jsmodule))
    throw new Exception("Invalid jsmodule specified: $jsmodule");
if(!["es3", "es5", "es6", "es2016", "es2017", "esnext"].contains(target))
    throw new Exception("Invalid target specified: $target")

def targetLibs = [
    "es3" : "es5,es2015.promise,es2015.symbol,dom,scripthost",
    "es5" : "es5,es2015.promise,es2015.symbol,dom,scripthost"
];

ext.packageName="$npmScope/$npmName";

def srcDir = "$projectDir/src"
def typingsDir = "$srcDir/typings"
def distDir = "$buildDir/dist"
def testDir = "$buildDir/test"
def lib = targetLibs[target] ?: "${target},dom";

println "lib: $lib";

def sourceSets = ["main", "amd", "cjs"];
def testSets = ["test", "testAmd", "testCjs"];

task beforeBuild {
}

def createSoursetTasks = { String name, String outDir ->
    def setName = name.capitalize();

    def destDir = "$buildDir/compile/$name"
    def declDir = "$typingsDir/$name"
    def setDir = "$projectDir/src/$name"

    def beforeBuildTask = task "beforeBuild$setName"(dependsOn: beforeBuild) {
    }

    def copyJsTask = task "copyJs$setName"(dependsOn: beforeBuildTask, type: Copy) {
        from "$setDir/js"
        into outDir
    }

    def compileTypingsTask = task "compileTypings$setName"(dependsOn: beforeBuildTask, type: Exec) {
        inputs.dir("$setDir/ts")
        inputs.file("$srcDir/tsconfig.json")
        inputs.file("$setDir/tsconfig.json")
        outputs.dir(declDir)

        commandLine 'node_modules/.bin/tsc',
            '-p', "$setDir/tsconfig.json",
            '-t', target,
            '-m', jsmodule,
            '-d',
            '--emitDeclarationOnly',
            '--declarationDir', declDir

        if (lib)
            args '--lib', lib
    }

    def compileTsTask = task "compileTs$setName"(dependsOn: beforeBuildTask, type: Exec) {
        inputs.dir("$setDir/ts")
        inputs.file("$srcDir/tsconfig.json")
        inputs.file("$setDir/tsconfig.json")
        outputs.dir(destDir)

        commandLine 'node_modules/.bin/tsc',
            '-p', "$setDir/tsconfig.json",
            '-t', target,
            '-m', jsmodule,
            '--outDir', destDir

        if (lib)
            args '--lib', lib
    }

    def copyTsOutputTask = task "copyTsOutput$setName"(dependsOn: compileTsTask, type: Copy) {
        from compileTsTask
        into outDir
    }

    def copyTypingsTask = task "copyTypings$setName"(dependsOn: compileTypingsTask, type: Copy) {
        from compileTypingsTask
        into outDir
    }

    task "build$setName"(dependsOn: [copyTypingsTask, copyTsOutputTask, copyJsTask]) {
    }
}

task printVersion {
    doLast {
        println "version: $version"
        println "packageName: $packageName"
        println "target: $target"
        println "module: $jsmodule"
    }
}

task clean {
    doLast {
        delete buildDir
        delete typingsDir
    }
}

task _initBuild {
    mustRunAfter clean

    def buildInfoFile = "$buildDir/platform";
    inputs.property('target',target);
    inputs.property('jsmodule',jsmodule);
    outputs.file(buildInfoFile);

    doLast {
        delete buildDir
        mkdir buildDir

        def f = new File(buildInfoFile);
        f << "$target-$jsmodule";
    }
}

task cleanNpm {
    doLast {
        delete 'node_modules'
    }
}

task _npmInstall() {
    inputs.file("package.json")
    outputs.dir("node_modules")
    doLast {
        exec {
            commandLine 'npm', 'install'
        }
    }
}

beforeBuild {
    dependsOn _initBuild
    dependsOn _npmInstall
}

sourceSets.each { createSoursetTasks(it, distDir) }

testSets.each { createSoursetTasks(it, testDir) }

compileTsAmd {
    dependsOn compileTypingsMain
}

compileTypingsAmd {
    dependsOn compileTypingsMain
}

task build(dependsOn: buildMain) {
    if (jsmodule == "amd")
        dependsOn buildAmd
}

compileTsTest {
    dependsOn build
}

compileTsTestAmd {
    dependsOn compileTypingsTestAmd
}

task test(dependsOn: [buildTest, buildTestAmd], type: Exec) {
    commandLine 'node', "$testDir/run-amd-tests.js"
}

task _packageMeta(type: Copy) {
    mustRunAfter build

    inputs.property("version", version)
    from('.') {
        include '.npmignore', 'readme.md', 'license', 'history.md'
    }
    from("$srcDir/package.${jsmodule}.tmpl.json") {
        expand project.properties
        rename { "package.json" }
    }
    into distDir
}

task pack(dependsOn: [build, _packageMeta], type: Exec) {
    workingDir distDir

    commandLine 'npm', 'pack'
}