Skip to content
Browse files

Improved task API, deprecating old one

  • Loading branch information...
1 parent 7b49e46 commit 9f6c15f3cc69b95df2c4b5da560bf7474957b9bd @eriwen committed Apr 6, 2012
View
10 build.gradle
@@ -5,14 +5,13 @@ apply plugin: 'idea'
defaultTasks 'clean', 'build'
-version = '0.3.0-SNAPSHOT'
+version = '0.3.2-SNAPSHOT'
group = 'com.eriwen'
artifact = 'gradle-css-plugin'
isSnapshot = version.endsWith("-SNAPSHOT")
repositories {
mavenCentral()
- maven { url 'http://m2repo.spockframework.org/snapshots/' }
}
dependencies {
@@ -21,7 +20,8 @@ dependencies {
compile('com.yahoo.platform.yui:yuicompressor:2.4.6') {
exclude module: 'junit'
}
- testCompile('org.spockframework:spock-core:0.6-groovy-1.8-SNAPSHOT') {
+// compile('org.jruby:jruby:1.6.7')
+ testCompile ('org.spockframework:spock-core:0.6-groovy-1.8') {
exclude module: "junit-dep"
exclude module: "groovy-all"
}
@@ -52,7 +52,7 @@ task groovydocJar(type: Jar) {
}
task wrapper(type: Wrapper) {
- gradleVersion = '1.0-milestone-8a'
+ gradleVersion = '1.0-milestone-9'
}
artifacts {
@@ -77,8 +77,6 @@ install.repositories.mavenInstaller {
}
}
-// NOTE: Must still rename build/pom-default.xml to pom.xml
-// NOTE: Must rename pom-default.xml.asc to gradle-css-plugin-${version}.pom.asc
uploadArchives {
repositories.mavenDeployer {
if (signing.signatory) {
View
66 plugin.gradle
@@ -1,28 +1,74 @@
buildscript {
repositories {
+ mavenCentral()
mavenLocal()
}
dependencies {
- classpath 'com.eriwen:gradle-css-plugin:0.1-SNAPSHOT'
+ classpath 'com.eriwen:gradle-css-plugin:0.3.2-SNAPSHOT'
}
}
+repositories {
+ mavenLocal()
+ mavenCentral()
+}
+
+configurations {
+ rhino
+}
+
+dependencies {
+ rhino 'org.mozilla:rhino:1.7R3'
+}
+
apply plugin: 'css'
+// Combine CSS files
combineCss {
- input = "${projectDir}/css"
- output = "${cssOutputDir}/all.${version}.css"
+ file2 = fileTree(dir: "${projectDir}/src/test/resources", includes: ['file2.css'])
+ file1 = fileTree(dir: "${projectDir}/src/test/resources", includes: ['file1.css'])
+ inputs.files file1 + file2
+ outputs.file file("${buildDir}/all.css")
+}
+
+task combine(type: com.eriwen.gradle.css.tasks.CombineCssTask) {
+ //javascript.source.custom.css.files.toList()
+ source = ["${projectDir}/src/test/resources/file1.css", "${projectDir}/src/test/resources/file2.css"]
+ dest = file("${buildDir}/all.css")
}
+// Minify with Google Closure Compiler
minifyCss {
- dependsOn << 'combineCss'
- input = "${cssOutputDir}/all.${version}.css"
- output = "${cssOutputDir}/${cssOutputFile}"
- lineBreakPos = 0
+ inputs.file file("${buildDir}/all.css")
+ outputs.file file("${buildDir}/all-min.css")
+ warningLevel = 'QUIET'
}
+task minify(type: com.eriwen.gradle.css.tasks.MinifyCssTask) {
+ source = file("${buildDir}/all.css")
+ dest = file("${buildDir}/all-min.css")
+}
+
+// GZip it!
gzipCss {
- dependsOn << 'minifyCss'
- input = "${cssOutputDir}/${cssOutputFile}"
- output = input
+ inputs.file file("${buildDir}/all-min.css")
+ outputs.file file("${buildDir}/all-min.css")
+}
+
+task gzip(type: com.eriwen.gradle.css.tasks.GzipCssTask) {
+ source = file("${buildDir}/all-min.css")
+ dest = file("${buildDir}/all-min.css")
+}
+
+csslint {
+ file2 = fileTree(dir: "${projectDir}/src/test/resources", includes: ['file2.css'])
+ file1 = fileTree(dir: "${projectDir}/src/test/resources", includes: ['file1.css'])
+ inputs.files file1 + file2
+ outputs.file file("${buildDir}/csslint.out")
+}
+
+task lint(type: com.eriwen.gradle.css.tasks.CssLintTask) {
+ source = ["${projectDir}/src/test/resources/file1.css", "${projectDir}/src/test/resources/file2.css"]
+ dest = file("${buildDir}/csslint.txt")
+ options = ['--format=compact']
}
View
46 src/main/groovy/com/eriwen/gradle/css/ResourceUtil.groovy
@@ -0,0 +1,46 @@
+/**
+ * Copyright 2012 Eric Wendelin
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.eriwen.gradle.css
+
+/**
+ * Operations to handle zip files and other resources on the classpath.
+ *
+ * @author Eric Wendelin
+ */
+class ResourceUtil {
+ /**
+ * Given a target dir and resource, extract resource to target if it's not there already.
+ *
+ * @param targetDirectory directory to extract to
+ * @param resourcePath path to resource to extract
+ * @return reference to extracted or existing file
+ */
+ File extractFileToDirectory(final File targetDirectory, final String resourcePath) {
+ if (targetDirectory.exists() && !targetDirectory.isDirectory()) {
+ throw new IllegalArgumentException("Target directory is a file!")
+ } else if (!targetDirectory.exists()) {
+ targetDirectory.mkdirs()
+ }
+
+ final File file = new File(targetDirectory, resourcePath)
+ if (!file.exists()) {
+ final InputStream inputStream = getClass().getClassLoader().getResourceAsStream(resourcePath)
+ file << inputStream
+ inputStream.close()
+ }
+ return file
+ }
+}
View
30 src/main/groovy/com/eriwen/gradle/css/RhinoExec.groovy
@@ -0,0 +1,30 @@
+package com.eriwen.gradle.css
+
+import org.gradle.api.Project
+
+/**
+ * Utility for executing JS with Rhino.
+ *
+ * @author Eric Wendelin
+ * @date 2/20/12
+ */
+class RhinoExec {
+ private static final String RHINO_MAIN_CLASS = 'org.mozilla.javascript.tools.shell.Main'
+ Project project
+
+ void execute(final Iterable<String> execargs, final File outputFile, final String workingDirIn = '.') {
+ def options = {
+ main = RHINO_MAIN_CLASS
+ classpath = project.configurations.rhino
+ args = execargs
+ workingDir = workingDirIn
+ standardOutput = new FileOutputStream(outputFile)
+ }
+
+ project.javaexec(options)
+ }
+
+ public RhinoExec(final Project projectIn) {
+ project = projectIn
+ }
+}
View
33 src/main/groovy/com/eriwen/gradle/css/tasks/CombineCssTask.groovy
@@ -17,19 +17,38 @@ package com.eriwen.gradle.css.tasks
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction
+import org.gradle.api.GradleException
class CombineCssTask extends DefaultTask {
+ def source
+ File dest
+
@TaskAction
def run() {
- def outputFiles = getOutputs().files
- if (outputFiles.files.size() == 1) {
- ant.concat(destfile: outputFiles.asPath, fixlastline: 'yes') {
- getInputs().files.each {
- fileset(file: it.canonicalPath)
+ if (!getInputs().files.files.empty) {
+ logger.warn('The syntax "inputs.files ..." is deprecated! Please use `source = ["path1", "path2"]`')
+ logger.warn('This will be removed in the next version of the CSS plugin')
+ source = getInputs().files.files.collect { it.canonicalPath }
+ }
+
+ if (!getOutputs().files.files.empty) {
+ logger.warn 'The syntax "outputs.files ..." is deprecated! Please use `dest = "dest/filename.js"`'
+ def outputFiles = getOutputs().files.files
+ if (outputFiles.size() == 1) {
+ dest = (outputFiles.toArray()[0] as File)
+ } else if (!dest) {
+ throw new GradleException('Output must be exactly 1 File object. Example: dest = "myFile"')
+ }
+ }
+
+ ant.concat(destfile: dest.canonicalPath, fixlastline: 'yes') {
+ source.each { String path ->
+ if (!(new File(path).exists())) {
+ throw new GradleException("Source CSS file ${path} does not exist!")
}
+ logger.info("Adding to fileset: ${path}")
+ fileset(file: path)
}
- } else {
- throw new IllegalArgumentException('Output must be exactly 1 File object. Example: outputs.file = file("myFile")')
}
}
}
View
45 src/main/groovy/com/eriwen/gradle/css/tasks/CssLintTask.groovy
@@ -1,5 +1,5 @@
/**
- * Copyright 2011 Eric Wendelin
+ * Copyright 2012 Eric Wendelin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,36 +17,37 @@ package com.eriwen.gradle.css.tasks
import org.gradle.api.tasks.TaskAction
import org.gradle.api.DefaultTask
+import com.eriwen.gradle.css.RhinoExec
+import com.eriwen.gradle.css.ResourceUtil
class CssLintTask extends DefaultTask {
private static final String CSSLINT_PATH = 'csslint-rhino.js'
private static final String TMP_DIR = 'tmp/css'
- Iterable<String> options = []
+ def options = []
+ private static final ResourceUtil RESOURCE_UTIL = new ResourceUtil()
+ private final RhinoExec rhino = new RhinoExec(project)
+
+ def source
+ File dest
@TaskAction
def run() {
- File csslintJsFile = loadCssLintJs()
- String outputPath = (getOutputs().files.files.toArray()[0] as File).canonicalPath
- ant.java(jar: project.configurations.rhino.asPath, fork: true, output: outputPath) {
- arg(value: csslintJsFile.canonicalPath)
- options.each {
- arg(value: it)
- }
- getInputs().files.files.each {
- arg(value: it.canonicalPath)
- }
+ 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 CSS plugin')
+ source = getInputs().files.files.collect { it.canonicalPath }
}
- }
- File loadCssLintJs() {
- InputStream inputStream = getClass().getClassLoader().getResourceAsStream(CSSLINT_PATH)
- File tempDir = new File(project.buildDir, TMP_DIR)
- tempDir.mkdirs()
- File csslintJsFile = new File(tempDir, CSSLINT_PATH)
- if (!csslintJsFile.exists()) {
- csslintJsFile << inputStream
+ if (!dest) {
+ logger.warn 'The syntax "outputs.file file(..)" is deprecated! Please use `dest = file(buildDir)`'
+ dest = getOutputs().files.files.toArray()[0] as File
}
- inputStream.close()
- return csslintJsFile
+
+ final File csslintJsFile = RESOURCE_UTIL.extractFileToDirectory(
+ new File(project.buildDir, TMP_DIR), CSSLINT_PATH)
+ final List<String> args = [csslintJsFile.canonicalPath]
+ args.addAll(source)
+ args.addAll(options)
+ rhino.execute(args, dest)
}
}
View
30 src/main/groovy/com/eriwen/gradle/css/tasks/GzipCssTask.groovy
@@ -1,5 +1,5 @@
/**
- * Copyright 2011 Eric Wendelin
+ * Copyright 2012 Eric Wendelin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,20 +17,30 @@ package com.eriwen.gradle.css.tasks
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction
+import org.gradle.api.GradleException
class GzipCssTask extends DefaultTask {
+ def source
+ File 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++) {
- String inputPath = (inputFiles[i] as File).canonicalPath
- ant.gzip(src: inputPath, destfile: "${inputPath}.gz")
- ant.move(file: "${inputPath}.gz", tofile: (outputFiles[i] as File).canonicalPath)
- }
+ if (!source) {
+ logger.warn('The syntax "inputs.files ..." is deprecated! Please use `source = file("path1")`')
+ logger.warn('This will be removed in the next version of the CSS plugin')
+ source = getInputs().files.files.toArray()[0] as File
+ }
+
+ if (!dest) {
+ logger.warn('The syntax "outputs.files ..." is deprecated! Please use `dest = file("dest/file.js")`')
+ dest = getOutputs().files.files.toArray()[0] as File
+ }
+
+ if (!source.exists()) {
+ throw new GradleException("CSS file ${source.canonicalPath} doesn't exist!")
} else {
- throw new IllegalArgumentException("Could not map input files to output files. Found ${inputFiles.size()} inputs and ${outputFiles.size()} outputs")
+ ant.gzip(src: source.canonicalPath, destfile: "${source.canonicalPath}.gz")
+ ant.move(file: "${source.canonicalPath}.gz", tofile: dest.canonicalPath)
}
}
}
View
33 src/main/groovy/com/eriwen/gradle/css/tasks/MinifyCssTask.groovy
@@ -1,5 +1,5 @@
/**
- * Copyright 2011 Eric Wendelin
+ * Copyright 2012 Eric Wendelin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,21 +18,32 @@ package com.eriwen.gradle.css.tasks
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction
import com.eriwen.gradle.css.CssMinifier
+import org.gradle.api.GradleException
class MinifyCssTask extends DefaultTask {
private static final CssMinifier MINIFIER = new CssMinifier()
+
Integer lineBreakPos = -1
+ def source
+ File 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.minifyCssFile(inputFiles[i] as File, outputFiles[i] as File, lineBreakPos)
- }
- } else {
- throw new IllegalArgumentException("Could not map input files to output files. Found ${inputFiles.size()} inputs and ${outputFiles.size()} outputs")
+ @TaskAction
+ def run() {
+ if (!source) {
+ logger.warn 'The syntax "inputs.files ..." is deprecated! Please use `source = file("path1")`'
+ logger.warn 'This will be removed in the next version of the CSS plugin'
+ source = getInputs().files.files.toArray()[0] as File
}
+
+ if (!source.exists()) {
+ throw new GradleException("CSS file ${source.canonicalPath} doesn't exist!")
+ }
+
+ if (!dest) {
+ logger.warn 'The syntax "outputs.files ..." is deprecated! Please use `dest = file("dest/file.js")`'
+ dest = getOutputs().files.files.toArray()[0] as File
+ }
+
+ MINIFIER.minifyCssFile(source, dest, lineBreakPos)
}
}

0 comments on commit 9f6c15f

Please sign in to comment.
Something went wrong with that request. Please try again.