Skip to content

Commit

Permalink
Use file-embed package to load resources. HeinrichApfelmus#148
Browse files Browse the repository at this point in the history
The use of `makeRelativeToProject` might make the
library easier to install with stack in some cases.

Do not use QuasiQuotes, but ordinary TemplateHaskell.
  • Loading branch information
HeinrichApfelmus authored and massudaw committed Jun 3, 2018
1 parent e39b447 commit 6cf291b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 36 deletions.
31 changes: 7 additions & 24 deletions src/Foreign/JavaScript/Include.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,17 @@
module Foreign.JavaScript.Include (include) where

import Data.Functor
import qualified Language.Haskell.TH as TH
import Language.Haskell.TH.Quote
import Data.FileEmbed (makeRelativeToProject)
import Language.Haskell.TH
import System.IO
import System.Environment(lookupEnv)
import Data.Maybe (fromMaybe)


#if defined(SAMPLES)
root :: FilePath
root = ".." -- we are running the examples from ghci
#else
root :: FilePath
root = "."
#endif

include :: QuasiQuoter
include = QuasiQuoter
{ quoteExp = f -- only used as an expression,
, quotePat = undefined -- hence all other use cases undefined
, quoteType = undefined
, quoteDec = undefined
}
where
f s = TH.LitE . TH.StringL <$> TH.runIO (readFileUTF8 s)
include :: FilePath -> Q Exp
include path = do
path <- makeRelativeToProject path
LitE . StringL <$> runIO (readFileUTF8 path)

readFileUTF8 :: FilePath -> IO String
readFileUTF8 path = do
env <- lookupEnv "THREEPENNY_ROOT"
h <- openFile (fromMaybe root env ++ "/" ++ path) ReadMode
h <- openFile path ReadMode
hSetEncoding h utf8
hGetContents h
25 changes: 13 additions & 12 deletions src/Foreign/JavaScript/Resources.hs
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
module Foreign.JavaScript.Resources where

import Data.Text (Text)
import qualified Data.Text as Text
import Foreign.JavaScript.Include

jsDriverCode :: Maybe String -> Text
jsDriverCode dict = Text.unlines $ map Text.pack
[ [include|js/lib/jquery.js|]
, [include|js/lib/jquery-cookie.js|]
, "var Haskell = { };" ++ maybe "" (\dict -> "dict = \""++ dict++ "\";") dict
, [include|js/comm.js|]
, [include|js/ffi.js|]
, [include|js/lib.js|]
, [include|js/log.js|]

jsDriverCode :: Text
jsDriverCode = Text.unlines $ map Text.pack
[ $(include "js/lib/jquery.js")
, $(include "js/lib/jquery-cookie.js")
, "var Haskell = {};" ++ maybe "" (\dict -> "dict = \""++ dict++ "\";") dict
, $(include "js/comm.js")
, $(include "js/ffi.js")
, $(include "js/lib.js")
, $(include "js/log.js")
]

cssDriverCode :: Text
cssDriverCode = Text.pack [include|js/haskell.css|]
cssDriverCode = Text.pack $(include "js/haskell.css")

defaultHtmlFile :: Text
defaultHtmlFile = Text.pack [include|js/index.html|]
defaultHtmlFile = Text.pack $(include "js/index.html")
1 change: 1 addition & 0 deletions threepenny-gui.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ Library
,deepseq >= 1.3.0 && < 1.5
,exceptions >= 0.6 && < 0.9
,filepath >= 1.3.0 && < 1.5.0
,file-embed == 0.0.10
,hashable >= 1.1.0 && < 1.3
,safe == 0.3.*
,snap-server >= 0.9.0 && < 1.1
Expand Down

0 comments on commit 6cf291b

Please sign in to comment.