Same package name's but different endpoints have now different caches.

This commit is contained in:
André Cruz
2012-10-26 08:45:33 +01:00
parent 197fc34618
commit dc1f142241

View File

@@ -26,6 +26,7 @@ var path = require('path');
var url = require('url');
var tmp = require('tmp');
var fs = require('fs');
var crypto = require('crypto');
var config = require('./config');
var source = require('./source');
@@ -84,11 +85,8 @@ var Package = function (name, endpoint, manager) {
// must be used by the manager later on
this.originalTag = this.tag;
// Generate an id and a resourceId
// The id is an unique id that describes this package
// The resourceId is an unique id that describes the resource of this package
this.id = new Buffer(this.name + '%' + this.tag + '%' + this.gitUrl + '%' + this.path + '%' + this.assetUrl).toString('base64');
this.resourceId = new Buffer(this.gitUrl + '%' + this.path + '%' + this.assetUrl).toString('base64');
this.id = crypto.createHash('md5').update(this.name + '%' + this.tag + '%' + this.gitUrl + '%' + this.path + '%' + this.assetUrl).digest('hex');
}
if (this.manager) {
@@ -120,14 +118,6 @@ Package.prototype.resolve = function () {
this.emit('resolve');
return this;
}
// Check if this exact package resource is the last resolved one
// This is to prevent it from being downloaded or copied over and over again in such case
if (data.resourceId === this.resourceId) {
this.path = data.path;
this.unitWork.lock(this.name, this);
this.once('loadJSON', this.saveUnit).checkout();
return this;
}
}
// If not, we lock and resolve it
@@ -200,7 +190,7 @@ Package.prototype.cleanUpLocal = function () {
var jsonStr = JSON.stringify(this.json, null, 2);
fs.writeFile(path.join(this.localPath, config.json), jsonStr);
fs.writeFile(path.join(path.resolve(config.cache, this.name), config.json), jsonStr);
if (this.gitUrl) fs.writeFile(path.join(path.resolve(config.cache, this.name, this.resourceId), config.json), jsonStr);
rimraf(path.join(this.localPath, '.git'), this.emit.bind(this, 'install'));
};
@@ -365,7 +355,8 @@ Package.prototype.exists = function (callback) {
Package.prototype.clone = function () {
template('action', { name: 'cloning', shizzle: this.gitUrl }).on('data', this.emit.bind(this, 'data'));
this.path = path.resolve(config.cache, this.name);
this.resourceId = crypto.createHash('md5').update(this.name + '%' + this.gitUrl).digest('hex');
this.path = path.resolve(config.cache, this.name, this.resourceId);
this.once('cache', function () {
this.once('loadJSON', this.copy.bind(this)).checkout();
}.bind(this)).cache();
@@ -375,10 +366,10 @@ Package.prototype.cache = function () {
// If the force options is true, we need to erase from the cache
// Be aware that a similar package might already flushed it
// To prevent that we check the unit of work storage
if (this.opts.force && !this.unitWork.retrieve('flushed#' + this.name)) {
if (this.opts.force && !this.unitWork.retrieve('flushed#' + this.name + '_' + this.resourceId)) {
rimraf(this.path, function (err) {
if (err) return this.emit('error', err);
this.unitWork.store('flushed#' + this.name, true);
this.unitWork.store('flushed#' + this.name + '_' + this.resourceId, true);
this.cache();
}.bind(this));
return this;
@@ -396,11 +387,16 @@ Package.prototype.cache = function () {
if (process.env.HTTP_PROXY) {
url = url.replace(/^git:/, 'https:');
}
var cp = spawn('git', ['clone', url, this.path]);
cp.on('close', function (code) {
if (code != 0) return this.emit('error', new Error('Git status: ' + code));
this.emit('cache');
mkdirp(this.path, function (err) {
if (err) return this.emit('error', ee);
var cp = spawn('git', ['clone', url, this.path]);
cp.on('close', function (code) {
if (code != 0) return this.emit('error', new Error('Git status: ' + code));
this.emit('cache');
}.bind(this));
}.bind(this));
}.bind(this));
}.bind(this));
@@ -454,7 +450,7 @@ Package.prototype.checkout = function () {
};
Package.prototype.describeTag = function () {
var cp = spawn('git', ['describe', '--always', '--tag'], { cwd: path.resolve(config.cache, this.name) });
var cp = spawn('git', ['describe', '--always', '--tag'], { cwd: path.resolve(config.cache, this.name, this.resourceId) });
var tag = '';
@@ -472,7 +468,7 @@ Package.prototype.describeTag = function () {
Package.prototype.versions = function () {
this.on('fetch', function () {
var cp = spawn('git', ['tag'], { cwd: path.resolve(config.cache, this.name) });
var cp = spawn('git', ['tag'], { cwd: path.resolve(config.cache, this.name, this.resourceId) });
var versions = '';
@@ -495,7 +491,7 @@ Package.prototype.versions = function () {
// If there is no versions tagged in the repo
// then we grab the hash of the last commit
versions = '';
cp = spawn('git', ['log', '-n', 1, '--format=%H'], { cwd: path.resolve(config.cache, this.name) });
cp = spawn('git', ['log', '-n', 1, '--format=%H'], { cwd: path.resolve(config.cache, this.name, this.resourceId) });
cp.stdout.setEncoding('utf8');
cp.stdout.on('data', function (data) {
@@ -510,10 +506,10 @@ Package.prototype.versions = function () {
};
Package.prototype.fetch = function () {
var cp = spawn('git', ['fetch'], { cwd: path.resolve(config.cache, this.name) });
var cp = spawn('git', ['fetch'], { cwd: path.resolve(config.cache, this.name, this.resourceId) });
cp.on('close', function (code) {
if (code != 0) return this.emit('error', new Error('Git status: ' + code));
cp = spawn('git', ['reset', '--hard', 'origin/HEAD'], { cwd: path.resolve(config.cache, this.name) });
cp = spawn('git', ['reset', '--hard', 'origin/HEAD'], { cwd: path.resolve(config.cache, this.name, this.resourceId) });
cp.on('close', function (code) {
if (code != 0) return this.emit('error', new Error('Git status: ' + code));
this.emit('fetch');