Skip to content

Commit

Permalink
Debug mode (allows dynamic change to javascript sources without node …
Browse files Browse the repository at this point in the history
…restart) is in and working. Assembly .getDebugTags() now working, so in effect this code is now 'working'
  • Loading branch information
Charlotte Gore committed May 13, 2012
1 parent a560772 commit d1e508d
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 34 deletions.
18 changes: 17 additions & 1 deletion lib/assembly.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,33 @@ var Assembly = base.createChild().addInstanceMethods({

}, this);

this.debugTags = '';
this.tag = '';

return this;

},

getDebugTags : function(){
getDebugTags : function( outputPath ){

if(this.debugTags === ''){

_.each(this.assets, function( asset ){

this.debugTags += "<script type='text/javascript' src='" + outputPath + asset.name + "'></script>"

}, this)

}

return this.debugTags;

},

getTag : function(){

return '<script></script>';


},

Expand Down
29 changes: 21 additions & 8 deletions lib/asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,24 @@ var assetsPath;

var Asset = base.createChild().addInstanceMethods({

init : function(name, lpath, assets){
init : function(name, lpath, assets, stat){

this.name = path.normalize(lpath + '/' + name);

this.path = path.normalize(lpath + '/');

assetsPath = assets;

this.status = {};

this.dependencies = {
files : [],
bundles : []
bundles : []
};

this.hasChanged();
if(stat && stat.mtime){
this.status = stat.mtime.getTime();
}else{
this.status = 0;
}
this.getData();
this.parseReferences();

Expand Down Expand Up @@ -86,15 +88,26 @@ var Asset = base.createChild().addInstanceMethods({

hasChanged : function(){

var status = fs.statSync(assetsPath + this.name);
var status;

if(_.isEqual(status, this.status)){
try{

status = fs.statSync(assetsPath + this.name);

}catch(e){

return true;

}

if(_.isEqual(status.mtime.getTime(), this.status)){

return false;

}else{

this.status = status;
this.status = status.mtime.getTime();

return true;

}
Expand Down
22 changes: 8 additions & 14 deletions lib/cassette.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ var cassette = base.createChild().addInstanceMethods({

outputPath = config.outputPath;

}else {

outputPath = '';

}

if(config.mode){
Expand Down Expand Up @@ -48,25 +52,15 @@ var cassette = base.createChild().addInstanceMethods({

useAsset : function( asset ){

var assembly;

if(self.assemblies[asset]){

assembly = self.assemblies[asset];

}else{

assembly = self.assemblies[asset] = self.manifest.createAssembly(asset);

}
var str;

if(self.mode==='debug'){

return assembly.getDebugTags();
str = self.manifest.getAssembly( asset, 'forceCheck' ).getDebugTags( outputPath );

}else{
} else {

return assembly.getTag();
str = self.manifest.getAssembly( asset ).getTag( outputPath );

}

Expand Down
115 changes: 104 additions & 11 deletions lib/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@ var Manifest = base.createChild().addInstanceMethods({

this.assetsPath = path.normalize(assetsPath);

this.bundleIndex = {};
this.assetIndex = {};

this.bundleDependencies = [];
this.externalFileDependencies = [];

this.scanDirectory('');
this.reset();

return this;

Expand All @@ -40,6 +34,7 @@ var Manifest = base.createChild().addInstanceMethods({
this.bundleIndex[currentPath] = {
files : [],
bundles : [],
mtime : fs.statSync(this.assetsPath + currentPath).mtime.getTime()
};

};
Expand All @@ -55,7 +50,7 @@ var Manifest = base.createChild().addInstanceMethods({

if(/\.js$/.test(file)){

var asset = Asset(file, currentPath, this.assetsPath );
var asset = Asset(file, currentPath, this.assetsPath, stat );

this.assetIndex[ asset.name ] = asset;
this.bundleIndex[ currentPath ].files.push( asset.name );
Expand All @@ -66,6 +61,8 @@ var Manifest = base.createChild().addInstanceMethods({
}else if(stat.isDirectory()){

this.bundleIndex[currentPath].bundles.push( path.normalize(currentPath + '/' + file) );


this.scanDirectory(path.normalize(currentPath + '/' + file));

}
Expand Down Expand Up @@ -109,12 +106,55 @@ var Manifest = base.createChild().addInstanceMethods({

},

createAssembly : function( key ){
reset : function(){
this.assemblies = {};
this.assetIndex = {};
this.bundleIndex = {};
this.scanDirectory('');
},

getAssembly : function( key, forceCheck ){

var dirtyFiles = false;

if(key){

key = path.normalize(key);

}else{

throw new Error('No key specified for asset request');

}

if(this.assemblies[key] && typeof forceCheck==='undefined'){

return this.assemblies[key];

}

if(forceCheck){

_.each(this.assetIndex, function(asset){

var hasChanged = asset.hasChanged();

if(hasChanged){

dirtyFiles = true;

};

});



if(dirtyFiles===true || (this.fileSystemChanged()===true) ){
// we invalidate all assemblies and get a fresh scan.
this.reset();

}

}

var list = [];
Expand All @@ -134,7 +174,9 @@ var Manifest = base.createChild().addInstanceMethods({

},this);

return Assembly(list);
this.assemblies[key] = Assembly(list);

return this.assemblies[key];

},

Expand Down Expand Up @@ -245,6 +287,50 @@ var Manifest = base.createChild().addInstanceMethods({

},

fileSystemChanged : function(){

var start = (new Date()).getTime();
var changesDetected = false;

_.each(this.bundleIndex, function(bundle, index){

try{

var current = fs.statSync(path.normalize(this.assetsPath + '/' + index)).mtime.getTime();

if(current!==bundle.mtime ){

changesDetected = true;
bundle.mtime = current;

}

}catch(e){

// we've got a missing directory.
changesDetected = true;

}


}, this);



if(changesDetected){

return true;

}else{

console.log( 'Elapsed: ' + ((new Date()).getTime() - start) );

return false;

}

},

// unit testing helper..
query : function(){

Expand All @@ -253,4 +339,11 @@ var Manifest = base.createChild().addInstanceMethods({
}
});

exports = module.exports = Manifest;
exports = module.exports = Manifest;

/*
*/

0 comments on commit d1e508d

Please sign in to comment.