git.iko.soy/typeable/cabal2nix
on 46fbdf7 help sign in
71 lines 2.6 kB history raw
1{-# LANGUAGE RecordWildCards #-}
3module Cabal2Nix.Normalize ( normalize, normalizeList ) where
5import Distribution.NixOS.Derivation.Cabal
6import Cabal2Nix.Name
7import Cabal2Nix.CorePackages
8import Data.List
9import Data.Char
10import Data.Function
11import Distribution.NixOS.Regex ( regsubmatch )
13normalize :: Derivation -> Derivation
14normalize deriv@(MkDerivation {..}) = deriv
15 { buildDepends = normalizeNixNames (filter (`notElem` (pname : corePackages)) buildDepends)
16 , testDepends = normalizeNixNames (filter (`notElem` (pname : corePackages)) testDepends)
17 , buildTools = normalizeNixBuildTools (filter (`notElem` coreBuildTools) buildTools)
18 , extraLibs = normalizeNixLibs extraLibs
19 , pkgConfDeps = normalizeNixLibs pkgConfDeps
20 , configureFlags = normalizeList configureFlags
21 , metaSection = normalizeMeta metaSection
22 }
24normalizeMeta :: Meta -> Meta
25normalizeMeta meta@(Meta {..}) = meta
26 { description = normalizeDescription description
27 , maintainers = normalizeMaintainers maintainers
28 , platforms = normalizePlatforms platforms
29 }
31normalizeDescription :: String -> String
32normalizeDescription desc
33 | null desc = []
34 | last desc == '.' && length (filter ('.'==) desc) == 1 = normalizeDescription (init desc)
35 | otherwise = quote (unwords (words desc))
37quote :: String -> String
38quote ('\\':c:cs) = '\\' : c : quote cs
39quote ('"':cs) = '\\' : '"' : quote cs
40quote (c:cs) = c : quote cs
41quote [] = []
43normalizeList :: [String] -> [String]
44normalizeList = nub . sortBy (compare `on` map toLower)
46normalizeNixNames :: [String] -> [String]
47normalizeNixNames = normalizeList . map toNixName
49normalizeNixLibs :: [String] -> [String]
50normalizeNixLibs = normalizeList . concatMap libNixName
52normalizeNixBuildTools :: [String] -> [String]
53normalizeNixBuildTools = normalizeList . concatMap buildToolNixName
55-- |Strip the "self.ghc.meta.platforms" prefix from platform names, filter
56-- duplicates, and sort the resulting list alphabetically.
57--
58-- >>> normalizeMaintainers ["self.stdenv.lib.maintainers.foobar", "foobar"]
59-- ["foobar"]
60--
61-- >>> normalizeMaintainers ["any.prefix.is.recognized.yo", "abc.def"]
62-- ["def","yo"]
64normalizeMaintainers :: [String] -> [String]
65normalizeMaintainers maints = normalizeList
66 [ (m `regsubmatch` "^([^.].*\\.)?([^.]+)$") !! 1 | m <- maints ]
68normalizePlatforms :: [String] -> [String]
69normalizePlatforms [] = ["ghc.meta.platforms"]
70normalizePlatforms plats = normalizeList
71 [ if '.' `elem` p then p else "stdenv.lib.platforms." ++ p | p <- plats ]