plugins {
  id "org.implab.gradle-typescript" version "1.0.1-rc1"
}

// если версия явно не заданы вычисляем ее из тэга ревизии v.{num}***
// результатом будет версия '{num}.{distance}' где distance - расстояние от
// текущей ревизии до ревизии с тэгом
def tagDistance = 0;

if (!version) {
    
    def rev = ["hg", "log", "-r", ".", "--template", "{latesttag('re:^v') % '{tag}-{distance}'}"].execute().text.trim();

    def tagVersion;

    def match = (rev =~ /^v(\d+\.\d+\.\d+).*-(\d+)$/);
    
    if (match.size()) {
        tagVersion = match[0][1];
        tagDistance = match[0][2].toInteger();
    } else {
        throw new Exception("A version must be specied");
    }

    version = tagVersion;

    if (tagDistance > 0)
        version++;
} else {
    println "explicit version: $version";
}

if (hasProperty('versionSuffix') && versionSuffix) {
    version += "-$versionSuffix"
}

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

ext {
    packageName = "@$npmScope/$npmName"
}

def jstarget = target;

sources {
    amd {
        typings {
            srcDir main.output.typingsDir
        }
    }

    cjs {
        typings {
            srcDir main.output.typingsDir
        }
    }

    testAmd {
        typings {
            srcDir main.output.typingsDir
            srcDir amd.output.typingsDir
            srcDir test.output.typingsDir
        }
    }

    testCjs {
        typings {
            srcDir main.output.typingsDir
            srcDir cjs.output.typingsDir
            srcDir test.output.typingsDir
        }
    }
}

typescript {
    compilerOptions {
        lib = [target, "dom", "scripthost"]
        if (jstarget in ["es5", "es3"])
            lib += ["es2015.promise", "es2015.symbol", "es2015.iterable"]
        
        target = jstarget
        module = jsmodule
        types = []
        declaration = true
        listFiles = true

    }
    tscCmd = "$projectDir/node_modules/.bin/tsc"
    tsLintCmd = "$projectDir/node_modules/.bin/tslint"
	esLintCmd = "$projectDir/node_modules/.bin/eslint"
	npmCmd = "npm"
}

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

task clean {
    doLast {
        delete buildDir
    }
}

npmPackMeta {
    meta {
        name = packageName
    }
}

compileTsAmd {
    dependsOn sources.main.output
}

configureTsAmd {
    compilerOptions {
        types += [ "requirejs", "dojo-typings" ]
    }
}

test {
    workingDir layout.buildDirectory.dir("test");
    commandLine "node", "tests/index.js"
}

assembleTest {
    dependsOn sources.main.output
    dependsOn sources.amd.output
    dependsOn sources.testAmd.output
    
    from(sources.main.output.compiledDir)
    from(sources.amd.output.compiledDir)
    from(sources.testAmd.output.compiledDir)
}