Monday, June 17, 2013

Increase your productivity with F# scripts (part 2)

Last post we discussed a real world problem, cleanup your test environment after a complex integration test. We used a F# script to do the work.

This time we look add more advanced scenario’s and solutions. First we want to split our script file into two files. One containing the scenario related code and one containing a library of facade function we want to reuse.

We start by creating a new script file in the Scripts directory calling it Lib.fsx and open the file with a text editor. Next we open also the script file we created last time and add the following lines of code:

#load "Lib.fsx"
open Lib 

The first line links the script file to the newly created Lib file. The second line opens the namespace “Lib”, the default namespace of the functions in the Lib script file.

Next we move the facade functions (including the reference to the namespaces) to the Lib file.

SplitSource

After this refactoring everything still works the way it used to work.

Next want to create a startup script. Every time we start the laptop we want start with a clean log directory, we like a clean machine and second we want open a Visual Studio Solution so we can start coding immediately.

We start by creating a new script file called Startup.fsx and the following code:

#load "Lib.fsx"
open Lib

["E:\\Log"] |> List.iter(deleteContentOfDirectory)

Again we connect to Lib file and reuse the deleteContentOfDirectory function we created last time to clean the Log directory.

Next we create a new shortcut like we did the last time:

StartUp_Shortcut

This time linking it to the Startup script.

After testing the script we move the shortcut to the following directory:

C:\Users\YourAccountName\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

  StartUp_3

After we restart our machine, the Log directory is cleaned.

There are other ways to associate scripts with the startup. You can use the group policy if your version of Windows has access to it (gpedit.msc):

LocalGroupPolicy_1

Next step is to open the Visual studio solution.

We add the following facade function to Lib.fsx

let startProcess fileName =
    let newProcess = new Process()
    newProcess.StartInfo.FileName <- fileName
    newProcess.Start() |> ignore

and the following line to the StartUp script

startProcess @"E:\VS\Test\Test.sln"

VS

the next time you start up the log directory is clean and your Solution will be directly available.

Adding extra functionality to the scripts is now a trivial activity.

My Lib files contains, for example, exception handling and some reporting:

ExceptionHandling

This will not stop the script in case a file can not be deleted. The exception will be reported to the console.

There are a lot of activities that can be automated, I have scripts that:

  • Start programs like a mail client.
  • Creating zipped backups of source code.

In this way I can improve my productivity with F# scripting. How can F# scripting increase your productivity?

No comments:

Total Pageviews