Skip to content

Instantly share code, notes, and snippets.

@jerriep
Created October 25, 2012 08:14
Show Gist options
  • Select an option

  • Save jerriep/3951361 to your computer and use it in GitHub Desktop.

Select an option

Save jerriep/3951361 to your computer and use it in GitHub Desktop.
TwitterBootstrapLessTransform
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Optimization;
namespace JSE.App.IPS.Web.Portal.App_start
{
public class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
Bundle applicationBundle = new StyleBundle("~/Content/css").Include(
"~/Content/application.less"
);
applicationBundle.Transforms.Clear();
applicationBundle.Transforms.Add(new TwitterBootstrapLessTransform());
bundles.Add(applicationBundle);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Optimization;
using dotless.Core;
using dotless.Core.configuration;
using dotless.Core.Input;
using System.IO;
namespace JSE.App.IPS.Web.Portal.App_start
{
public class TwitterBootstrapLessTransform : IBundleTransform
{
public static string BundlePath { get; private set; }
public void Process(BundleContext context, BundleResponse response)
{
SetBasePath(context);
var config = new DotlessConfiguration(DotlessConfiguration.GetDefault());
config.LessSource = typeof(TwitterBootstrapLessMinifyBundleFileReader);
response.Content = Less.Parse(response.Content, config);
response.ContentType = "text/css";
}
private void SetBasePath(BundleContext context)
{
BundlePath = context.HttpContext.Server.MapPath("~/Content/");
}
}
public class TwitterBootstrapLessMinifyBundleFileReader : IFileReader
{
public IPathResolver PathResolver { get; set; }
private string basePath;
public TwitterBootstrapLessMinifyBundleFileReader()
: this(new RelativePathResolver())
{
}
public TwitterBootstrapLessMinifyBundleFileReader(IPathResolver pathResolver)
{
PathResolver = pathResolver;
basePath = TwitterBootstrapLessTransform.BundlePath;
}
public bool DoesFileExist(string fileName)
{
fileName = PathResolver.GetFullPath(Path.Combine(basePath, fileName));
return File.Exists(fileName);
}
public byte[] GetBinaryFileContents(string fileName)
{
throw new System.NotImplementedException();
}
public string GetFileContents(string fileName)
{
fileName = PathResolver.GetFullPath(basePath + fileName);
return File.ReadAllText(fileName);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment