{-# LANGUAGE NoImplicitPrelude #-}

module Stack.Options.NixParser
  ( nixOptsParser
  ) where

import qualified Data.Text as T
import           Options.Applicative
                   ( Parser, completer, help, long, metavar, option, str )
import           Options.Applicative.Args ( argsOption )
import           Options.Applicative.Builder.Extra
                   ( fileExtCompleter, firstBoolFlagsFalse
                   , firstBoolFlagsNoDefault, optionalFirst
                   )
import           Stack.Nix ( nixCmdName )
import           Stack.Options.Utils ( hideMods )
import           Stack.Prelude
import           Stack.Types.Nix ( NixOptsMonoid (..) )

nixOptsParser :: Bool -> Parser NixOptsMonoid
nixOptsParser :: Bool -> Parser NixOptsMonoid
nixOptsParser Bool
hide0 = NixOptsMonoid -> NixOptsMonoid
overrideActivation (NixOptsMonoid -> NixOptsMonoid)
-> Parser NixOptsMonoid -> Parser NixOptsMonoid
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
  (   First Bool
-> First Bool
-> First [Text]
-> First String
-> First [Text]
-> First [Text]
-> FirstFalse
-> NixOptsMonoid
NixOptsMonoid
  (First Bool
 -> First Bool
 -> First [Text]
 -> First String
 -> First [Text]
 -> First [Text]
 -> FirstFalse
 -> NixOptsMonoid)
-> Parser (First Bool)
-> Parser
     (First Bool
      -> First [Text]
      -> First String
      -> First [Text]
      -> First [Text]
      -> FirstFalse
      -> NixOptsMonoid)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String
-> String -> Mod FlagFields (Maybe Bool) -> Parser (First Bool)
firstBoolFlagsNoDefault
        String
nixCmdName
        String
"use of a Nix-shell. Implies 'system-ghc: true'."
        Mod FlagFields (Maybe Bool)
forall {f :: * -> *} {a}. Mod f a
hide
  Parser
  (First Bool
   -> First [Text]
   -> First String
   -> First [Text]
   -> First [Text]
   -> FirstFalse
   -> NixOptsMonoid)
-> Parser (First Bool)
-> Parser
     (First [Text]
      -> First String
      -> First [Text]
      -> First [Text]
      -> FirstFalse
      -> NixOptsMonoid)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> String
-> String -> Mod FlagFields (Maybe Bool) -> Parser (First Bool)
firstBoolFlagsNoDefault
        String
"nix-pure"
        String
"use of a pure Nix-shell. Implies '--nix' and 'system-ghc: true'."
        Mod FlagFields (Maybe Bool)
forall {f :: * -> *} {a}. Mod f a
hide
  Parser
  (First [Text]
   -> First String
   -> First [Text]
   -> First [Text]
   -> FirstFalse
   -> NixOptsMonoid)
-> Parser (First [Text])
-> Parser
     (First String
      -> First [Text] -> First [Text] -> FirstFalse -> NixOptsMonoid)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser [Text] -> Parser (First [Text])
forall (f :: * -> *) a. Alternative f => f a -> f (First a)
optionalFirst (Mod OptionFields [String] -> Parser [Text]
textArgsOption
        (  String -> Mod OptionFields [String]
forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"nix-packages"
        Mod OptionFields [String]
-> Mod OptionFields [String] -> Mod OptionFields [String]
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields [String]
forall (f :: * -> *) a. HasMetavar f => String -> Mod f a
metavar String
"NAMES"
        Mod OptionFields [String]
-> Mod OptionFields [String] -> Mod OptionFields [String]
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields [String]
forall (f :: * -> *) a. String -> Mod f a
help String
"List of packages that should be available in the nix-shell \
                \(space separated)."
        Mod OptionFields [String]
-> Mod OptionFields [String] -> Mod OptionFields [String]
forall a. Semigroup a => a -> a -> a
<> Mod OptionFields [String]
forall {f :: * -> *} {a}. Mod f a
hide
        ))
  Parser
  (First String
   -> First [Text] -> First [Text] -> FirstFalse -> NixOptsMonoid)
-> Parser (First String)
-> Parser
     (First [Text] -> First [Text] -> FirstFalse -> NixOptsMonoid)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser String -> Parser (First String)
forall (f :: * -> *) a. Alternative f => f a -> f (First a)
optionalFirst (ReadM String -> Mod OptionFields String -> Parser String
forall a. ReadM a -> Mod OptionFields a -> Parser a
option ReadM String
forall s. IsString s => ReadM s
str
        (  String -> Mod OptionFields String
forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"nix-shell-file"
        Mod OptionFields String
-> Mod OptionFields String -> Mod OptionFields String
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields String
forall (f :: * -> *) a. HasMetavar f => String -> Mod f a
metavar String
"FILE"
        Mod OptionFields String
-> Mod OptionFields String -> Mod OptionFields String
forall a. Semigroup a => a -> a -> a
<> Completer -> Mod OptionFields String
forall (f :: * -> *) a. HasCompleter f => Completer -> Mod f a
completer ([String] -> Completer
fileExtCompleter [String
".nix"])
        Mod OptionFields String
-> Mod OptionFields String -> Mod OptionFields String
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields String
forall (f :: * -> *) a. String -> Mod f a
help String
"Nix file to be used to launch a nix-shell (for regular Nix \
                \users)."
        Mod OptionFields String
-> Mod OptionFields String -> Mod OptionFields String
forall a. Semigroup a => a -> a -> a
<> Mod OptionFields String
forall {f :: * -> *} {a}. Mod f a
hide
        ))
  Parser
  (First [Text] -> First [Text] -> FirstFalse -> NixOptsMonoid)
-> Parser (First [Text])
-> Parser (First [Text] -> FirstFalse -> NixOptsMonoid)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser [Text] -> Parser (First [Text])
forall (f :: * -> *) a. Alternative f => f a -> f (First a)
optionalFirst (Mod OptionFields [String] -> Parser [Text]
textArgsOption
        (  String -> Mod OptionFields [String]
forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"nix-shell-options"
        Mod OptionFields [String]
-> Mod OptionFields [String] -> Mod OptionFields [String]
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields [String]
forall (f :: * -> *) a. HasMetavar f => String -> Mod f a
metavar String
"OPTIONS"
        Mod OptionFields [String]
-> Mod OptionFields [String] -> Mod OptionFields [String]
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields [String]
forall (f :: * -> *) a. String -> Mod f a
help String
"Additional options passed to nix-shell."
        Mod OptionFields [String]
-> Mod OptionFields [String] -> Mod OptionFields [String]
forall a. Semigroup a => a -> a -> a
<> Mod OptionFields [String]
forall {f :: * -> *} {a}. Mod f a
hide
        ))
  Parser (First [Text] -> FirstFalse -> NixOptsMonoid)
-> Parser (First [Text]) -> Parser (FirstFalse -> NixOptsMonoid)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser [Text] -> Parser (First [Text])
forall (f :: * -> *) a. Alternative f => f a -> f (First a)
optionalFirst (Mod OptionFields [String] -> Parser [Text]
textArgsOption
        (  String -> Mod OptionFields [String]
forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"nix-path"
        Mod OptionFields [String]
-> Mod OptionFields [String] -> Mod OptionFields [String]
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields [String]
forall (f :: * -> *) a. HasMetavar f => String -> Mod f a
metavar String
"PATH_OPTIONS"
        Mod OptionFields [String]
-> Mod OptionFields [String] -> Mod OptionFields [String]
forall a. Semigroup a => a -> a -> a
<> String -> Mod OptionFields [String]
forall (f :: * -> *) a. String -> Mod f a
help String
"Additional options to override NIX_PATH parts (notably \
                \'nixpkgs')."
        Mod OptionFields [String]
-> Mod OptionFields [String] -> Mod OptionFields [String]
forall a. Semigroup a => a -> a -> a
<> Mod OptionFields [String]
forall {f :: * -> *} {a}. Mod f a
hide
        ))
  Parser (FirstFalse -> NixOptsMonoid)
-> Parser FirstFalse -> Parser NixOptsMonoid
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> String -> String -> Mod FlagFields FirstFalse -> Parser FirstFalse
firstBoolFlagsFalse
        String
"nix-add-gc-roots"
        String
"addition of packages to the nix GC roots so nix-collect-garbage does \
        \not remove them."
        Mod FlagFields FirstFalse
forall {f :: * -> *} {a}. Mod f a
hide
  )
 where
  hide :: Mod f a
hide = Bool -> Mod f a
forall (f :: * -> *) a. Bool -> Mod f a
hideMods Bool
hide0
  overrideActivation :: NixOptsMonoid -> NixOptsMonoid
overrideActivation NixOptsMonoid
m =
    if Bool -> First Bool -> Bool
forall a. a -> First a -> a
fromFirst Bool
False (NixOptsMonoid -> First Bool
nixMonoidPureShell NixOptsMonoid
m)
      then NixOptsMonoid
m { nixMonoidEnable :: First Bool
nixMonoidEnable = (Maybe Bool -> First Bool
forall a. Maybe a -> First a
First (Maybe Bool -> First Bool)
-> (First Bool -> Maybe Bool) -> First Bool -> First Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> Maybe Bool
forall a. a -> Maybe a
Just (Bool -> Maybe Bool)
-> (First Bool -> Bool) -> First Bool -> Maybe Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> First Bool -> Bool
forall a. a -> First a -> a
fromFirst Bool
True) (NixOptsMonoid -> First Bool
nixMonoidEnable NixOptsMonoid
m) }
      else NixOptsMonoid
m
  textArgsOption :: Mod OptionFields [String] -> Parser [Text]
textArgsOption = ([String] -> [Text]) -> Parser [String] -> Parser [Text]
forall a b. (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((String -> Text) -> [String] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map String -> Text
T.pack) (Parser [String] -> Parser [Text])
-> (Mod OptionFields [String] -> Parser [String])
-> Mod OptionFields [String]
-> Parser [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Mod OptionFields [String] -> Parser [String]
argsOption