module Hscaffold.Generator.Hsfiles where
import Control.Arrow
import Control.Monad.Writer
import qualified Data.Text as Text
import System.FilePath
import Text.Regex
import Hscaffold.Types
fromHsfiles :: String -> ScaffoldAction e
fromHsfiles h = snd $ foldr helper ("", []) matches
where
helper (Just (fp : _), _) (lacc, current) =
("", File (normalise fp) lacc : current)
helper (_, l) (lacc, current) =
(Text.stripEnd (l <> "\n" <> lacc), current)
re = mkRegex "^{-# START_FILE (.+) #-}( +)?$"
matches = map (matchRegex re &&& Text.pack) (lines h)
fromHsfilesW :: MonadWriter (ScaffoldAction e) m => String -> m ()
fromHsfilesW h = tell (fromHsfiles h)
readHsfiles :: FilePath -> IO (ScaffoldAction e)
readHsfiles fp = fromHsfiles <$> readFile fp