on master help sign in
asset-manifest: Reduce the amount of code in the generated module
commit549ce6ac1bf4d9924435c0287f61c7ca94874d1d authorAli Abrar <aliabrar@gmail.com> dateFri 18 Dec 2020, 00:13 (6 years ago) parent281195a
3 files changed+26−27
lib/asset/manifest/src-bin/static-th.hs+5−23 lib/asset/manifest/src/Obelisk/Asset/TH.hs+20−3 lib/command/src/Obelisk/Command/Run.hs+1−1
lib/asset/manifest/src-bin/static-th.hs+5−23
@@ -15,14 +15,11 @@ main = do
1515 , _simplePkg_moduleName = T.pack moduleName
1616 , _simplePkg_dependencies = map T.pack
1717 [ "base"
18− , "filepath"
1918 , "obelisk-asset-manifest"
2019 , "template-haskell"
2120 ]
2221 , _simplePkg_moduleContents = T.pack $ unlines
23− [ "{-# Language BangPatterns #-}"
24− , "{-# Language CPP #-}"
25− , "{-# Language OverloadedStrings #-}"
22+ [ "{-# Language CPP #-}"
2623 , "{-|"
2724 , " Description:"
2825 , " Automatically generated module that provides the 'static' TH function"
@@ -30,29 +27,14 @@ main = do
3027 , "-}"
3128 , "module " <> moduleName <> " ( static ) where"
3229 , ""
33− , "import Obelisk.Asset.Gather"
30+ , "import Obelisk.Asset.TH"
3431 , "import Language.Haskell.TH"
35− , "import Language.Haskell.TH.Syntax"
36− , "import System.FilePath"
37− , ""
38− , "-- | Produces a string literal with the hashed path of the file"
39− , "assetPath :: FilePath -> FilePath -> Q Exp"
40− , "assetPath root relativePath = do"
41− , " qAddDependentFile $ root </> relativePath"
42− , " LitE . StringL . (prefix</>) <$> runIO (toHashedPath root relativePath)"
43− , ""
44− , "-- | Produces a string literal with the raw (i.e., unhashed) path of the file"
45− , "assetPathRaw :: FilePath -> Q Exp"
46− , "assetPathRaw fp = returnQ $ LitE $ StringL $ prefix </> fp"
47− , ""
48− , "prefix :: FilePath"
49− , "prefix = \"/static\""
5032 , ""
5133 , "static :: FilePath -> Q Exp"
52− , "#ifdef PASSTHRU"
53− , "static = assetPathRaw"
34+ , "#ifdef OBELISK_ASSET_PASSTHRU"
35+ , "static = staticAssetRaw"
5436 , "#else"
55− , "static = assetPath " <> show root
37+ , "static = staticAssetHashed " <> show root
5638 , "#endif"
5739 ]
5840 }
lib/asset/manifest/src/Obelisk/Asset/TH.hs+20−3
@@ -4,6 +4,8 @@ Description:
44-}
55module Obelisk.Asset.TH
66 ( assetPath
7+ , staticAssetRaw
8+ , staticAssetHashed
79 ) where
810
911import Obelisk.Asset.Gather
@@ -12,8 +14,23 @@ import Language.Haskell.TH
1214import Language.Haskell.TH.Syntax
1315import System.FilePath.Posix
1416
17+-- | Produces the hashed path of a file
18+hashedAssetFilePath :: FilePath -> FilePath -> Q FilePath
19+hashedAssetFilePath root relativePath = do
20+ qAddDependentFile $ root </> relativePath
21+ runIO (toHashedPath root relativePath)
22+
1523-- | Produces a string literal with the hashed path of the file
1624assetPath :: FilePath -> FilePath -> Q Exp
17−assetPath root relativePath = do
18− qAddDependentFile $ root </> relativePath
19− LitE . StringL <$> runIO (toHashedPath root relativePath)
25+assetPath root relativePath =
26+ LitE . StringL <$> hashedAssetFilePath root relativePath
27+
28+staticPrefix :: FilePath
29+staticPrefix = "/static"
30+
31+staticAssetRaw :: FilePath -> Q Exp
32+staticAssetRaw fp = returnQ $ LitE $ StringL $ staticPrefix </> fp
33+
34+staticAssetHashed :: FilePath -> FilePath -> Q Exp
35+staticAssetHashed root fp = do
36+ LitE . StringL . (staticPrefix </>) <$> hashedAssetFilePath root fp
lib/command/src/Obelisk/Command/Run.hs+1−1
@@ -460,7 +460,7 @@ getGhciSessionSettings (toList -> packageInfos) pathBase useRelativePaths = do
460460
461461 pure
462462 $ baseGhciOptions
463− <> ["-DPASSTHRU"] -- For passthrough static assets
463+ <> ["-DOBELISK_ASSET_PASSTHRU"] -- For passthrough static assets
464464 <> ["-F", "-pgmF", selfExe, "-optF", preprocessorIdentifier]
465465 <> concatMap (\p -> ["-optF", p]) pkgFiles
466466 <> [ "-i" <> intercalate ":" (concatMap toList pkgSrcPaths) ]