Skip to content

Commit

Permalink
Implementing new task api for minifyJs task
Browse files Browse the repository at this point in the history
  • Loading branch information
eriwen committed Mar 10, 2012
1 parent f0be9a8 commit 1bd34f3
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ apply plugin: 'idea'

defaultTasks 'clean', 'build'

version = '0.3.13-SNAPSHOT'
version = '0.3.37-SNAPSHOT'
group = 'com.eriwen'
archivesBaseName = 'gradle-js-plugin'
isSnapshot = version.endsWith("-SNAPSHOT")
Expand Down
14 changes: 12 additions & 2 deletions plugin.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.eriwen:gradle-js-plugin:0.3.13-SNAPSHOT'
classpath 'com.eriwen:gradle-js-plugin:0.3.37-SNAPSHOT'
}
}

Expand All @@ -27,17 +27,27 @@ apply plugin: 'js'
combineJs {
file2 = fileTree(dir: "${projectDir}/src/test/resources", includes: ['file2.js'])
file1 = fileTree(dir: "${projectDir}/src/test/resources", includes: ['file1.js'])
inputs.files file2 + file1
inputs.files file1 + file2
outputs.file file("${buildDir}/all.js")
}

task combine(type: com.eriwen.gradle.js.tasks.CombineJsTask) {
source = ["${projectDir}/src/test/resources/file1.js", "${projectDir}/src/test/resources/file2.js"]
dest = "${buildDir}/all.js"
}

// Minify with Google Closure Compiler
minifyJs {
inputs.file file("${buildDir}/all.js")
outputs.file file("${buildDir}/all-min.js")
warningLevel = 'QUIET'
}

task minify(type: com.eriwen.gradle.js.tasks.MinifyJsTask) {
source = "${buildDir}/all.js"
dest = "${buildDir}/all-min.js"
}

// GZip it!
gzipJs {
inputs.file file("${buildDir}/all-min.js")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class CombineJsTask extends DefaultTask {
def run() {
if (!getInputs().files.files.empty) {
logger.warn('The syntax "inputs.files ..." is deprecated! Please use `source = ["path1", "path2"]`')
logger.warn('This will be removed very soon')
logger.warn('This will be removed in the next version of the JS plugin')
source = getInputs().files.files.collect { it.canonicalPath }
}

Expand Down
29 changes: 23 additions & 6 deletions src/main/groovy/com/eriwen/gradle/js/tasks/MinifyJsTask.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,41 @@ import org.gradle.api.tasks.TaskAction

import com.google.javascript.jscomp.CompilerOptions
import com.eriwen.gradle.js.JsMinifier
import org.codehaus.groovy.runtime.GStringImpl

class MinifyJsTask extends DefaultTask {
private static final JsMinifier MINIFIER = new JsMinifier()

// FIXME: Wire defaults in properly through convention (#14)
CompilerOptions compilerOptions = new CompilerOptions()
String compilationLevel = 'SIMPLE_OPTIMIZATIONS'
String warningLevel = 'DEFAULT'
def source
def dest

@TaskAction
def run() {
def inputFiles = getInputs().files.files.toArray()
def outputFiles = getOutputs().files.files.toArray()
if (outputFiles.size() == inputFiles.size()) {
for (int i = 0; i < inputFiles.size(); i++) {
MINIFIER.minifyJsFile(inputFiles[i] as File, outputFiles[i] as File, compilerOptions, warningLevel, compilationLevel)
if (!source) {
logger.warn('The syntax "inputs.files ..." is deprecated! Please use `source = "path1"`')
logger.warn('This will be removed in the next version of the JS plugin')
source = getInputs().files.files.collect { it.canonicalPath }
} else if (source instanceof GStringImpl || source instanceof String) {
source = [source]
}

if (!dest) {
logger.warn('The syntax "outputs.files ..." is deprecated! Please use `dest = "dest/filename.js"`')
dest = getOutputs().files.files.collect { it.canonicalPath }
} else if (dest instanceof GStringImpl || dest instanceof String) {
dest = [dest]
}

if (dest.size() == source.size()) {
for (int i = 0; i < source.size(); i++) {
MINIFIER.minifyJsFile(project.file(source[i]), project.file(dest[i]), compilerOptions, warningLevel, compilationLevel)
}
} else {
throw new IllegalArgumentException("Could not map input files to output files. Found ${inputFiles.size()} inputs and ${outputFiles.size()} outputs")
throw new IllegalArgumentException("Could not map input files to output files. Found ${source.size()} inputs and ${dest.size()} outputs")
}
}
}

0 comments on commit 1bd34f3

Please sign in to comment.