1{-# LANGUAGE RecordWildCards #-}
3module Cabal2Nix.Normalize ( normalize, normalizeList, normalizeCabalFlags ) where
5import Cabal2Nix.CorePackages
10import Distribution.NixOS.Derivation.Cabal
11import Distribution.NixOS.Util.Regex ( regsubmatch )
12import Distribution.PackageDescription ( FlagAssignment, FlagName(..) )
13import Distribution.Simple.Utils ( lowercase )
15normalize :: Derivation -> Derivation
16normalize deriv@(MkDerivation {..}) = deriv
17 { buildDepends = normalizeNixNames (filter (`notElem` (pname : corePackages)) buildDepends)
18 , testDepends = normalizeNixNames (filter (`notElem` (pname : corePackages)) testDepends)
19 , buildTools = normalizeNixBuildTools (filter (`notElem` coreBuildTools) buildTools)
20 , extraLibs = normalizeNixLibs extraLibs
21 , pkgConfDeps = normalizeNixLibs pkgConfDeps
22 , configureFlags = normalizeList configureFlags
23 , cabalFlags = normalizeCabalFlags cabalFlags
24 , metaSection = normalizeMeta metaSection
27normalizeMeta :: Meta -> Meta
28normalizeMeta meta@(Meta {..}) = meta
29 { description = normalizeDescription description
30 , maintainers = normalizeMaintainers maintainers
31 , platforms = normalizePlatforms platforms
34normalizeDescription :: String -> String
35normalizeDescription desc
37 | last desc == '.' && length (filter ('.'==) desc) == 1 = normalizeDescription (init desc)
38 | otherwise = quote (unwords (words desc))
40quote :: String -> String
41quote ('\\':c:cs) = '\\' : c : quote cs
42quote ('"':cs) = '\\' : '"' : quote cs
43quote (c:cs) = c : quote cs
46normalizeList :: [String] -> [String]
47normalizeList = nub . sortBy (compare `on` map toLower) . filter (not . null)
49normalizeNixNames :: [String] -> [String]
50normalizeNixNames = normalizeList . map toNixName
52normalizeNixLibs :: [String] -> [String]
53normalizeNixLibs = normalizeList . concatMap libNixName
55normalizeNixBuildTools :: [String] -> [String]
56normalizeNixBuildTools = normalizeList . concatMap buildToolNixName
58-- |Strip the "self.ghc.meta.platforms" prefix from platform names, filter
59-- duplicates, and sort the resulting list alphabetically.
61-- >>> normalizeMaintainers ["self.stdenv.lib.maintainers.foobar", "foobar"]
64-- >>> normalizeMaintainers ["any.prefix.is.recognized.yo", "abc.def"]
67normalizeMaintainers :: [String] -> [String]
68normalizeMaintainers maints = normalizeList
69 [ (m `regsubmatch` "^([^.].*\\.)?([^.]+)$") !! 1 | m <- maints ]
71normalizePlatforms :: [String] -> [String]
72normalizePlatforms [] = ["ghc.meta.platforms"]
73normalizePlatforms plats = normalizeList
74 [ if '.' `elem` p then p else "stdenv.lib.platforms." ++ p | p <- plats ]
77-- |When a flag is specified multiple times, the first occurrence
78-- counts. This is counter-intuitive, IMHO, but it's how cabal does it.
79-- Flag names are spelled in all lowercase.
81-- >>> normalizeCabalFlags [(FlagName "foo", True), (FlagName "FOO", True), (FlagName "Foo", False)]
82-- [(FlagName "foo",True)]
84normalizeCabalFlags :: FlagAssignment -> FlagAssignment
85normalizeCabalFlags flags' = nubBy ((==) `on` fst) (sortBy (compare `on` fst) flags)
87 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