Using F# for development and devops scripts
Twenty six low-risk ways to use F# at work (part 2)
Last updated
Was this helpful?
Twenty six low-risk ways to use F# at work (part 2)
Last updated
Was this helpful?
This post is a continuation of the series on . 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 and in the previous post.
Here's a list of shortcuts to the twenty six ways:
Part 1 - Using F# to explore and develop interactively
Part 2 - Using F# for development and devops scripts
Part 3 - Using F# for testing
Part 4. Using F# for database related tasks
Part 5: Other interesting ways of using F#
The next set of suggestions relates to using F# for the various scripts that revolve around development activities: builds, continuous integration, deployment, etc.
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.
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:
Simple file copying, directory traversal, and archiving (e.g. of log files).
class to do zipping and unzipping without needing a third party library.
Doing things with JSON, either with a known format
Extracting data from, or manipulating data in, Excel. F# supports COM for doing Office automation, or you can use one of the type providers or libraries.
Web crawling, link checking, and screenscraping. The built-in async workflows and agents make this kind of "multithreaded" code very easy to write.
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.
to halt at a certain point.
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.
The syntax takes a little getting used to, but that effort is well spent.
Some further reading on FAKE:
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 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:
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.
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.
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.
For these kinds of small tasks, you need a good scripting language with a REPL. You could use PowerShell, or , or even Python. But why not give F# a go?
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 !
If you're using .NET 4.5, you can use the new
(using the )
or unknown format (using the ).
Interacting with GitHub using .
Doing numerics with .
Scheduling things with .
If these suggestions whet your interest, and you want to use more F#, then check out the page. It's a great source of useful libraries being written for F#, and most of them will work well with F# scripting.
You can use the tool to inspect and watch variables in an interactive session.
Finally, you can still use the Visual Studio debugger. The trick is to to the
fsi.exe process, and then you can use
The code for this section is .
Let's start with , which is a cross platform build automation tool written in F#, analogous to Ruby's .
You can even use it with .
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 instead, but in practice, no thanks, because XML. 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 instead, which means you can run full builds without having TeamCity installed.
Here's an example of a very simple FAKE script, taken from .
.
. Many of the comments are from people who are using FAKE actively.
.
The code for this section is .
Note that I'm using the Http utilities code in Fsharp.Data
, which provides a nice wrapper around HttpClient
. .
The code for this section is .
Here's a little script that uses the Xml type provider to parse an RSS feed (in this case, ) and convert it to a CSV file for later analysis.
For more on the XML type provider, .
The code for this section is .
.
One area which deserves special mention is using F# for configuring and managing cloud services. The at fsharp.org has many helpful links.
For simple scripting, is a nice wrapper for Azure.