on 1c26925 help sign in
99 lines 4.7 kB history raw
1{-# LANGUAGE LambdaCase #-}
2{-# LANGUAGE OverloadedStrings #-}
4module Obelisk.Command.Preprocessor where
6import qualified Data.ByteString.Lazy as BL
7import qualified Data.ByteString.Lazy.Builder as BU
8import Data.Foldable (for_)
9import Data.List (intersperse, isPrefixOf, sortOn)
10import Data.Maybe (fromMaybe)
11import qualified Data.Text.Lazy.Builder as TL
12import qualified Data.Text.Lazy.Encoding as TL
13import Distribution.Compiler (CompilerFlavor (..))
14import Language.Haskell.Extension (Extension (..), Language(..))
15import System.Directory (canonicalizePath)
16import System.IO (IOMode (..), hPutStrLn, stderr, withFile)
17import System.FilePath (hasTrailingPathSeparator, joinPath, splitPath)
19import Obelisk.Command.Run (CabalPackageInfo (..), parseCabalPackage')
21-- | This code is intended to be executed via ghci's -pgmF preprocessor option.
22-- The command line arguments are passed in via ghc, which dictates the first three options and meanings
23-- In order for this code to execute, origPath must contain either a '.' character or a '/' character.
24-- (This is to avoid the possibility of the command line syntax conflicting with another ob command)
25-- We do have control over the remaining arguments, but they must be the same for all files.
26-- Thus, the fourth command line argument must be "apply-packages", which has already been handled.
27-- We assume all the remaining arguments passed in are paths to cabal or hpack package specifications.
28-- Thus we must select among the packagePaths for the file we are going to parse.
29applyPackages :: FilePath -> FilePath -> FilePath -> [FilePath] -> IO ()
30applyPackages origPath inPath outPath packagePaths' = do
31 origPathCanonical <- canonicalizePath origPath
32 packagePaths <- traverse canonicalizePath packagePaths'
34 let
35 takeDirs = takeWhile hasTrailingPathSeparator
36 packageDirs = sortOn (negate . length . takeDirs) $ map splitPath packagePaths
37 origDir = splitPath origPathCanonical
38 matches = [ joinPath d | d <- packageDirs, takeDirs d `isPrefixOf` origDir ]
40 -- The first element of matches is going to be the deepest path to a package spec that contains
41 -- our file as a subdirectory.
42 packageInfo' <- case matches of
43 [] -> do
44 hPutStrLn stderr $ "Error: Unable to find cabal information for " <> origPath <> "; Skipping preprocessor."
45 pure Nothing
46 packagePath:_ -> parseCabalPackage' packagePath >>= \case
47 Left err -> do
48 hPutStrLn stderr $ "Error: Unable to parse cabal package " <> packagePath <> "; Skipping preprocessor on " <> origPath <> ". Error: " <> show err
49 pure Nothing
50 Right (_, packageInfo) -> pure $ Just packageInfo
52 writeOutput packageInfo' inPath outPath
54writeOutput :: Maybe CabalPackageInfo -> FilePath -> FilePath -> IO ()
55writeOutput packageInfo' origPath outPath = withFile outPath WriteMode $ \hOut -> do
56 for_ packageInfo' $ \packageInfo -> hPutTextBuilder hOut (generateHeader origPath packageInfo)
57 BL.readFile origPath >>= BL.hPut hOut
58 where
59 hPutTextBuilder h = BU.hPutBuilder h . TL.encodeUtf8Builder . TL.toLazyText
61generateHeader :: FilePath -> CabalPackageInfo -> TL.Builder
62generateHeader origPath packageInfo =
63 hsExtensions <> ghcOptions <> lineNumberPragma origPath
64 where
65 hsExtensions =
66 if null extList
67 then mempty
68 else pragma $ TL.fromText "LANGUAGE " <> mconcat (intersperse (TL.fromText ", ") extList)
69 extList = addDefaultLanguage $ concatMap showExt $ _cabalPackageInfo_defaultExtensions packageInfo
70 addDefaultLanguage =
71 case _cabalPackageInfo_defaultLanguage packageInfo of
72 Nothing -> id
73 Just x -> case x of
74 UnknownLanguage ext -> (TL.fromString ext :)
75 ext -> (TL.fromString (show ext) :)
76 showExt = \case
77 EnableExtension ext -> [TL.fromString (show ext)]
78 DisableExtension _ -> []
79 UnknownExtension ext -> [TL.fromString ext]
81 ghcOptions =
82 if null optList
83 then mempty
84 else pragma $
85 TL.fromText "OPTIONS_GHC " <> mconcat (intersperse (TL.fromText " ") (map TL.fromString optList))
86 ghcOptList
87 = filter (not . isPrefixOf "-O")
88 $ fromMaybe []
89 $ lookup GHC (_cabalPackageInfo_compilerOptions packageInfo)
90 optList = _cabalPackageInfo_cppOptions packageInfo <> ghcOptList
92lineNumberPragma :: FilePath -> TL.Builder
93lineNumberPragma origPath = pragma $ TL.fromText "LINE 1 " <> quoted '"' (TL.fromString origPath)
95pragma :: TL.Builder -> TL.Builder
96pragma x = TL.fromText "{-# " <> x <> TL.fromText " #-}\n"
98quoted :: Char -> TL.Builder -> TL.Builder
99quoted char x = TL.singleton char <> x <> TL.singleton char