module Static.Resources.Spec (
parseSpec
) where
import Static.Resources.Types
import Data.List
import Data.Char
import Data.Generics.Builders
setPrefix :: String
setPrefix = "set"
ignorePrefix :: String
ignorePrefix = "ignore"
typePrefix :: ResourceType -> String
typePrefix CSS = "css"
typePrefix JS = "js"
typePrefix JSX = "jsx"
typePrefix LESS = "less"
parseSpec :: FilePath -> IO ResourceSpec
parseSpec specPath = do
c <- readFile specPath
return $ foldl parse (ResourceSpec [] []) $ map trim $ lines c
where
trim = (reverse . dropWhile isSpace) . (reverse . dropWhile isSpace)
clear pr s = trim $ drop (length pr) s
parse :: ResourceSpec -> String -> ResourceSpec
parse (ResourceSpec rs is) s
| (ignorePrefix ++ " ") `isPrefixOf` s = ResourceSpec rs ((clear ignorePrefix s) : is)
| (setPrefix ++ " ") `isPrefixOf` s = ResourceSpec ((ResourceSet (clear setPrefix s) []) : rs) is
| otherwise = case find (\t -> (typePrefix t ++ " ") `isPrefixOf` s) constrs of
Just rt -> case rs of
(r:rs') -> ResourceSpec (r {resources = (resources r) ++ [Resource rt (clear (typePrefix rt) s )]}:rs') is
_ -> error $ "Resource " ++ clear (typePrefix rt) s ++ " defined outside resource set in file " ++ specPath
_ -> ResourceSpec rs is