The new version 0.27 of “FAKE – F# Make” comes with new syntactic sugar for build targets and build dependencies. Don’t be afraid the old version is still supported – all scripts should still work with the new version.
The problem
Consider the following target definition:
let buildDir = "./build/"
Target "Clean" (fun _ ->
CleanDir buildDir
)
Target "Default" (fun _ ->
trace "Hello World from FAKE"
)
"Default" <== ["Clean"]
run "Default"
As you can see we are having a lot of “magic strings” for the target names and the dependency definitions. This was always a small shortcoming in FAKE, since this doesn’t allow refactoring and may result in runtime errors.
One of my goals for “FAKE – F# Make” is to remove these strings in future versions. Unfortunately this is not that easy, because it causes a lot of internal issues. In particular logging to the build server is much harder if you don’t have a target name.
The first step
As posted in a bitbucket comment by cipher we could use the “dynamic lookup operator” to remove some of the magic strings without breaking any internal code.
As a result we can rewrite the above sample as:
let buildDir = "./build/"
Target? Clean <-
fun _ -> CleanDir buildDir
Target? Default <-
fun _ -> trace "Hello World from FAKE"
For? Default <- Dependency? Clean
Run? Default
All magic strings suddenly disappeared. I think this syntax looks really nice, but unfortunately the strings are not really gone, since the token Default is only checked at runtime.
The idea for future versions
Since the new syntax is really just syntactic sugar I’m always interested in a better solution. Currently I’m working on a syntax using monads. The result could look like this:
let buildDir = "./build/"
let Clean = target { CleanDir buildDir }
let Default =
target {
trace "Hello World from FAKE"
trace "Another line"
}
Default <== [Clean]
Run Default
This way the magic string are really gone, but my current problem is retrieving the target name from the let-binding name. Please leave a comment if you have an idea to solve this issue.
Tags: F#, F-sharp Make, Fake
Excellent idea! I’ve been following your progress on fake because I think F# is a great replacement for a script language and certainly better than using XML as a programming language. I’ve seen some horrible build scripts.
Also I wasn’t too fond of using magic strings like you. Unfortunately I can’t think of a solution other than reflection or the expression syntax. I’m sure you’ve considered those already.
Excuse me my blabbering, I hope it encourages you to continue working on fake!
Comment by Frank de Groot — Tuesday, 9. February 2010 um 11:32 Uhr
Hi Frank,
thanks for your feedback.
>> I can’t think of a solution other than reflection
This would be ok for me. But unfortunately I don’t really see how this could look like.
Regards,
Steffen
Comment by Steffen Forkmann — Tuesday, 9. February 2010 um 11:43 Uhr