Using Windows CSCRIPT to compile a Handlebar.js template

I was looking for an alternative to using Node.JS for a JavaScript build process today on a Windows machine. I wanted something that relied as much on natively installed elements of a modern Windows PC as possible so that the build process would be portable.

So, I broke out my rusty Windows Script Host skills.

First, I created a file called, compile.wsf with the following contents:

image

When using cscript.exe, you can execute a more complex combination of scripts and include other script files by using a Windows Script File. The content of the file is an XML definition of jobs. A job represents a unit of work. If you only have one job in a file, the name won’t matter as the script engine will select it by default. If you do have more than one job you’d like to store in a single WSF file, you can use the /Job:{id} parameter of cscript.exe to run a single job.

Using the WSF file, you can include other script files using a script element (much like the script tag in HTML). In the example above, I’ve referenced a local copy of handlebars.js and a custom script called compile.js.

You can also inline script as shown above. After doing a basic check on the number of arguments provided to the script, compile is called, which is from the compile.js script reference.

Compile.js is simple:

image

Using an instance of the FileSystemObject, first the input file is verified to exist. Next, both the input and output files are opened. The Handlebars object is available globally by including it in the WSF definition and is used to precompile the contents of the input file’s template definition. Once compiled, it’s written to the output file and both files are closed.

I threw the three files in a folder called lib, and created a simple batch file called handlebars.bat which called the cscript executable with the Windows Script File shown above as the first parameter and then the values of the other parameters passed along:

image

While this solution only works on Windows, it doesn’t hurt to keep the Windows Script Host in mind when throwing together general repeatable tasks that you:

  • consider too complex for a batch file
  • consider too simple for a full .NET application
  • require usage of existing JavaScript libraries, like Handlebars.js for some work

Quilt Inspired by Microsoft Windows 8

My loving wife made me a quilt with her new sewing machine.

IMG_0151

For her first project with her new Brother DreamWeaver™ VQ3000, she wanted to start with something small. After a very short discussion, she and I agreed that the Windows 8 Start Screen/Modern UI interface would be a great starter project. Not only would it be fun to work on, it could be colorful, use a variety of stitching options, and use the ability to use a few different fonts to add some text.

Once I told her I would hang it in my office at work (which I have), she got started. (She knows me well enough that there would be a reasonable chance it could end up in a pile somewhere in my den or office).

IMG_0114She did not track how long it took to make, as it was her first project with the new sewing machine, but more than once she would drop by my den on a lazy Sunday afternoon with praise for her new sewing machine. Thankfully, she was able to use nearly all scraps for the quilt itself as it didn’t require larger fabric pieces. We did go to a famous fabric store once to buy some light blue thread.

She’d never owned a sewing machine that auto-threads the needle. I’m sure for many, that’s not news, but I was very impressed. I did a LOT of experimenting with colors and stitching patterns a few weekends and got extremely good at loading the thread so it could be auto-threaded onto the needle. I did it enough that she declared that I was much faster at it than her!

IMG_0127My wife is extremely organized, so she made many notes and sketches along the way so that the effort would be a “cut once, sew once” affair. (she’s has CS BS and MS, and Software Architect by day, so it’s maybe in her blood Smile).

She graphed out the whole thing mainly because there were so many pieces. While I didn’t mind if colors moved around after I had selected a location, she did. Smile Further, our cat has a tendency to shuffle fabric piles a bit.

In any case, a several weeks later working on weekend afternoons and a few evenings, she completed the quilt.

I asked about making a Surface sized quilt and grinning, she answered, “maybe.”

IMG_0140

IMG_0139For complete documentation and instructions, download this PDF.

image

Finding duplicates in MongoDB via the shell

I thought this was an interesting question to answer on StackOverflow (summarized here):

I’m trying to create an index, but an error is returned that duplicates exist for the field I want to index. What should I do?

I answered with one possibility.

The summary is that you can use the power of MongoDB’s aggregation framework to search and return the duplicates. It’s really quite slick.

For example, in the question, Wall documents had a field called event_time. Here’s one approach:

db.Wall.aggregate([
       {$group : { _id: "$event_time" ,  count : { $sum: 1}}},
       {$match : { count : { $gt : 1 } }} ])

The trick is to use the $group pipeline operator to select and count each unique event_time. Then, match on only those groups that contained more than one match.

While it’s not necessarily as readable as the equivalent SQL statement potentially, it’s still easy to read. The only really odd thing is the mapping of the event_time into the _id. As all documents pass through the pipeline, the event_time is used as the new aggregate document key. The $ sign is used as the field reference to a property of the document in the pipeline (a Wall document). Remember that the _id field of a MongoDB document must be unique (and this is how the $group pipeline operator does its magic).

So, if the following event_times were in the documents:

event_time
4:00am
5:00am
4:00am
6:00pm
7:00a

It would results in a aggregate set of documents:

_id count
4:00am 2
5:00am 1
6:00pm 1
7:00am 1

Notice how the _id is the event_time. The aggregate results would look like this:

{
        "result" : [
                {
                        "_id" : "4:00am",
                        "count" : 2
                }
        ],
        "ok" : 1
}

Windows 8 IIS Express with Windows Authentication Prompts for Credentials

If you’re seeing a credentials prompt every time you launch a local IIS or IIS Express web application that requires Windows Authentication, you’ll likely grow as tired of typing in your password as I was. To add to the annoyance, the Remember my credentials checkbox doesn’t work (nothing is saved).

image

The setup:

  • Windows 8
  • Visual Studio 2012, .NET 4.5,
  • ASP.NET MVC web application
  • Non domain-joined computer (it’s on my home network)
  • Using a Microsoft account (FKA Live ID) as the local account
  • Using IIS Express/IIS
  • Windows Authentication Enabled
  • Anonymous Logon Disabled
<system.webServer>
    <security>
        <authentication>
            <windowsAuthentication enabled="true" />
            <anonymousAuthentication enabled="false" />
        </authentication>
    </security>
</system.webServer>

While there are a few proposed solutions, this is the only one that worked for me so far.

  1. Start Internet Explorer
  2. Click the settings Menu
  3. Select Internet options
  4. Click the Security tab
  5. Select Local intranet
  6. Click Sites
  7. Click Advanced
  8. Add http://localhost to the Local intranet zone
  9. Close.
  10. Verify it’s now working.

image

image

image

image

image

Done.

Alternatives welcomed. 🙂