installAction :: FilePath -> Bool -> Release -> Action ()
installAction depFile incExtras hpRel = do
    let pkg = extractPackage depFile
    let srcDir = packageSourceDir pkg
    need [ dir srcDir ]

    (Exit _, Stdout out, Stderr err) <- localCommand [Cwd srcDir]
        "cabal" cabalInstallArgs
    case decode out of
        Right deps -> writeFileLinesChanged depFile deps
        Left msg -> error $ "Error computing dependencies of "
                            ++ show pkg ++ ":\n"
                            ++ msg ++ "\n"
                            ++ err
  where
    cabalInstallArgs =
        [ "install"
        , "--dry-run"
        , "--only-dependencies"
        , "--package-db=clear"
        , "--package-db=global"
        ] ++ map ("--constraint=" ++) constraints

    constraints =
        map (\p -> pkgName p ++ "==" ++ showVersion (pkgVersion p)) $
            (allPackages incExtras) hpRel

    decode out = case drop 1 $ lines out of
        ("All the requested packages are already installed:":_) ->
            Right []
        ("In order, the following would be installed (use -v for more details):":ls) ->
            let deps = map (head . words) ls
            in if all (`elem` packages) deps
                  || "alex" `isInfixOf` depFile
                  || "happy" `isInfixOf` depFile
                  || any (`isInfixOf` depFile) ["QuickCheck","tf-random"]
                then Right deps
                else Left $ "Depends on non-HP packages: "
                            ++ unwords (filter (not . (`elem` packages)) deps) ++ "\n" ++ "Could not build " ++ depFile
        _ -> Left out

    packages = map show $ (allPackages incExtras) hpRel
installAction :: FilePath -> Bool -> Release -> Action ()
installAction depFile incExtras hpRel = do
    let pkg = extractPackage depFile
    let srcDir = packageSourceDir pkg
    need [ dir srcDir ]

    (Exit _, Stdout out, Stderr err) <- localCommand [Cwd srcDir]
        "cabal" cabalInstallArgs
    case decode out of
        Right deps -> writeFileLinesChanged depFile deps
        Left msg -> error $ "Error computing dependencies of "
                            ++ show pkg ++ ":\n"
                            ++ msg ++ "\n"
                            ++ err
  where
    cabalInstallArgs =
        [ "install"
        , "--dry-run"
        , "--only-dependencies"
        , "--package-db=clear"
        , "--package-db=global"
        ] ++ map ("--constraint=" ++) constraints

    constraints =
        map (\p -> pkgName p ++ "==" ++ showVersion (pkgVersion p)) $
            (allPackages incExtras) hpRel

    decode out = case drop 1 $ lines out of
        ("All the requested packages are already installed:":_) ->
            Right []
        ("In order, the following would be installed (use -v for more details):":ls) ->
            let deps = map (head . words) ls
            in if all (`elem` packages) deps
                  || "alex" `isInfixOf` depFile
                  || "happy" `isInfixOf` depFile
                  || any (`isInfixOf` depFile) ["QuickCheck","tf-random"]
                then Right deps
                else Left $ "Depends on non-HP packages: "
                            ++ unwords (filter (not . (`elem` packages)) deps) ++ "\n" ++ "Could not build " ++ depFile
        _ -> Left out

    packages = map show $ (allPackages incExtras) hpRel
installAction depFile incExtras hpRel = do
    let pkg = extractPackage depFile
    let srcDir = packageSourceDir pkg
    need [ dir srcDir ]

    (Exit _, Stdout out, Stderr err) <- localCommand [Cwd srcDir]
        "cabal" cabalInstallArgs
    case decode out of
        Right deps -> writeFileLinesChanged depFile deps
        Left msg -> error $ "Error computing dependencies of "
                            ++ show pkg ++ ":\n"
                            ++ msg ++ "\n"
                            ++ err
  where
    cabalInstallArgs =
        [ "install"
        , "--dry-run"
        , "--only-dependencies"
        , "--package-db=clear"
        , "--package-db=global"
        ] ++ map ("--constraint=" ++) constraints

    constraints =
        map (\p -> pkgName p ++ "==" ++ showVersion (pkgVersion p)) $
            (allPackages incExtras) hpRel

    decode out = case drop 1 $ lines out of
        ("All the requested packages are already installed:":_) ->
            Right []
        ("In order, the following would be installed (use -v for more details):":ls) ->
            let deps = map (head . words) ls
            in if all (`elem` packages) deps
                  || "alex" `isInfixOf` depFile
                  || "happy" `isInfixOf` depFile
                  || any (`isInfixOf` depFile) ["QuickCheck","tf-random"]
                then Right deps
                else Left $ "Depends on non-HP packages: "
                            ++ unwords (filter (not . (`elem` packages)) deps) ++ "\n" ++ "Could not build " ++ depFile
        _ -> Left out

    packages = map show $ (allPackages incExtras) hpRel