Update codebase to the almost finalised architecture.

The GitRemoteResolver is almost done.
This commit is contained in:
André Cruz
2013-04-14 03:40:25 +01:00
parent 1642a7714f
commit 59fbc308b0
18 changed files with 409 additions and 343 deletions

View File

@@ -1,240 +0,0 @@
var util = require('util');
var fs = require('fs');
var path = require('path');
var events = require('events');
var mout = require('mout');
var Q = require('q');
var tmp = require('tmp');
var UnitOfWork = require('./UnitOfWork');
var config = require('./config');
var createPackage;
var createError = require('../util/createError');
var Package = function (endpoint, options) {
options = options || {};
this._endpoint = endpoint;
this._name = options.name;
this._guessedName = !this.name;
this._range = options.range || '*';
this._unitOfWork = options.unitOfWork || new UnitOfWork();
this._config = options.config || config;
};
util.inherits(Package, events.EventEmitter);
// -----------------
Package.prototype.getName = function () {
return this._name;
};
Package.prototype.getEndpoint = function () {
return this._endpoint;
};
Package.prototype.getRange = function () {
return this._range;
};
Package.prototype.getTempDir = function () {
return this._tempDir;
};
Package.prototype.resolve = function () {
// Throw if already resolved
if (this._resolved) {
throw createError('Package is already resolved', 'EALREADYRES');
}
// 1st - Enqueue the package in the unit of work
return this._unitOfWork.enqueue(this)
.then(function (done) {
// 2nd - Create temporary dir
return this._createTempDir()
// 3nd - Resolve self
.then(this._resolveSelf.bind(this))
// 4th - Read json
.then(this._readJson.bind(this))
// 5th - Parse json
.then(this._parseJson.bind(this))
// 6th - Mark as resolved & call done
// to inform the unit of work
.then(function (dependencies) {
this._resolved = true;
done();
return dependencies;
}.bind(this), function (err) {
this._resolveError = err;
done();
throw err;
}.bind(this))
// 7th - Resolve dependencies
.then(this._resolveDependencies.bind(this));
}.bind(this), function (err) {
// If error is of a duplicate package,
// copy everything from the resolved package (duplicate) to itself
if (err.code === 'EDUPL') {
mout.object.mixIn(this, err.pkg);
} else {
this._resolveError = err;
throw err;
}
});
};
Package.prototype.getResolveError = function () {
return this._resolveError;
};
Package.prototype.getJson = function () {
this._assertResolved();
return this._json;
};
Package.prototype.getDependencies = function () {
this._assertResolved();
return this._dependencies;
};
Package.prototype.install = function () {
this._assertResolved();
// TODO
};
// -----------------
Package.prototype._resolveSelf = function () {};
// -----------------
Package.prototype._createTempDir = function () {
console.log('_createTempDir');
// Resolved if cached
if (this._tempDir) {
return Q.fcall(this._tempDir);
}
return Q.nfcall(tmp.dir, {
prefix: 'bower-' + this.name + '-',
mode: parseInt('0777', 8) & (~process.umask())
})
.then(function (dir) {
this._tempDir = dir;
return dir;
}.bind(this));
};
Package.prototype._readJson = function () {
console.log('_readJson');
// Resolve if cached
if (this._json) {
return Q.fcall(this._json);
}
var jsonFile;
// Try bower.json
jsonFile = path.join(this.getTempDir(), 'bower.json');
return Q.nfcall(fs.readFile, jsonFile)
// Try component.json
.then(null, function (err) {
if (err.code !== 'ENOENT') {
throw err;
}
jsonFile = path.join(this.getTempDir(), 'component.json');
return Q.nfcall(fs.readFile, jsonFile)
// Issue a deprecation message if it exists
.then(function (contents) {
this.emit('warn', 'Package "' + this.name + '" is using the deprecated component.json file');
return contents;
}.bind(this));
}.bind(this))
// If we got the file contents, validate them
.then(function (contents) {
// TODO: change the validation to a separate module in the bower organization
try {
this._json = JSON.parse(contents);
return this._json;
} catch (e) {
throw createError('Unable to parse "' + jsonFile + '" file', e.code, {
details: e.message
});
}
// Otherwise there was an error
}.bind(this), function (err) {
// If no json file was found, return one just with the name
if (err.code === 'ENOENT') {
this._json = { name: this.name };
return this._json;
}
// If we got here, the error code is something else so we re-throw it
throw err;
}.bind(this));
};
Package.prototype._parseJson = function (json) {
console.log('_parseJson');
// Resolve if cached
if (this._dependencies) {
return Q.fcall(this._dependencies);
}
// Check if name defined in the json is different
// If so and if the name was "guessed", assume the json name
if (this._guessedName && json.name !== this.name) {
this.name = json.name;
this.emit('name_change', this.name);
}
// Handle ignore property, deleting all files from the temporary directory
return Q.fcall(function () {
// Delete all the files specified in the ignore from the temporary directory
// TODO:
}.bind(this))
// Handle the dependencies property
.then(function () {
var key,
promises = [];
// Read the dependencies, creating a package for each one
createPackage = createPackage || require('./createPackage');
if (json.dependencies) {
for (key in json.dependencies) {
promises.push(createPackage(json.dependencies[key], { name: key, unitOfWork: this._unitOfWork }));
}
}
// Resolve all the create packages promises
return Q.all(promises).then(function (packages) {
this._dependencies = packages;
return packages;
}.bind(this));
});
};
Package.prototype._resolveDependencies = function (dependencies) {
console.log('_resolveDependencies');
var promises = dependencies.map(function (dep) {
return dep.resolve();
});
return Q.all(promises);
};
Package.prototype._assertResolved = function () {
if (!this._resolved) {
throw createError('Package is not yet resolved', 'ENOTRES');
}
};
module.exports = Package;

