Jakew
Consulting, hacking, and motorcycles

This looks like fun

Sunday, 16 March 2008 17:31 by jakew

from Instapundit:

 

for about 5 seconds.

 

Were kids dumber back then or was the world really that boring?

Categories:  
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Visual Studio Templates presentation materials

Sunday, 16 March 2008 14:47 by jakew

Ok, finally got around to zipping up the files and posting them.  Go here to get it while its hot.

I'll post some more stuff and finish up the work with the T4 engine and the template later this week (2008-03-16) and get it posted.

 

Enjoy!

 

If you have any questions please let me know and I'll do my best to respond.

Categories:  
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

A bit over the top but kinda cool

Saturday, 15 March 2008 13:01 by jakew

Go this from InstaPundit: "Go ahead, make your own joke" - knoxnews.com

 

redneck-mansion.jpg

 

Ok, it wouldn't go over here in yuppy acres where I live.  But then again, I can't stand this neighborhood most of the time so I'll consider that a point in its favor.  Further, for all the yuppy snobs out there - if you replaced the trailers with shipping containers or just simple box structures you'd be making your Calvin Klein undies moist. 

 

I like it.  Very hip red neck. 

Categories:  
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

User group materials

Friday, 14 March 2008 14:52 by jakew

I've been stuck in meetings all day.  I'll get my materials posted tonight. 

Categories:  
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Follow me on twitter

Friday, 14 March 2008 13:12 by jakew

I'm using twitter for quick comments.  Follow me:  http://twitter.com//jakew

Categories:  
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Visual Studio Templates Part 3

Saturday, 8 March 2008 20:35 by jakew

Ok, I’m still at it and am still just at the surface of what is possible with Visual Studio Templates. Seriously. If you go look at PAG’s software factory stuff – they’re just putting out templates on steroids. Whatever disagreements I have in regard to how they write (overly complex) software, the factories are a great example of how far you can push things.

This time around I want to talk about adding code to our template. Adding code to a template is done by writing a class that implements the IWizard interface, GACing said class and then referencing it in your VSTemplate file. Simplicity itself!

Overview

The goal of writing a Wizard is to introduce more control over how our new project or item is created. Without being able to add some code we are stuck with the few parameters that Visual Studio has built in. While those parameters are great and all, I’m still stuck with an incorrectly names class file in my project! I also don’t have much control over my namespace. Oh, and the time is formatted wrong – just want the date. I’m sure there are a dozen other things I’d like to add, but for now let’s correct those few things.

IWizard

Before jumping in to the code we should look at the interface that needs to be implemented. To work with IWizard you need to add a reference to the Microsoft.VisualStudio.TemplateWizardInterface assembly and add add a using statement referencing Microsoft.VisualStudio.TemplateWizard. With that out of the way you can look at IWazard. IWizard defines 6 methods:

· BeforeOpeningFile

· ProjectFinishedGenerating

· RunFinished

· RunStarted

· ShouldAddProjectItem

The first four methods occur at the phases they are named after. Most of the work we do will be in RunStarted. You can read the MSDN docs here. For our purposes everything can be done inside RunStarted so I’m going to ignore the other members of the interface.

Inside RunStarted we are given the DTE object (automationObject), a dictionary of replacement parameters, the kind of Wizard we are (Project or Item) and then an array of custom parameters.

I’ve talked about working with DTE before. Everything I said this is true now. You can go in and access all of the Visual Studio environment and do whatever you want in there. In this case we don’t need anything fancy. We also won’t care about the Wizard kind here, the wizard we’ll put together will work regardless. The customParams won’t be used either. That leaves the replacementDictionary.

The replacement dictionary brings up the VSTemplate file I mentioned earlier. One of the sections in the file is for specifying a wizard to run whenever the template is used. Registering your template is easy enough. Just specify the assembly for your wizard and the name of the class. Here is the XML you’ll use:

clip_image002

Why they didn’t just do a full strongname is beyond me, but I’m not going to complain too much. The actual implementation of your template is also really easy. In the RunStarted method I’m going to throw up a dialog box and get the namespace and class name we want for our first class. When the user hits OK I put those two values in to the replacement dictionary and let VS do the rest . Here is the code:

clip_image004

