Skip to content

Commit

Permalink
Implement commercialhaskell#3264 adding --cwd to exec
Browse files Browse the repository at this point in the history
Welcome for any suggestions as to better methods to use/docs to update.
  • Loading branch information
Khan Thompson authored and tswelsh committed Nov 7, 2017
1 parent f528850 commit f8d70ce
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/Stack/Options/ExecParser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ execOptsExtraParser = eoPlainParser <|>
<$> eoEnvSettingsParser
<*> eoPackagesParser
<*> eoRtsOptionsParser
<*> eoCwdParser
where
eoEnvSettingsParser :: Parser EnvSettings
eoEnvSettingsParser = EnvSettings
Expand Down Expand Up @@ -70,3 +71,9 @@ execOptsExtraParser = eoPlainParser <|>
eoPlainParser = flag' ExecOptsPlain
(long "plain" <>
help "Use an unmodified environment (only useful with Docker)")

eoCwdParser :: Parser (Maybe FilePath)
eoCwdParser = optional
(strOption (long "cwd"
<> help "Sets the working directory before executing")
)
1 change: 1 addition & 0 deletions src/Stack/Types/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ data ExecOptsExtra
{ eoEnvSettings :: !EnvSettings
, eoPackages :: ![String]
, eoRtsOptions :: ![String]
, eoCwd :: !(Maybe FilePath)
}
deriving (Show)

Expand Down
19 changes: 17 additions & 2 deletions src/main/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ import qualified Stack.Upload as Upload
import qualified System.Directory as D
import System.Environment (getProgName, getArgs, withArgs)
import System.Exit
import System.FilePath (pathSeparator)
import System.FilePath (isRelative, isValid, pathSeparator)
import System.IO (hIsTerminalDevice, stderr, stdin, stdout, hSetBuffering, BufferMode(..), hPutStrLn, hGetEncoding, hSetEncoding)

-- | Change the character encoding of the given Handle to transliterate
Expand Down Expand Up @@ -767,7 +767,8 @@ execCmd ExecOpts {..} go@GlobalOpts{..} =
(ExecRunGhc, args) ->
getGhcCmd "run" menv eoPackages args
munlockFile lk -- Unlock before transferring control away.
exec menv cmd args

runWithPath eoCwd $ exec menv cmd args
where
-- return the package-id of the first package in GHC_PACKAGE_PATH
getPkgId menv wc name = do
Expand All @@ -788,6 +789,20 @@ execCmd ExecOpts {..} go@GlobalOpts{..} =
pkgopts <- getPkgOpts menv wc pkgs
return (prefix ++ compilerExeName wc, pkgopts ++ args)

runWithPath path callback = case path of
Nothing -> callback
Just p | not (isValid p) -> callback
Just p ->
if isRelative p
then parseRelDir p >>= runInDirectory
else parseAbsDir p >>= runInDirectory
where
runInDirectory :: (Path t Dir) -> RIO EnvConfig ()
runInDirectory directory =
withUnliftIO $ \unlift ->
withCurrentDir directory $ unliftIO unlift callback


-- | Evaluate some haskell code inline.
evalCmd :: EvalOpts -> GlobalOpts -> IO ()
evalCmd EvalOpts {..} go@GlobalOpts {..} = execCmd execOpts go
Expand Down

0 comments on commit f8d70ce

Please sign in to comment.