Dec
21
2012
0

Downloading source for binary node.js packages

I’ve recently developed a website in node.js which relies on various third-party packages. The recommended way to redistribute these packages (especially if you don’t require your end user to have an internet connection) is to add your node_modules folder to source control and ship it with your site. See:

http://www.mikealrogers.com/posts/nodemodules-in-git.html

This is all well and good if the packages you use don’t require compiling, you can simply add them to source control and ship the lot.
But if the packages need compiling, two problems arise. Firstly no-one likes binaries in their source control system and secondly, the binaries created on one OS may not work on another. This means these binary packages need to be compiled as a post-install step.

The package manager for node, npm, has a great way of compiling or recompiling the packages once deployed. Simply execute the following in the root of your project:

$> npm rebuild

Unfortunately, npm doesn’t have a way to retrieve just the source for you. The ‘npm install’ command both downloads and compiles the packages. To this end, I have created a node.js script which takes advantage of the ‘npm cache’ method to recursively discover and download just the source for your node.js project. To execute it, save the following to a file (npm-cache.js for example) and execute it in the root of your node project. It parses the package.json file and grabs source into the node_modules directory in place. Enjoy!

var fs = require('fs'),
path = require('path'),
exec = require('child_process').exec,
util = require('util');

var packageFileName = 'package.json';
var modulesDirName = 'node_modules';
var cacheDirectory = process.cwd();
var npmCacheAddMask = 'npm cache add %s@%s; echo %s';
var sourceDirMask = '%s/%s/%s/package';
var targetDirMask = '%s/node_modules/%s';

function deleteFolder(folder) {
    if (fs.existsSync(folder)) {
        var files = fs.readdirSync(folder);
        files.forEach(function(file) {
            file = folder + "/" + file;
            if (fs.lstatSync(file).isDirectory()) {
                deleteFolder(file);
            } else {
                fs.unlinkSync(file);
            }
        });
        fs.rmdirSync(folder);
    }
}

function downloadSource(folder) {
    var packageFile = path.join(folder, packageFileName);
    if (fs.existsSync(packageFile)) {
        var data = fs.readFileSync(packageFile);
        var package = JSON.parse(data);

        function getVersion(data) {
            var version = data.match(/-([^-]+)\.tgz/);
            return version[1];
        }

        var callback = function(error, stdout, stderr) {
            var dependency = stdout.trim();
            var version = getVersion(stderr);
            var sourceDir = util.format(sourceDirMask, cacheDirectory, dependency, version);
            var targetDir = util.format(targetDirMask, folder, dependency);
            var modulesDir = folder + '/' + modulesDirName;

            if (!fs.existsSync(modulesDir)) {
                fs.mkdirSync(modulesDir);
            }

            fs.renameSync(sourceDir, targetDir);
            deleteFolder(cacheDirectory + '/' + dependency);
            downloadSource(targetDir);
        };

        for (dependency in package.dependencies) {
            var version = package.dependencies[dependency];
            exec(util.format(npmCacheAddMask, dependency, version, dependency), callback);
        }
    }
}

if (!fs.existsSync(path.join(process.cwd(), packageFileName))) {
    console.log(util.format("Unable to find file '%s'.", packageFileName));
    process.exit();
}

deleteFolder(path.join(process.cwd(), modulesDirName));
process.env.npm_config_cache = cacheDirectory;
downloadSource(process.cwd());
Written by Rob in: Development | Tags: , ,
Jun
01
2010
19

Pluggable MVC 2.0 using MEF and strongly-typed views

The problem…

I’ve been putting together a pluggable MVC framework using MEF in C# 4.0.

MEF was chosen over other solutions such as Portable Areas as it allows all plugins to be composed together as a single site at runtime without the assemblies needing to reference each other.

After scouring the web for ideas, I stumbled upon Maarten Balliauw’s excellent posts for accomplishing this task:

ASP.NET MVC and the Managed Extensibility Framework (MEF)

Revised: ASP.NET MVC and the Managed Extensibility Framework (MEF)

