Skip to content

Commit 0d511e6

Browse files
author
Khan Thompson
committed
Implement #3264 adding --cwd to exec
Welcome for any suggestions as to better methods to use/docs to update.
1 parent baf5b29 commit 0d511e6

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

src/Stack/Options/ExecParser.hs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ execOptsExtraParser = eoPlainParser <|>
4343
<$> eoEnvSettingsParser
4444
<*> eoPackagesParser
4545
<*> eoRtsOptionsParser
46+
<*> eoCwdParser
4647
where
4748
eoEnvSettingsParser :: Parser EnvSettings
4849
eoEnvSettingsParser = EnvSettings
@@ -70,3 +71,9 @@ execOptsExtraParser = eoPlainParser <|>
7071
eoPlainParser = flag' ExecOptsPlain
7172
(long "plain" <>
7273
help "Use an unmodified environment (only useful with Docker)")
74+
75+
eoCwdParser :: Parser (Maybe FilePath)
76+
eoCwdParser = optional
77+
(strOption (long "cwd"
78+
<> help "Sets the working directory before executing")
79+
)

src/Stack/Types/Config.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ data ExecOptsExtra
430430
{ eoEnvSettings :: !EnvSettings
431431
, eoPackages :: ![String]
432432
, eoRtsOptions :: ![String]
433+
, eoCwd :: !(Maybe FilePath)
433434
}
434435
deriving (Show)
435436

src/main/Main.hs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ import qualified Stack.Upload as Upload
105105
import qualified System.Directory as D
106106
import System.Environment (getProgName, getArgs, withArgs)
107107
import System.Exit
108-
import System.FilePath (pathSeparator)
108+
import System.FilePath (isRelative, isValid, pathSeparator)
109109
import System.IO (hIsTerminalDevice, stderr, stdin, stdout, hSetBuffering, BufferMode(..), hPutStrLn, hGetEncoding, hSetEncoding)
110110

111111
-- | Change the character encoding of the given Handle to transliterate
@@ -767,7 +767,8 @@ execCmd ExecOpts {..} go@GlobalOpts{..} =
767767
(ExecRunGhc, args) ->
768768
getGhcCmd "run" menv eoPackages args
769769
munlockFile lk -- Unlock before transferring control away.
770-
exec menv cmd args
770+
771+
runWithPath eoCwd $ exec menv cmd args
771772
where
772773
-- return the package-id of the first package in GHC_PACKAGE_PATH
773774
getPkgId menv wc name = do
@@ -788,6 +789,20 @@ execCmd ExecOpts {..} go@GlobalOpts{..} =
788789
pkgopts <- getPkgOpts menv wc pkgs
789790
return (prefix ++ compilerExeName wc, pkgopts ++ args)
790791

792+
runWithPath path callback = case path of
793+
Nothing -> callback
794+
Just p | not (isValid p) -> callback
795+
Just p ->
796+
if isRelative p
797+
then parseRelDir p >>= runInDirectory
798+
else parseAbsDir p >>= runInDirectory
799+
where
800+
runInDirectory :: (Path t Dir) -> RIO EnvConfig ()
801+
runInDirectory directory =
802+
withUnliftIO $ \unlift ->
803+
withCurrentDir directory $ unliftIO unlift callback
804+
805+
791806
-- | Evaluate some haskell code inline.
792807
evalCmd :: EvalOpts -> GlobalOpts -> IO ()
793808
evalCmd EvalOpts {..} go@GlobalOpts {..} = execCmd execOpts go

0 commit comments

Comments
 (0)