Skip to content

Commit

Permalink
Add needsCache capability.
Browse files Browse the repository at this point in the history
  • Loading branch information
rwjblue committed Dec 2, 2016
1 parent 6b3c117 commit 7b299d1
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 11 deletions.
14 changes: 10 additions & 4 deletions docs/node-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,21 @@ hypothetical "broccoli-fooscript" plugin would communicate with Broccoli:
| |
| broccoli-fooscript |
+----+ |
| +-----------+------------+
| +-----------|------------+
| |
npm dependency: | | broccoli-plugin base class interface (easy to
broccoli-plugin ^1.2.3 | | use), described in the broccoli-plugin README
| |
| +-----------+------------+
| +-----------|------------+
+----> |
| broccoli-plugin 1.2.3 |
| |
+-----------+------------+
+-----------|------------+
|
(no npm dependency here) | Broccoli node API (versioned, complex),
| described in this document
|
+-----------+------------+
+-----------|------------+
| |
| broccoli (Builder) |
| |
Expand Down Expand Up @@ -242,6 +242,9 @@ following `nodeInfo` properties are specific to "transform" nodes:
Broccoli process terminates (for example, when the Broccoli server is
restarted).

If a `cachePath` is not needed/desired, a plugin can opt-out of its creation
via the `needsCache` flag metioned below.

* `nodeInfo.getCallbackObject` {`function()`, returns an object}:
The Builder will call this function once after it has called `setup`. This
function will not be called more than once throughout the lifetime of the
Expand Down Expand Up @@ -271,6 +274,9 @@ following `nodeInfo` properties are specific to "transform" nodes:
between Broccoli server restarts or `broccoli build` invocations even if
`persistentOutput` is true.

* `nodeInfo.needsCache` {boolean}:
If `false`, a cache directory will not be created. If `true`, a cache directory
will be created and its path will be available as `this.cachePath`.

#### Source nodes

Expand Down
8 changes: 6 additions & 2 deletions lib/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,10 @@ Builder.prototype.setupTmpDirs = function() {
return nw.outputPath
})
nodeWrapper.outputPath = this.mkTmpDir(nodeWrapper, 'out')
nodeWrapper.cachePath = this.mkTmpDir(nodeWrapper, 'cache')

if (nodeWrapper.nodeInfo.needsCache) {
nodeWrapper.cachePath = this.mkTmpDir(nodeWrapper, 'cache')
}
} else { // nodeType === 'source'
// We could name this .sourcePath, but with .outputPath the code is simpler.
nodeWrapper.outputPath = nodeWrapper.nodeInfo.sourceDirectory
Expand Down Expand Up @@ -502,7 +505,8 @@ TransformNodeWrapper.prototype.nodeInfoToJSON = function() {
nodeType: 'transform',
name: this.nodeInfo.name,
annotation: this.nodeInfo.annotation,
persistentOutput: this.nodeInfo.persistentOutput
persistentOutput: this.nodeInfo.persistentOutput,
needsCache: this.nodeInfo.needsCache
// leave out instantiationStack (too long), inputNodes, and callbacks
})
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"homepage": "https://github.com/broccolijs/broccoli",
"dependencies": {
"broccoli-node-info": "1.0.0",
"broccoli-node-info": "1.1.0",
"broccoli-slow-trees": "^2.0.0",
"broccoli-source": "^1.1.0",
"commander": "^2.5.0",
Expand All @@ -42,6 +42,7 @@
"mocha": "^3.0.0",
"mocha-jshint": "^2.2.5",
"multidep": "^2.0.0",
"semver": "^5.3.0",
"sinon": "^1.17.1",
"sinon-chai": "^2.8.0",
"symlink-or-copy": "^1.0.1"
Expand Down
48 changes: 45 additions & 3 deletions test/builder_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ var chai = require('chai'), expect = chai.expect
var chaiAsPromised = require('chai-as-promised'); chai.use(chaiAsPromised)
var sinonChai = require('sinon-chai'); chai.use(sinonChai)
var multidepRequire = require('multidep')('test/multidep.json')
var semver = require('semver')

var Plugin = multidepRequire('broccoli-plugin', '1.2.2')
var Plugin = multidepRequire('broccoli-plugin', '1.3.0')
var broccoliSource = multidepRequire('broccoli-source', '1.1.0')

// Clean up left-over temporary directories on uncaught exception.
Expand Down Expand Up @@ -117,7 +118,7 @@ describe('Builder', function() {
})
})

it('supplies a cachePath', function() {
it('supplies a cachePath by default', function() {
// inputPath and outputPath are tested implicitly by the other tests,
// but cachePath isn't, so we have this test case

Expand All @@ -133,7 +134,47 @@ describe('Builder', function() {
builder = new Builder(new CacheTestPlugin)
return builder.build()
})

it('supplies a cachePath when requested', function() {
// inputPath and outputPath are tested implicitly by the other tests,
// but cachePath isn't, so we have this test case

CacheTestPlugin.prototype = Object.create(Plugin.prototype)
CacheTestPlugin.prototype.constructor = CacheTestPlugin
function CacheTestPlugin() {
Plugin.call(this, [], {
needsCache: true
})
}
CacheTestPlugin.prototype.build = function() {
expect(fs.existsSync(this.cachePath)).to.be.true
}

builder = new Builder(new CacheTestPlugin)
return builder.build()
})
})

if (version === 'master' || semver.gt(version, '1.3.0')) {
it('does not create a cachePath when opt-ed out', function() {
// inputPath and outputPath are tested implicitly by the other tests,
// but cachePath isn't, so we have this test case

CacheTestPlugin.prototype = Object.create(Plugin.prototype)
CacheTestPlugin.prototype.constructor = CacheTestPlugin
function CacheTestPlugin() {
Plugin.call(this, [], {
needsCache: false
})
}
CacheTestPlugin.prototype.build = function() {
expect(this.cachePath).to.equal(undefined)
}

builder = new Builder(new CacheTestPlugin)
return builder.build()
})
}
})

describe('persistentOutput flag', function() {
Expand Down Expand Up @@ -557,7 +598,8 @@ describe('Builder', function() {
nodeType: 'transform',
name: 'MergePlugin',
annotation: null,
persistentOutput: false
persistentOutput: false,
needsCache: true
},
buildState: {
selfTime: 1,
Expand Down
2 changes: 1 addition & 1 deletion test/multidep.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"path": "test/multidep",
"versions": {
"broccoli-plugin": ["1.0.0", "1.1.0", "1.2.2"],
"broccoli-plugin": ["1.0.0", "1.1.0", "1.2.3", "1.3.0"],
"broccoli-source": ["1.1.0"]
}
}

0 comments on commit 7b299d1

Please sign in to comment.