View File

@@ -1,216 +0,0 @@
var Q = require('q');
var util = require('util');
var mout = require('mout');
var events = require('events');
var createError = require('../util/createError');
var UnitOfWork = function (options) {
// Ensure options defaults
this._options = mout.object.mixIn({
failFast: true,
maxConcurrent: 5
}, options);
// Parse some of the options
this._options.failFast = !!this._options.failFast;
this._options.maxConcurrent = this._options.maxConcurrent > 0 ? this._options.maxConcurrent : 0;
// Initialize some needed properties
this._queue = [];
this._beingResolved = [];
this._beingResolvedEndpoints = {};
this._resolved = {};
this._failed = {};
this._completed = {};
};
util.inherits(UnitOfWork, events.EventEmitter);
// -----------------
UnitOfWork.prototype.enqueue = function (pkg) {
var deferred = Q.defer(),
index;
// Throw it if already queued
index = this._indexOf(this._queue, pkg);
if (index !== -1) {
throw new Error('Package is already queued');
}
// Throw if already resolving
index = this._indexOf(this._beingResolved, pkg);
if (index !== -1) {
throw new Error('Package is already being resolved');
}
// Add to the queue
this._queue.push({
pkg: pkg,
deferred: deferred
});
this.emit('enqueue', pkg);
// Process the queue shortly later so that handlers can be attached to the returned promise
Q.fcall(this._processQueue.bind(this));
return deferred.promise;
};
UnitOfWork.prototype.dequeue = function (pkg) {
var index;
// Throw if the package is already is being resolved
index = this._indexOf(this._beingResolved, pkg);
if (index !== -1) {
throw new Error('Package is already being resolved');
}
// Attempt to remove from the queue
index = this._indexOf(this._queue, pkg);
if (index !== -1) {
this._queue.splice(index, 1);
this.emit('dequeue', pkg);
}
return this;
};
UnitOfWork.prototype.getResolved = function (name) {
return name ? this._resolved[name] || [] : this._resolved;
};
UnitOfWork.prototype.getFailed = function (name) {
return name ? this._failed[name] || [] : this._failed;
};
// -----------------
UnitOfWork.prototype._processQueue = function () {
// If marked to fail all, reject everything
if (this._failAll) {
return this._rejectAll();
}
// Check if the number of allowed packages being resolved reached the maximum
if (this._options.maxConcurrent && this._beingResolved.length >= this._options.maxConcurrent) {
return;
}
// Find candidates for the free spots
var freeSpots = this._options.maxConcurrent ? this._options.maxConcurrent - this._beingResolved.length : -1,
endpoint,
duplicate,
entry,
x;
for (x = 0; x < this._queue.length && freeSpots; ++x) {
entry = this._queue[x];
endpoint = entry.pkg.getEndpoint();
// Skip if there is a package being resolved with the same endpoint
if (this._beingResolvedEndpoints[endpoint]) {
continue;
}
// Remove from the queue
this._queue.splice(x--, 1);
this.emit('dequeue', entry.pkg);
// Check if the exact same package has been resolved (same endpoint and range)
// If so, we reject the promise with an appropriate error
duplicate = this._findDuplicate(entry.pkg);
if (duplicate) {
entry.deferred.reject(createError('Package with same endpoint and range was already resolved', 'EDUPL', { pkg: duplicate }));
continue;
}
// Package is ok to resolve
// Put it in the being resolved list
this._beingResolved.push(entry);
this._beingResolvedEndpoints[endpoint] = true;
// Decrement the free spots available
freeSpots--;
// Resolve the promise to let the package know that it can proceed
this.emit('before_resolve', entry.pkg);
entry.deferred.resolve(this._onPackageDone.bind(this, entry.pkg));
}
};
UnitOfWork.prototype._rejectAll = function () {
var error,
queue;
// Reset the queue and being resolved list
queue = this._queue;
this._queue = [];
this._beingResolved = [];
this._beingResolvedEndpoints = {};
// Reject every deferred
error = createError('Package rejected to be resolved', 'EFFAST');
queue.forEach(function (entry) {
entry.deferred.reject(error);
});
};
UnitOfWork.prototype._onPackageDone = function (pkg, err) {
var pkgName = pkg.getName(),
pkgEndpoint = pkg.getEndpoint(),
arr,
index;
// Ignore if already completed
if (this._completed[pkgEndpoint] && this._completed[pkgEndpoint].indexOf(pkg) !== -1) {
return;
}
// Add it as completed
arr = this._completed[pkgEndpoint] = this._completed[pkgEndpoint] || [];
arr.push(pkg);
// Remove the package from the being resolved list
index = this._indexOf(this._beingResolved, pkg);
this._beingResolved.splice(index, 1);
delete this._beingResolvedEndpoints[pkg.getEndpoint()];
// If called with no error then add it as resolved
if (!err) {
arr = this._resolved[pkgName] = this._resolved[pkgName] || [];
arr.push(pkg);
this.emit('resolve', pkg);
// Otherwise, it failed to resolve so we mark it as failed
} else {
arr = this._failed[pkgName] = this._failed[pkgName] || [];
arr.push(pkg);
this.emit('failed', pkg);
// If fail fast is enabled, make every other package in the queue to fail
this._failAll = this._options.failFast;
}
// Call process queue in order to allow packages to take over the free spots in the queue
this._processQueue();
};
UnitOfWork.prototype._indexOf = function (arr, pkg) {
return mout.array.findIndex(arr, function (item) {
return item.pkg === pkg;
});
};
UnitOfWork.prototype._findDuplicate = function (pkg) {
var arr = this._completed[pkg.getEndpoint()];
if (!arr) {
return null;
}
return mout.array.find(arr, function (item) {
return item.getRange() === pkg.getRange();
});
};
module.exports = UnitOfWork;

