@@ -1,12 +1,16 @@
1+{-# LANGUAGE LambdaCase #-}
12{-# LANGUAGE OverloadedStrings #-}
23
34module Obelisk.Command.Preprocessor where
45
56import qualified Data.ByteString as BS
67import qualified Data.ByteString.Lazy as BL
8+import qualified Data.ByteString.Lazy.Builder as BU
79import Data.List
10+import Data.Maybe
811import qualified Data.Text.Lazy as TL
912import qualified Data.Text.Lazy.Builder as TL
13+import qualified Data.Text.Lazy.Encoding as TL
1014import Distribution.Compiler
1115import Language.Haskell.Extension
1216import System.Environment
@@ -14,6 +18,7 @@ import System.IO
1418import System.FilePath
1519
1620import Obelisk.App
21+import Obelisk.Command (mkObeliskConfig)
1722import Obelisk.Command.Run
1823
1924main :: IO ()
@@ -26,8 +31,8 @@ main = do
2631
2732 args@(origPath:inPath:outPath:packagePaths) <- getArgs
2833
29− outFile <- hOpen outPath WriteMode
30− let hPutTextBuilder h = BL.hPutBuilder h . TL.encodeUtf8Builder . TL.toLazyText
34+ outFile <- openFile outPath WriteMode
35+ let hPutTextBuilder h = BU.hPutBuilder h . TL.encodeUtf8Builder . TL.toLazyText
3136
3237 putStr "--------------------------------------------------------------------------------\n"
3338 print args
@@ -35,18 +40,20 @@ main = do
3540
3641 -- Thus we must select among the packagePaths for the file we are going to parse.
3742
38− let takeDirs = takeWhile hasTrailingPathSeperator
43+ let takeDirs = takeWhile hasTrailingPathSeparator
3944 packageDirs = sortOn (negate . length . takeDirs) $ map (splitPath . normalise) packagePaths
4045 origDir = splitPath $ normalise $ origPath
41− matches = [ d | d <- packageDirs, takeDirs d `isPrefixOf` origDir ]
46+ matches = [ joinPath d | d <- packageDirs, takeDirs d `isPrefixOf` origDir ]
4247
4348 -- So the first element of matches is going to be the deepest path to a package spec that contains
4449 -- our file as a subdirectory.
4550
51+ config <- mkObeliskConfig
52+
4653 case matches of
4754 [] -> hPutTextBuilder outFile (lineNumberPragma origPath) -- TODO: probably should produce a warning
4855 (packagePath:_) -> do
49− runObelisk (ObeliskT mempty) (parseCabalPackage packagePath) >>= \case
56+ runObelisk config (parseCabalPackage packagePath) >>= \case
5057 Just packageInfo -> do
5158 hPutTextBuilder outFile (generateHeader origPath packageInfo)
5259
@@ -74,7 +81,7 @@ generateHeader origPath packageInfo =
7481
7582 ghcOptions =
7683 if not (null optList)
77− then TL.fromText "{-# OPTIONS_GHC " <>
84+ then TL.fromText "{-# OPTIONS_GHC "
7885 <> mconcat (intersperse (TL.fromText " ") optList)
7986 <> TL.fromText " #-}\n"
8087 else mempty