ghc-mod/Language/Haskell/GhcMod/GhcPkg.hs

73 lines
2.7 KiB
Haskell
Raw Normal View History

{-# LANGUAGE BangPatterns, ScopedTypeVariables, TupleSections #-}
module Language.Haskell.GhcMod.GhcPkg (
ghcPkgDbOpt
, ghcPkgDbStackOpts
, ghcDbStackOpts
, ghcDbOpt
2014-11-01 21:02:47 +00:00
, getPackageCachePaths
) where
2014-09-23 04:47:32 +00:00
import Config (cProjectVersion, cTargetPlatformString, cProjectVersionInt)
import Control.Applicative ((<$>))
2014-04-30 23:48:49 +00:00
import Data.List.Split (splitOn)
import Data.Maybe
2014-09-23 04:47:32 +00:00
import Exception (handleIO)
2014-04-16 02:50:31 +00:00
import Language.Haskell.GhcMod.Types
2014-09-23 04:47:32 +00:00
import System.Directory (doesDirectoryExist, getAppUserDataDirectory)
2014-04-16 02:32:36 +00:00
import System.FilePath ((</>))
ghcVersion :: Int
ghcVersion = read cProjectVersionInt
2014-09-23 04:47:32 +00:00
----------------------------------------------------------------
-- | Get options needed to add a list of package dbs to ghc-pkg's db stack
ghcPkgDbStackOpts :: [GhcPkgDb] -- ^ Package db stack
-> [String]
2014-04-16 02:52:49 +00:00
ghcPkgDbStackOpts dbs = ghcPkgDbOpt `concatMap` dbs
-- | Get options needed to add a list of package dbs to ghc's db stack
ghcDbStackOpts :: [GhcPkgDb] -- ^ Package db stack
2014-04-16 02:50:31 +00:00
-> [String]
2014-04-16 02:52:49 +00:00
ghcDbStackOpts dbs = ghcDbOpt `concatMap` dbs
2014-09-23 04:47:32 +00:00
----------------------------------------------------------------
ghcPkgDbOpt :: GhcPkgDb -> [String]
ghcPkgDbOpt GlobalDb = ["--global"]
ghcPkgDbOpt UserDb = ["--user"]
ghcPkgDbOpt (PackageDb pkgDb)
2014-04-16 02:50:31 +00:00
| ghcVersion < 706 = ["--no-user-package-conf", "--package-conf=" ++ pkgDb]
| otherwise = ["--no-user-package-db", "--package-db=" ++ pkgDb]
ghcDbOpt :: GhcPkgDb -> [String]
2014-04-16 02:50:31 +00:00
ghcDbOpt GlobalDb
| ghcVersion < 706 = ["-global-package-conf"]
| otherwise = ["-global-package-db"]
ghcDbOpt UserDb
| ghcVersion < 706 = ["-user-package-conf"]
| otherwise = ["-user-package-db"]
ghcDbOpt (PackageDb pkgDb)
2014-04-16 02:50:31 +00:00
| ghcVersion < 706 = ["-no-user-package-conf", "-package-conf", pkgDb]
| otherwise = ["-no-user-package-db", "-package-db", pkgDb]
2014-09-23 04:47:32 +00:00
----------------------------------------------------------------
2014-11-01 21:02:47 +00:00
getPackageCachePaths :: FilePath -> Cradle -> IO [FilePath]
getPackageCachePaths sysPkgCfg crdl =
catMaybes <$> resolvePackageConfig sysPkgCfg `mapM` cradlePkgDbStack crdl
2014-09-23 04:47:32 +00:00
2014-11-01 21:02:47 +00:00
-- TODO: use PkgConfRef
2014-09-23 04:47:32 +00:00
--- Copied from ghc module `Packages' unfortunately it's not exported :/
2014-11-01 21:02:47 +00:00
resolvePackageConfig :: FilePath -> GhcPkgDb -> IO (Maybe FilePath)
resolvePackageConfig sysPkgCfg GlobalDb = return $ Just sysPkgCfg
resolvePackageConfig _ UserDb = handleIO (\_ -> return Nothing) $ do
appdir <- getAppUserDataDirectory "ghc"
let dir = appdir </> (target_arch ++ '-':target_os ++ '-':cProjectVersion)
pkgconf = dir </> "package.conf.d"
exist <- doesDirectoryExist pkgconf
return $ if exist then Just pkgconf else Nothing
where
2014-09-23 04:47:32 +00:00
[target_arch,_,target_os] = splitOn "-" cTargetPlatformString
2014-11-01 21:02:47 +00:00
resolvePackageConfig _ (PackageDb name) = return $ Just name