@@ -7,27 +7,39 @@ module Ondim.Loading
77 ( loadTemplates ,
88 loadTemplatesDynamic ,
99 loadTemplatesEmbed ,
10+
1011 -- * \"Advanced\" usage
12+
1113 --
14+
1215 -- | There are default 'LoadConfig's inside each target's respective
1316 -- modules, but you can also use the definitions below to customize them if
1417 -- you wish.
1518 LoadConfig (.. ),
1619 LoadFn ,
1720 loadFnSimple ,
18- TemplateLoadingException (.. )
21+ TemplateLoadingException (.. ),
1922 ) where
2023
2124import Control.Exception (throw )
22- import Control.Monad.IO.Unlift (MonadUnliftIO )
23- import Control.Monad.Logger (MonadLogger , runNoLoggingT )
2425import Data.Map ((!) )
2526import Ondim
26- import Ondim.Debug
27+ ( NamespaceItem (TemplateData ),
28+ OndimNode ,
29+ OndimState (expansions ),
30+ delete ,
31+ insert ,
32+ )
33+ import Ondim.Internal.Basic (fileSite )
2734import Relude.Extra (minimumOn1 , toPairs )
2835import System.FilePath (splitDirectories , (</>) )
2936import System.FilePattern (FilePattern , matchMany )
3037import System.UnionMount
38+ ( Change ,
39+ FileAction (Delete , Refresh ),
40+ Logger ,
41+ unionMount ,
42+ )
3143
3244-- | Some template loading (impure) exception.
3345newtype TemplateLoadingException = TemplateLoadingException String
@@ -61,35 +73,36 @@ loadFnToUpdate fn fp name bs s =
6173
6274-- | Configuration for loading templates of a specific type.
6375data LoadConfig n = LoadConfig
64- { patterns :: [FilePattern ],
65- -- ^ Glob patterns to search for files.
76+ { -- | Glob patterns to search for files.
77+ patterns :: [FilePattern ],
78+ -- | Recipe to load the templates.
6679 loadFn :: LoadFn n ,
67- -- ^ Recipe to load the templates.
68- initialState :: OndimState n
69- -- ^ Initial state. You can use this to set some default expansions or
80+ -- | Initial state. You can use this to set some default expansions or
7081 -- templates that may be overshadowed by file templates.
82+ initialState :: OndimState n
7183 }
7284
7385{- | Load templates from a list of directories in descending order of priority,
7486 and return the inital state and a watcher action that takes a handler to
7587 update the state when templates get updated on disk.
7688-}
7789loadTemplatesDynamic ::
78- forall m n .
79- (MonadLogger m , MonadIO m , MonadUnliftIO m ) =>
90+ forall n .
8091 -- | Loading configurations
8192 [LoadConfig n ] ->
8293 -- | Places to look for templates and their (optional) mount point,
8394 -- in descending order of priority.
8495 [(FilePath , Maybe FilePath )] ->
85- m (OndimState n , (OndimState n -> m () ) -> m () )
86- loadTemplatesDynamic cfgs places =
96+ -- | Logger
97+ Logger ->
98+ IO (OndimState n , (OndimState n -> IO () ) -> IO () )
99+ loadTemplatesDynamic cfgs places logger =
87100 let sources = fromList (zip (zip [1 .. ] (fst <$> places)) places)
88101 cfgMap = fromList $ [(i, f) | (i, loadFn -> f) <- zip [1 .. ] cfgs]
89102 patts = [(i, p) | (i, patterns -> ps) <- zip [1 .. ] cfgs, p <- ps]
90103 exclude = []
91104 initial = foldMap' initialState cfgs
92- handler :: Change (Int , FilePath ) Int -> m (OndimState n -> OndimState n )
105+ handler :: Change (Int , FilePath ) Int -> IO (OndimState n -> OndimState n )
93106 handler chg =
94107 appEndo . mconcat . coerce . join
95108 <$> forM (toPairs chg) \ (i, chg') ->
@@ -101,11 +114,11 @@ loadTemplatesDynamic cfgs places =
101114 fp = dir </> file
102115 in readFileLBS fp <&> loadFnToUpdate (cfgMap ! i) fp name
103116 Delete -> pure \ s -> s {expansions = delete name (expansions s)} <> initial
104- in unionMount sources patts exclude initial handler
117+ in unionMount sources patts exclude initial logger handler
105118
106119-- | Load templates from a list of directories in descending order of priority.
107- loadTemplates :: [LoadConfig n ] -> [(FilePath , Maybe FilePath )] -> IO (OndimState n )
108- loadTemplates cfgs dirs = fst <$> runNoLoggingT ( loadTemplatesDynamic cfgs dirs)
120+ loadTemplates :: [LoadConfig n ] -> [(FilePath , Maybe FilePath )] -> Logger -> IO (OndimState n )
121+ loadTemplates cfgs dirs logger = fst <$> loadTemplatesDynamic cfgs dirs logger
109122
110123{- | Load templates in pure code from a list of filepaths and bytestrings. Meant
111124 to be used with the @file-embed@ package.
0 commit comments