View File

@@ -1,71 +0,0 @@
var path = require('path');
var fs = require('fs');
var mout = require('mout');
var mkdirp = require('mkdirp');
// Guess some needed properties based on the user OS
var temp = process.env.TMPDIR
|| process.env.TMP
|| process.env.TEMP
|| process.platform === 'win32' ? 'c:\\windows\\temp' : '/tmp';
var home = (process.platform === 'win32'
? process.env.USERPROFILE
: process.env.HOME) || temp;
var roaming = process.platform === 'win32'
? path.join(path.resolve(process.env.APPDATA || home || temp), 'bower')
: path.join(path.resolve(home || temp), '.bower');
// Guess proxy defined in the env
var proxy = process.env.HTTPS_PROXY
|| process.env.https_proxy
|| process.env.HTTP_PROXY
|| process.env.http_proxy;
// -----------
// Read global bower config
var config;
try {
config = require('rc')('bower', {
json: 'bower.json',
directory: 'bower_components',
proxy: proxy,
roaming: roaming
});
} catch (e) {
throw new Error('Unable to parse global .bowerrc file: ' + e.message);
}
// Merge global with local bower config
var localConfig = path.join(config.cwd, '.bowerrc');
try {
localConfig = fs.readFileSync(localConfig);
try {
mout.object.mixIn(config, JSON.parse(localConfig));
} catch (e) {
throw new Error('Unable to parse local .bowerrc file: ' + e.message);
}
} catch (e) {}
// Create some aliases to be used internally
mout.object.mixIn(config, {
_cache: path.join(config.roaming, 'cache'),
_links: path.join(config.roaming, 'links'),
_completion: path.join(config.roaming, 'completion'),
_gitTemplate: path.join(config.roaming, 'git_template')
});
// -----------
// Make sure that we have our git template directory
// The git template directory is an empty dir that will be set up for every git command
// So that the user git hooks won't be used
try {
mkdirp.sync(config.gitTemplate);
} catch (e) {
throw new Error('Unable to create git_template directory: ' + e.message);
}
module.exports = config;

