on master help sign in
asset-manifest: Reduce the amount of code in the generated module
549ce6aAli Abrar
lib/asset/manifest/src-bin/static-th.hs+5−23
@@ -15,14 +15,11 @@ main = do
15 , _simplePkg_moduleName = T.pack moduleName
15 , _simplePkg_moduleName = T.pack moduleName
16 , _simplePkg_dependencies = map T.pack
16 , _simplePkg_dependencies = map T.pack
17 [ "base"
17 [ "base"
18− , "filepath"
19 , "obelisk-asset-manifest"
18 , "obelisk-asset-manifest"
20 , "template-haskell"
19 , "template-haskell"
21 ]
20 ]
22 , _simplePkg_moduleContents = T.pack $ unlines
21 , _simplePkg_moduleContents = T.pack $ unlines
23− [ "{-# Language BangPatterns #-}"
22+ [ "{-# Language CPP #-}"
24− , "{-# Language CPP #-}"
25− , "{-# Language OverloadedStrings #-}"
26 , "{-|"
23 , "{-|"
27 , " Description:"
24 , " Description:"
28 , " Automatically generated module that provides the 'static' TH function"
25 , " Automatically generated module that provides the 'static' TH function"
@@ -30,29 +27,14 @@ main = do
30 , "-}"
27 , "-}"
31 , "module " <> moduleName <> " ( static ) where"
28 , "module " <> moduleName <> " ( static ) where"
32 , ""
29 , ""
33− , "import Obelisk.Asset.Gather"
30+ , "import Obelisk.Asset.TH"
34 , "import Language.Haskell.TH"
31 , "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\""
50 , ""
32 , ""
51 , "static :: FilePath -> Q Exp"
33 , "static :: FilePath -> Q Exp"
52− , "#ifdef PASSTHRU"
34+ , "#ifdef OBELISK_ASSET_PASSTHRU"
53− , "static = assetPathRaw"
35+ , "static = staticAssetRaw"
54 , "#else"
36 , "#else"
55− , "static = assetPath " <> show root
37+ , "static = staticAssetHashed " <> show root
56 , "#endif"
38 , "#endif"
57 ]
39 ]
58 }
40 }
lib/asset/manifest/src/Obelisk/Asset/TH.hs+20−3
@@ -4,6 +4,8 @@ Description:
4-}
4-}
5module Obelisk.Asset.TH
5module Obelisk.Asset.TH
6 ( assetPath
6 ( assetPath
7+ , staticAssetRaw
8+ , staticAssetHashed
7 ) where
9 ) where
8
10
9import Obelisk.Asset.Gather
11import Obelisk.Asset.Gather
@@ -12,8 +14,23 @@ import Language.Haskell.TH
12import Language.Haskell.TH.Syntax
14import Language.Haskell.TH.Syntax
13import System.FilePath.Posix
15import System.FilePath.Posix
14
16
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+
15-- | Produces a string literal with the hashed path of the file
23-- | Produces a string literal with the hashed path of the file
16assetPath :: FilePath -> FilePath -> Q Exp
24assetPath :: FilePath -> FilePath -> Q Exp
17−assetPath root relativePath = do
25+assetPath root relativePath =
18− qAddDependentFile $ root </> relativePath
26+ LitE . StringL <$> hashedAssetFilePath root relativePath
19− LitE . StringL <$> runIO (toHashedPath 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
460
460
461 pure
461 pure
462 $ baseGhciOptions
462 $ baseGhciOptions
463− <> ["-DPASSTHRU"] -- For passthrough static assets
463+ <> ["-DOBELISK_ASSET_PASSTHRU"] -- For passthrough static assets
464 <> ["-F", "-pgmF", selfExe, "-optF", preprocessorIdentifier]
464 <> ["-F", "-pgmF", selfExe, "-optF", preprocessorIdentifier]
465 <> concatMap (\p -> ["-optF", p]) pkgFiles
465 <> concatMap (\p -> ["-optF", p]) pkgFiles
466 <> [ "-i" <> intercalate ":" (concatMap toList pkgSrcPaths) ]
466 <> [ "-i" <> intercalate ":" (concatMap toList pkgSrcPaths) ]