plugins {
  id "org.implab.gradle-typescript" version "1.3.3"
  id "org.implab.gradle-hg"
  id "ivy-publish"
}

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
}

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 {
        types = []
        declaration = true
        experimentalDecorators = true
        strict = true
        // dojo-typings are sick
        skipLibCheck = true
        
        if(symbols != 'none') {
            sourceMap = true
            sourceRoot = 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
        }
    }
}

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

npmPackMeta {
    meta {
        name = packageName
    }
}

configureTsCjs {
    dependsOn sources.main.output
    compilerOptions {
        types += [ "node" ]
    }
}

configureTsAmd {
    dependsOn sources.main.output
    compilerOptions {
        types += [ "requirejs", "dojo-typings" ]
    }
}

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

assemble {
    if (flavour == 'browser') {
        dependsOn sources.amd.output
        from sources.amd.output.compiledDir
        from sources.amd.resources
    }
    if (flavour == 'node') {
        dependsOn sources.cjs.output
        from sources.cjs.output.compiledDir
        from sources.cjs.resources
    }
}

assembleTest {
    if (flavour == 'browser') {
        dependsOn sources.amd.output, sources.testAmd.output

        from sources.amd.output.compiledDir
        from sources.testAmd.output.compiledDir
        from sources.amd.resources
        from sources.testAmd.resources
    }
    if (flavour == 'node') {
        dependsOn sources.cjs.output, sources.testCjs.output

        from sources.cjs.output.compiledDir
        from sources.testCjs.output.compiledDir
        from sources.cjs.resources
        from sources.testCjs.resources
    }
}

typings {
    if (flavour == 'browser') {
        dependsOn sources.amd.output
        from sources.amd.output.typingsDir
    }
    if (flavour == 'node') {
        dependsOn sources.cjs.output
        from sources.cjs.output.typingsDir
    }
}

task npmPackTypings(type: Copy) {
    npmPackContents.dependsOn it
    dependsOn sources.main.output
    
    from sources.main.output.typingsDir

    if (flavour == 'browser') {
        dependsOn sources.amd.output
        from sources.amd.output.typingsDir
    }
    if (flavour == 'node') {
        dependsOn sources.cjs.output
        from sources.cjs.output.typingsDir
    }

    into npm.packageDir
}

task npmPackSources(type: Copy) {
    from sources.main.ts
    if (symbols == 'pack') {
       npmPackContents.dependsOn npmPackSources
    }
    
    if (flavour == 'browser') {
        from sources.amd.ts
    }
    if (flavour == 'node') {
        from sources.cjs.ts
    }

    into npm.packageDir.dir("_src")
}



task packJsTar(type: Tar) {
    dependsOn assemble;

    archiveBaseName = provider { packageName }

    destinationDirectory = buildDir
    archiveClassifier = provider { typescript.compilerOptions.module }
    compression = Compression.GZIP

    from(assemble.outputs)

    doLast {
        println archiveName;
    }
}

task packTypingsTar(type: Tar) {
}

publishing {
    publications {
        local(IvyPublication) {
            artifact(packJsTar) {
                type = "js"
            }
        }
    }

    repositories {
        ivy {
            url "ivy-repo"
        }
    }
}