description = "Spring JS Resources"

configurations {
    shrinksafe
}

dependencies {
    shrinksafe "org.dojotoolkit:dojo-shrinksafe:1.7.2"
}

task prepareResources(type: Jar) { jar ->
    doLast() {
        minify("Spring.js")
        minify("Spring-Dojo.js")
        ant.unzip(src: "${projectDir}/dojo-build/dojo-build.zip", 
                  dest: "${buildDir}/resources/main/META-INF/web-resources")
    }
}

void minify(fileName) {

    project.ant {
        def sourceDir = "${basedir}/src/main/resources/META-INF/web-resources/spring/"
        def outputDir = "${buildDir}/resources/main/META-INF/web-resources/spring/"

        copy(file: "${outputDir}/${fileName}", tofile: "${outputDir}/${fileName}.uncompressed.js")
     
        java(classname: "org.dojotoolkit.shrinksafe.Main",
                classpath: configurations.shrinksafe.asPath, fork: true, 
                output: "${outputDir}/${fileName}.TMP.js") {
            arg(value: "${sourceDir}/${fileName}")
        }

        move(file: "${outputDir}/${fileName}.TMP.js", toFile: "${outputDir}/${fileName}") {
            filterchain {
                filterreader(classname: "org.apache.tools.ant.filters.StripLineBreaks")
            }
            filterchain {
                concatfilter(prepend: "${basedir}/../src/dist/javascript-copyright.txt")
            }
        }
    }
}

jar {
	dependsOn prepareResources
}

publishing {
	publications {
		mavenJava(MavenPublication) {
			from components.java
		}
	}
}

