Skip to content

Commit 53df9e0

Browse files
committed
Switch from 'hastache' to 'mustache' for rendering templates
hastache is deprecated and does not support GHC 8.4
1 parent 8732a21 commit 53df9e0

File tree

3 files changed

+25
-27
lines changed

3 files changed

+25
-27
lines changed

package.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ dependencies:
6060
- generic-deriving
6161
- hackage-security
6262
- hashable
63-
- hastache
6463
- hpack
6564
- hpc
6665
- http-client
@@ -74,6 +73,7 @@ dependencies:
7473
- monad-logger
7574
- mono-traversable
7675
- mtl
76+
- mustache
7777
- neat-interpolation
7878
- network-uri
7979
- open-browser

src/Stack/Coverage.hs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ import Stack.Types.Version
4545
import System.FilePath (isPathSeparator)
4646
import qualified RIO
4747
import RIO.Process
48-
import Text.Hastache (htmlEscape)
4948
import Trace.Hpc.Tix
5049
import Web.Browser (openBrowser)
5150

@@ -406,6 +405,18 @@ generateHpcErrorReport dir err = do
406405
pathToHtml :: Path b t -> Text
407406
pathToHtml = T.dropWhileEnd (=='/') . sanitize . toFilePath
408407

408+
-- | Escape HTML symbols (copied from Text.Hastache)
409+
htmlEscape :: LT.Text -> LT.Text
410+
htmlEscape = LT.concatMap proc_
411+
where
412+
proc_ '&' = "&"
413+
proc_ '\\' = "\"
414+
proc_ '"' = """
415+
proc_ '\'' = "'"
416+
proc_ '<' = "&lt;"
417+
proc_ '>' = "&gt;"
418+
proc_ h = LT.singleton h
419+
409420
sanitize :: String -> Text
410421
sanitize = LT.toStrict . htmlEscape . LT.pack
411422

src/Stack/New.hs

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import qualified Data.Text as T
3434
import qualified Data.Text.Encoding as T
3535
import qualified Data.Text.Encoding.Error as T (lenientDecode)
3636
import qualified Data.Text.IO as T
37-
import qualified Data.Text.Lazy as LT
3837
import Data.Time.Calendar
3938
import Data.Time.Clock
4039
import qualified Data.Yaml as Yaml
@@ -48,8 +47,8 @@ import Stack.Types.Config
4847
import Stack.Types.PackageName
4948
import Stack.Types.TemplateName
5049
import 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
5352
import Text.Printf
5453
import 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.
236223
checkForOverwrite :: (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

Comments
 (0)