Friday, December 19, 2008

Writing PowerShell cmdlets in F#

Note: This issue was resolved by the May 2009 CTP. The cmdlet code is unchanged in the Feb 2010 (v 1.9.9.9) CTP.

There's a catch to this, as I discovered today. You can't -- or to be accurate, you can't inherit PSSnapIn in F# yet. As also noted here, the F# compiler gets its knickers in a twist about the internal abstract members higher up the inheritance chain, culminating in an error stating that PSInstaller.get_RegKey() and PSInstaller.get_RegValues() are not implemented.

You can't fix this with cunning reflection tricks (if you make an override method to call via reflection into the base class, you get told that there's nothing public to override; if you make it a member, you get back to the same error as before). And you can't fix it by delegating the install to a PSSnapIn subclass in a different assembly, because that just registers the delegated assembly (which probably contains no cmdlets at all).

But you can use a CustomPSSnapIn subclass in an assembly that references the F# code. For a very simple "Hello World", based on the code samples from the Wrox PowerShell Programming book we do something like

in the custom snap-in subclass, which is the only class needed in a C# project, which generates the assembly that you actually register via installutil. This project references your F# module for the cmdlet classes that do the real work, and just does the work of publishing them to the PoSh infrastructure. The essentials of the F# code are (in the .fsi file):

where the constructor declaration is essential for the cmdlet to work, and in the .fs file:

And then we can write F# cmdlets as we wish...

Arch-druid talks sense shock!

If even the Archdruid of Canterbury can see what's going wrong, how bad must it really be getting?

Thursday, December 18, 2008

Sunday, December 14, 2008

Erlang is great stuff!

Following up on Erlang is Interesting and Heartfelt thanks for the Erlang tips!

I have completed the reimplementation of what was about 20kloc of C/C++ (15kloc of which being an SDK of which I actually used only about half the capabilities of), in just over 1kloc. I could probably refactor/rewrite the C/C++ to reduce it in size by a factor of 3-4 -- I have an approximately 1kloc replacement for the parts of the SDK I actually use -- but there is a limit around that mark where the amount of code needed just to placate the Win32 gods applies a hard lower bound for the tout ensemble.

And not only is it shorter (a big win for keeping what's going on in your head in the first place), using the right language for the job means that the threading story is far clearer, too. And, as an unlooked for bonus, the whole thing (a protocol terminating data pump) performs far better under load, without being at all resource heavy.

Given the choice, I don't think there's another platform that I'd choose for doing such bits of network plumbing infrastructure.

Saturday, December 13, 2008

Maximum Moon

A blanket of rain clouds hid the full moon that was both close to perigee and close to the winter solstice last night; but Thursday's clear night was very bright and steely grey, sparkling in hard frost under a cloudless sky with a near as makes no difference full moon.

IronPython 2.0 RTW + Silverlight 2.0

15-Apr-15 : Please ignore -- obsolete technologies

Following on from the β2 sample…

Changes made as follows:

  • web page page, silverlight object type : type="application/x-silverlight-2"
  • web page, fallback download location : "http://go.microsoft.com/fwlink/?LinkID=124807"
  • AppManifest : RuntimeVersion="2.0.31005.0"
  • AppManifest : add <AssemblyPart Source="Microsoft.Scripting.ExtensionAttribute.dll" />
  • AppManifest : remove <AssemblyPart Name="System.Windows.Controls.Extended" Source="System.Windows.Controls.Extended.dll" />
  • app.py -- remove the line clr.AddReference('System.Windows.Controls.Extended, Version=2.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35')
  • Delete all earlier assemblies and use all the assemblies from C:\Program Files\IronPython 2.0\Silverlight\bin.

Or, from scratch, with the embedding paged base around C:\Program Files\IronPython 2.0\Silverlight\script\templates\python\index.html ...

app.py

app.xaml

AppManifest.xaml

The XAP file is now down to 935kb, which considerably closes the gap with Jython 2.2.1's 670k but is still noticeably larger than a minimal Jython 2.1 applet:

274467 Aug 13 21:48 applet.jar
       956662 Dec 13 17:50 app.xap
         2266 Dec 13 17:53 index.html
         2071 Aug 13 22:02 jython.html

However, now both products are at a stable release, it starts to make sense to actually use the technology.

Following up here.

Sunday, December 07, 2008

Crisp and clear

The weekend has been clear, sunny or moonlit by turns, and very cold; the days very agreeable in the level of sunshine. The cats agree, by going out to sit on the picnic table and bask.

The Tonks are showing that they have pedigrees, by coming in to use the litter tray at intervals -- Kami being an accomplished master of the one-way catflap now.

It was funny to watch them when going out to find something to tastier to drink than tapwater licking at the film of meltwater on the ice in the various containers on the patio.

Wednesday, December 03, 2008

Syntax Highlight Brush for F#

Following on from Scott Hanselman's post about the SyntaxHighlighter script, here's the secret sauce I needed to get it to work.

And here's a first stab at an F# brush, which is in use for this post.

The symbolic class is not yet working; ocaml and reserved do work, and there's more to add, as noted in the introductory comment, if I can get past the symbolic hurdle.

Winter wonderland

Last night wheeling Karen to WI and back, the going was very slippery underfoot and everything was sparkling in the street lights.

This morning, I not only waited until well after sun-up to go to work, but decided to take main roads into work; but that still meant a mile or so on minor roads to get there.

And almost as soon as I got out of the village, traffic started piling up. The reason -- two recovery trucks pulling cars out of a ditch, the only length of ditch without hedges along the whole road.

Very slow journey.

Links for 3-Dec

Reducing memory leaks in JavaScript.

nUnit 2.5 now in beta.

So is Moonlight 1.0 (Silverlight 1.0 for Mono).

JavaScript syntax highlighter -- example page -- Now I need to write F# and Erlang brushes.

Testing that DLR language implementations honour interface contracts.

Monday, December 01, 2008

Belatedly, New Year's Resolution

Finally, belatedly, I have achieved my New Year's Resolution (of 1920x1200) -- for the last couple of years since my 1997 vintage 19" 1600x1200 VDU died, I have had to put up with having a smaller screen resolution at home than in the office (equivalent 19" 1600x1200 VDU, upgraded a few weeks ago to 24" 1920x1200 LCD), which has meant that when I GotoMyPC (the best acquisition Citrix has made during my time with them so far as end-user dog-fooding goes) to home-work, I've had to suffer blurred/shrunken text.

No longer. Now, for the same £200 price-tag as the 19" 1280x1024 screen I got as a replacement for the dead VDU at the time, I now have the same headline spec as at work; this being about 1/4 the price I would have had to pay had I gone straight to this spec. So now I see my office screen at 1:1; and playing video, especially wide-screen video, is just amazing.

This has not been for want of trying, though -- just that actually getting my hands on such an item for the instant gratification facto has taken 3 months : they seemed never to be in stock when I tried to buy.

Amused

The SEO benefits of clean web design emerged in thread on /a/ over the weekend. Apparently my NGE fan-site comes very high up Google search.

This emerged in the context that some who feel that my opinions are not so much heterodox as heretical were uncomfortable that my writings should be so easily stumbled across by newcomers.

Nothing stopping them writing their own sites with cleaner design, though.

Links for 1-Dec

PowerShell synch script for SysInternals tools.

Expression<T> demystified.

Xen makes OCaml news.

Minimal Erlang distro.