getHighestVersion :: PackageName -> GhcPkg -> Hsenv Version
getHighestVersion (PackageName packageName) ghcPkg = do
  debug $ "Checking the highest installed version of package " ++ packageName
  out <- indentMessages $ ghcPkg ["field", packageName, "version"] Nothing
  -- example output:
  -- version: 1.1.4
  -- version: 1.2.0.3
  let extractVersionString :: String -> Hsenv String
      extractVersionString line =
          case words line of
            [_, x] -> return x
            _   -> throwError $ HsenvException $ "Couldn't extract version string from: " ++ line
  versionStrings <- mapM extractVersionString $ lines out
  indentMessages $ trace $ "Found version strings: " ++ unwords versionStrings
  versions <- mapM parseVersion versionStrings
  case versions of
    []     -> throwError $ HsenvException $ "No versions of package " ++ packageName ++ " found"
    (v:vs) -> do
      indentMessages $ debug $ "Found: " ++ unwords (map prettyVersion versions)
      return $ foldr max v vs

-- choose the highest installed version of package with this name
getHighestVersion :: PackageName -> GhcPkg -> Hsenv Version
getHighestVersion (PackageName packageName) ghcPkg = do
  debug $ "Checking the highest installed version of package " ++ packageName
  out <- indentMessages $ ghcPkg ["field", packageName, "version"] Nothing
  -- example output:
  -- version: 1.1.4
  -- version: 1.2.0.3
  let extractVersionString :: String -> Hsenv String
      extractVersionString line =
          case words line of
            [_, x] -> return x
            _   -> throwError $ HsenvException $ "Couldn't extract version string from: " ++ line
  versionStrings <- mapM extractVersionString $ lines out
  indentMessages $ trace $ "Found version strings: " ++ unwords versionStrings
  versions <- mapM parseVersion versionStrings
  case versions of
    []     -> throwError $ HsenvException $ "No versions of package " ++ packageName ++ " found"
    (v:vs) -> do
      indentMessages $ debug $ "Found: " ++ unwords (map prettyVersion versions)
      return $ foldr max v vs

-- choose the highest installed version of package with this name
getHighestVersion (PackageName packageName) ghcPkg = do
  debug $ "Checking the highest installed version of package " ++ packageName
  out <- indentMessages $ ghcPkg ["field", packageName, "version"] Nothing
  -- example output:
  -- version: 1.1.4
  -- version: 1.2.0.3
  let extractVersionString :: String -> Hsenv String
      extractVersionString line =
          case words line of
            [_, x] -> return x
            _   -> throwError $ HsenvException $ "Couldn't extract version string from: " ++ line
  versionStrings <- mapM extractVersionString $ lines out
  indentMessages $ trace $ "Found version strings: " ++ unwords versionStrings
  versions <- mapM parseVersion versionStrings
  case versions of
    []     -> throwError $ HsenvException $ "No versions of package " ++ packageName ++ " found"
    (v:vs) -> do
      indentMessages $ debug $ "Found: " ++ unwords (map prettyVersion versions)
      return $ foldr max v vs

-- choose the highest installed version of package with this name