haskellshake-build-system

shake build: read filepath list from config file


I have a config.cfg file where a the variable file_list is a list of relative path to files

file_list = file1 dir1/file2 ../dir2/file3 

How do I read this variable in to get a file_list::[FilePath]?
Tried to follow the Development.Shake.Config API Doc without success. I need something to achieve that


file_list <- getConfig "file_list"
let fl = ??? file_list
need fl

ps. I'am an Haskell beginner


Solution

  • The type of file_list is Maybe String, and the type of fl needs to be [FilePath], so the question becomes how to write a function to transform between the two. One option is:

    let fl = words (fromMaybe "" file_list)
    

    The fromMaybe function replaces Nothing with "" - so you now have a String. The words function splits a string on the spaces, to produce [String]. In Haskell FilePath is a synonym for String so it all works out.

    If instead you want to error out if the key is missing, you can do:

    Just file_list <- getConfig "file_list"
    let fl = words file_list
    need fl
    

    Now you are asserting and unwrapping the Maybe in file_list, so if it is Nothing you get a runtime crash, and if it is Just you get it without the Just wrapper, so can simply use words.