Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

skip packages that have already been visited #953

Merged
merged 2 commits into from Jan 28, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 17 additions & 3 deletions lib/app/autoload/store.server.js
Expand Up @@ -174,6 +174,7 @@ YUI.add('mojito-resource-store', function(Y, NAME) {
this._config.dir;
this._config.mojitoRoot = this._config.mojitoRoot ||
this._libs.path.join(__dirname, '../..');

this._jsonCache = {}; // fullPath: contents as JSON object
this._ycbCache = {}; // fullPath: context: YCB config object
this._routesCache = {}; // serialized context: route
Expand All @@ -182,6 +183,7 @@ YUI.add('mojito-resource-store', function(Y, NAME) {
this._getMojitTypeDetailsCache = {}; // env+posl+lang+mojitType: value
this._expandSpecCache = {}; // env+ctx+spec: value

this._packagesVisited = {}; // package@version: path
this._appRVs = []; // array of resource versions
this._mojitRVs = {}; // mojitType: array of resource versions
this._appResources = {}; // env: posl: array of resources
Expand Down Expand Up @@ -877,8 +879,8 @@ YUI.add('mojito-resource-store', function(Y, NAME) {
if (res.url && res.source.fs.isFile) {
if (urls[res.url]) {
Y.log('Url collision for ' + res.url +
'. Choosing: ' + urls[res.url].source.fs.fullPath +
' over ' + res.source.fs.fullPath, 'warn', NAME);
'. Choosing:\n' + urls[res.url].source.fs.fullPath +
' over\n' + res.source.fs.fullPath, 'warn', NAME);
} else {
urls[res.url] = res;
}
Expand Down Expand Up @@ -1093,6 +1095,7 @@ YUI.add('mojito-resource-store', function(Y, NAME) {
this.selectors = {};
this._appRVs = [];
this._mojitRVs = {};
this._packagesVisited = {};

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is _packagesVisited initialized twice?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's initialized for every call to preloadResourceVersions(). Internally, preload() calls preloadResourceVersions() twice. The first time is mainly to find RS addons (which can also be found in node_modules). The second time is the "real" load, done once the store has all its addons plugged in. If we didn't clear out packagesVisited then the RS addons wouldn't get a chance to see the resources in the NPM modules.

Said another way, the packages (and app itself) are used to populate _appRVs and _mojitRVs. Anytime we (re)initialized those structures we need to (re)initialize _packagesVisited as well, so that those RV structures can get repopulated.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it!

walker = new this._libs.walker.BreadthFirst(this._config.root);
walker.walk(function(err, info) {
Expand Down Expand Up @@ -1495,12 +1498,14 @@ YUI.add('mojito-resource-store', function(Y, NAME) {
//console.log(selectors);
//console.log(affinities);

// app-level
if (!this._appResources[env]) {
this._appResources[env] = {};
}
this._appResources[env][poslKey] =
this._resolveVersions(affinities, selectors, sourceBase, [ this._appRVs ]);

// mojit-level
if (!this._mojitResources[env]) {
this._mojitResources[env] = {};
}
Expand Down Expand Up @@ -1643,7 +1648,8 @@ YUI.add('mojito-resource-store', function(Y, NAME) {
*/
_preloadPackage: function(info) {
var dir,
pkg;
pkg,
visitKey;
// FUTURE: use info.inherit to scope mojit dependencies
/*
console.log('--PACKAGE-- ' + info.depth + ' ' + info.pkg.name + '@' + info.pkg.version
Expand All @@ -1666,6 +1672,13 @@ YUI.add('mojito-resource-store', function(Y, NAME) {
if (!info.pkg.yahoo || !info.pkg.yahoo.mojito) {
return;
}
visitKey = [info.pkg.name, info.pkg.version].join('@');
if (this._packagesVisited[visitKey]) {
Y.log('skipping duplicate package ' + visitKey + '\nskipping ' +
info.dir + '\nprev used ' + this._packagesVisited[visitKey], 'info', NAME);
return;
}

switch (info.pkg.yahoo.mojito.type) {
case 'bundle':
dir = this._libs.path.join(info.dir, info.pkg.yahoo.mojito.location);
Expand All @@ -1679,6 +1692,7 @@ YUI.add('mojito-resource-store', function(Y, NAME) {
Y.log('Unknown package type "' + info.pkg.yahoo.mojito.type + '"', 'warn', NAME);
break;
}
this._packagesVisited[visitKey] = info.dir;
},


Expand Down
20 changes: 20 additions & 0 deletions tests/fixtures/packages/node_modules/b/node_modules/a/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 13 additions & 4 deletions tests/unit/lib/app/autoload/test-package-walker.server.js
Expand Up @@ -21,6 +21,7 @@ YUI().use('test', function(Y) {
'breadth first basics': function() {
var order = [];
var walker = new libwalker.BreadthFirst(fixtures);
var whichPkgA = 0;
walker.walk(function(err, info) {
order.push(info.pkg.name + '@' + info.pkg.version);
switch(info.pkg.name) {
Expand All @@ -32,13 +33,20 @@ YUI().use('test', function(Y) {
break;

case 'a':
A.areSame(1, info.depth);
whichPkgA += 1;
A.areSame('666.1.0', info.pkg.version);
AA.itemsAreSame(['root'], info.parents);
A.areSame(libpath.join(fixtures,'node_modules/a'), info.dir);
if (1 === whichPkgA) {
A.areSame(1, info.depth, 'a depth');
AA.itemsAreSame(['root'], info.parents);
A.areSame(libpath.join(fixtures,'node_modules/a'), info.dir);
} else {
A.areSame(2, info.depth, 'a depth');
AA.itemsAreSame(['b', 'root'], info.parents);
A.areSame(libpath.join(fixtures,'node_modules/b/node_modules/a'), info.dir);
}
break;
case 'aa':
A.areSame(2, info.depth);
A.areSame(2, info.depth, 'a depth');
A.areSame('666.1.1', info.pkg.version);
AA.itemsAreSame(['a','root'], info.parents);
A.areSame(libpath.join(fixtures,'node_modules/a/node_modules/aa'), info.dir);
Expand Down Expand Up @@ -102,6 +110,7 @@ YUI().use('test', function(Y) {
'c@999.999.999',
'aa@666.1.1',
'ab@666.1.2',
'a@666.1.0',
'ba@666.2.1',
'bb@666.2.2',
//'ca@999.999.999',
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/lib/app/autoload/test-store.server.js
Expand Up @@ -520,6 +520,26 @@ YUI().use(
A.areSame('a', details.controller);
},


'skip loaded packages': function() {
var fixtures = libpath.join(__dirname, '../../../../fixtures/packages'),
store = new Y.mojito.ResourceStore({ root: fixtures });
store.preload();
var oldlog = Y.log;
var logged = false;
Y.log = function(msg, lvl, src) {
if ('info' === lvl && 'mojito-resource-store' === src && msg.match(/^skipping duplicate package a@666\.1\.0/)) {
logged = true;
}
};
try {
store.preload();
} finally {
Y.log = oldlog;
}
A.isTrue(logged, 'info logged');
},

'find and parse resources by convention': function() {
var fixtures = libpath.join(__dirname, '../../../../fixtures/conventions'),
store = new Y.mojito.ResourceStore({ root: fixtures });
Expand Down