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 {

    }

    cjs {
        
    }
}

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

    }
    tsLintCmd = "tslint"
	esLintCmd = "eslint"
	npmCmd = "npm"
}

configureTsMain {
    compilerOptions {
        if (jstarget in ["es5", "es3"])
            lib += ["es2015.symbol", "es2015.iterable"]
    }
}

configureTsTest {
    compilerOptions {
        types += [ "node" ]
    }
}

task printVersion {
    doLast {
        println "version: $version";
        println "tagDistance: $tagDistance";
        println "packageName: $packageName";
        println "bundle: ${npmPack.outputs.files.join(',')}";
        println "target: $jstarget";
        println "module: $jsmodule";
    }
}

task clean {
    doLast {
        delete buildDir
    }
}

npmPackMeta {
    meta {
        name = packageName
    }
}
