Using F# for development and devops scripts

Twenty six low-risk ways to use F# at work (part 2)

This post is a continuation of the series on low-risk ways to use F# at work. I've been suggesting a number of ways you can get your hands dirty with F# in a low-risk, incremental way, without affecting any mission critical code.

In this one, we'll talk about using F# for builds and other development and devops scripts.

If you're new to F#, you might want to read the sections on getting started and working with NuGet in the previous post.

Series contents

Here's a list of shortcuts to the twenty six ways:

Part 1 - Using F# to explore and develop interactively

1. Use F# to explore the .NET framework interactivelyarrow-up-right 2. Use F# to test your own code interactivelyarrow-up-right 3. Use F# to play with webservices interactivelyarrow-up-right 4. Use F# to play with UI's interactivelyarrow-up-right

Part 2 - Using F# for development and devops scripts

5. Use FAKE for build and CI scriptsarrow-up-right 6. An F# script to check that a website is respondingarrow-up-right 7. An F# script to convert an RSS feed into CSVarrow-up-right 8. An F# script that uses WMI to check the stats of a processarrow-up-right 9. Use F# for configuring and managing the cloudarrow-up-right

Part 3 - Using F# for testing

10. Use F# to write unit tests with readable namesarrow-up-right 11. Use F# to run unit tests programmaticallyarrow-up-right 12. Use F# to learn to write unit tests in other waysarrow-up-right 13. Use FsCheck to write better unit testsarrow-up-right 14. Use FsCheck to create random dummy dataarrow-up-right 15. Use F# to create mocksarrow-up-right 16. Use F# to do automated browser testingarrow-up-right 17. Use F# for Behaviour Driven Developmentarrow-up-right

Part 4. Using F# for database related tasks

18. Use F# to replace LINQpadarrow-up-right 19. Use F# to unit test stored proceduresarrow-up-right 20. Use FsCheck to generate random database recordsarrow-up-right 21. Use F# to do simple ETLarrow-up-right 22. Use F# to generate SQL Agent scriptsarrow-up-right

Part 5: Other interesting ways of using F#

23. Use F# for parsingarrow-up-right 24. Use F# for diagramming and visualizationarrow-up-right 25. Use F# for accessing web-based data storesarrow-up-right 26. Use F# for data science and machine learningarrow-up-right (BONUS) 27: Balance the generation schedule for the UK power station fleetarrow-up-right

Part 2: Using F# for development and devops scripts

The next set of suggestions relates to using F# for the various scripts that revolve around development activities: builds, continuous integration, deployment, etc.

For these kinds of small tasks, you need a good scripting language with a REPL. You could use PowerShell, or ScriptCSarrow-up-right, or even Python. But why not give F# a go?

  • F# feels lightweight like Python (few or no type declarations).

  • F# can access .NET libraries, both the core ones and those downloaded via NuGet.

  • F# has type providers (a big advantage over PowerShell and ScriptCS) that let you easily access a wide range of data sources.

  • All this in a concise, type-safe manner, with intellisense too!

Using F# in this way will allow you and your fellow developers to use F# code to solve practical problems. There shouldn't be any resistance from managers for this low-risk approach -- in the worse case you can easily switch to using a different tool.

A hidden agenda, of course, is that once your fellow developers get a chance to play with F#, they'll be hooked, and you'll be one step closer to using F# end to endarrow-up-right!

What can you do with F# scripts?

In the next few sections we'll see three examples of F# scripts:

But of course, you can integrate F# scripts with almost any .NET library. Here are other suggestions for utilities that can be scripted:

If these suggestions whet your interest, and you want to use more F#, then check out the F# community projectsarrow-up-right page. It's a great source of useful libraries being written for F#, and most of them will work well with F# scripting.

Debugging F# scripts

A great thing about using F# scripts is that you don't need to create a whole project, nor launch Visual Studio.

But if you need to debug a script, and you're not in Visual Studio, what can you do? Here are some tips:

  • First, you can just use tried and true printing to the console using printfn.

    I generally wrap this in a simple log function so that I can turn logging on or off with a flag.

  • You can use the FsEyearrow-up-right tool to inspect and watch variables in an interactive session.

  • Finally, you can still use the Visual Studio debugger. The trick is to attach the debuggerarrow-up-right to the

    fsi.exe process, and then you can use Debugger.Breakarrow-up-right

    to halt at a certain point.

5. Use FAKE for build and CI scripts

The code for this section is available on githubarrow-up-right.

Let's start with FAKEarrow-up-right, which is a cross platform build automation tool written in F#, analogous to Ruby's Rakearrow-up-right.

FAKE has built-in support for git, NuGet, unit tests, Octopus Deploy, Xamarin and more, and makes it easy to develop complex scripts with dependencies.

You can even use it with TFS to avoid using XAMLarrow-up-right.

One reason to use FAKE rather than something like Rake is that you can standardize on .NET code throughout your tool chain. In theory, you could use NAntarrow-up-right instead, but in practice, no thanks, because XML. PSakearrow-up-right is also a possibility, but more complicated than FAKE, I think.

You can also use FAKE to remove dependencies on a particular build server. For example, rather than using TeamCity's integration to run tests and other tasks, you might consider doing them in FAKEarrow-up-right instead, which means you can run full builds without having TeamCity installed.

Here's an example of a very simple FAKE script, taken from a more detailed example on the FAKE sitearrow-up-right.

The syntax takes a little getting used to, but that effort is well spent.

Some further reading on FAKE:

6. An F# script to check that a website is responding

The code for this section is available on githubarrow-up-right.

This script checks that a website is responding with a 200. This might be useful as the basis for a post-deployment smoke test, for example.

The result is:

Note that I'm using the Http utilities code in Fsharp.Data, which provides a nice wrapper around HttpClient. More on HttpUtilities herearrow-up-right.

7. An F# script to convert an RSS feed into CSV

The code for this section is available on githubarrow-up-right.

Here's a little script that uses the Xml type provider to parse an RSS feed (in this case, F# questions on StackOverflowarrow-up-right) and convert it to a CSV file for later analysis.

Note that the RSS parsing code is just one line of code! Most of the code is concerned with writing the CSV. Yes, I could have used a CSV library (there are lots on NuGet) but I thought I'd leave it as is to show you how simple it is.

Note that the type provider generates intellisense (shown below) to show you the available properties based on the actual contents of the feed. That's very cool.

The result is something like this:

For more on the XML type provider, see the FSharp.Data pagesarrow-up-right.

8. An F# script that uses WMI to check the stats of a process

The code for this section is available on githubarrow-up-right.

If you use Windows, being able to access WMI is very useful. Luckily there is an F# type provider for WMI that makes using it easy.

In this example, we'll get the system time and also check some stats for a process. This could be useful during and after a load test, for example.

The output is something like this:

Again, using a type provider means that you get intellisense (shown below). Very useful for the hundreds of WMI options.

More on the WMI type provider herearrow-up-right.

9. Use F# for configuring and managing the cloud

One area which deserves special mention is using F# for configuring and managing cloud services. The cloud pagearrow-up-right at fsharp.org has many helpful links.

For simple scripting, Fogarrow-up-right is a nice wrapper for Azure.

So for example, to upload a blob, the code is as simple as this:

or to add and receive messages:

What's especially nice about using F# for this is that you can do it in micro scripts -- you don't need any heavy tooling.

Summary

I hope you found these suggestions useful. Let me know in the comments if you apply them in practice.

Next up: using F# for testing.

Last updated