Skip to content

Commit

Permalink
Merge pull request #903 from commercialhaskell/806-exec-help-hack
Browse files Browse the repository at this point in the history
Hacky workaround for optparse-applicative issue with stack exec --help
  • Loading branch information
borsboom committed Sep 2, 2015
2 parents 1c2db32 + d36a58a commit c17c5a6
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
2 changes: 2 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Other enhancements:

Bug fixes:

* Hacky workaround for optparse-applicative issue with `stack exec --help` [#806](https://github.com/commercialhaskell/stack/issues/806)

## 0.1.4.0

Major changes:
Expand Down
11 changes: 6 additions & 5 deletions src/Stack/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -445,18 +445,19 @@ execOptsParser :: Maybe String -- ^ command
-> Parser ExecOpts
execOptsParser mcmd =
ExecOpts
<$> maybe eoCmdParser pure mcmd
<$> pure mcmd
<*> eoArgsParser
<*> (eoPlainParser <|>
ExecOptsEmbellished
<$> eoEnvSettingsParser
<*> eoPackagesParser)
where
eoCmdParser :: Parser String
eoCmdParser = strArgument (metavar "CMD")

eoArgsParser :: Parser [String]
eoArgsParser = many (strArgument (metavar "-- ARGS (e.g. stack ghc -- X.hs -o x)"))
eoArgsParser = many (strArgument (metavar meta))
where
meta =
(maybe ("CMD ") (const "") mcmd) ++
"-- ARGS (e.g. stack ghc -- X.hs -o x)"

eoEnvSettingsParser :: Parser EnvSettings
eoEnvSettingsParser = EnvSettings
Expand Down
5 changes: 4 additions & 1 deletion src/Stack/Types/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,10 @@ data EnvSettings = EnvSettings
deriving (Show, Eq, Ord)

data ExecOpts = ExecOpts
{ eoCmd :: !String
{ eoCmd :: !(Maybe String)
-- ^ Usage of @Maybe@ here is nothing more than a hack, to avoid some weird
-- bug in optparse-applicative. See:
-- https://github.com/commercialhaskell/stack/issues/806
, eoArgs :: ![String]
, eoExtra :: !ExecOptsExtra
}
Expand Down
13 changes: 9 additions & 4 deletions src/main/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -769,19 +769,24 @@ sdistCmd dirs go =

-- | Execute a command.
execCmd :: ExecOpts -> GlobalOpts -> IO ()
execCmd ExecOpts {..} go@GlobalOpts{..} =
execCmd ExecOpts {..} go@GlobalOpts{..} = do
(cmd, args) <-
case (eoCmd, eoArgs) of
(Just cmd, args) -> return (cmd, args)
(Nothing, cmd:args) -> return (cmd, args)
(Nothing, []) -> error "You must provide a command to exec, e.g. 'stack exec echo Hello World'"
case eoExtra of
ExecOptsPlain -> do
(manager,lc) <- liftIO $ loadConfigWithOpts go
withUserFileLock (configStackRoot $ lcConfig lc) $ \lk ->
runStackTGlobal manager (lcConfig lc) go $
Docker.execWithOptionalContainer
(lcProjectRoot lc)
(return (eoCmd, eoArgs, [], id))
(return (cmd, args, [], id))
-- Unlock before transferring control away, whether using docker or not:
(Just $ liftIO $ unlockFile lk)
(runStackTGlobal manager (lcConfig lc) go $ do
exec plainEnvSettings eoCmd eoArgs)
exec plainEnvSettings cmd args)
Nothing
Nothing -- Unlocked already above.
ExecOptsEmbellished {..} ->
Expand All @@ -792,7 +797,7 @@ execCmd ExecOpts {..} go@GlobalOpts{..} =
{ boptsTargets = map T.pack targets
}
liftIO $ unlockFile lk -- Unlock before transferring control away.
exec eoEnvSettings eoCmd eoArgs
exec eoEnvSettings cmd args

-- | Run GHCi in the context of a project.
ghciCmd :: GhciOpts -> GlobalOpts -> IO ()
Expand Down

0 comments on commit c17c5a6

Please # to comment.