-
Notifications
You must be signed in to change notification settings - Fork 843
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2457 from AndrewRademacher/refactoring-ghci-support
Adding support for rendering GHCi scripts targeting different GHCi like applications.
- Loading branch information
Showing
15 changed files
with
673 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module Stack.Ghci.Script | ||
( GhciScript | ||
, ModuleName | ||
|
||
, cmdAdd | ||
, cmdAddFile | ||
, cmdCdGhc | ||
, cmdModule | ||
|
||
, scriptToLazyByteString | ||
, scriptToBuilder | ||
, scriptToFile | ||
) where | ||
|
||
import Control.Exception | ||
import Data.ByteString.Lazy (ByteString) | ||
import Data.ByteString.Builder | ||
import Data.Monoid | ||
import Data.List | ||
import Data.Set (Set) | ||
import qualified Data.Set as S | ||
import Data.Text (Text) | ||
import Data.Text.Encoding (encodeUtf8Builder) | ||
import Path | ||
import System.IO | ||
|
||
import Distribution.ModuleName hiding (toFilePath) | ||
|
||
newtype GhciScript = GhciScript { unGhciScript :: [GhciCommand] } | ||
|
||
instance Monoid GhciScript where | ||
mempty = GhciScript [] | ||
(GhciScript xs) `mappend` (GhciScript ys) = GhciScript (ys <> xs) | ||
|
||
data GhciCommand | ||
= Add (Set ModuleName) | ||
| AddFile (Path Abs File) | ||
| CdGhc (Path Abs Dir) | ||
| Module (Set ModuleName) | ||
deriving (Show) | ||
|
||
cmdAdd :: Set ModuleName -> GhciScript | ||
cmdAdd = GhciScript . (:[]) . Add | ||
|
||
cmdAddFile :: Path Abs File -> GhciScript | ||
cmdAddFile = GhciScript . (:[]) . AddFile | ||
|
||
cmdCdGhc :: Path Abs Dir -> GhciScript | ||
cmdCdGhc = GhciScript . (:[]) . CdGhc | ||
|
||
cmdModule :: Set ModuleName -> GhciScript | ||
cmdModule = GhciScript . (:[]) . Module | ||
|
||
scriptToLazyByteString :: GhciScript -> ByteString | ||
scriptToLazyByteString = toLazyByteString . scriptToBuilder | ||
|
||
scriptToBuilder :: GhciScript -> Builder | ||
scriptToBuilder backwardScript = mconcat $ fmap commandToBuilder script | ||
where | ||
script = reverse $ unGhciScript backwardScript | ||
|
||
scriptToFile :: Path Abs File -> GhciScript -> IO () | ||
scriptToFile path script = | ||
bracket (openFile filepath WriteMode) hClose | ||
$ \hdl -> do hSetBuffering hdl (BlockBuffering Nothing) | ||
hSetBinaryMode hdl True | ||
hPutBuilder hdl (scriptToBuilder script) | ||
where | ||
filepath = toFilePath path | ||
|
||
-- Command conversion | ||
|
||
fromText :: Text -> Builder | ||
fromText = encodeUtf8Builder | ||
|
||
commandToBuilder :: GhciCommand -> Builder | ||
|
||
commandToBuilder (Add modules) | ||
| S.null modules = mempty | ||
| otherwise = | ||
fromText ":add " | ||
<> (mconcat $ intersperse (fromText " ") | ||
$ fmap (stringUtf8 . quoteFileName . mconcat . intersperse "." . components) | ||
$ S.toAscList modules) | ||
<> fromText "\n" | ||
|
||
commandToBuilder (AddFile path) = | ||
fromText ":add " <> stringUtf8 (quoteFileName (toFilePath path)) <> fromText "\n" | ||
|
||
commandToBuilder (CdGhc path) = | ||
fromText ":cd-ghc " <> stringUtf8 (quoteFileName (toFilePath path)) <> fromText "\n" | ||
|
||
commandToBuilder (Module modules) | ||
| S.null modules = fromText ":module +\n" | ||
| otherwise = | ||
fromText ":module + " | ||
<> (mconcat $ intersperse (fromText " ") | ||
$ fmap (stringUtf8 . quoteFileName . mconcat . intersperse "." . components) | ||
$ S.toAscList modules) | ||
<> fromText "\n" | ||
|
||
-- | Make sure that a filename with spaces in it gets the proper quotes. | ||
quoteFileName :: String -> String | ||
quoteFileName x = | ||
if any (==' ') x | ||
then show x | ||
else x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.