Buried in the depths of our code base (.NET house) has been a single
Perl script, hacked together by an ex-college one afternoon to
eliminate an infrequent and boring (read: error prone) task.
Which is great and all, but that ‘infrequent’ moment has arrived once
more, and none of the rest of us have Perl installed. None of the rest
of us really want to install it, either…
An other afternoon later, and we now have an F# version of the script
checked into source control, and the first (and last) piece of Perl
has left source control. The new version is just as quick and dirty as
the old, but at least can be run on any of our machines. As a matter
of general interest, before and after versions below:
Before:
#!/usr/bin/perl | |
use strict; | |
use Data::Dumper; | |
use File::Find; | |
my $abc_deploy = '\\\\cvsw90163\\c$\\Apps\\ABC2\\apache-tomcat-6.0.18\\webapps\\ABC'; | |
my $svc_util = 'C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\SvcUtil.exe'; | |
my @files = (); | |
find(sub | |
{ | |
push(@files, $File::Find::name) if (/^deploy.+wsdd$/); | |
}, $abc_deploy); | |
foreach (@files) { | |
open (WSDD, $_) or die($!); | |
my @lines = (); | |
while (<WSDD>) { | |
push(@lines, $_); | |
} | |
close (WSDD); | |
my $content = join('', @lines); | |
$content =~ /service.+name="(\w+)"/; | |
my $serviceName = $1; | |
my $cmd = "\"$svc_util\" /out:${serviceName}.cs /noConfig /namespace:\"*,CCC.CRM.Integration.Academy.Contracts.${serviceName}\" /serializer:XmlSerializer http://cvsw90163:9090/ABC/services/${serviceName}?wsdl >nul 2>&1"; | |
my $ret = system($cmd); | |
print "Error generating proxy class for $serviceName.\n" if ($ret != 0); | |
} |
After:
open System.IO | |
open System.Text.RegularExpressions | |
open System.Diagnostics | |
let (|Match|_|) pattern input = | |
let m = Regex.Match(input, pattern) | |
if m.Success then Some (List.tail [for g in m.Groups -> g.Value]) else None | |
let abcDeploy = @"\\cvsw90163\c$\Apps\ABC2\apache-tomcat-6.0.33\webapps\ABC" | |
let svc = @"c:\Program Files\Microsoft SDKs\Windows\v7.0a\bin\SvcUtil.exe" | |
let getFilesInDir dirName = | |
Directory.EnumerateFiles(dirName, "deploy_*.wsdd") | |
let filterServiceName line = | |
match line with | |
| Match "<service.+name=\"(\\w+)\"" result -> Some(result.Head) | |
| _ -> None | |
let getServiceName = File.ReadAllLines >> Seq.pick filterServiceName | |
let runSvc serviceName = | |
let p = Process.Start (svc, sprintf "/out:%s /noconfig /namespace:\"*,CCC.CRM.Integration.Academy.Contracts.%s\" /serializer:XmlSerializer http://cvsw90163:9090/ABC/services/%s?wsdl" serviceName serviceName serviceName) | |
p.WaitForExit () | |
if p.ExitCode <> 0 then | |
printfn "%s failed!" serviceName | |
getFilesInDir abcDeploy | |
|> Seq.map getServiceName | |
|> Seq.map runSvc | |
|> Seq.length | |
|> ignore | |
System.Console.Read () |> ignore |