Need help to create new engine

Dec 19, 2010 at 7:21 PM
Edited Dec 19, 2010 at 7:23 PM

Hi,

I'm working for a website editor that has put his images on a CDN that can be accessed with two different urls, one for HTTP and one for HTTPS.

Those 2 urls are part of the website configuration (web.config).

For the moment, we made a custom script combiner that dynamically replaces the background image urls that are stored in css files by the http or https CDN url depending on the page context.

For example ~/img/img.gif is replaced (depending on the context) by http://img.cdn.editor.com/img/img.gif or https://otheraddress.editor.net/img/img.gif

I'm trying to make a new engine that would, for a given css file named file.cdn.css :

- minify it using yui css engine (output would be file.min.css)

- use the minified file to generate a file to be used in a HTTP context by replacing all ~/ by the HTTP CDN url. (output would be file.http.min.css)

- use the minified file to generate a file to be used in a HTTPS context by replacing all ~/ by the HTTPS CDN url. (output would be file.https.min.css)

I wrote the following engine but it does not seems to work (basically ,nothing happens in Visual Studio) :

 

using System;
using System.Text.RegularExpressions;
using System.Linq;
using Yahoo.Yui.Compressor;
using Zippy.Chirp.Xml;

namespace Zippy.Chirp.Engines {
    public class CdnCssEngine : TransformEngine {
	public CdnCssEngine()
		 {
            Extensions = new[] { Settings.ChirpCdnCssFile };
            OutputExtension = ".css";
        }

        public override string Transform(string fullFileName, string text, EnvDTE.ProjectItem projectItem) {
			  return CssCompressor.Compress(text, 0,CssCompressionType.StockYuiCompressor);
		  }

        public override void Process(Manager.VSProjectItemManager manager, string fullFileName, EnvDTE.ProjectItem projectItem, string baseFileName, string outputText) {
            base.Process(manager, fullFileName, projectItem, baseFileName, outputText);

				string inputText = System.IO.File.ReadAllText(fullFileName);

				string http = inputText.Replace("~/", "http://img.cdn.editor.com/");
				manager.AddFileByFileName(baseFileName + ".http.min.css", http);

				string https = inputText.Replace("~/", "https://otheraddress.editor.net/");
				manager.AddFileByFileName(baseFileName + ".https.min.css", https);

        }

    }
}

Thanks in advance for your help,
kindanam

 

Dec 19, 2010 at 9:20 PM

ok. I found the solution.

For those who could be interested, here is the code (not final because the CDN urls should be taken from configuration not constants):

 

// In Settings.css
 public static string ChirpCdnCssFile = ".cdn.css";

// New class : CdnCssEngine.cs

using System;
using System.Text.RegularExpressions;
using System.Linq;
using Yahoo.Yui.Compressor;
using Zippy.Chirp.Xml;

namespace Zippy.Chirp.Engines {
    public class CdnCssEngine : TransformEngine {
		 public CdnCssEngine()
		 {
            Extensions = new[] { Settings.ChirpCdnCssFile };
            OutputExtension = ".min.css";
        }

        public override string Transform(string fullFileName, string text, EnvDTE.ProjectItem projectItem) {
			  return CssCompressor.Compress(text, 0,CssCompressionType.StockYuiCompressor);
		  }

		  public override int Handles(string fullFileName)
		  {

			  // if (fullFileName.EndsWith(GetOutputExtension(fullFileName), StringComparison.InvariantCultureIgnoreCase)) return 0; --remove for handle less.css workitem=31,34
			  var match = Extensions.Where(x => fullFileName.EndsWith(x, StringComparison.InvariantCultureIgnoreCase))
					.FirstOrDefault() ?? string.Empty;
			  return match.Length;
		  }

        public override void Process(Manager.VSProjectItemManager manager, string fullFileName, EnvDTE.ProjectItem projectItem, string baseFileName, string outputText) {
            base.Process(manager, fullFileName, projectItem, baseFileName, outputText);

				string http = outputText.Replace("~/", "http://img.cdn.editor.com/");
				manager.AddFileByFileName(baseFileName + ".http.min.css", http);

				string https = outputText.Replace("~/", "ttps://otheraddress.editor.net/");
				manager.AddFileByFileName(baseFileName + ".https.min.css", https);

        }

    }
}

// In Chirp.cs
internal CdnCssEngine CdnCssEngine { get; set; }

// ...

public void LoadActions() {
// register new engine
_EngineManager.Add(CdnCssEngine = new CdnCssEngine());
}
Kindanam

 

Coordinator
Jan 2, 2011 at 11:17 PM

kindanam,

Apologies for not responding, but thanks for posting the solution. 

This is a really cool use of Chirpy's Engine infrastructure. I'm wondering if others might find it useful? Methinks so.

 

Cheers,

Evan