@@ -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
1922import 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
0 commit comments