From b0278a40e587a008ccf74dc04e29604123f82a1a Mon Sep 17 00:00:00 2001 From: Thomas Scheinecker Date: Thu, 28 Jan 2016 18:14:13 +0100 Subject: [PATCH] improve less build again --- .../gradle/less/task/InstallLess.groovy | 46 +++++++++++++++++-- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/cat-gradle-less-plugin/src/main/groovy/cc/catalysts/gradle/less/task/InstallLess.groovy b/cat-gradle-less-plugin/src/main/groovy/cc/catalysts/gradle/less/task/InstallLess.groovy index 2a01637..f0dea7c 100644 --- a/cat-gradle-less-plugin/src/main/groovy/cc/catalysts/gradle/less/task/InstallLess.groovy +++ b/cat-gradle-less-plugin/src/main/groovy/cc/catalysts/gradle/less/task/InstallLess.groovy @@ -2,15 +2,55 @@ package cc.catalysts.gradle.less.task import cc.catalysts.gradle.less.LessExtension import com.moowork.gradle.node.task.NpmTask +import org.gradle.execution.commandline.TaskConfigurationException + /** * @author Thomas Scheinecker, Catalysts GmbH */ class InstallLess extends NpmTask { + private static final PACKAGE_JSON = """{ + "private": true, + "dependencies": { + "less": "2.5.3", + "less-plugin-autoprefix": "1.5.1", + "less-plugin-clean-css": "1.5.1" + } +} +"""; + + public InstallLess() { this.group = 'Cat-Boot LESS' this.description = 'Prepares the less compiler' - File nodeModulesDir = LessExtension.get(project).nodeModulesDir - setWorkingDir(nodeModulesDir) - setNpmCommand('install', 'less', 'less-plugin-autoprefix', 'less-plugin-clean-css') + + + project.afterEvaluate({ + File nodeModulesDir = LessExtension.get(project).nodeModulesDir + + if (!nodeModulesDir.exists() && !nodeModulesDir.mkdirs()) { + throw new TaskConfigurationException(path, "Couldn't create ${nodeModulesDir}!", null) + } + + File projectPackageJson = new File(nodeModulesDir, 'package.json') + + if (!projectPackageJson.exists()) { + // first run + outputs.upToDateWhen { false } + } else if (!projectPackageJson.delete()) { + throw new TaskConfigurationException(path, "Couldn't delete ${projectPackageJson}!", null) + } + + projectPackageJson.text = PACKAGE_JSON + + if (!projectPackageJson.exists()) { + throw new TaskConfigurationException(path, "Couldn't create ${projectPackageJson}!", null) + } + + setWorkingDir(nodeModulesDir) + setNpmCommand('install') + + inputs.file(projectPackageJson) + outputs.dir(new File(nodeModulesDir, 'node_modules')) + }) } }