1module Cabal2Nix.Normalize ( normalize, normalizeCabalFlags ) where
3import Cabal2Nix.CorePackages
9import qualified Data.Set as Set
10import Distribution.Nixpkgs.Haskell
11import Distribution.Nixpkgs.Meta
12import Distribution.Nixpkgs.Util.Regex ( regsubmatch )
13import Distribution.Package
14import Distribution.PackageDescription ( FlagAssignment, FlagName(..) )
15import Distribution.Simple.Utils ( lowercase )
16import Distribution.Version
18normalize :: Derivation -> Derivation
20 & over libraryDepends (normalizeBuildInfo (packageName drv))
21 & over executableDepends (normalizeBuildInfo (packageName drv))
22 & over testDepends (normalizeBuildInfo (packageName drv))
23 & over metaSection normalizeMeta
24 & over cabalFlags normalizeCabalFlags
26normalizeBuildInfo :: PackageName -> BuildInfo -> BuildInfo
27normalizeBuildInfo pname bi = bi
28 & haskell . contains (Dependency pname anyVersion) .~ False
29 & tool %~ normalizeNixBuildTools . Set.filter (\(Dependency (PackageName n) _) -> n `notElem` coreBuildTools)
33 , testDepends = normalizeNixNames (Set.delete pname testDepends)
34 , extraLibs = normalizeNixLibs extraLibs
35 , pkgConfDeps = normalizeNixLibs pkgConfDeps
36 , configureFlags = normalizeSet configureFlags
40normalizeMeta :: Meta -> Meta
41normalizeMeta = over description normalizeSynopsis
42 . over maintainers normalizeMaintainers
43 . over platforms normalizePlatforms
45normalizeSynopsis :: String -> String
48 | last desc == '.' && length (filter ('.'==) desc) == 1 = normalizeSynopsis (init desc)
49 | otherwise = quote (unwords (words desc))
51quote :: String -> String
52quote ('\\':c:cs) = '\\' : c : quote cs
53quote ('"':cs) = '\\' : '"' : quote cs
54quote (c:cs) = c : quote cs
57normalizeSet :: Set String -> Set String
58normalizeSet = Set.filter (not . null)
60-- normalizeNixNames :: Set String -> Set String
61-- normalizeNixNames = normalizeSet . Set.map toNixName
63-- normalizeNixLibs :: Set String -> Set String
64-- normalizeNixLibs = normalizeSet . Set.fromList . concatMap libNixName . Set.toList
66normalizeNixBuildTools :: Set Dependency -> Set Dependency
67normalizeNixBuildTools = Set.fromList . concatMap buildToolNixName' . Set.toList
69 buildToolNixName' :: Dependency -> [Dependency]
70 buildToolNixName' (Dependency (PackageName n) vr) = [ Dependency (PackageName n') vr | n' <- buildToolNixName n ]
72-- |Strip any kind of path prefix from maintainer names, filter duplicates, and
73-- sort the resulting list alphabetically.
75-- >>> normalizeMaintainers $ Set.fromList ["self.stdenv.lib.maintainers.foobar", "foobar"]
78-- >>> normalizeMaintainers $ Set.fromList ["any.prefix.is.recognized.yo", "abc.def"]
79-- fromList ["def","yo"]
81normalizeMaintainers :: Set String -> Set String
82normalizeMaintainers = normalizeSet . Set.map
83 (\m -> (m `regsubmatch` "^([^.].*\\.)?([^.]+)$") !! 1)
85normalizePlatforms :: Set String -> Set String
86normalizePlatforms = normalizeSet . Set.map
87 (\p -> if '.' `elem` p then p else "stdenv.lib.platforms." ++ p)
90-- |When a flag is specified multiple times, the first occurrence
91-- counts. This is counter-intuitive, IMHO, but it's how cabal does it.
92-- Flag names are spelled in all lowercase.
94-- >>> normalizeCabalFlags [(FlagName "foo", True), (FlagName "FOO", True), (FlagName "Foo", False)]
95-- [(FlagName "foo",True)]
97normalizeCabalFlags :: FlagAssignment -> FlagAssignment
98normalizeCabalFlags flags' = nubBy ((==) `on` fst) (sortBy (compare `on` fst) flags)
100 flags = [ (FlagName (lowercase n), b) | (FlagName n, b) <- flags' ]
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