Skip to content

Commit

Permalink
[systemjs] plugin improvements
Browse files Browse the repository at this point in the history
 * extract a separate install task
 * make plugin self-contained: no external package.json and script files necessary anymore
 * only depend on systemjs-builder and command-line-args
  • Loading branch information
tsalzinger committed Feb 4, 2016
1 parent 04f2622 commit 539aa9d
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 15 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'
]
}
```
2 changes: 2 additions & 0 deletions cat-gradle-systemjs-plugin/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
dependencies {
compile 'com.moowork.gradle:gradle-node-plugin:0.11'

compile project(':cat-gradle-utils')
}
Original file line number Diff line number Diff line change
@@ -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<String, String> 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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -13,15 +14,15 @@ class SystemjsPlugin implements Plugin<Project> {
@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')) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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'))
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
}
}
Original file line number Diff line number Diff line change
@@ -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));
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import org.gradle.api.Project
* @author Thomas Scheinecker, Catalysts GmbH
*/
abstract class AbstractNpmAwareExtension {
Map<String, String> npmDependencies = [:]
abstract Map<String, String> getNpmDependencies()
abstract void setNpmDependencies(Map<String, String> npmDependencies)
File destinationDir
File nodeModulesDir

Expand Down

0 comments on commit 539aa9d

Please sign in to comment.