2014-05-18 23:20:58 +00:00
|
|
|
{-# LANGUAGE BangPatterns, ScopedTypeVariables, TupleSections #-}
|
2017-05-28 02:22:56 +00:00
|
|
|
module GhcMod.GhcPkg (
|
2014-05-08 03:42:45 +00:00
|
|
|
ghcPkgDbOpt
|
2014-04-15 03:13:10 +00:00
|
|
|
, ghcPkgDbStackOpts
|
|
|
|
, ghcDbStackOpts
|
|
|
|
, ghcDbOpt
|
2015-08-14 08:28:32 +00:00
|
|
|
, getPackageDbStack
|
2014-11-01 21:02:47 +00:00
|
|
|
, getPackageCachePaths
|
2015-09-02 05:19:11 +00:00
|
|
|
, getGhcPkgProgram
|
2014-04-15 03:13:10 +00:00
|
|
|
) where
|
|
|
|
|
2014-09-23 04:47:32 +00:00
|
|
|
import Config (cProjectVersion, cTargetPlatformString, cProjectVersionInt)
|
2015-08-03 01:09:56 +00:00
|
|
|
import Control.Applicative
|
2014-04-30 23:48:49 +00:00
|
|
|
import Data.List.Split (splitOn)
|
2014-10-14 17:52:58 +00:00
|
|
|
import Data.Maybe
|
2014-09-23 04:47:32 +00:00
|
|
|
import Exception (handleIO)
|
|
|
|
import System.Directory (doesDirectoryExist, getAppUserDataDirectory)
|
2014-04-16 02:32:36 +00:00
|
|
|
import System.FilePath ((</>))
|
2015-08-03 01:09:56 +00:00
|
|
|
import Prelude
|
2014-04-15 03:13:10 +00:00
|
|
|
|
2017-05-28 02:22:56 +00:00
|
|
|
import GhcMod.Types
|
|
|
|
import GhcMod.Monad.Types
|
|
|
|
import GhcMod.CabalHelper
|
|
|
|
import GhcMod.PathsAndFiles
|
|
|
|
import GhcMod.CustomPackageDb
|
|
|
|
import GhcMod.Stack
|
2015-08-07 04:47:34 +00:00
|
|
|
|
2014-04-15 15:14:10 +00:00
|
|
|
ghcVersion :: Int
|
|
|
|
ghcVersion = read cProjectVersionInt
|
|
|
|
|
2014-09-23 04:47:32 +00:00
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2014-04-15 03:13:10 +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
|
2014-04-15 03:13:10 +00:00
|
|
|
|
|
|
|
-- | 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-04-15 03:13:10 +00:00
|
|
|
|
2014-09-23 04:47:32 +00:00
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2014-04-15 03:13:10 +00:00
|
|
|
ghcPkgDbOpt :: GhcPkgDb -> [String]
|
|
|
|
ghcPkgDbOpt GlobalDb = ["--global"]
|
|
|
|
ghcPkgDbOpt UserDb = ["--user"]
|
2014-04-15 15:14:10 +00:00
|
|
|
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]
|
2014-04-15 03:13:10 +00:00
|
|
|
|
|
|
|
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"]
|
2014-04-15 15:14:10 +00:00
|
|
|
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
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2015-09-02 05:19:11 +00:00
|
|
|
getGhcPkgProgram :: IOish m => GhcModT m FilePath
|
|
|
|
getGhcPkgProgram = do
|
|
|
|
crdl <- cradle
|
|
|
|
progs <- optPrograms <$> options
|
2015-09-11 01:48:52 +00:00
|
|
|
case cradleProject crdl of
|
|
|
|
(StackProject senv) -> do
|
|
|
|
Just ghcPkg <- getStackGhcPkgPath senv
|
2015-09-02 05:19:11 +00:00
|
|
|
return ghcPkg
|
|
|
|
_ ->
|
|
|
|
return $ ghcPkgProgram progs
|
|
|
|
|
2015-08-14 08:28:32 +00:00
|
|
|
getPackageDbStack :: IOish m => GhcModT m [GhcPkgDb]
|
|
|
|
getPackageDbStack = do
|
2015-08-12 07:04:35 +00:00
|
|
|
crdl <- cradle
|
2015-08-14 08:28:32 +00:00
|
|
|
mCusPkgStack <- getCustomPkgDbStack
|
2015-09-11 01:48:52 +00:00
|
|
|
stack <- case cradleProject crdl of
|
2015-08-12 07:25:13 +00:00
|
|
|
PlainProject ->
|
|
|
|
return [GlobalDb, UserDb]
|
|
|
|
SandboxProject -> do
|
2015-08-19 06:46:56 +00:00
|
|
|
Just db <- liftIO $ getSandboxDb crdl
|
2015-08-12 07:25:13 +00:00
|
|
|
return $ [GlobalDb, db]
|
|
|
|
CabalProject ->
|
2015-08-14 08:28:32 +00:00
|
|
|
getCabalPackageDbStack
|
2015-09-11 01:48:52 +00:00
|
|
|
(StackProject StackEnv {..}) ->
|
2016-12-04 00:08:06 +00:00
|
|
|
return $ [GlobalDb, PackageDb seSnapshotPkgDb, PackageDb seLocalPkgDb]
|
2015-08-14 08:28:32 +00:00
|
|
|
return $ fromMaybe stack mCusPkgStack
|
2015-08-12 07:04:35 +00:00
|
|
|
|
2015-08-14 08:28:32 +00:00
|
|
|
getPackageCachePaths :: IOish m => FilePath -> GhcModT m [FilePath]
|
|
|
|
getPackageCachePaths sysPkgCfg = do
|
|
|
|
pkgDbStack <- getPackageDbStack
|
2015-08-07 04:47:34 +00:00
|
|
|
catMaybes <$> (liftIO . resolvePackageConfig sysPkgCfg) `mapM` pkgDbStack
|
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
|