import org.apache.tools.ant.taskdefs.condition.Os

class Ansi {
    static final String NORMAL = "\u001B[0m"

    static final String BOLD = "\u001B[1m"
    static final String ITALIC = "\u001B[3m"
    static final String UNDERLINE = "\u001B[4m"
    static final String BLINK = "\u001B[5m"
    static final String RAPID_BLINK = "\u001B[6m"
    static final String REVERSE_VIDEO = "\u001B[7m"
    static final String INVISIBLE_TEXT = "\u001B[8m"

    static final String BLACK = "\u001B[30m"
    static final String RED = "\u001B[31m"
    static final String GREEN = "\u001B[32m"
    static final String YELLOW = "\u001B[33m"
    static final String BLUE = "\u001B[34m"
    static final String MAGENTA = "\u001B[35m"
    static final String CYAN = "\u001B[36m"
    static final String WHITE = "\u001B[37m"

    static final String DARK_GRAY = "\u001B[1;30m"
    static final String LIGHT_RED = "\u001B[1;31m"
    static final String LIGHT_GREEN = "\u001B[1;32m"
    static final String LIGHT_YELLOW = "\u001B[1;33m"
    static final String LIGHT_BLUE = "\u001B[1;34m"
    static final String LIGHT_PURPLE = "\u001B[1;35m"
    static final String LIGHT_CYAN = "\u001B[1;36m"

    static String color(String text, String ansiValue) {
        ansiValue + text + NORMAL
    }

    static void write(String text, String ansiValue) {
        println(color(text, ansiValue))
    }

    static void green(String text) {
        write(text, Ansi.GREEN)
    }

    static void cyan(String text) {
        write(text, Ansi.CYAN)
    }

    static void red(String text) {
        write(text, Ansi.RED)
    }

    static void yellow(String text) {
        write(text, Ansi.YELLOW)
    }

    static void blue(String text) {
        write(text, Ansi.BLUE)
    }
}

Object.metaClass.ansi = new Ansi()
project.ext.set("ansi", new Ansi())

if (Os.isFamily(Os.FAMILY_WINDOWS)) {
    tasks.register('pathingJar', Jar) {
        description = "Produces a 'pathing' JAR whose manifest class-path contains project runtime dependencies"
        dependsOn configurations.runtimeClasspath
        archiveAppendix.set("pathing")

        def files = configurations.runtimeClasspath.files
        doFirst {
            manifest {
                attributes "Class-Path":
                        files.collect {
                            it.toURI().toString().replaceFirst("/file:/+/", "/")
                        }.join(" ")
            }
        }
    }
}

tasks.register('testJar', Jar) {
    dependsOn testClasses
    description = "Archive the testClasses"
    archiveClassifier.set("tests")
    from sourceSets.test.output
}

tasks.register('sourcesJar', Jar) {
    dependsOn classes
    archiveClassifier.set("sources")
    from sourceSets.main.allSource
}

tasks.register('resourcesJar', Jar) {
    archiveClassifier.set("resources")
    from project.file("src/main/resources")
}

tasks.register('javadocJar', Jar) {
    dependsOn javadoc
    archiveClassifier.set("javadoc")
    from javadoc.destinationDir
}

tasks.register('downloadDependencies') {
    description "Pre-download project dependencies for all configurations"
    configurations.getAsMap().each { name, config ->
        logger.info "Retrieving dependencies for $project:$name"
        try {
            config.files
        } catch (e) {
            project.logger.info e.message
        }
    }
}
