@@ -271,8 +271,7 @@ applyTemplate project template nonceParams dir templateText = do
271
271
throwM (InvalidTemplate template
272
272
" Template does not contain a .cabal or package.yaml file" )
273
273
274
- -- Apply Mustache templating to a single file within the project
275
- -- template.
274
+ -- Apply Mustache templating to a single file within the project template.
276
275
let applyMustache bytes
277
276
-- Workaround for performance problems with mustache and
278
277
-- large files, applies to Yesod templates with large
@@ -286,29 +285,40 @@ applyTemplate project template nonceParams dir templateText = do
286
285
Right t -> return t
287
286
let (substitutionErrors, applied) = Mustache. checkedSubstitute templateCompiled context
288
287
missingKeys = S. fromList $ concatMap onlyMissingKeys substitutionErrors
289
- unless (S. null missingKeys)
290
- (logInfo (" \n " <> displayShow (MissingParameters project template missingKeys (configUserConfigPath config)) <> " \n " ))
291
- pure $ LB. fromStrict $ encodeUtf8 applied
288
+ pure (LB. fromStrict $ encodeUtf8 applied, missingKeys)
292
289
293
290
-- Too large or too binary
294
- | otherwise = pure bytes
295
-
296
- liftM
297
- M. fromList
298
- (mapM
299
- (\ (fpOrig,bytes) ->
300
- do -- Apply the mustache template to the filenames
301
- -- as well, so that we can have file names
302
- -- depend on the project name.
303
- fp <- applyMustache $ TLE. encodeUtf8 $ TL. pack fpOrig
304
- path <- parseRelFile $ TL. unpack $ TLE. decodeUtf8 fp
305
- bytes' <- applyMustache bytes
306
- return (dir </> path, bytes'))
307
- (M. toList files))
291
+ | otherwise = pure (bytes, S. empty)
292
+
293
+ -- Accumulate any missing keys as the file is processed
294
+ processFile mks (fpOrig, bytes) = do
295
+ -- Apply the mustache template to the filenames as well, so that we
296
+ -- can have file names depend on the project name.
297
+ (fp, mks1) <- applyMustache $ TLE. encodeUtf8 $ TL. pack fpOrig
298
+ path <- parseRelFile $ TL. unpack $ TLE. decodeUtf8 fp
299
+ (bytes', mks2) <- applyMustache bytes
300
+ return (mks <> mks1 <> mks2, (dir </> path, bytes'))
301
+
302
+ (missingKeys, results) <- mapAccumLM processFile S. empty (M. toList files)
303
+ unless (S. null missingKeys) $ do
304
+ let missingParamters = MissingParameters
305
+ project
306
+ template
307
+ missingKeys
308
+ (configUserConfigPath config)
309
+ logInfo (" \n " <> displayShow missingParamters <> " \n " )
310
+ return $ M. fromList results
308
311
where
309
312
onlyMissingKeys (Mustache. VariableNotFound ks) = map T. unpack ks
310
313
onlyMissingKeys _ = []
311
314
315
+ mapAccumLM :: Monad m => (a -> b -> m (a , c )) -> a -> [b ] -> m (a , [c ])
316
+ mapAccumLM _ a [] = return (a, [] )
317
+ mapAccumLM f a (x: xs) = do
318
+ (a', c) <- f a x
319
+ (a'', cs) <- mapAccumLM f a' xs
320
+ return (a'', c: cs)
321
+
312
322
-- | Check if we're going to overwrite any existing files.
313
323
checkForOverwrite :: (MonadIO m , MonadThrow m ) => [Path Abs File ] -> m ()
314
324
checkForOverwrite files = do
0 commit comments