@@ -34,7 +34,6 @@ import qualified Data.Text as T
3434import qualified Data.Text.Encoding as T
3535import qualified Data.Text.Encoding.Error as T (lenientDecode )
3636import qualified Data.Text.IO as T
37- import qualified Data.Text.Lazy as LT
3837import Data.Time.Calendar
3938import Data.Time.Clock
4039import qualified Data.Yaml as Yaml
@@ -48,8 +47,8 @@ import Stack.Types.Config
4847import Stack.Types.PackageName
4948import Stack.Types.TemplateName
5049import RIO.Process
51- import Text.Hastache
52- import Text.Hastache.Context
50+ import qualified Text.Mustache as Mustache
51+ import qualified Text.Mustache.Render as Mustache
5352import Text.Printf
5453import Text.ProjectTemplate
5554
@@ -187,17 +186,17 @@ applyTemplate project template nonceParams dir templateText = do
187186 , (" name-as-module" , nameAsModule) ]
188187 configParams = configTemplateParams config
189188 yearParam = M. singleton " year" currentYear
190- (applied,missingKeys) <-
191- runWriterT
192- (hastacheStr
193- defaultConfig { muEscapeFunc = id }
194- templateText
195- (mkStrContextM (contextFunction context)))
189+ etemplateCompiled = Mustache. compileTemplate ( T. unpack (templateName template)) templateText
190+ templateCompiled <- case etemplateCompiled of
191+ Left e -> throwM $ InvalidTemplate template ( show e)
192+ Right t -> return t
193+ let (substitutionErrors, applied) = Mustache. checkedSubstitute templateCompiled context
194+ missingKeys = S. fromList $ concatMap onlyMissingKeys substitutionErrors
196195 unless (S. null missingKeys)
197196 (logInfo (" \n " <> displayShow (MissingParameters project template missingKeys (configUserConfigPath config)) <> " \n " ))
198197 files :: Map FilePath LB. ByteString <-
199198 catch (execWriterT $ runConduit $
200- yield (T. encodeUtf8 ( LT. toStrict applied) ) .|
199+ yield (T. encodeUtf8 applied) .|
201200 unpackTemplate receiveMem id
202201 )
203202 (\ (e :: ProjectTemplateException ) ->
@@ -217,20 +216,8 @@ applyTemplate project template nonceParams dir templateText = do
217216 return (dir </> path, bytes))
218217 (M. toList files))
219218 where
220- -- | Does a lookup in the context and returns a moustache value,
221- -- on the side, writes out a set of keys that were requested but
222- -- not found.
223- contextFunction
224- :: Monad m
225- => Map Text Text
226- -> String
227- -> WriterT (Set String ) m (MuType (WriterT (Set String ) m ))
228- contextFunction context key =
229- case M. lookup (T. pack key) context of
230- Nothing -> do
231- tell (S. singleton key)
232- return MuNothing
233- Just value -> return (MuVariable value)
219+ onlyMissingKeys (Mustache. VariableNotFound ks) = map T. unpack ks
220+ onlyMissingKeys _ = []
234221
235222-- | Check if we're going to overwrite any existing files.
236223checkForOverwrite :: (MonadIO m , MonadThrow m ) => [Path Abs File ] -> m ()
@@ -421,7 +408,7 @@ instance Show NewException where
421408 show (InvalidTemplate name why) =
422409 " The template \" " <> T. unpack (templateName name) <>
423410 " \" is invalid and could not be used. " <>
424- " The error was: \" " <> why <> " \" "
411+ " The error was: " <> why
425412 show (AttemptedOverwrites fps) =
426413 " The template would create the following files, but they already exist:\n " <>
427414 unlines (map ((" " ++ ) . toFilePath) fps) <>
0 commit comments