git.iko.soy/typeable/cabal2nix
on 1b92b21 help sign in
60 lines 2.2 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
12normalize :: Derivation -> Derivation
13normalize deriv@(MkDerivation {..}) = deriv
14 { buildDepends = normalizeNixNames (filter (`notElem` (pname : corePackages)) buildDepends)
15 , testDepends = normalizeNixNames (filter (`notElem` (pname : corePackages)) testDepends)
16 , buildTools = normalizeNixBuildTools (filter (`notElem` coreBuildTools) buildTools)
17 , extraLibs = normalizeNixLibs extraLibs
18 , pkgConfDeps = normalizeNixLibs pkgConfDeps
19 , metaSection = normalizeMeta metaSection
20 }
22normalizeMeta :: Meta -> Meta
23normalizeMeta meta@(Meta {..}) = meta
24 { description = normalizeDescription description
25 , maintainers = normalizeMaintainers maintainers
26 , platforms = normalizePlatforms platforms
27 }
29normalizeDescription :: String -> String
30normalizeDescription desc
31 | null desc = []
32 | last desc == '.' && length (filter ('.'==) desc) == 1 = normalizeDescription (init desc)
33 | otherwise = quote (unwords (words desc))
35quote :: String -> String
36quote ('\\':c:cs) = '\\' : c : quote cs
37quote ('"':cs) = '\\' : '"' : quote cs
38quote (c:cs) = c : quote cs
39quote [] = []
41normalizeList :: [String] -> [String]
42normalizeList = nub . sortBy (compare `on` map toLower)
44normalizeNixNames :: [String] -> [String]
45normalizeNixNames = normalizeList . map toNixName
47normalizeNixLibs :: [String] -> [String]
48normalizeNixLibs = normalizeList . concatMap libNixName
50normalizeNixBuildTools :: [String] -> [String]
51normalizeNixBuildTools = normalizeList . concatMap buildToolNixName
53normalizeMaintainers :: [String] -> [String]
54normalizeMaintainers maints = normalizeList
55 [ if '.' `elem` m then m else "self.stdenv.lib.maintainers." ++ m | m <- maints ]
57normalizePlatforms :: [String] -> [String]
58normalizePlatforms [] = ["self.ghc.meta.platforms"]
59normalizePlatforms plats = normalizeList
60 [ if '.' `elem` p then p else "self.stdenv.lib.platforms." ++ p | p <- plats ]