@@ -14,8 +14,8 @@ import Control.Monad.Par.Combinator
14
14
import Control.Monad.Par.IO
15
15
import Control.Monad.Trans ( liftIO )
16
16
import Data.Function
17
+ import System.FilePath
17
18
import Data.List
18
- -- import Data.Char
19
19
import Data.Map.Strict ( Map )
20
20
import qualified Data.Map.Strict as Map
21
21
import Data.Maybe
@@ -36,7 +36,6 @@ import Distribution.Text
36
36
import Distribution.Version
37
37
import Language.Nix.Identifier
38
38
import Options.Applicative
39
- -- import qualified Distribution.Compat.ReadP as Parse
40
39
41
40
type Nixpkgs = PackageMap -- Map String (Set [String])
42
41
type PackageSet = Map String Version
@@ -53,6 +52,12 @@ resolveConstraint' (Dependency (PackageName name) vrange) hackage | Set.null vs
53
52
| otherwise = Just (Set. findMax vs)
54
53
where vs = Set. filter (`withinRange` vrange) (Map. keysSet (hackage Map. ! name))
55
54
55
+ satisfiesConstraint :: PackageIdentifier -> Constraint -> Bool
56
+ satisfiesConstraint (PackageIdentifier pn v) (Dependency cn vr) = (pn /= cn) || (v `withinRange` vr)
57
+
58
+ satisfiesConstraints :: PackageIdentifier -> [Constraint ] -> Bool
59
+ satisfiesConstraints p = all (satisfiesConstraint p)
60
+
56
61
data Configuration = Configuration
57
62
{
58
63
-- | Target architecture. Used by 'finalizePackageDescription' to
@@ -111,12 +116,19 @@ main = do
111
116
112
117
hackage <- readHackage hackageRepository
113
118
nixpkgs <- readNixpkgPackageMap nixpkgsRepository
119
+ preferredVersions <- readPreferredVersions (hackageRepository </> " preferred-versions" )
114
120
let fixup = Map. delete " acme-everything" -- TODO: https://github.com/NixOS/cabal2nix/issues/164
115
121
. Map. delete " som" -- TODO: https://github.com/NixOS/cabal2nix/issues/164
116
122
. Map. delete " type" -- TODO: https://github.com/NixOS/cabal2nix/issues/163
117
123
. Map. delete " dictionary-sharing" -- TODO: https://github.com/NixOS/cabal2nix/issues/175
124
+ . Map. filter (/= Map. empty)
125
+ . Map. mapWithKey (enforcePreferredVersions preferredVersions)
118
126
runParIO $ generatePackageSet defaultConfiguration (fixup hackage) nixpkgs
119
127
128
+ enforcePreferredVersions :: [Constraint ] -> String -> Map Version GenericPackageDescription
129
+ -> Map Version GenericPackageDescription
130
+ enforcePreferredVersions cs pkg = Map. filterWithKey (\ v _ -> PackageIdentifier (PackageName pkg) v `satisfiesConstraints` cs)
131
+
120
132
generatePackageSet :: Configuration -> Hackage -> Nixpkgs -> ParIO ()
121
133
generatePackageSet config hackage nixpkgs = do
122
134
let
@@ -272,6 +284,16 @@ cabal2nix resolver cabal = (missingDeps, flags, drv)
272
284
enableTest :: TestSuite -> TestSuite
273
285
enableTest t = t { testEnabled = True }
274
286
287
+ readPreferredVersions :: FilePath -> IO [Constraint ]
288
+ readPreferredVersions path = mapMaybe parsePreferredVersionsLine . lines <$> readFile path
289
+
290
+
291
+ parsePreferredVersionsLine :: String -> Maybe Constraint
292
+ parsePreferredVersionsLine (' -' : ' -' : _) = Nothing
293
+ parsePreferredVersionsLine l = case simpleParse l of
294
+ Just c -> Just c
295
+ Nothing -> error (" invalid preferred-versions line: " ++ show l)
296
+
275
297
defaultConfiguration :: Configuration
276
298
defaultConfiguration = Configuration
277
299
{ platform = Platform X86_64 Linux
@@ -3880,22 +3902,3 @@ defaultConfiguration = Configuration
3880
3902
]
3881
3903
3882
3904
}
3883
-
3884
- -- instance Text Configuration where
3885
- -- parse = do platform <- token (Parse.string "platform") >> token (Parse.char '=') >> parse
3886
- -- return $ Configuration
3887
- -- { platform = platform
3888
- -- }
3889
-
3890
- -- token :: Parse.ReadP r a -> Parse.ReadP r a
3891
- -- -- token p = p >>= \r -> Parse.skipSpaces >> return r
3892
- -- token p = Parse.skipSpaces >> p
3893
-
3894
- -- listOf :: Parse.ReadP r a -> Parse.ReadP r [a]
3895
- -- listOf p = Parse.between (token (Parse.char '[')) (token (Parse.char ']'))
3896
- -- (Parse.sepBy (token p) (token (Parse.char ',')))
3897
-
3898
- -- run :: Parse.ReadP a a -> String -> Maybe a
3899
- -- run p str = case [ r' | (r', s) <- Parse.readP_to_S (Parse.skipSpaces >> p) str, all isSpace s ] of
3900
- -- [] -> Nothing
3901
- -- (r:_) -> Just r
0 commit comments