Skip to content

Commit 782eda8

Browse files
author
AndrewRademacher
committed
Extracted renderLegacyGhciScriptFunction
1 parent d501da6 commit 782eda8

3 files changed

Lines changed: 91 additions & 14 deletions

File tree

src/Stack/Ghci.hs

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ module Stack.Ghci
1414
, GhciException(..)
1515
, ghciSetup
1616
, ghci
17+
18+
-- TODO: Address what should and should not be exported.
19+
, renderLegacyGhciScript
1720
) where
1821

1922
import Control.Applicative
@@ -171,20 +174,41 @@ ghci opts@GhciOpts{..} = do
171174
-- include CWD.
172175
"-i" :
173176
odir <> pkgopts <> ghciArgs <> extras)
174-
withSystemTempDir "ghci" $ \tmpDir -> do
175-
let macrosFile = tmpDir </> $(mkRelFile "cabal_macros.h")
176-
macrosOpts <- preprocessCabalMacros pkgs macrosFile
177-
if ghciNoLoadModules
178-
then execGhci macrosOpts
179-
else do
180-
let scriptPath = tmpDir </> $(mkRelFile "ghci-script")
181-
fp = toFilePath scriptPath
182-
loadModules = ":add " <> unwords (map quoteFileName modulesToLoad)
183-
addMainFile = maybe "" ((":add " <>) . quoteFileName . toFilePath) mainFile
184-
bringIntoScope = ":module + " <> unwords modulesToLoad
185-
liftIO (writeFile fp (unlines [loadModules,addMainFile,bringIntoScope]))
186-
setScriptPerms fp
187-
execGhci (macrosOpts ++ ["-ghci-script=" <> fp])
177+
178+
withSystemTempDir "ghci" $ \tmpDirectory -> do
179+
macrosOptions <- writeMacrosFile tmpDirectory pkgs
180+
if ghciNoLoadModules
181+
then execGhci macrosOptions
182+
else do
183+
scriptPath <- writeGhciScript tmpDirectory (renderLegacyGhciScript modulesToLoad mainFile)
184+
execGhci (macrosOptions ++ ["-ghci-script=" <> toFilePath scriptPath])
185+
186+
writeMacrosFile :: (MonadIO m) => Path Abs Dir -> [GhciPkgInfo] -> m [String]
187+
writeMacrosFile tmpDirectory packages = do
188+
macrosOptions <- preprocessCabalMacros packages macrosFile
189+
return macrosOptions
190+
where
191+
macrosFile = tmpDirectory </> $(mkRelFile "cabal_macros.h")
192+
193+
writeGhciScript :: (MonadIO m) => Path Abs Dir -> String -> m (Path Abs File)
194+
writeGhciScript tmpDirectory script = do
195+
liftIO $ writeFile scriptFilePath script
196+
setScriptPerms scriptFilePath
197+
return scriptPath
198+
where
199+
scriptPath = tmpDirectory </> $(mkRelFile "ghci-script")
200+
scriptFilePath = toFilePath scriptPath
201+
202+
renderLegacyGhciScript :: [String] -> Maybe (Path b t) -> String
203+
renderLegacyGhciScript modulesToLoad mainFile =
204+
let loadModules = ":add" <> case unwords (map quoteFileName modulesToLoad) of
205+
[] -> ""
206+
xs -> " " <> xs
207+
addMainFile = maybe "" ((":add " <>) . quoteFileName . toFilePath) mainFile
208+
bringIntoScope = ":module +" <> case unwords modulesToLoad of
209+
[] -> ""
210+
xs -> " " <> xs
211+
in unlines [loadModules,addMainFile,bringIntoScope]
188212

189213
-- | Figure out the main-is file to load based on the targets. Sometimes there
190214
-- is none, sometimes it's unambiguous, sometimes it's

src/test/Stack/GhciSpec.hs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{-# LANGUAGE QuasiQuotes #-}
2+
{-# LANGUAGE TemplateHaskell #-}
3+
4+
-- | Test suite for GHCi like applications including both GHCi and Intero.
5+
module Stack.GhciSpec where
6+
7+
import Data.Text (Text)
8+
import qualified Data.Text as T
9+
import Test.Hspec
10+
import NeatInterpolation
11+
import Path
12+
13+
import Stack.Ghci
14+
15+
spec :: Spec
16+
spec = do
17+
describe "GHCi" $ do
18+
describe "Script rendering" $ do
19+
it "should render legacy script when given project:exe" $ do
20+
renderLegacyGhciScript [] (Just $(mkAbsFile "/Users/someone/src/project-a/exe/Main.hs"))
21+
`shouldBe` T.unpack ghciScript_projectWithMain
22+
23+
it "should render legacy script when given project" $ do
24+
renderLegacyGhciScript ["Lib.A"] (Just $(mkAbsFile "/Users/someone/src/project-a/exe/Main.hs"))
25+
`shouldBe` T.unpack ghciScript_projectWithLibAndMain
26+
27+
it "should render legacy script when given multiple project:lib" $ do
28+
renderLegacyGhciScript ["Lib.A", "Lib.B"] Nothing
29+
`shouldBe` T.unpack ghciScript_multipleProjectsWithLib
30+
31+
-- Exptected GHCi scripts
32+
33+
ghciScript_projectWithMain :: Text
34+
ghciScript_projectWithMain = [text|
35+
:add
36+
:add /Users/someone/src/project-a/exe/Main.hs
37+
:module +
38+
|]
39+
40+
ghciScript_projectWithLibAndMain :: Text
41+
ghciScript_projectWithLibAndMain = [text|
42+
:add Lib.A
43+
:add /Users/someone/src/project-a/exe/Main.hs
44+
:module + Lib.A
45+
|]
46+
47+
ghciScript_multipleProjectsWithLib :: Text
48+
ghciScript_multipleProjectsWithLib = [text|
49+
:add Lib.A Lib.B
50+
51+
:module + Lib.A Lib.B
52+
|]

stack.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ test-suite stack-test
306306
, hspec <2.3
307307
, http-conduit
308308
, monad-logger
309+
, neat-interpolation
309310
, path >= 0.5.7
310311
, path-io >= 1.1.0 && < 2.0.0
311312
, resourcet

0 commit comments

Comments
 (0)