{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE UndecidableInstances #-}

module Shrun.Configuration.Data.MergedConfig
  ( MergedConfig (..),
  )
where

import Shrun.Command.Types (CommandP1)
import Shrun.Configuration.Data.Core (CoreConfigMerged)
import Shrun.Configuration.Data.Graph (CommandGraph)
import Shrun.Prelude

-- | Merged Args + Toml
data MergedConfig notifyEnv = MkMergedConfig
  { -- | Core config.
    forall notifyEnv.
MergedConfig notifyEnv -> CoreConfigMerged notifyEnv
coreConfig :: CoreConfigMerged notifyEnv,
    -- | Command graph.
    forall notifyEnv. MergedConfig notifyEnv -> CommandGraph
commandGraph :: CommandGraph,
    -- | Commands.
    forall notifyEnv. MergedConfig notifyEnv -> NESeq CommandP1
commands :: NESeq CommandP1,
    -- | Whether to print the config.
    forall notifyEnv. MergedConfig notifyEnv -> Bool
dryRun :: Bool,
    -- | Toml paths used in this config.
    forall notifyEnv. MergedConfig notifyEnv -> Seq OsPath
tomlPaths :: Seq OsPath
  }
  deriving stock (MergedConfig notifyEnv -> MergedConfig notifyEnv -> Bool
(MergedConfig notifyEnv -> MergedConfig notifyEnv -> Bool)
-> (MergedConfig notifyEnv -> MergedConfig notifyEnv -> Bool)
-> Eq (MergedConfig notifyEnv)
forall notifyEnv.
MergedConfig notifyEnv -> MergedConfig notifyEnv -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall notifyEnv.
MergedConfig notifyEnv -> MergedConfig notifyEnv -> Bool
== :: MergedConfig notifyEnv -> MergedConfig notifyEnv -> Bool
$c/= :: forall notifyEnv.
MergedConfig notifyEnv -> MergedConfig notifyEnv -> Bool
/= :: MergedConfig notifyEnv -> MergedConfig notifyEnv -> Bool
Eq, Int -> MergedConfig notifyEnv -> ShowS
[MergedConfig notifyEnv] -> ShowS
MergedConfig notifyEnv -> String
(Int -> MergedConfig notifyEnv -> ShowS)
-> (MergedConfig notifyEnv -> String)
-> ([MergedConfig notifyEnv] -> ShowS)
-> Show (MergedConfig notifyEnv)
forall notifyEnv. Int -> MergedConfig notifyEnv -> ShowS
forall notifyEnv. [MergedConfig notifyEnv] -> ShowS
forall notifyEnv. MergedConfig notifyEnv -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall notifyEnv. Int -> MergedConfig notifyEnv -> ShowS
showsPrec :: Int -> MergedConfig notifyEnv -> ShowS
$cshow :: forall notifyEnv. MergedConfig notifyEnv -> String
show :: MergedConfig notifyEnv -> String
$cshowList :: forall notifyEnv. [MergedConfig notifyEnv] -> ShowS
showList :: [MergedConfig notifyEnv] -> ShowS
Show)

instance
  ( k ~ A_Lens,
    a ~ CoreConfigMerged r,
    b ~ CoreConfigMerged r
  ) =>
  LabelOptic "coreConfig" k (MergedConfig r) (MergedConfig r) a b
  where
  labelOptic :: Optic k NoIx (MergedConfig r) (MergedConfig r) a b
labelOptic =
    LensVL (MergedConfig r) (MergedConfig r) a b
-> Lens (MergedConfig r) (MergedConfig r) a b
forall s t a b. LensVL s t a b -> Lens s t a b
lensVL
      (LensVL (MergedConfig r) (MergedConfig r) a b
 -> Lens (MergedConfig r) (MergedConfig r) a b)
-> LensVL (MergedConfig r) (MergedConfig r) a b
-> Lens (MergedConfig r) (MergedConfig r) a b
forall a b. (a -> b) -> a -> b
$ \a -> f b
f (MkMergedConfig CoreConfigMerged r
a1 CommandGraph
a2 NESeq CommandP1
a3 Bool
a4 Seq OsPath
a5) ->
        (CoreConfigMerged r -> MergedConfig r)
-> f (CoreConfigMerged r) -> f (MergedConfig r)
forall a b. (a -> b) -> f a -> f b
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap
          (\CoreConfigMerged r
b -> CoreConfigMerged r
-> CommandGraph
-> NESeq CommandP1
-> Bool
-> Seq OsPath
-> MergedConfig r
forall notifyEnv.
CoreConfigMerged notifyEnv
-> CommandGraph
-> NESeq CommandP1
-> Bool
-> Seq OsPath
-> MergedConfig notifyEnv
MkMergedConfig CoreConfigMerged r
b CommandGraph
a2 NESeq CommandP1
a3 Bool
a4 Seq OsPath
a5)
          (a -> f b
f a
CoreConfigMerged r
a1)
  {-# INLINE labelOptic #-}

instance
  ( k ~ A_Lens,
    a ~ CommandGraph,
    b ~ CommandGraph
  ) =>
  LabelOptic "commandGraph" k (MergedConfig r) (MergedConfig r) a b
  where
  labelOptic :: Optic k NoIx (MergedConfig r) (MergedConfig r) a b
labelOptic =
    LensVL (MergedConfig r) (MergedConfig r) a b
-> Lens (MergedConfig r) (MergedConfig r) a b
forall s t a b. LensVL s t a b -> Lens s t a b
lensVL
      (LensVL (MergedConfig r) (MergedConfig r) a b
 -> Lens (MergedConfig r) (MergedConfig r) a b)
-> LensVL (MergedConfig r) (MergedConfig r) a b
-> Lens (MergedConfig r) (MergedConfig r) a b
forall a b. (a -> b) -> a -> b
$ \a -> f b
f (MkMergedConfig CoreConfigMerged r
a1 CommandGraph
a2 NESeq CommandP1
a3 Bool
a4 Seq OsPath
a5) ->
        (CommandGraph -> MergedConfig r)
-> f CommandGraph -> f (MergedConfig r)
forall a b. (a -> b) -> f a -> f b
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap
          (\CommandGraph
b -> CoreConfigMerged r
-> CommandGraph
-> NESeq CommandP1
-> Bool
-> Seq OsPath
-> MergedConfig r
forall notifyEnv.
CoreConfigMerged notifyEnv
-> CommandGraph
-> NESeq CommandP1
-> Bool
-> Seq OsPath
-> MergedConfig notifyEnv
MkMergedConfig CoreConfigMerged r
a1 CommandGraph
b NESeq CommandP1
a3 Bool
a4 Seq OsPath
a5)
          (a -> f b
f a
CommandGraph
a2)
  {-# INLINE labelOptic #-}

instance
  ( k ~ A_Lens,
    a ~ NESeq CommandP1,
    b ~ NESeq CommandP1
  ) =>
  LabelOptic "commands" k (MergedConfig r) (MergedConfig r) a b
  where
  labelOptic :: Optic k NoIx (MergedConfig r) (MergedConfig r) a b
labelOptic =
    LensVL (MergedConfig r) (MergedConfig r) a b
-> Lens (MergedConfig r) (MergedConfig r) a b
forall s t a b. LensVL s t a b -> Lens s t a b
lensVL
      (LensVL (MergedConfig r) (MergedConfig r) a b
 -> Lens (MergedConfig r) (MergedConfig r) a b)
-> LensVL (MergedConfig r) (MergedConfig r) a b
-> Lens (MergedConfig r) (MergedConfig r) a b
forall a b. (a -> b) -> a -> b
$ \a -> f b
f (MkMergedConfig CoreConfigMerged r
a1 CommandGraph
a2 NESeq CommandP1
a3 Bool
a4 Seq OsPath
a5) ->
        (NESeq CommandP1 -> MergedConfig r)
-> f (NESeq CommandP1) -> f (MergedConfig r)
forall a b. (a -> b) -> f a -> f b
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap
          (\NESeq CommandP1
b -> CoreConfigMerged r
-> CommandGraph
-> NESeq CommandP1
-> Bool
-> Seq OsPath
-> MergedConfig r
forall notifyEnv.
CoreConfigMerged notifyEnv
-> CommandGraph
-> NESeq CommandP1
-> Bool
-> Seq OsPath
-> MergedConfig notifyEnv
MkMergedConfig CoreConfigMerged r
a1 CommandGraph
a2 NESeq CommandP1
b Bool
a4 Seq OsPath
a5)
          (a -> f b
f a
NESeq CommandP1
a3)
  {-# INLINE labelOptic #-}

instance
  ( k ~ A_Lens,
    a ~ Bool,
    b ~ Bool
  ) =>
  LabelOptic "dryRun" k (MergedConfig r) (MergedConfig r) a b
  where
  labelOptic :: Optic k NoIx (MergedConfig r) (MergedConfig r) a b
labelOptic =
    LensVL (MergedConfig r) (MergedConfig r) a b
-> Lens (MergedConfig r) (MergedConfig r) a b
forall s t a b. LensVL s t a b -> Lens s t a b
lensVL
      (LensVL (MergedConfig r) (MergedConfig r) a b
 -> Lens (MergedConfig r) (MergedConfig r) a b)
-> LensVL (MergedConfig r) (MergedConfig r) a b
-> Lens (MergedConfig r) (MergedConfig r) a b
forall a b. (a -> b) -> a -> b
$ \a -> f b
f (MkMergedConfig CoreConfigMerged r
a1 CommandGraph
a2 NESeq CommandP1
a3 Bool
a4 Seq OsPath
a5) ->
        (Bool -> MergedConfig r) -> f Bool -> f (MergedConfig r)
forall a b. (a -> b) -> f a -> f b
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap
          (\Bool
b -> CoreConfigMerged r
-> CommandGraph
-> NESeq CommandP1
-> Bool
-> Seq OsPath
-> MergedConfig r
forall notifyEnv.
CoreConfigMerged notifyEnv
-> CommandGraph
-> NESeq CommandP1
-> Bool
-> Seq OsPath
-> MergedConfig notifyEnv
MkMergedConfig CoreConfigMerged r
a1 CommandGraph
a2 NESeq CommandP1
a3 Bool
b Seq OsPath
a5)
          (a -> f b
f a
Bool
a4)
  {-# INLINE labelOptic #-}

instance
  ( k ~ A_Lens,
    a ~ Seq OsPath,
    b ~ Seq OsPath
  ) =>
  LabelOptic "tomlPaths" k (MergedConfig r) (MergedConfig r) a b
  where
  labelOptic :: Optic k NoIx (MergedConfig r) (MergedConfig r) a b
labelOptic =
    LensVL (MergedConfig r) (MergedConfig r) a b
-> Lens (MergedConfig r) (MergedConfig r) a b
forall s t a b. LensVL s t a b -> Lens s t a b
lensVL
      (LensVL (MergedConfig r) (MergedConfig r) a b
 -> Lens (MergedConfig r) (MergedConfig r) a b)
-> LensVL (MergedConfig r) (MergedConfig r) a b
-> Lens (MergedConfig r) (MergedConfig r) a b
forall a b. (a -> b) -> a -> b
$ \a -> f b
f (MkMergedConfig CoreConfigMerged r
a1 CommandGraph
a2 NESeq CommandP1
a3 Bool
a4 Seq OsPath
a5) ->
        (Seq OsPath -> MergedConfig r)
-> f (Seq OsPath) -> f (MergedConfig r)
forall a b. (a -> b) -> f a -> f b
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap
          (\Seq OsPath
b -> CoreConfigMerged r
-> CommandGraph
-> NESeq CommandP1
-> Bool
-> Seq OsPath
-> MergedConfig r
forall notifyEnv.
CoreConfigMerged notifyEnv
-> CommandGraph
-> NESeq CommandP1
-> Bool
-> Seq OsPath
-> MergedConfig notifyEnv
MkMergedConfig CoreConfigMerged r
a1 CommandGraph
a2 NESeq CommandP1
a3 Bool
a4 Seq OsPath
b)
          (a -> f b
f a
Seq OsPath
a5)
  {-# INLINE labelOptic #-}

instance Pretty (MergedConfig r) where
  pretty :: forall ann. MergedConfig r -> Doc ann
pretty MergedConfig r
c =
    [Doc ann] -> Doc ann
forall ann. [Doc ann] -> Doc ann
vcat
      ([Doc ann] -> Doc ann)
-> (Seq (Doc ann) -> [Doc ann]) -> Seq (Doc ann) -> Doc ann
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> Type) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Seq (Doc ann) -> [Doc ann]
forall a. Seq a -> [a]
forall (t :: Type -> Type) a. Foldable t => t a -> [a]
toList
      (Seq (Doc ann) -> Doc ann) -> Seq (Doc ann) -> Doc ann
forall a b. (a -> b) -> a -> b
$ Seq (Doc ann)
prettyConfigPaths
      Seq (Doc ann) -> Seq (Doc ann) -> Seq (Doc ann)
forall a. Semigroup a => a -> a -> a
<> [ Item (Seq (Doc ann))
Doc ann
"config:",
           Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann
indentField (Doc ann -> Doc ann) -> Doc ann -> Doc ann
forall a b. (a -> b) -> a -> b
$ CoreConfigMerged r -> Doc ann
forall a ann. Pretty a => a -> Doc ann
forall ann. CoreConfigMerged r -> Doc ann
pretty (CoreConfigMerged r -> Doc ann) -> CoreConfigMerged r -> Doc ann
forall a b. (a -> b) -> a -> b
$ MergedConfig r
c MergedConfig r
-> Optic' A_Lens NoIx (MergedConfig r) (CoreConfigMerged r)
-> CoreConfigMerged r
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx (MergedConfig r) (CoreConfigMerged r)
#coreConfig,
           Item (Seq (Doc ann))
Doc ann
"command-graph:",
           Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann
indentField (Doc ann -> Doc ann) -> Doc ann -> Doc ann
forall a b. (a -> b) -> a -> b
$ CommandGraph -> Doc ann
forall a ann. Pretty a => a -> Doc ann
forall ann. CommandGraph -> Doc ann
pretty (CommandGraph -> Doc ann) -> CommandGraph -> Doc ann
forall a b. (a -> b) -> a -> b
$ MergedConfig r
c MergedConfig r
-> Optic' A_Lens NoIx (MergedConfig r) CommandGraph -> CommandGraph
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx (MergedConfig r) CommandGraph
#commandGraph,
           Item (Seq (Doc ann))
Doc ann
"commands:",
           Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann
indentField Doc ann
prettyCommands
         ]
    where
      prettyCommands :: Doc ann
prettyCommands =
        [Doc ann] -> Doc ann
forall ann. [Doc ann] -> Doc ann
vcat
          ([Doc ann] -> Doc ann)
-> (NESeq CommandP1 -> [Doc ann]) -> NESeq CommandP1 -> Doc ann
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> Type) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. NESeq (Doc ann) -> [Doc ann]
forall a. NESeq a -> [a]
forall (t :: Type -> Type) a. Foldable t => t a -> [a]
toList
          (NESeq (Doc ann) -> [Doc ann])
-> (NESeq CommandP1 -> NESeq (Doc ann))
-> NESeq CommandP1
-> [Doc ann]
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> Type) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (CommandP1 -> Doc ann) -> NESeq CommandP1 -> NESeq (Doc ann)
forall a b. (a -> b) -> NESeq a -> NESeq b
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap CommandP1 -> Doc ann
forall {k} {k} {a} {a} {s} {ann}.
(Is k A_Getter, Is k A_Getter, Pretty a, Pretty a,
 LabelOptic "index" k s s a a, LabelOptic "command" k s s a a) =>
s -> Doc ann
prettyCommand
          (NESeq CommandP1 -> Doc ann) -> NESeq CommandP1 -> Doc ann
forall a b. (a -> b) -> a -> b
$ MergedConfig r
c
          MergedConfig r
-> Optic' A_Lens NoIx (MergedConfig r) (NESeq CommandP1)
-> NESeq CommandP1
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx (MergedConfig r) (NESeq CommandP1)
#commands

      prettyCommand :: s -> Doc ann
prettyCommand s
cmd =
        [Doc ann] -> Doc ann
forall a. Monoid a => [a] -> a
mconcat
          [ a -> Doc ann
forall ann. a -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty (s
cmd s -> Optic' k NoIx s a -> a
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' k NoIx s a
#index),
            Item [Doc ann]
Doc ann
". ",
            a -> Doc ann
forall ann. a -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty (s
cmd s -> Optic' k NoIx s a -> a
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' k NoIx s a
#command)
          ]

      prettyConfigPaths :: Seq (Doc ann)
prettyConfigPaths = case MergedConfig r
c MergedConfig r
-> Optic' A_Lens NoIx (MergedConfig r) (Seq OsPath) -> Seq OsPath
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. Optic' A_Lens NoIx (MergedConfig r) (Seq OsPath)
#tomlPaths of
        Seq OsPath
Empty -> [Item (Seq (Doc ann))
Doc ann
"config-paths: off"]
        ps :: Seq OsPath
ps@(OsPath
_ :<| Seq OsPath
_) ->
          Doc ann
"config-paths:"
            Doc ann -> Seq (Doc ann) -> Seq (Doc ann)
forall a. a -> Seq a -> Seq a
:<| (Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann
indentField (Doc ann -> Doc ann) -> (OsPath -> Doc ann) -> OsPath -> Doc ann
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> Type) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (Doc ann
"- " Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<>) (Doc ann -> Doc ann) -> (OsPath -> Doc ann) -> OsPath -> Doc ann
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> Type) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. String -> Doc ann
forall ann. String -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty (String -> Doc ann) -> (OsPath -> String) -> OsPath -> Doc ann
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> Type) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. OsPath -> String
decodeLenient (OsPath -> Doc ann) -> Seq OsPath -> Seq (Doc ann)
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> Seq OsPath
ps)