It seems that there are issues using strongly-typed views in this scenario, though, as the types referenced in the views are not found or loaded at runtime. Maarten’s solution is to copy the plugins into the main website’s bin directory so that they can be discovered at runtime.

The solution…

This can also be solved in another (more elegant?) way by using AssemblyResolve to tell the framework where to load a type from when it is requested by the views.
In this way, your plugin directory needn’t be a sub-directory of the website’s bin folder.

Unfortunately, this doesn’t work without a bit of fettling and the key to getting it working is to give the framework a hint to the assembly the type is in. This in turn forces the AssemblyResolve event to fire. To accomplish this, use an assembly directive at the top of your strongly-typed views as follows:

<%@ Assembly Name="Web.Plugins.Controls" %>
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Web.Plugins.Controls.Models.MenuModel>" %>

An example of using AssemblyResolve by David Morton can be found here:

Assembly Resolution with AppDomain.AssemblyResolve

Which can be bent to our will:

public void Initialize()
{
	// Add assembly handler for strongly-typed view models
	AppDomain.CurrentDomain.AssemblyResolve += PluginAssemblyResolve;
}

private Assembly PluginAssemblyResolve(object sender, ResolveEventArgs resolveArgs)
{
	Assembly[] currentAssemblies = AppDomain.CurrentDomain.GetAssemblies();

	// Check we don't already have the assembly loaded
	foreach (Assembly assembly in currentAssemblies)
	{
		if (assembly.FullName == resolveArgs.Name || assembly.GetName().Name == resolveArgs.Name)
		{
			return assembly;
		}
	}

	// Load from directory
	return LoadAssemblyFromPath(resolveArgs.Name, _fullPluginPath);
}

private static Assembly LoadAssemblyFromPath(string assemblyName, string directoryPath)
{
	foreach (string file in Directory.GetFiles(directoryPath))
	{
		Assembly assembly;

		if (TryLoadAssemblyFromFile(file, assemblyName, out assembly))
		{
			return assembly;
		}
	}

	return null;
}

private static bool TryLoadAssemblyFromFile(string file, string assemblyName, out Assembly assembly)
{
	try
	{
		// Convert the filename into an absolute file name for
		// use with LoadFile.
		file = new FileInfo(file).FullName;

		if (AssemblyName.GetAssemblyName(file).Name == assemblyName)
		{
			assembly = Assembly.LoadFile(file);
			return true;
		}
	}
	catch
	{
	}

	assembly = null;
	return false;
}

The download…

http://www.thegecko.org/uploads/MefMvc.zip

This is an example framework based on Maarten’s using this method. It requires C# 4.0 and Visual Studio 2010 and it is recommended to run without debugging to avoid slow execution when view caching is disabled (Don’t Panic).

The sample framework includes the following functionality:

  • Embedded resources handling
  • Plugin registration priority for selecting controllers and views
  • Menu and menu item registration with ordering
  • Sample authentication module
  • Automatic site.master resolving

The resource handling is borrowed from the Spark view engine sample modules:

Spark modules

This should serve as a nice starting point for your next MVC pluggable project :)

Written by Rob in: Development | Tags: , , , ,
Jan
22
2010
0

BunnyBot version 1.0.1 released!

Hot on the heels of my recent announcement for version 1.0.0, version 1.0.1 has been released.

This includes a minor update which automatically adds any new contacts to the nabaztag MSN contact list.

Enjoy!

http://code.google.com/p/bunnybot/

Written by Rob in: Development | Tags: , , ,
Jan
03
2010
1

BunnyBot version 1.0.0 released!

nabaztag

I have just released version 1.0.0 of my current pet project, BunnyBot.
This C#/.Net project is a windows service or console applicatrion which logs your nabaztag bunny into an MSN account whenever it is awake. Any messages sent to the MSN account is spoken by the bunny.

To download the binaries, jump over to:

http://code.google.com/p/bunnybot/

Written by Rob in: Development | Tags: , , ,

Powered by WordPress | Design: TheBuckmaker.com WordPress Themes | Modifications: theGecko