View File

@@ -1,20 +0,0 @@
var Q = require('Q');
var packages = require('./packages');
function createPackage(endpoint, options) {
var split = endpoint.split('#'),
range;
// Extract the range from the endpoint
endpoint = split[0];
range = split[1];
// Ensure options
options = options || {};
options.range = options.range || range;
// TODO: analyze endpoint and create appropriate package
return Q.fcall(new packages.UrlPackage(endpoint, options));
}
module.exports = createPackage;

View File

@@ -1,43 +0,0 @@
var util = require('util');
var Q = require('q');
var Package = require('../Package');
var GitFsPackage = function (endpoint, options) {
Package.call(this, endpoint, options);
};
util.inherits(GitFsPackage, Package);
// -----------------
GitFsPackage.prototype._resolveSelf = function () {
var promise;
console.log('_resolveSelf of git local package');
promise = this._copy()
.then(this._fetch.bind(this))
.then(this._versions.bind(this))
.then(this._checkout.bind(this));
return promise;
};
GitFsPackage.prototype._copy = function () {
// create temporary folder
// copy over
};
GitFsPackage.prototype._fetch = function () {
// fetch origin
// reset --hard
};
GitFsPackage.prototype._versions = function () {
// retrieve versions
};
GitFsPackage.prototype._checkout = function () {
// resolve range to a specific version and check it out
};
module.exports = GitFsPackage;

View File

@@ -1,75 +0,0 @@
var util = require('util');
var Q = require('q');
var Package = require('../Package');
var GitRemotePackage = function (endpoint, options) {
Package.call(this, endpoint, options);
};
util.inherits(GitRemotePackage, Package);
// -----------------
GitRemotePackage.prototype._resolveSelf = function () {
var promise;
console.log('_resolveSelf of git remote package');
promise = this._clone()
.then(this._fetch.bind(this))
.then(this._versions.bind(this))
.then(this._checkout.bind(this));
return promise;
};
GitRemotePackage.prototype._clone = function () {
// check cache
// clone only if not cached
var deferred = Q.defer();
console.log('_clone');
setTimeout(function () {
deferred.resolve();
}, 1000);
return deferred.promise;
};
GitRemotePackage.prototype._fetch = function () {
// fetch origin with --prune
// reset --hard origin/HEAD
var deferred = Q.defer();
console.log('_fetch');
setTimeout(function () {
deferred.resolve();
}, 1000);
return deferred.promise;
};
GitRemotePackage.prototype._versions = function () {
// retrieve versions
var deferred = Q.defer();
console.log('_versions');
setTimeout(function () {
deferred.resolve();
}, 1000);
return deferred.promise;
};
GitRemotePackage.prototype._checkout = function () {
// resolve range to a specific version and check it out
var deferred = Q.defer();
console.log('_checkout');
setTimeout(function () {
deferred.resolve();
}, 1000);
return deferred.promise;
};
module.exports = GitRemotePackage;

View File

@@ -1,47 +0,0 @@
var util = require('util');
var Q = require('q');
var Package = require('../Package');
var UrlPackage = function (endpoint, options) {
Package.call(this, endpoint, options);
};
util.inherits(UrlPackage, Package);
// -----------------
UrlPackage.prototype._resolveSelf = function () {
var promise;
console.log('_resolveSelf of url package');
promise = this._download()
.then(this._extract.bind(this));
return promise;
};
UrlPackage.prototype._download = function () {
var deferred = Q.defer();
console.log('_download');
setTimeout(function () {
deferred.resolve();
}, 1000);
return deferred.promise;
};
UrlPackage.prototype._extract = function () {
var deferred = Q.defer();
// If the file extension is not a zip and a tar, resolve the promise on next tick
console.log('_extract');
setTimeout(function () {
deferred.resolve();
}, 1000);
return deferred.promise;
};
module.exports = UrlPackage;

View File

@@ -1,5 +0,0 @@
module.exports = {
UrlPackage: require('./UrlPackage'),
GitFsPackage: require('./GitFsPackage'),
GitRemotePackage: require('./GitRemotePackage')
};