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

if (!symbols in ['local', 'pack', 'none'])
    throw new Exception("The symbols property value is invalid: $symbols");

if (!flavour in ['browser', 'node'])
    throw new Exception("The flavour property value is invalid: $flavour");

ext {
    packageName = flavour == 'browser' ? "@$npmScope/$name-amd" : "@$npmScope/$name"
    lint = project.hasProperty('lint') ? project.lint ?: true : false
}

mercurial {
    preReleasePolicy { it
        .addPatch(versionDistance)
        .withPreRelease('dev')
        .withMeta(changeset)
    }

    applyVersioningPolicy()
}

typescript {
    compilerOptions {
        types = []
        declaration = true
        experimentalDecorators = true
        strict = true
        // dojo-typings are sick
        skipLibCheck = true
        
        if(symbols != 'none') {
            sourceMap = true
            sourceRoot = "pack:${packageName}"
        }

        if (flavour == 'node') {
            module = "commonjs"
            target = "es2017"
            lib = ["es2017", "dom", "scripthost"]
        } else if (flavour == 'browser') {
            module = "amd"
            target = "es5"
            lib = ["es5", "dom", "scripthost", "es2015.promise", "es2015.symbol", "es2015.iterable" ]
        }
    }
    tscCmd = "$projectDir/node_modules/.bin/tsc"
    tsLintCmd = "$projectDir/node_modules/.bin/tslint"
	esLintCmd = "$projectDir/node_modules/.bin/eslint"
}

tasks.matching{ it.name =~ /^lint/ }.configureEach {
    onlyIf { lint }
}

if (symbols == 'local') {
    tasks.matching{ it.name =~ /^configureTs/ }.configureEach {
        compilerOptions {
            sourceRoot = "file://" + it.rootDir
        }
    }
}

printVersion {
    doLast {
        println "packageName: $packageName";
        println "flavour: $flavour";
        println "target: $typescript.compilerOptions.target";
        println "module: $typescript.compilerOptions.module";
        println "lint: $lint";
        println "symbols: $symbols";
    }
}
