module Descript.Build.Read.File ( mkFile , loadFile , defaultResolver ) where import Descript.Build.Read.Read import qualified Descript.BasicInj as BasicInj import Descript.Misc import Data.Text (Text) import qualified Data.Text.IO as Text import Core.Control.Monad.Trans import System.FilePath -- | Creates a file with the given path and contents, which uses the -- default resolver. mkFile :: FilePath -> Text -> DFile IO mkFile = mkDepFile [] -- | Creates a file with the given path and contents, which uses the -- default resolver, provided that this is a dependency of the given -- modules. mkDepFile :: [RelScope] -> FilePath -> Text -> DFile IO mkDepFile dpds path contents = DFile { depResolver = defaultDepResolver dpds $ takeDirectory path , sfile = mkSFile path contents } -- | Reads the file with the given path. loadFile :: FilePath -> IO (DFile IO) loadFile path = mkFile path <$> Text.readFile path defaultResolver :: FilePath -> DepResolver IO defaultResolver = defaultDepResolver [] -- | The default resolver for a dependency of the given modules. defaultDepResolver :: [RelScope] -> FilePath -> DepResolver IO defaultDepResolver dpds path = readDepResolver dpds path =<< defaultTextResolver path readDepResolver :: [RelScope] -> FilePath -> Text -> DepResolver IO readDepResolver dpds basePath contents = DepResolver { showDepResolver = "readDepResolver " ++ show basePath ++ show contents , resolveDep = resolve } where resolve relScope = do guardCycleT relScope dpds readDep (relScope : dpds) (scopeFilepath basePath relScope) contents readDep :: [RelScope] -> FilePath -> Text -> DepResultT IO readDep dpds path contents = fmap BasicInj.dsourceAModule . validateR =<< readSrcR file where readSrcR = gresToDres (summaryF $ sfile file) . readSrc validateR ddsrc = do guardPromoteCycleT $ dirtyWarnings $ depdDep ddsrc gresToDres validateErrorSummary $ hoist $ BasicInj.validate ddsrc gresToDres summary' = mapErrorT $ DepNotBuild . summary' file = mkDepFile dpds path contents