module Development.Shake.Plus.Loaders (
batchLoad
, batchLoadWithin
, batchLoadWithin'
) where
import Control.Comonad.Env as E
import Development.Shake.Plus.Core
import Development.Shake.Plus.Directory
import Path
import RIO
import qualified RIO.HashMap as HM
import Within
traverseToSnd :: Functor f => (a -> f b) -> a -> f (a, b)
traverseToSnd f a = (a,) <$> f a
batchLoad :: MonadAction m
=> Path b Dir
-> [FilePattern]
-> (Path b File -> m a)
-> m (HashMap (Path Rel File) a)
batchLoad dir pat f = do
xs <- getDirectoryFiles dir pat
xs' <- mapM (traverseToSnd $ f . (dir </>)) xs
return . HM.fromList $ xs'
batchLoadWithin :: MonadAction m
=> Within b [FilePattern]
-> (Within b (Path Rel File) -> m a)
-> m (Within b (HashMap (Path Rel File) a))
batchLoadWithin w f = do
xs <- getDirectoryFiles (E.ask w) (extract w)
xs' <- mapM (traverseToSnd $ f . (`within` E.ask w)) xs
return $ (`within` E.ask w) $ HM.fromList xs'
batchLoadWithin' :: MonadAction m
=> Within b [FilePattern]
-> (Within b (Path Rel File) -> m a)
-> m (HashMap (Within b (Path Rel File)) a)
batchLoadWithin' w f = do
xs <- getDirectoryFiles (E.ask w) (extract w)
xs' <- mapM (traverseToSnd $ f . (`within` E.ask w)) xs
return $ HM.fromList (first (`within` E.ask w) <$> xs')