Travis's lame ol' blog

yay, Travis finally changed his blog!

- Friday, May 13, 2011

Adding An Easy Shortcut to the JSHint javascript validator to Visual Studio

I went to add JSLint to my Visual Studio tools and was disappointed that the WSH version has disappeared (or was discontinued). Luckily the new and improved JSHint supports a WSH wrapper.

  1. Grab JSHLint.

  2. In Visual Studio → Tools → External Tools…

  3. Add a new tool with the following field values:

    Title:
    &JSHint
    Command:
    %systemroot%\system32\cscript.exe
    Arguments:
    //Nologo "%YourJSHintPath%\env\wsh.js" $(ItemPath)
    Initial directory:
    "%YourJSHintPath%\"
    Use Output window
    checked

    JSHint-VSTools-Adding

  4. Open up any *.js file you'd like to validate

  5. Select the all new JSHint from your tools menu:

    JSHint-VSTools-Running

The output that JSHint provides appears to be much more useful than the old JSLint version as well.

- Monday, July 26, 2010

Embedding an XSLT as a string in a DLL binary

Project Properties - Resources

If you want to distribute a class library that relies on an XSL document and only want to worry about a single DLL then this is probably the easiest solution. In Visual Studio follow these commands (and save): Project Properties → Resources → Add Existing file

In this case, I am embedding a file named Core.xsl with a key named Core. That will generate a bit of code that looks like this:

~/Properties/Resources.resx:

<data name="Core" type="System.Resources.ResXFileRef, System.Windows.Forms">
	<value>..\Resources\Core.xsl;System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
</data>

~/Properties/Resources.Designer.cs:

public static string Core {
	get {
		return ResourceManager.GetString("Core", resourceCulture);
	}
}
XSL file properties

Be sure to set the properties of the file so that its Build Action is set as an Embedded Resource.

Now when you compile, the entire contents of the XSL file get embedded directly into the DLL as a string. You can now use the new property like this:

public static XslCompiledTransform GetXsl(XmlDocument xDoc)
{
	bool enableDocumentFunction = false;
	bool enableScript = true;
	XsltSettings xslSettings = new XsltSettings(enableDocumentFunction, enableScript);

	// only enable debugging in DEBUG mode
	bool isDebug = true;
#if (!DEBUG)
	isDebug = false;
#endif

	XslCompiledTransform xsl = new XslCompiledTransform(isDebug);
	xsl.Load(xDoc, xslSettings, new XmlUrlResolver());

	return xsl;
}

public static XslCompiledTransform GetXsl()
{
	XmlDocument xDoc = new XmlDocument();
	xDoc.LoadXml(Properties.Resources.Core);
	XslCompiledTransform xsl = GetXsl(null, xDoc);
	return xsl;
}

Previous XSLT stuff:

Compiling An XSLT Into A DLL Binary

- Monday, July 19, 2010

Compiling an XSLT into a DLL binary

Project Properties - Pre-Build Event

Did you know that Microsoft provides tools to compile XSLT files into DLLs? Neither did I before a client recently suggested that I do that on a recent project. It turns out it's extremely easy.

Fire up your favorite console and run the following:

"%PROGRAMFILES%\Microsoft SDKs\Windows\v6.0A\bin\xsltc.exe" /out:SomeProjectPath\bin\SomeXsltDllFileName.dll /class:SomeXsltClassName PathAndFileNameOfXslt.xsl

xsltc.exe might be located somewhere else on your PC and be sure to update the appropriate info in the command above. If it works you can add a reference to this new DLL in your project and use the new class in your code:

XslCompiledTransform xsl = new XslCompiledTransform();
xsl.Load(typeof(SomeXsltClassName));

You can also add a pre-build event command line to your project:

"%PROGRAMFILES%\Microsoft SDKs\Windows\v6.0A\bin\xsltc.exe" /out:$(ProjectDir)bin\SomeXsltDllFileName.dll /class:SomeXsltClassName $(ProjectDir)RelativePathAndFileNameOfXsltInProject.xsl

Then every time you hit CTRL+SHIFT+B it will build your XSLT's DLL automagically each time. Here's a sample of what your output should look like:

------ Build started: Project: ProjectName, Configuration: Debug Any CPU ------
Build started 7/22/2010 2:36:11 PM.

Target PreBuildEvent:
    "%PROGRAMFILES%\Microsoft SDKs\Windows\v6.0A\bin\xsltc.exe" /settings:script+ /out:D:\Projects\ProjectName\bin\SomeXsltDllFileName.dll /class:SomeXsltClassName D:\Projects\ProjectName\Xslt\NameOfXslt.xsl
    Microsoft (R) XSLT Compiler version 3.5.30729
    [Microsoft (R) .NET Framework version 2.0.50727]
    Copyright (C) Microsoft Corporation. All rights reserved.
    
Target CoreCompile:
    C:\WINDOWS\Microsoft.NET\Framework\v3.5\Csc.exe /noconfig /nowarn:1701,1702 /errorreport:prompt /warn:4 /define:DEBUG;TRACE /reference:bin\SomeXsltDllFileName.dll /reference:bin\SomeXsltDllFileName.script.dll /debug+ /debug:full /filealign:512 /optimize- /out:obj\Debug\ProjectName.dll /target:library ClassFiles.cs

Compile complete -- 0 errors, 0 warnings

Target _CopyFilesMarkedCopyLocal:
    Copying file from "bin\SomeXsltDllFileName.dll" to "bin\Debug\SomeXsltDllFileName.dll".
    Copying file from "bin\SomeXsltDllFileName.script.dll" to "bin\Debug\SomeXsltDllFileName.script.dll".
Target CopyFilesToOutputDirectory:
    Copying file from "obj\Debug\ProjectName.dll" to "bin\Debug\ProjectName.dll".
    ProjectName -> D:\Projects\ProjectName\bin\Debug\ProjectName.dll
    Copying file from "obj\Debug\ProjectName.pdb" to "bin\Debug\ProjectName.pdb".

Build succeeded.

Time Elapsed 00:00:02.31

More info:

- Monday, August 31, 2009

Adding An Easy Shortcut to the JSLint javascript validator to Visual Studio

Update: 2011-05-13: JSLint WSH no longer available, see JSHint WSH.


Ever wish that Visual Studio had a JSLint add-in? Inspired by a reply to my tweet, I discovered the following solution.

  1. Grab the WSH version of JSLint.

  2. Add this jslint.bat file to the same folder you have JSLint in:

    @echo off
    cscript jslint.js //Nologo < %1
    echo.
    echo Done.
  3. In Visual Studio → Tools → External Tools…

  4. Add a new tool with the following field values:

    Title:
    &JSLint
    Command:
    %YourJSLintPath%\jslint.bat
    Arguments:
    $(ItemPath)
    Initial directory:
    %YourJSLintPath%\
    Use Output window
    checked

    VS: External Tools

  5. Open up any *.js file you'd like to validate

  6. Select the all new JSLint from your tools menu:

    VS: Tools Menu

  7. Try not to let JSLint hurt your feelings too much:

    VS: Output From JSLint

In short, JSLint is awesome.