New CodeRush Plugin - Template Importer

I was rereading my previous post New CodeRush Plugin Template – NewAction

In it I asked you to ….

  • Visit a page on github.
  • Locate an XML file.
  • Download that file.
  • …and finally Import the file.

However it occurs to me that this is rather lengthy and awkward.

What if we could automate that a little more?
What if we could write a plugin which was able to download templates and install them itself?
What if….

Aw nuts! Never mind ‘What if’……….Let’s just do it!

CR_TemplateImporter

This is Template Importer.

TemplateManagerImporting
Note: It’s pretty clear that I’m no UI expert, but that’s another good reason to open source and accept pull requests.

In brief:

It can do 2 amazing things.

  • It can import exactly 1 exported set of templates by providing the address of an exported template file to the 1st box and pressing Import.
  • It can import multiple sets of exported templates by providing the address of a package to the 2nd box and pressing Import.

More Detail

Import Templates (from the Web)

A exported set of templates (see Exporting and Importing CodeRush Templates) is just an XML file. They’re easy to create, easy to store and easy to publish for others to use. All that’s really needed is an easy way to strip them off the web, and import them into your system.

The top half of the screen does exactly this. You can specify the url of a raw template file, and then click the Import button.
(For your convenience, the little “>” button on the left will copy the example into the live box)

The example file is published on github, but there’s no limit to where you can put these files.

  • Host them on your intranet.
  • Put them in your dropbox account.
  • Attach them to your blog.
  • ftp them into place.
  • ‘git push’ them into place.

Anywhere they can be reached by http is good enough.

I’m planning on uploading a lot more of my templates to various appropriate github locations.

But wait …. It gets better!

Import Packages (from the Web)

Q: Hang on… What is a package?

A: A package is a simple xml file (Yes I like XML files. Sorry.) which contains the urls of one or more exported template files.

For example here is the contents of the example package that is listed within the TemplateImporter by default.

TemplatePackageDefinition

The xml is fairly simple.

The template node has a url attribute (pointing the way to a template file) and a language attribute (specifying the language of the templates within the file).

Of course you can add as many ‘template’ nodes as you want, which means that you can create single package file that lists all your favourite 3rd party templates and then import them onto any machine you like in one hit. Great for being able to pull your standard templates onto your co-worker’s machine.

Limitations

The Current Template Importer is limited in a few ways.

  • Template Import only understands VB.Net and CSharp templates.
    There is no information held in a set of exported templates regarding the source language from which it came. The importer makes an intelligent guess based on the prefix of the filename you point it at. It understands Basic, VB, CSharp and CS.
    Note: Package Import does understand the other languages. As indicated, the package will contain a language attribute for each url resource listed. this is used by the importer in order to place the templates in the correct location.
  • There is no preview.
    There is no option to preview what you’re getting in advance of getting it. Unless of course you’re prepared to wade (and I mean wade) into the xml of the files you’re instructing the Importer to import.
  • There is no selective import.
    You get every template in the file(s) or nothing at all.
  • The import is additive only.
    It will add these templates at their own specified locations in addition to anything else that might be there already. This includes copies of the exact same templates that you imported 5 seconds previously. In order to get only the latest version of a set of templates previously imported, I recommend deleting any previous revisions of those templates prior to each import.

Guidelines

There are no real safeguards in place yet. Be careful.

  • Don’t import templates from a source unless you explicitly trust that source.
    Really this one should speak for itself.
  • Don’t export templates from outside your custom folder.
    If someone were to import those they’d wind up with duplicate core templates. I’m honestly not sure what the effect would be, but it would certainly cause a mess
    If templates are always exported from within custom, they they’ll import there as well. This then limits the potential mess to the users custom folder.
  • Only import templates into your Custom folder.
    If you only ever import templates into you custom folder, then you can always restore your system to normal simply by hosing that folder.

So where can I get it?

As usual, the source code and binary for this plugin are available on github

Like many others, this plugin is listed on the CodeRush Plugins Site.

Usage

You can either:

  • Bind a key to the ‘ImportTemplates’ command, and trigger it with the keystroke of your choice.

OR

  • Use the ‘Import Templates’ menu item I’ve placed on the CodeRush menu.

In future when I provide one or more templates, you should be able to use this plugin to point directly at the file’s url and suck the templates straight off the web without all that tedious mucking about with Right click … Save As etc

1 comment(s)
Floyd Gladden
Floyd Gladden

It would be great to be able to automate or have a 1 button way of exporting our custom templates to a share directory.

3 February, 2016

Please login or register to post comments.