{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE UndecidableInstances #-}
module Language.Ninja.IR.Ninja
(
Ninja, makeNinja
, ninjaMeta, ninjaBuilds, ninjaPhonys, ninjaDefaults, ninjaPools
, NinjaConstraint
) where
import qualified Control.Lens as Lens
import Data.Text (Text)
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HM
import Data.HashSet (HashSet)
import qualified Data.HashSet as HS
import Data.Aeson ((.:), (.=))
import qualified Data.Aeson as Aeson
import qualified Data.Versions as Ver
import Control.DeepSeq (NFData)
import Data.Hashable (Hashable)
import GHC.Generics (Generic)
import qualified Test.SmallCheck.Series as SC
import GHC.Exts (Constraint)
import Language.Ninja.IR.Build (Build)
import Language.Ninja.IR.Meta (Meta)
import qualified Language.Ninja.IR.Meta as Ninja
import Language.Ninja.IR.Pool (Pool)
import Language.Ninja.IR.Target (Target)
import Flow ((|>))
data Ninja
= MkNinja
{ _ninjaMeta :: !Meta
, _ninjaBuilds :: !(HashSet Build)
, _ninjaPhonys :: !(HashMap Target (HashSet Target))
, _ninjaDefaults :: !(HashSet Target)
, _ninjaPools :: !(HashSet Pool)
}
deriving (Eq, Show, Generic)
{-# INLINE makeNinja #-}
makeNinja :: Ninja
makeNinja = MkNinja
{ _ninjaMeta = Ninja.makeMeta
, _ninjaBuilds = HS.empty
, _ninjaPhonys = HM.empty
, _ninjaDefaults = HS.empty
, _ninjaPools = HS.empty
}
{-# INLINE ninjaMeta #-}
ninjaMeta :: Lens.Lens' Ninja Meta
ninjaMeta = Lens.lens _ninjaMeta
$ \(MkNinja {..}) x -> MkNinja { _ninjaMeta = x, .. }
{-# INLINE ninjaBuilds #-}
ninjaBuilds :: Lens.Lens' Ninja (HashSet Build)
ninjaBuilds = Lens.lens _ninjaBuilds
$ \(MkNinja {..}) x -> MkNinja { _ninjaBuilds = x, .. }
{-# INLINE ninjaPhonys #-}
ninjaPhonys :: Lens.Lens' Ninja (HashMap Target (HashSet Target))
ninjaPhonys = Lens.lens _ninjaPhonys
$ \(MkNinja {..}) x -> MkNinja { _ninjaPhonys = x, .. }
{-# INLINE ninjaDefaults #-}
ninjaDefaults :: Lens.Lens' Ninja (HashSet Target)
ninjaDefaults = Lens.lens _ninjaDefaults
$ \(MkNinja {..}) x -> MkNinja { _ninjaDefaults = x, .. }
{-# INLINE ninjaPools #-}
ninjaPools :: Lens.Lens' Ninja (HashSet Pool)
ninjaPools = Lens.lens _ninjaPools
$ \(MkNinja {..}) x -> MkNinja { _ninjaPools = x, .. }
instance Aeson.ToJSON Ninja where
toJSON (MkNinja {..})
= [ "meta" .= _ninjaMeta
, "builds" .= _ninjaBuilds
, "phonys" .= _ninjaPhonys
, "defaults" .= _ninjaDefaults
, "pools" .= _ninjaPools
] |> Aeson.object
instance Aeson.FromJSON Ninja where
parseJSON = (Aeson.withObject "Ninja" $ \o -> do
_ninjaMeta <- (o .: "meta") >>= pure
_ninjaBuilds <- (o .: "builds") >>= pure
_ninjaPhonys <- (o .: "phonys") >>= pure
_ninjaDefaults <- (o .: "defaults") >>= pure
_ninjaPools <- (o .: "pools") >>= pure
pure (MkNinja {..}))
instance Hashable Ninja
instance NFData Ninja
instance (Monad m, NinjaConstraint (SC.Serial m)) => SC.Serial m Ninja
instance (Monad m, NinjaConstraint (SC.CoSerial m)) => SC.CoSerial m Ninja
type NinjaConstraint (c :: * -> Constraint)
= ( c Text
, c Ver.Version
, c (HashMap Target (HashSet Target))
, c (HashSet Build)
, c (HashSet Target)
, c (HashSet Pool)
)