{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE CPP #-}
module Distribution.Nixpkgs.Meta
(
Meta, nullMeta
, homepage, description, license, platforms, badPlatforms, hydraPlatforms, mainProgram, maintainers, broken
, NixpkgsPlatform (..)
, nixpkgsPlatformFromString
) where
#if MIN_VERSION_base(4,11,0)
import Prelude hiding ( (<>) )
#endif
import Control.Applicative ( (<|>) )
import Control.DeepSeq
import Control.Lens hiding ( Strict )
import Data.List ( stripPrefix )
import Data.Set ( Set )
import qualified Data.Set as Set
import Distribution.Nixpkgs.License
import qualified Distribution.Pretty as CabalPretty
import Distribution.System
import GHC.Generics ( Generic )
import Language.Nix.Identifier
import Language.Nix.Path ( path )
import Language.Nix.PrettyPrinting
data NixpkgsPlatform
= NixpkgsPlatformSingle Platform
| NixpkgsPlatformGroup Identifier
deriving (Int -> NixpkgsPlatform -> ShowS
[NixpkgsPlatform] -> ShowS
NixpkgsPlatform -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [NixpkgsPlatform] -> ShowS
$cshowList :: [NixpkgsPlatform] -> ShowS
show :: NixpkgsPlatform -> String
$cshow :: NixpkgsPlatform -> String
showsPrec :: Int -> NixpkgsPlatform -> ShowS
$cshowsPrec :: Int -> NixpkgsPlatform -> ShowS
Show, NixpkgsPlatform -> NixpkgsPlatform -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: NixpkgsPlatform -> NixpkgsPlatform -> Bool
$c/= :: NixpkgsPlatform -> NixpkgsPlatform -> Bool
== :: NixpkgsPlatform -> NixpkgsPlatform -> Bool
$c== :: NixpkgsPlatform -> NixpkgsPlatform -> Bool
Eq, Eq NixpkgsPlatform
NixpkgsPlatform -> NixpkgsPlatform -> Bool
NixpkgsPlatform -> NixpkgsPlatform -> Ordering
NixpkgsPlatform -> NixpkgsPlatform -> NixpkgsPlatform
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: NixpkgsPlatform -> NixpkgsPlatform -> NixpkgsPlatform
$cmin :: NixpkgsPlatform -> NixpkgsPlatform -> NixpkgsPlatform
max :: NixpkgsPlatform -> NixpkgsPlatform -> NixpkgsPlatform
$cmax :: NixpkgsPlatform -> NixpkgsPlatform -> NixpkgsPlatform
>= :: NixpkgsPlatform -> NixpkgsPlatform -> Bool
$c>= :: NixpkgsPlatform -> NixpkgsPlatform -> Bool
> :: NixpkgsPlatform -> NixpkgsPlatform -> Bool
$c> :: NixpkgsPlatform -> NixpkgsPlatform -> Bool
<= :: NixpkgsPlatform -> NixpkgsPlatform -> Bool
$c<= :: NixpkgsPlatform -> NixpkgsPlatform -> Bool
< :: NixpkgsPlatform -> NixpkgsPlatform -> Bool
$c< :: NixpkgsPlatform -> NixpkgsPlatform -> Bool
compare :: NixpkgsPlatform -> NixpkgsPlatform -> Ordering
$ccompare :: NixpkgsPlatform -> NixpkgsPlatform -> Ordering
Ord, forall x. Rep NixpkgsPlatform x -> NixpkgsPlatform
forall x. NixpkgsPlatform -> Rep NixpkgsPlatform x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep NixpkgsPlatform x -> NixpkgsPlatform
$cfrom :: forall x. NixpkgsPlatform -> Rep NixpkgsPlatform x
Generic)
nixpkgsPlatformFromCabal :: Platform -> String
nixpkgsPlatformFromCabal :: Platform -> String
nixpkgsPlatformFromCabal (Platform Arch
arch OS
os) = String
"\"" forall a. [a] -> [a] -> [a]
++ String
nixArch forall a. [a] -> [a] -> [a]
++ String
"-" forall a. [a] -> [a] -> [a]
++ String
nixOs forall a. [a] -> [a] -> [a]
++ String
"\""
where nixArch :: String
nixArch =
case Arch
arch of
Arch
I386 -> String
"i686"
Arch
PPC -> String
"powerpc"
Arch
PPC64 -> String
"powerpc64"
Arch
JavaScript -> String
"js"
Arch
_ -> forall a. Pretty a => a -> String
CabalPretty.prettyShow Arch
arch
nixOs :: String
nixOs =
case OS
os of
OS
OSX -> String
"darwin"
OS
_ -> forall a. Pretty a => a -> String
CabalPretty.prettyShow OS
os
nixpkgsPlatformFromString :: String -> Maybe NixpkgsPlatform
nixpkgsPlatformFromString :: String -> Maybe NixpkgsPlatform
nixpkgsPlatformFromString String
s = Maybe NixpkgsPlatform
platformGroup forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Maybe NixpkgsPlatform
singlePlatform
where platformGroup :: Maybe NixpkgsPlatform
platformGroup = do
String
name <- forall a. Eq a => [a] -> [a] -> Maybe [a]
stripPrefix String
"lib.platforms." String
s forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall a. Eq a => [a] -> [a] -> Maybe [a]
stripPrefix String
"platforms." String
s
forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Identifier -> NixpkgsPlatform
NixpkgsPlatformGroup (Iso' Identifier String
ident forall t b. AReview t b -> b -> t
# String
name)
singlePlatform :: Maybe NixpkgsPlatform
singlePlatform = Platform -> NixpkgsPlatform
NixpkgsPlatformSingle forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> Maybe Platform
cabalPlatformFromSystem String
s
cabalPlatformFromSystem :: String -> Maybe Platform
cabalPlatformFromSystem :: String -> Maybe Platform
cabalPlatformFromSystem String
s =
case forall a. (a -> Bool) -> [a] -> ([a], [a])
break (forall a. Eq a => a -> a -> Bool
== Char
'-') String
s of
(String
arch, Char
'-':String
os) ->
if forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
arch Bool -> Bool -> Bool
|| forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
os
then forall a. Maybe a
Nothing
else forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Arch -> OS -> Platform
Platform (String -> Arch
parseArch String
arch) (String -> OS
parseOS String
os)
(String, String)
_ -> forall a. Maybe a
Nothing
where
parseOS :: String -> OS
parseOS = ClassificationStrictness -> String -> OS
classifyOS ClassificationStrictness
Permissive
parseArch :: String -> Arch
parseArch String
as =
case ClassificationStrictness -> String -> Arch
classifyArch ClassificationStrictness
Strict String
as of
OtherArch String
"i686" -> Arch
I386
OtherArch String
"js" -> Arch
JavaScript
OtherArch String
"powerpc" -> Arch
PPC
OtherArch String
"powerpc64" -> Arch
PPC64
Arch
a -> Arch
a
instance Pretty NixpkgsPlatform where
pPrint :: NixpkgsPlatform -> Doc
pPrint (NixpkgsPlatformSingle Platform
p) = String -> Doc
text forall a b. (a -> b) -> a -> b
$ Platform -> String
nixpkgsPlatformFromCabal Platform
p
pPrint (NixpkgsPlatformGroup Identifier
p) = forall a. Pretty a => a -> Doc
pPrint
forall a b. (a -> b) -> a -> b
$ Iso' Path [Identifier]
path forall t b. AReview t b -> b -> t
# [ Iso' Identifier String
ident forall t b. AReview t b -> b -> t
# String
"lib", Iso' Identifier String
ident forall t b. AReview t b -> b -> t
# String
"platforms", Identifier
p ]
instance NFData NixpkgsPlatform
data Meta = Meta
{ Meta -> String
_homepage :: String
, Meta -> String
_description :: String
, Meta -> License
_license :: License
, Meta -> Maybe (Set NixpkgsPlatform)
_platforms :: Maybe (Set NixpkgsPlatform)
, Meta -> Maybe (Set NixpkgsPlatform)
_badPlatforms :: Maybe (Set NixpkgsPlatform)
, Meta -> Maybe (Set NixpkgsPlatform)
_hydraPlatforms :: Maybe (Set NixpkgsPlatform)
, Meta -> Maybe String
_mainProgram :: Maybe String
, Meta -> Set Identifier
_maintainers :: Set Identifier
, Meta -> Bool
_broken :: Bool
}
deriving (Int -> Meta -> ShowS
[Meta] -> ShowS
Meta -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Meta] -> ShowS
$cshowList :: [Meta] -> ShowS
show :: Meta -> String
$cshow :: Meta -> String
showsPrec :: Int -> Meta -> ShowS
$cshowsPrec :: Int -> Meta -> ShowS
Show, Meta -> Meta -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Meta -> Meta -> Bool
$c/= :: Meta -> Meta -> Bool
== :: Meta -> Meta -> Bool
$c== :: Meta -> Meta -> Bool
Eq, Eq Meta
Meta -> Meta -> Bool
Meta -> Meta -> Ordering
Meta -> Meta -> Meta
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Meta -> Meta -> Meta
$cmin :: Meta -> Meta -> Meta
max :: Meta -> Meta -> Meta
$cmax :: Meta -> Meta -> Meta
>= :: Meta -> Meta -> Bool
$c>= :: Meta -> Meta -> Bool
> :: Meta -> Meta -> Bool
$c> :: Meta -> Meta -> Bool
<= :: Meta -> Meta -> Bool
$c<= :: Meta -> Meta -> Bool
< :: Meta -> Meta -> Bool
$c< :: Meta -> Meta -> Bool
compare :: Meta -> Meta -> Ordering
$ccompare :: Meta -> Meta -> Ordering
Ord, forall x. Rep Meta x -> Meta
forall x. Meta -> Rep Meta x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Meta x -> Meta
$cfrom :: forall x. Meta -> Rep Meta x
Generic)
makeLenses ''Meta
instance NFData Meta
instance Pretty Meta where
pPrint :: Meta -> Doc
pPrint Meta {Bool
String
Maybe String
Maybe (Set NixpkgsPlatform)
Set Identifier
License
_broken :: Bool
_maintainers :: Set Identifier
_mainProgram :: Maybe String
_hydraPlatforms :: Maybe (Set NixpkgsPlatform)
_badPlatforms :: Maybe (Set NixpkgsPlatform)
_platforms :: Maybe (Set NixpkgsPlatform)
_license :: License
_description :: String
_homepage :: String
_broken :: Meta -> Bool
_maintainers :: Meta -> Set Identifier
_mainProgram :: Meta -> Maybe String
_hydraPlatforms :: Meta -> Maybe (Set NixpkgsPlatform)
_badPlatforms :: Meta -> Maybe (Set NixpkgsPlatform)
_platforms :: Meta -> Maybe (Set NixpkgsPlatform)
_license :: Meta -> License
_description :: Meta -> String
_homepage :: Meta -> String
..} = [Doc] -> Doc
vcat
[ Bool -> Doc -> Doc
onlyIf (Bool -> Bool
not (forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
_homepage)) forall a b. (a -> b) -> a -> b
$ String -> Doc -> Doc
attr String
"homepage" forall a b. (a -> b) -> a -> b
$ String -> Doc
string String
_homepage
, Bool -> Doc -> Doc
onlyIf (Bool -> Bool
not (forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
_description)) forall a b. (a -> b) -> a -> b
$ String -> Doc -> Doc
attr String
"description" forall a b. (a -> b) -> a -> b
$ String -> Doc
string String
_description
, String -> Doc -> Doc
attr String
"license" forall a b. (a -> b) -> a -> b
$ forall a. Pretty a => a -> Doc
pPrint License
_license
, forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty (String -> Set NixpkgsPlatform -> Doc
renderPlatforms String
"platforms") Maybe (Set NixpkgsPlatform)
_platforms
, forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty (String -> Set NixpkgsPlatform -> Doc
renderPlatforms String
"badPlatforms") Maybe (Set NixpkgsPlatform)
_badPlatforms
, forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty (String -> Set NixpkgsPlatform -> Doc
renderPlatforms String
"hydraPlatforms") Maybe (Set NixpkgsPlatform)
_hydraPlatforms
, forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty (String -> Doc -> Doc
attr String
"mainProgram" forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Doc
string) Maybe String
_mainProgram
, String -> Doc -> [Doc] -> Doc
listattrDoc String
"maintainers" forall a. Monoid a => a
mempty forall a b. (a -> b) -> a -> b
$ Set Identifier -> [Doc]
renderMaintainers Set Identifier
_maintainers
, String -> Bool -> Bool -> Doc
boolattr String
"broken" Bool
_broken Bool
_broken
]
renderPlatforms :: String -> Set NixpkgsPlatform -> Doc
renderPlatforms :: String -> Set NixpkgsPlatform -> Doc
renderPlatforms String
field Set NixpkgsPlatform
ps
| forall a. Set a -> Bool
Set.null Set NixpkgsPlatform
ps = [Doc] -> Doc
sep [ String -> Doc
text String
field Doc -> Doc -> Doc
<+> Doc
equals Doc -> Doc -> Doc
<+> String -> Doc
text String
"lib.platforms.none" Doc -> Doc -> Doc
<> Doc
semi ]
| Bool
otherwise =
[Doc] -> Doc
sep [ String -> Doc
text String
field Doc -> Doc -> Doc
<+> Doc
equals Doc -> Doc -> Doc
<+> forall {p}. Monoid p => Bool -> p -> p
unless (forall a. Set a -> Bool
Set.null Set NixpkgsPlatform
cabalPs) Doc
lbrack
, forall {p}. Monoid p => Bool -> p -> p
unless (forall a. Set a -> Bool
Set.null Set NixpkgsPlatform
cabalPs) (Int -> Doc -> Doc
nest Int
2 forall a b. (a -> b) -> a -> b
$ [Doc] -> Doc
fsep [Doc]
renderedCabalPs)
, forall {p}. Monoid p => Bool -> p -> p
unless (forall a. Set a -> Bool
Set.null Set NixpkgsPlatform
cabalPs) Doc
rbrack
, forall {p}. Monoid p => Bool -> p -> p
unless (forall a. Set a -> Bool
Set.null Set NixpkgsPlatform
nixpkgsPs) (Int -> Doc -> Doc
nest Int
2 Doc
renderedNixpkgsPs)
]
Doc -> Doc -> Doc
<> Doc
semi
where
(Set NixpkgsPlatform
cabalPs, Set NixpkgsPlatform
nixpkgsPs) = forall a. (a -> Bool) -> Set a -> (Set a, Set a)
Set.partition NixpkgsPlatform -> Bool
isSinglePlatform Set NixpkgsPlatform
ps
isSinglePlatform :: NixpkgsPlatform -> Bool
isSinglePlatform (NixpkgsPlatformSingle Platform
_) = Bool
True
isSinglePlatform NixpkgsPlatform
_ = Bool
False
renderedCabalPs :: [Doc]
renderedCabalPs = forall a b. (a -> b) -> [a] -> [b]
map forall a. Pretty a => a -> Doc
pPrint forall a b. (a -> b) -> a -> b
$ forall a. Set a -> [a]
Set.toAscList Set NixpkgsPlatform
cabalPs
appendNixpkgsP :: Doc -> a -> Doc
appendNixpkgsP Doc
acc a
elem = Doc
acc Doc -> Doc -> Doc
$$
if Doc -> Bool
isEmpty Doc
acc Bool -> Bool -> Bool
&& forall a. Set a -> Bool
Set.null Set NixpkgsPlatform
cabalPs
then Int -> Doc -> Doc
nest Int
3 forall a b. (a -> b) -> a -> b
$ forall a. Pretty a => a -> Doc
pPrint a
elem
else String -> Doc
text String
"++" Doc -> Doc -> Doc
<+> forall a. Pretty a => a -> Doc
pPrint a
elem
renderedNixpkgsPs :: Doc
renderedNixpkgsPs = forall a b. (a -> b -> a) -> a -> Set b -> a
Set.foldl' forall {a}. Pretty a => Doc -> a -> Doc
appendNixpkgsP forall a. Monoid a => a
mempty Set NixpkgsPlatform
nixpkgsPs
unless :: Bool -> p -> p
unless Bool
False p
x = p
x
unless Bool
True p
_ = forall a. Monoid a => a
mempty
renderMaintainers :: Set Identifier -> [Doc]
renderMaintainers :: Set Identifier -> [Doc]
renderMaintainers = forall a b. (a -> b) -> [a] -> [b]
map (forall a. Pretty a => a -> Doc
pPrint forall b c a. (b -> c) -> (a -> b) -> a -> c
. Identifier -> Path
toPath) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> String) -> Set a -> [a]
toAscListSortedOn (forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Iso' Identifier String
ident)
where toPath :: Identifier -> Path
toPath Identifier
m = Iso' Path [Identifier]
path forall t b. AReview t b -> b -> t
# [ Iso' Identifier String
ident forall t b. AReview t b -> b -> t
# String
"lib", Iso' Identifier String
ident forall t b. AReview t b -> b -> t
# String
"maintainers", Identifier
m]
nullMeta :: Meta
nullMeta :: Meta
nullMeta = Meta
{ _homepage :: String
_homepage = forall a. HasCallStack => String -> a
error String
"undefined Meta.homepage"
, _description :: String
_description = forall a. HasCallStack => String -> a
error String
"undefined Meta.description"
, _license :: License
_license = forall a. HasCallStack => String -> a
error String
"undefined Meta.license"
, _platforms :: Maybe (Set NixpkgsPlatform)
_platforms = forall a. HasCallStack => String -> a
error String
"undefined Meta.platforms"
, _badPlatforms :: Maybe (Set NixpkgsPlatform)
_badPlatforms = forall a. HasCallStack => String -> a
error String
"undefined Meta.badPlatforms"
, _hydraPlatforms :: Maybe (Set NixpkgsPlatform)
_hydraPlatforms = forall a. HasCallStack => String -> a
error String
"undefined Meta.hydraPlatforms"
, _mainProgram :: Maybe String
_mainProgram = forall a. HasCallStack => String -> a
error String
"undefined Meta.mainProgram"
, _maintainers :: Set Identifier
_maintainers = forall a. HasCallStack => String -> a
error String
"undefined Meta.maintainers"
, _broken :: Bool
_broken = forall a. HasCallStack => String -> a
error String
"undefined Meta.broken"
}