{-# LANGUAGE TupleSections #-}
module Aura.Cache
(
Cache(..)
, cacheContents
, defaultPackageCache
, cacheMatches
, pkgsInCache
) where
import Aura.Settings
import Aura.Types
import RIO
import RIO.Directory
import RIO.FilePath
import qualified RIO.Map as M
import qualified RIO.Set as S
import qualified RIO.Text as T
newtype Cache = Cache { _cache :: Map SimplePkg PackagePath }
defaultPackageCache :: FilePath
defaultPackageCache = "/var/cache/pacman/pkg/"
cache :: [PackagePath] -> Cache
cache = Cache . M.fromList . mapMaybe (\p -> (,p) <$> simplepkg p)
cacheContents :: FilePath -> IO Cache
cacheContents pth = cache . mapMaybe (packagePath . (pth </>)) <$> listDirectory pth
pkgsInCache :: Settings -> Set PkgName -> IO (Set PkgName)
pkgsInCache ss ps = do
c <- cacheContents . either id id . cachePathOf $ commonConfigOf ss
pure . S.filter (`S.member` ps) . S.map spName . M.keysSet $ _cache c
cacheMatches :: Settings -> Text -> IO [PackagePath]
cacheMatches ss input = do
c <- cacheContents . either id id . cachePathOf $ commonConfigOf ss
pure . filter (T.isInfixOf input . T.pack . ppPath) . M.elems $ _cache c