diff --git a/README.md b/README.md index 31a9faa..b7b105f 100644 --- a/README.md +++ b/README.md @@ -193,10 +193,8 @@ SYSTEMJS ------ Takes all configured files of a configured source folder and creates a systemjs bundle for it. -Currently the `package.json` and `gulpfile.js` files which perform the actual task need to be part of the module where the plugin is applied. -This will change in a future version. -```groovy +``` buildscript { dependencies { classpath 'cc.catalysts.gradle:cat-gradle-systemjs-plugin:' + catGradleVersion @@ -206,9 +204,18 @@ buildscript { apply plugin: 'cc.catalysts.systemjs' buildinfo { + // The source directory of your js files srcDir = new File(project.projectDir, 'src/main/resources') - destinationDir = new File(project.buildDir, "generated-resources/cat-systemjs") + // A glob pattern to specify which js files to include includePath = "**${File.separator}*.js" + // The destination directory which will be treated as a 'resource' folder when the java plugin is present + destinationDir = new File(project.buildDir, "generated-resources/cat-systemjs") + // The path to where the bundle file will be located bundlePath = "META-INF/resources/webjars/${project.name}/${project.rootProject.version}" + // A map of npm dependencies to use - can be used to change versions + npmDependencies = [ + 'command-line-args': '2.1.4', + 'systemjs-builder' : '0.15.3' + ] } ``` diff --git a/cat-gradle-systemjs-plugin/build.gradle b/cat-gradle-systemjs-plugin/build.gradle index a63f6cb..6b1241b 100644 --- a/cat-gradle-systemjs-plugin/build.gradle +++ b/cat-gradle-systemjs-plugin/build.gradle @@ -1,3 +1,5 @@ dependencies { compile 'com.moowork.gradle:gradle-node-plugin:0.11' + + compile project(':cat-gradle-utils') } \ No newline at end of file diff --git a/cat-gradle-systemjs-plugin/src/main/groovy/cc/catalysts/gradle/systemjs/SystemjsExtension.groovy b/cat-gradle-systemjs-plugin/src/main/groovy/cc/catalysts/gradle/systemjs/SystemjsExtension.groovy index f064898..0e576d6 100644 --- a/cat-gradle-systemjs-plugin/src/main/groovy/cc/catalysts/gradle/systemjs/SystemjsExtension.groovy +++ b/cat-gradle-systemjs-plugin/src/main/groovy/cc/catalysts/gradle/systemjs/SystemjsExtension.groovy @@ -1,20 +1,24 @@ package cc.catalysts.gradle.systemjs +import cc.catalysts.gradle.npm.AbstractNpmAwareExtension import org.gradle.api.Project /** * @author Thomas Scheinecker, Catalysts GmbH */ -class SystemjsExtension { +class SystemjsExtension extends AbstractNpmAwareExtension { File srcDir - File destinationDir String includePath = "**${File.separator}*.js" String bundlePath + Map npmDependencies = [ + 'command-line-args': '2.1.4', + 'systemjs-builder' : '0.15.3' + ] SystemjsExtension(Project project) { + super(project, 'cat-systemjs') srcDir = new File(project.projectDir, 'src/main/resources') bundlePath = "META-INF/resources/webjars/${project.name}/${project.version}" - destinationDir = new File(project.buildDir, "generated-resources/cat-systemjs") } File getBundleLocation() { diff --git a/cat-gradle-systemjs-plugin/src/main/groovy/cc/catalysts/gradle/systemjs/SystemjsPlugin.groovy b/cat-gradle-systemjs-plugin/src/main/groovy/cc/catalysts/gradle/systemjs/SystemjsPlugin.groovy index 09bc866..047d6f1 100644 --- a/cat-gradle-systemjs-plugin/src/main/groovy/cc/catalysts/gradle/systemjs/SystemjsPlugin.groovy +++ b/cat-gradle-systemjs-plugin/src/main/groovy/cc/catalysts/gradle/systemjs/SystemjsPlugin.groovy @@ -2,6 +2,7 @@ package cc.catalysts.gradle.systemjs import cc.catalysts.gradle.systemjs.task.CleanSystemjsBundle import cc.catalysts.gradle.systemjs.task.CreateSystemjsBundle +import cc.catalysts.gradle.systemjs.task.InstallSystemjsBuilder import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.Task @@ -13,15 +14,15 @@ class SystemjsPlugin implements Plugin { @Override void apply(Project project) { applyNodePluginAndDefaults(project) - project.extensions.add('systemjs', new SystemjsExtension(project)) + project.extensions.create('systemjs', SystemjsExtension, project) addDestinationDirToSourceSets(project) + InstallSystemjsBuilder installSystemjsBuilder = project.tasks.create('systemjs-builder-install', InstallSystemjsBuilder) + Task bundle = project.task('systemJsBundle', - dependsOn: 'npmInstall', - type: CreateSystemjsBundle, - description: 'Creates a systemjs bundle of all specified js files', - group: 'cat-boot') + dependsOn: installSystemjsBuilder, + type: CreateSystemjsBundle) project.tasks.getByName(JavaPlugin.PROCESS_RESOURCES_TASK_NAME).dependsOn(bundle) if (!project.tasks.findByName('clean')) { diff --git a/cat-gradle-systemjs-plugin/src/main/groovy/cc/catalysts/gradle/systemjs/task/CreateSystemjsBundle.groovy b/cat-gradle-systemjs-plugin/src/main/groovy/cc/catalysts/gradle/systemjs/task/CreateSystemjsBundle.groovy index ab43448..3fd1dd1 100644 --- a/cat-gradle-systemjs-plugin/src/main/groovy/cc/catalysts/gradle/systemjs/task/CreateSystemjsBundle.groovy +++ b/cat-gradle-systemjs-plugin/src/main/groovy/cc/catalysts/gradle/systemjs/task/CreateSystemjsBundle.groovy @@ -1,7 +1,6 @@ package cc.catalysts.gradle.systemjs.task import cc.catalysts.gradle.systemjs.SystemjsExtension -import com.moowork.gradle.node.NodeExtension import com.moowork.gradle.node.task.NodeTask import org.gradle.api.artifacts.Configuration import org.gradle.api.artifacts.ModuleVersionIdentifier @@ -12,10 +11,13 @@ import org.gradle.execution.commandline.TaskConfigurationException */ class CreateSystemjsBundle extends NodeTask { CreateSystemjsBundle() { + description = 'Creates a systemjs bundle of all specified js files' + group = 'cat-boot' + project.afterEvaluate({ - setScript(new File(NodeExtension.get(project).nodeModulesDir, 'node_modules/gulp/bin/gulp.js')) SystemjsExtension config = SystemjsExtension.get(project) + setScript(new File(config.nodeModulesDir, 'systemjs-bundle.es6')) inputs.dir(config.srcDir) inputs.file(new File(project.projectDir, 'gulpfile.js')) diff --git a/cat-gradle-systemjs-plugin/src/main/groovy/cc/catalysts/gradle/systemjs/task/InstallSystemjsBuilder.groovy b/cat-gradle-systemjs-plugin/src/main/groovy/cc/catalysts/gradle/systemjs/task/InstallSystemjsBuilder.groovy new file mode 100644 index 0000000..d7f5864 --- /dev/null +++ b/cat-gradle-systemjs-plugin/src/main/groovy/cc/catalysts/gradle/systemjs/task/InstallSystemjsBuilder.groovy @@ -0,0 +1,71 @@ +package cc.catalysts.gradle.systemjs.task + +import cc.catalysts.gradle.systemjs.SystemjsExtension +import cc.catalysts.gradle.systemjs.SystemjsPlugin +import com.moowork.gradle.node.task.NpmTask +import org.gradle.execution.commandline.TaskConfigurationException + +import java.nio.file.Files +import java.nio.file.StandardCopyOption + +/** + * @author Thomas Scheinecker, Catalysts GmbH + */ +class InstallSystemjsBuilder extends NpmTask { + public InstallSystemjsBuilder() { + this.group = 'Cat-Boot' + this.description = 'Prepares the systemjs builder' + + project.afterEvaluate({ + SystemjsExtension config = SystemjsExtension.get(project) + + outputs.upToDateWhen { + return config + .getPackageJson() + .equals(config.getPackageJsonFile()) + } + + setWorkingDir(config.nodeModulesDir) + setNpmCommand('install') + + outputs.dir(new File(config.nodeModulesDir, 'node_modules')) + }) + } + + private void writePackageJson() { + SystemjsExtension config = SystemjsExtension.get(project) + File packageJsonFile = config.getPackageJsonFile() + + config.getPackageJson() + .toFile(packageJsonFile) + + if (!packageJsonFile.exists()) { + throw new TaskConfigurationException(path, "Couldn't create ${packageJsonFile}!", null) + } + + logger.lifecycle("Successfully created ${packageJsonFile}") + } + + private void writeSystemjsBundleScript() { + File nodeModulesDir = SystemjsExtension.get(project).nodeModulesDir + + + InputStream systemjsBundleScriptSource = SystemjsPlugin.classLoader.getResourceAsStream('cat-gradle/systemjs/systemjs-bundle.es6') + + File systemjsBundleScript = new File(nodeModulesDir, 'systemjs-bundle.es6') + Files.copy(systemjsBundleScriptSource, systemjsBundleScript.toPath(), StandardCopyOption.REPLACE_EXISTING) + + if (!systemjsBundleScript.exists()) { + throw new TaskConfigurationException(path, "Couldn't create ${systemjsBundleScript}!", null) + } + + logger.lifecycle("Successfully created ${systemjsBundleScript}") + } + + @Override + void exec() { + writePackageJson(); + writeSystemjsBundleScript(); + super.exec() + } +} diff --git a/cat-gradle-systemjs-plugin/src/main/resources/cat-gradle/systemjs/systemjs-bundle.es6 b/cat-gradle-systemjs-plugin/src/main/resources/cat-gradle/systemjs/systemjs-bundle.es6 new file mode 100644 index 0000000..b31de01 --- /dev/null +++ b/cat-gradle-systemjs-plugin/src/main/resources/cat-gradle/systemjs/systemjs-bundle.es6 @@ -0,0 +1,51 @@ +'use strict'; + +var path = require('path'); +var commandLineArgs = require('command-line-args'); + +var cli = commandLineArgs([ + {name: 'project.version', type: String}, + {name: 'source.dir', type: String}, + {name: 'include.path', type: String}, + {name: 'destination.dir', type: String}, + {name: 'bundle.name', type: String}, + {name: 'no-color', type: Boolean}, + {name: 'color', type: Boolean}, + {name: 'cwd', type: String} +]); + +var options = cli.parse(); + +var SystemJsBuilder = require('systemjs-builder'); + +var systemJsBuilderOptions = { + minify: false, + sourceMaps: false +}; + + +function buildBundle(options) { + var sourceDir = path.relative(process.cwd(), options['source.dir']); + var systemJsBuilder = new SystemJsBuilder(sourceDir); + + systemJsBuilder.config({ + meta: {} + }); + + var sourcePath = path.relative(process.cwd(), options['source.dir'] + '/' + options['include.path']); + var bundleFile = path.relative(process.cwd(), options['destination.dir'] + '/' + options['bundle.name'] + '.js'); + return systemJsBuilder + .bundle('[./' + sourcePath + ']', + bundleFile, + systemJsBuilderOptions) + .then(function (result) { + console.log('Sucessfully created bundles for modules:'); + result.modules.forEach(function (module) { + console.log(module); + }); + console.log('Bundle file was saved to', bundleFile); + }); +} + +buildBundle(options) + .then(null, console.error.bind(console)); \ No newline at end of file diff --git a/cat-gradle-utils/src/main/groovy/cc/catalysts/gradle/npm/AbstractNpmAwareExtension.groovy b/cat-gradle-utils/src/main/groovy/cc/catalysts/gradle/npm/AbstractNpmAwareExtension.groovy index 5ee97e7..2881327 100644 --- a/cat-gradle-utils/src/main/groovy/cc/catalysts/gradle/npm/AbstractNpmAwareExtension.groovy +++ b/cat-gradle-utils/src/main/groovy/cc/catalysts/gradle/npm/AbstractNpmAwareExtension.groovy @@ -6,7 +6,8 @@ import org.gradle.api.Project * @author Thomas Scheinecker, Catalysts GmbH */ abstract class AbstractNpmAwareExtension { - Map npmDependencies = [:] + abstract Map getNpmDependencies() + abstract void setNpmDependencies(Map npmDependencies) File destinationDir File nodeModulesDir