Easy. However, renaming the actual file takes more work and involves using the VisualStudio objects. Even so, we now have a wizard that allows us to provide additional parameters to our templates. In this case I’m controlling the file’s namespace and class name with my own parameters. You could also use information collected inside your wizard to configure the project differently.

Conclusion

I still feel that I’m barely scratching the surface of what you can do with Visual Studio’s templates. In my next go around with them I’m going to introduce microsoft’s template engine to the mix so we can make some really kick ass templates.

Categories:  
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Visual Studio Templates part 2

Saturday, 8 March 2008 09:49 by jakew

In my previous article I covered the absolute basics of how to work with Visual Studio Templates. At the point you should be able to crank out either an item template or a project template. Now I’m going to talk about organizing your templates so they show up the way you want them to in your environment.

The basics

When you export a template and then put it in your “My Templates” folder, it shows up like this:

clip_image002

Which is pretty cool. However, notice that I had to scroll down past all the other stuff. You could change the sort order value to 10 and then copy your template in to Visual Studio’s cache. Doing so will cause your template to show up before everything else. If you leave the template in your “my templates” folder the sort order has no affect – VS sorts those templates alphabetically by Name. Sorry.

So changing sort order and moving in to VS’s template area causes things to look like this:

clip_image004

Better. But how do we get our template to show up in those folders under “Visual C#” or better yet: How do I make my own folder? Turns out to be really simple: create a folder. In the spirit of full disclosure, in order to get my template to show up at the very top under “Visual C#”, I had to create a 1033 folder under the CSharp folder (<VSInstall>\Common7\IDE\ProjectTemplates\CSharp). I’m not sure it that was because I’m screwing around with stuff or something else. Don’t really care now because I figured it out.

Anyway, what if I have a pile of templates that I want to organize together in my own subfolder? Create a subfolder, create a locale under it and then put your templates there. For instance I’ll create a “Jake’s Templates” folder and move my template there:

clip_image006

Pretty fickin cool. So know we know that we can make our templates show up wherever we want or even give them their own place.

Go forth and make templates!

Categories:  
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Visual Studio Templates

Saturday, 8 March 2008 08:40 by jakew

Maybe it’s just me with my head buried in work but I’ve really not noticed people singing the praises of VS’s templates feature as much as I would think they should. If you are not familiar with them you can read about them here. The VS docs are good, a tad bland and generic but they explain what can be done.

In case you don’t have time to read everything I’ll provide some details. But first I want to explain why I think this feature is important. As a developer I produce code. That is what developers do, or should be doing. I have particular ways that I like to have things done, the boiler plate stuff that goes in to every project, and class file I write. Having templates around saves me time. Without the template stuff I end up having to put in the stuff I like by hand. Worst case I’m doing a cut & paste job all over the place. Prior to templates I wrote Macros. Actually, I’m not whining about macros b/c macros let you do cool stuff. But for now just focusing on adding a class to a file VS’s templates rock!

So how do you make your own template and then how do you make it do the cool stuff that makes it special? I’ll drill in to that in a couple of steps. The first step will be generating our first version of the template. Then we will customize the template to do some cool stuff.

Generating a template

To get started open Visual Studio and create a new C# class library project. Visual Studio creates all the usual stuff you are used to. Go to the solution explorer and select class1.cs and open it. You should see this:

clip_image002

Everything you see in the solution and in the editor came from a template. We will use this as the starting point for out template. First, I’m going to add a file header (yeah, I like those) and a bunch of regions. It will now look like this:

clip_image004

I’m happy with this setup; this is how I like my class files to look. The only problem right now is that if I make a template out of this the namespace will be wrong and the class will be wrong. I need to tell Visual Studio to substitute the correct values for those. For an added bonus I’d like the date to be put in the header (less work for me). Visual Studio provides a few default parameters to do this. You can read the docs here. In this case we need three parameters: safeitemname, safeprojectname, and time. To use the parameters in the template you put the parameter inside a pair of dollar signs. The template will now look like this:

clip_image006

Now we are ready to export the template. For convenience we will export the template twice. First we will export a project template and then we will export an item template. Exporting the template is really easy: go to File/Export Template. If the menu item is not on the menu go to Tools/Customize and add it.

