module Distribution.Hackage.DB.Path where
import Distribution.Hackage.DB.Errors
import Control.Exception
import System.Directory
import System.Environment (lookupEnv)
import System.FilePath
cabalStateDir :: IO FilePath
cabalStateDir :: IO FilePath
cabalStateDir = do
Maybe FilePath
envCabal <- FilePath -> IO (Maybe FilePath)
lookupEnv FilePath
"CABAL_DIR"
FilePath
dotCabal <- FilePath -> IO FilePath
getAppUserDataDirectory FilePath
"cabal"
Bool
dotCabalExists <- FilePath -> IO Bool
doesDirectoryExist FilePath
dotCabal
case Maybe FilePath
envCabal of
Just FilePath
dir -> forall (f :: * -> *) a. Applicative f => a -> f a
pure FilePath
dir
Maybe FilePath
Nothing ->
if Bool
dotCabalExists
then forall (f :: * -> *) a. Applicative f => a -> f a
pure FilePath
dotCabal
else XdgDirectory -> FilePath -> IO FilePath
getXdgDirectory XdgDirectory
XdgCache FilePath
"cabal"
cabalTarballDir :: String -> IO FilePath
cabalTarballDir :: FilePath -> IO FilePath
cabalTarballDir FilePath
repo = do
FilePath
csd <- IO FilePath
cabalStateDir
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ [FilePath] -> FilePath
joinPath [FilePath
csd, FilePath
"packages", FilePath
repo]
hackageTarballDir :: IO FilePath
hackageTarballDir :: IO FilePath
hackageTarballDir = FilePath -> IO FilePath
cabalTarballDir FilePath
"hackage.haskell.org"
hackageTarball :: IO FilePath
hackageTarball :: IO FilePath
hackageTarball = do
FilePath
htd <- IO FilePath
hackageTarballDir
let idx00 :: FilePath
idx00 = FilePath
htd FilePath -> FilePath -> FilePath
</> FilePath
"00-index.tar"
idx01 :: FilePath
idx01 = FilePath
htd FilePath -> FilePath -> FilePath
</> FilePath
"01-index.tar"
Bool
have01 <- FilePath -> IO Bool
doesFileExist FilePath
idx01
if Bool
have01 then forall (m :: * -> *) a. Monad m => a -> m a
return FilePath
idx01 else do
Bool
have00 <- FilePath -> IO Bool
doesFileExist FilePath
idx00
if Bool
have00 then forall (m :: * -> *) a. Monad m => a -> m a
return FilePath
idx00 else
forall e a. Exception e => e -> IO a
throwIO NoHackageTarballFound
NoHackageTarballFound