1module Stackage.LoadDatabase where
3import qualified Codec.Archive.Tar as Tar
4import Control.Exception (throwIO)
5import qualified Data.ByteString.Lazy as L
6import qualified Data.ByteString.Lazy.Char8 as L8
7import qualified Data.Map as Map
8import Data.Monoid (Monoid (..))
9import Data.Set (member)
10import qualified Data.Set as Set
11import Distribution.Package (Dependency (Dependency))
12import Distribution.PackageDescription (condExecutables,
16 condTreeConstraints, condTreeComponents, ConfVar (..), Condition(..), flagName, flagDefault, genPackageFlags)
17import Distribution.PackageDescription.Parse (ParseResult (ParseOk),
18 parsePackageDescription)
19import Distribution.Version (withinRange)
23import Data.Maybe (mapMaybe)
24import Distribution.System (buildOS, buildArch)
25import Distribution.Compiler (CompilerFlavor (GHC))
27-- | Load the raw package database.
29-- We want to put in some restrictions:
31-- * Drop all core packages. We never want to install a new version of
32-- those, nor include them in the package list.
34-- * For packages with a specific version bound, find the maximum matching
37-- * For other packages, select the maximum version number.
38loadPackageDB :: Set PackageName -- ^ core packages
39 -> Map PackageName VersionRange -- ^ additional deps
41loadPackageDB core deps = do
42 tarName <- getTarballName
43 lbs <- L.readFile tarName
44 addEntries mempty $ Tar.read lbs
46 addEntries _ (Tar.Fail e) = error $ show e
47 addEntries db Tar.Done = return db
48 addEntries db (Tar.Next e es) = addEntry db e >>= flip addEntries es
50 addEntry :: PackageDB -> Tar.Entry -> IO PackageDB
52 case getPackageVersion e of
55 | p `member` core -> return pdb
57 case Map.lookup p deps of
59 | not $ withinRange v vrange -> return pdb
61 case Tar.entryContent e of
62 Tar.NormalFile bs _ -> return $ mappend pdb $ PackageDB $ Map.singleton p PackageInfo
64 , piDeps = parseDeps bs
69 case parsePackageDescription $ L8.unpack lbs of
70 ParseOk _ gpd -> mconcat
71 [ maybe mempty (go gpd) $ condLibrary gpd
72 , mconcat $ map (go gpd . snd) $ condExecutables gpd
73 , mconcat $ map (go gpd . snd) $ condTestSuites gpd
74 , mconcat $ map (go gpd . snd) $ condBenchmarks gpd
80 $ Set.fromList (map (\(Dependency p _) -> p) $ condTreeConstraints tree)
81 : map (go gpd) (mapMaybe (checkCond gpd) $ condTreeComponents tree)
83 checkCond gpd (cond, tree, melse)
84 | checkCond' cond = Just tree
87 checkCond' (Var (OS os)) = os == buildOS
88 checkCond' (Var (Arch arch)) = arch == buildArch
89 checkCond' (Var (Flag flag)) = flag `elem` flags
90 checkCond' (Var (Impl compiler range)) =
91 compiler == GHC && withinRange targetCompilerVersion range
92 checkCond' (Lit b) = b
93 checkCond' (CNot c) = not $ checkCond' c
94 checkCond' (COr c1 c2) = checkCond' c1 || checkCond' c2
95 checkCond' (CAnd c1 c2) = checkCond' c1 && checkCond' c2
97 flags = map flagName $ filter flagDefault $ genPackageFlags gpd
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list