Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional warnings for 'cabal run' #2510

Merged
merged 3 commits into from Apr 1, 2015
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
74 changes: 57 additions & 17 deletions cabal-install/Distribution/Client/Run.hs
Expand Up @@ -14,7 +14,10 @@ module Distribution.Client.Run ( run, splitRunArgs )
import Distribution.Client.Utils (tryCanonicalizePath)

import Distribution.PackageDescription (Executable (..),
PackageDescription (..))
TestSuite(..),
Benchmark(..),
PackageDescription (..),
BuildInfo(buildable))
import Distribution.Simple.Compiler (compilerFlavor, CompilerFlavor(..))
import Distribution.Simple.Build.PathsModule (pkgPathEnvVar)
import Distribution.Simple.BuildPaths (exeExtension)
Expand All @@ -25,7 +28,7 @@ import Distribution.Simple.LocalBuildInfo (ComponentName (..),
import Distribution.Simple.Utils (die, notice, rawSystemExitWithEnv,
addLibraryPath)
import Distribution.System (Platform (..))
import Distribution.Verbosity (Verbosity)
import Distribution.Verbosity (Verbosity, normal)

import qualified Distribution.Simple.GHCJS as GHCJS

Expand All @@ -42,23 +45,60 @@ import System.FilePath ((<.>), (</>))
-- forwarded to it.
splitRunArgs :: LocalBuildInfo -> [String] -> IO (Executable, [String])
splitRunArgs lbi args =
case exes of
[] -> die "Couldn't find any executables."
[exe] -> case args of
[] -> return (exe, [])
(x:xs) | x == exeName exe -> return (exe, xs)
| otherwise -> return (exe, args)
_ -> case args of
[] -> die $ "This package contains multiple executables. "
++ "You must pass the executable name as the first argument "
++ "to 'cabal run'."
(x:xs) -> case find (\exe -> exeName exe == x) exes of
Nothing -> die $ "No executable named '" ++ x ++ "'."
Just exe -> return (exe, xs)
case whichExecutable of -- Either err (wasManuallyChosen, exe, restParams)
Left err -> do printMaybeWarningWithAdditional ""
die err
Right (True, exe, xs) -> return (exe, xs)
Right (False, exe, xs) -> do printMaybeWarningWithAdditional
$ " Interpreting all parameters to `run`"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should use a trailing whitespace in printMaybeWarningWithAdditional instead of a prefix whitespace here (since this can be only message that gets printed).

++ " as a parameter to the"
++ " default executable."
return (exe, xs)
where
pkg_descr = localPkgDescr lbi
exes = executables pkg_descr

whichExecutable :: Either String -- error string
( Bool -- if it was manually chosen
, Executable -- the executable
, [String] -- the remaining parameters
)
whichExecutable = case (enabledExes, args) of
([] , _) -> Left "Couldn't find any enabled executables."
([exe], []) -> return (False, exe, [])
([exe], (x:xs))
| x == exeName exe -> return (True, exe, xs)
| otherwise -> return (False, exe, args)
(_ , []) -> Left
$ "This package contains multiple executables. "
++ "You must pass the executable name as the first argument "
++ "to 'cabal run'."
(_ , (x:xs)) ->
case find (\exe -> exeName exe == x) enabledExes of
Nothing -> Left $ "No executable named '" ++ x ++ "'."
Just exe -> return (True, exe, xs)
where
enabledExes = filter (buildable . buildInfo) (executables pkg_descr)
printMaybeWarningWithAdditional :: String -> IO ()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's simpler to just make this Maybe String and then use e.g. notice $ concat (maybeToList mbWarn ++ [myWarn]).

printMaybeWarningWithAdditional add = case firstParamComponentWarning of
Nothing -> return ()
Just warning -> notice normal $ warning ++ add
where
firstParamComponentWarning = case args of
[] -> Nothing
(x:_) -> lookup x components
components :: [(String, String)] -- component name, label
components = [ (name, "The executable '" ++ name ++ "' is disabled.")
| e <- executables pkg_descr
, let name = exeName e]
++ [ (name, "There is a test-suite '" ++ name ++ "',"
++ " but the `run` command is only for"
++ " executables.")
| t <- testSuites pkg_descr
, let name = testName t]
++ [ (name, "There is a benchmark '" ++ name ++ "',"
++ " but the run command is only for"
++ " executables.")
| b <- benchmarks pkg_descr
, let name = benchmarkName b]

-- | Run a given executable.
run :: Verbosity -> LocalBuildInfo -> Executable -> [String] -> IO ()
Expand Down