Skip to content

Commit

Permalink
Support for cabal-install 2
Browse files Browse the repository at this point in the history
  • Loading branch information
hamishmack committed Aug 12, 2017
1 parent 445f83f commit 4b8321b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
17 changes: 14 additions & 3 deletions src/IDE/Utils/CabalPlan.hs
Expand Up @@ -20,13 +20,14 @@ module IDE.Utils.CabalPlan (
import GHC.Generics (Generic)
import Data.List (sortOn)
import qualified Data.Set as S (fromList, Set)
import qualified Data.Map as M (Map, toList)
import qualified Data.Map as M (empty, Map, toList)
import Data.Text (Text)
import qualified Data.Text as T
(breakOnEnd, init, all, null, Text, pack, unpack, splitOn)
import Data.Aeson (FromJSON(..), withObject, (.:))
import Data.Aeson (FromJSON(..), withObject, (.:), (.:?))
import Distribution.Package (PackageIdentifier, UnitId)
import Distribution.Text (display, simpleParse)
import Data.Maybe (fromMaybe)

-- $setup
-- >>> import Data.Aeson (eitherDecodeStrict')
Expand Down Expand Up @@ -73,11 +74,21 @@ import Distribution.Text (display, simpleParse)
-- Right (PlanJson {pjPlan = [PlanItem {piId = "Cabal-1.24.0.0", piType = "pre-existing", piComps = [(ComponentLib,fromList ["array-0.5.1.1","base-4.9.0.0"])]},PlanItem {piId = "QuickCheck-2.9.1-ec9a1c39266d75ed2c3314f6e846a8f11853eff43fc45db79c7256d9bfd94602", piType = "configured", piComps = [(ComponentLib,fromList ["base-4.9.0.0","containers-0.5.7.1","random-1.1-fe6ccf72ebd63a2d68570bb45b42bd08df5570c6151cb9af54907d40ef9af454"])]}]})
data PlanJson = PlanJson
{ pjPlan :: [PlanItem]
, pjCabalVersion :: String
, pjCabalLibVersion :: String
, pjCompilerId :: Maybe String
, pjOS :: Maybe String
, pjArch :: Maybe String
} deriving Show

instance FromJSON PlanJson where
parseJSON = withObject "PlanJson" $ \o ->
PlanJson <$> o .: "install-plan"
<*> o .: "cabal-version"
<*> o .: "cabal-lib-version"
<*> o .:? "compiler-id"
<*> o .:? "os"
<*> o .:? "arch"

type PID = Text

Expand All @@ -92,7 +103,7 @@ instance FromJSON PlanItem where
parseJSON = withObject "PlanItem" $ \o ->
PlanItem <$> o .: "id"
<*> o .: "type"
<*> (doComps <$> o .: "components")
<*> (doComps . fromMaybe M.empty <$> o .:? "components")
where
doComps :: M.Map Text CompInfo -> [(Component, S.Set PID)]
doComps m = sortOn fst [ (toComp k, S.fromList v) | (k,CompInfo v) <- M.toList m ]
Expand Down
30 changes: 29 additions & 1 deletion src/IDE/Utils/FileUtils.hs
Expand Up @@ -44,6 +44,7 @@ module IDE.Utils.FileUtils (
, getCabalPackages
, getInstalledPackages
, findProjectRoot
, cabalProjectBuildDir
, getPackageDBs'
, getPackageDBs
, figureOutGhcOpts
Expand All @@ -57,7 +58,8 @@ import Control.Applicative
import Prelude hiding (readFile)
import System.FilePath
(splitFileName, dropExtension, takeExtension,
combine, addExtension, (</>), normalise, splitPath, takeFileName,takeDirectory)
combine, addExtension, (</>), normalise, splitPath, takeFileName,
takeDirectory)
import Distribution.ModuleName (toFilePath, ModuleName)
import Control.Monad (when, foldM, filterM, forM)
import Data.Maybe (mapMaybe, catMaybes)
Expand Down Expand Up @@ -445,6 +447,32 @@ getCabalPackages ghcVer dir = do
Right plan -> return . map (,packageDBs) $
mapMaybe (T.simpleParse . T.unpack . piId) (pjPlan plan)

cabalProjectBuildDir :: FilePath -> IO (FilePath, FilePath -> FilePath)
cabalProjectBuildDir projectRoot = do
let distNewstyle = projectRoot </> "dist-newstyle"
planFile = distNewstyle </> "cache" </> "plan.json"
defaultDir = (distNewstyle </> "build", const "build")
doesFileExist planFile >>= \case
False -> do
debugM "leksah" $ "cabal plan not found : " <> planFile
return defaultDir
True ->
(eitherDecodeStrict' <$> BS.readFile planFile)
>>= \ case
Right PlanJson { pjCabalVersion = v } | "1.24." `isPrefixOf` v ->
return defaultDir
Right PlanJson
{ pjCompilerId = Just compilerId
, pjOS = Just os
, pjArch = Just arch
} -> return (distNewstyle </> "build" </> arch <> "-" <> os </> compilerId,
\component -> "c" </> component </> "build")
Right plan -> do
errorM "leksah" $ "Unexpected cabal plan : " <> show plan
return defaultDir
Left err -> do
errorM "leksah" $ "Error parsing cabal plan : " <> err
return defaultDir

getPackages' :: [FilePath] -> IO [UnitId]
getPackages' packageDBs = do
Expand Down

0 comments on commit 4b8321b

Please sign in to comment.