Question

I am trying to use FAKE for automating my project builds. In the process, I wrote the following F# code -

open System
open System.IO

module FileUtils =

    type BuildConfiguration = {Name:string; Directory:string}
    let DebugBuildConfiguration = {Name = "Debug"; Directory = @"\bin\Debug"}
    let ReleaseBuildConfiguration = {Name = "Release"; Directory = @"\bin\Release"}
    let StagingBuildConfiguration = {Name = "Staging"; Directory = @"\bin\Staging"}

    let codeDir = @"C:\source-control\s4sold\src"
    let sourceDirs = [| "co"; "lo"; "mo"; "po"; "pr"; "pro"; "re"; "ro" |]

    let GetAllBuildConfigurationDirs buildConfiguration =
        let allSourceDirs = sourceDirs
                                |> Seq.map (fun i -> Path.Combine(codeDir, i))
                                |> Seq.map (fun d -> Directory.GetDirectories(d))
                                |> Array.concat
        allSourceDirs |> printf "%A"
        List.ofArray allSourceDirs
            |> List.map (fun i -> Path.Combine(i, buildConfiguration.Directory))
            // |> Seq.toArray

Now the problem I am facing is that when I print allSourceDirs, it correctly prints the directories under my /src folder. But when I run GetAllBuildConfigurationDirs, all I get is an array of "\bin\Debug", which means it doesn't take the output of allSourceDirs into consideration.

I am really at a loss to understand what's happening here. I am an F# newbie, but know some Clojure. Also how can I use Seq.map over a seq of seqs (so that I might avoid the Array.concat call)?

Was it helpful?

Solution

The problem is the way you are combining paths, you have to remove the \ at the beginning, just write @"bin\Debug"

Also you can combine both maps into one, since map f |> map g is equivalent to map (f |> g) and instead of map + concat you can use collect.

Here's your original code with these corrections:

    open System
    open System.IO

module FileUtils =

    type BuildConfiguration = {Name:string; Directory:string}
    let DebugBuildConfiguration = {Name = "Debug"; Directory = @"bin\Debug"}
    let ReleaseBuildConfiguration = {Name = "Release"; Directory = @"bin\Release"}
    let StagingBuildConfiguration = {Name = "Staging"; Directory = @"bin\Staging"}

    let codeDir = @"C:\source-control\s4sold\src"
    let sourceDirs = [| "co"; "lo"; "mo"; "po"; "pr"; "pro"; "re"; "ro" |]

    let allSourceDirs = sourceDirs
                            |> Array.collect (fun i -> Path.Combine(codeDir, i) |> Directory.GetDirectories)
        allSourceDirs |> printf "%A"
        List.ofArray allSourceDirs
            |> List.map (fun i -> Path.Combine(i, buildConfiguration.Directory))
            // |> Seq.toArray
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top