First export the project template. Click File/Export Template:

clip_image008

Select Project and click Next.

clip_image010

Fill in the template name, description and optionally put in an icon. Leave the check boxes checked so Visual Studio will automatically add the templates for use. Click Finish. Visual Studio export the template to “My Exported Templates” Under the Visual Studio directory in your “My Documents” area. Just copy the project template over to the Templates directory (in my case “My Documents\Visual Studio 2005\Templates\Project Templates\Visual C#”. Once you’ve got the template file where it goes go create a new project. When you go to File\New Project in Visual Studio you should see:

clip_image012

“Extended Library” is my template. Notice the sweet icon I made for it. This will let us create a class library just the way we like it. Now repeat the process and export the class file as an item template. It’s the same process as the project, but this time switch to item template in the first page of the wizard. After you complete the project, copy the template over to your C# item templates library. Now you can add your customized class to your customized project and have everything match.

These are small improvements, but even so it saves me a little time (30 seconds maybe) each time I add a new project or class to a project. You can also add references and stuff to your project so that those will be included each time you start a new project. For instance if you use EntLib in your project you can create a template that already references the parts of EntLib you use. That way when you create a project and add classes to it they already have the references you need in place.

What is in the template file?

So far we’ve been able to do everything by generating our templates from within Visual Studio. What if you want to either hand craft your template or edit an existing template? No problem. Visual Studio’s templates are just ZIP files with a few items inside. The most important file inside the template has the extension .vstemplate. There can be only one and VS looks for it when you run the template. Inside the vstemplate file is XML that describes the template and tells VS what to do in order to expland it. The XML contains things like the name to display in the new file dialog, the type of project, the icon to display (if any). The most important thing in the XML is the section that tells VS what files and directories to add. A Project template can contain an entire directory structure if needed. An Item template will only contain a few files (think about partial classes and resources). The XML also allows you to reference executable code to be run as part of the template. I’ll get in to that later. But for a full run down on the Template XML go here. Looking at the schema it is apparent that I’m only scratching the surface in this article. You can do a lot inside your template to help yourself.

Besides the VSTemplate file there will be the actual template files that you have generated. You can just drag files in to the template and then reference them inside the VSTemplate and Visual Studio will take care of putting them where you want them.

Finally, you can throw an icon in the zip file and reference it in the XML and VisualStudio will display your icon in the new file/project dialog. In some ways this is a trivial thing, but it also helps because you can more quickly identify your template. If you already have an icon you like laying around then use it. If not, don’t waste too much time making one.

Up to this point we have been throwing all of our templates in to your “My Documents” folder. Most of the time this is a good idea and keeps things simple. This approach even works great when giving your friends your favorite template. Just have them throw it in the MyTemplates folder and be done with it. However, eventually you might want to make your template look like it shipped as part of Visual Studio. To do that you need to put your templates where Visual Studio keeps its templates. Again, MSDN tells you all about it. Go here.

Once you are happy with your template copy it to either:

· <VSinstall dir>\Common7\IDE\ItemTemplates\(Language)\(Locale)

· <VSinstall dir>\Common7\IDE\ProjectTemplates\(Language)\(Locale)

In my case, being a C# bigot in the US the directories would be:

· \Program File\Microsoft Visual Studio 8\Common7\IDE\ItemTemplates\CSharp\1033

· \Program File\Microsoft Visual Studio 8\Common7\IDE\ProjectTemplates\CSharp\Windows\1033

The same stuff works for Visual Studio 2008, just change the directories. After you copy the template to the directory you will need to delete the contents of the cache and tell visual studio to rebuild the cache. Skipping this step will prevent you from seeing your shiny new template.

To make your template show up exit Visual Studio, go to <VSInstalldir>\Common7\IDE\ItemTemplatesCache and delete all of its contents. Then at the command line run “deveenv /installvstempltes” you might have to run the visual studio command prompt to make it work, just go to the Visual Studio menu in your start menu.

Conclusion

I feel that I’ve barely scratched the surface of the possibilities that Visual Studio’s templates represent. Even so, what I’ve covered can have a significant impact on your productivity. Begin creating templates for common elements you use in your everyday work and reap the benefits.

Categories:  
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed