-- | Get all of the dependencies for a given package, including guessed build
-- tool dependencies.
packageDepsWithTools :: Package -> M (Map PackageName VersionRange)
packageDepsWithTools p = do
    ctx <- ask
    -- TODO: it would be cool to defer these warnings until there's an
    -- actual issue building the package.
    let toEither name mp =
            case Map.toList mp of
                [] -> Left (ToolWarning name (packageName p) Nothing)
                [_] -> Right mp
                ((x, _):(y, _):zs) ->
                  Left (ToolWarning name (packageName p) (Just (x, y, map fst zs)))
        (warnings0, toolDeps) =
             partitionEithers $
             map (\dep -> toEither dep (toolToPackages ctx dep)) (Map.keys (packageTools p))
    -- Check whether the tool is on the PATH before warning about it.
    warnings <- fmap catMaybes $ forM warnings0 $ \warning@(ToolWarning (ExeName toolName) _ _) -> do
        config <- view configL
        menv <- liftIO $ configEnvOverride config minimalEnvSettings { esIncludeLocals = True }
        mfound <- findExecutable menv $ T.unpack toolName
        case mfound of
            Nothing -> return (Just warning)
            Just _ -> return Nothing
    tell mempty { wWarnings = (map toolWarningText warnings ++) }
    return $ Map.unionsWith intersectVersionRanges
           $ packageDeps p
           : toolDeps

-- | Warn about tools in the snapshot definition. States the tool name
-- expected, the package name using it, and found packages. If the
-- last value is Nothing, it means the tool was not found
-- anywhere. For a Just value, it was found in at least two packages.
packageDepsWithTools :: Package -> M (Map PackageName VersionRange)
packageDepsWithTools p = do
    ctx <- ask
    -- TODO: it would be cool to defer these warnings until there's an
    -- actual issue building the package.
    let toEither name mp =
            case Map.toList mp of
                [] -> Left (ToolWarning name (packageName p) Nothing)
                [_] -> Right mp
                ((x, _):(y, _):zs) ->
                  Left (ToolWarning name (packageName p) (Just (x, y, map fst zs)))
        (warnings0, toolDeps) =
             partitionEithers $
             map (\dep -> toEither dep (toolToPackages ctx dep)) (Map.keys (packageTools p))
    -- Check whether the tool is on the PATH before warning about it.
    warnings <- fmap catMaybes $ forM warnings0 $ \warning@(ToolWarning (ExeName toolName) _ _) -> do
        config <- view configL
        menv <- liftIO $ configEnvOverride config minimalEnvSettings { esIncludeLocals = True }
        mfound <- findExecutable menv $ T.unpack toolName
        case mfound of
            Nothing -> return (Just warning)
            Just _ -> return Nothing
    tell mempty { wWarnings = (map toolWarningText warnings ++) }
    return $ Map.unionsWith intersectVersionRanges
           $ packageDeps p
           : toolDeps

-- | Warn about tools in the snapshot definition. States the tool name
-- expected, the package name using it, and found packages. If the
-- last value is Nothing, it means the tool was not found
-- anywhere. For a Just value, it was found in at least two packages.
packageDepsWithTools p = do
    ctx <- ask
    -- TODO: it would be cool to defer these warnings until there's an
    -- actual issue building the package.
    let toEither name mp =
            case Map.toList mp of
                [] -> Left (ToolWarning name (packageName p) Nothing)
                [_] -> Right mp
                ((x, _):(y, _):zs) ->
                  Left (ToolWarning name (packageName p) (Just (x, y, map fst zs)))
        (warnings0, toolDeps) =
             partitionEithers $
             map (\dep -> toEither dep (toolToPackages ctx dep)) (Map.keys (packageTools p))
    -- Check whether the tool is on the PATH before warning about it.
    warnings <- fmap catMaybes $ forM warnings0 $ \warning@(ToolWarning (ExeName toolName) _ _) -> do
        config <- view configL
        menv <- liftIO $ configEnvOverride config minimalEnvSettings { esIncludeLocals = True }
        mfound <- findExecutable menv $ T.unpack toolName
        case mfound of
            Nothing -> return (Just warning)
            Just _ -> return Nothing
    tell mempty { wWarnings = (map toolWarningText warnings ++) }
    return $ Map.unionsWith intersectVersionRanges
           $ packageDeps p
           : toolDeps

-- | Warn about tools in the snapshot definition. States the tool name
-- expected, the package name using it, and found packages. If the
-- last value is Nothing, it means the tool was not found
-- anywhere. For a Just value, it was found in at least two packages.