{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}

module Stack.Options.DotParser
  ( dotOptsParser
  , formatSubCommand
  , licenseParser
  , listDepsConstraintsParser
  , listDepsFormatOptsParser
  , listDepsJsonParser
  , listDepsOptsParser
  , listDepsTextParser
  , listDepsTreeParser
  , separatorParser
  , toListDepsOptsParser
  ) where

import           Data.Char ( isSpace )
import           Data.List.Split ( splitOn )
import qualified Data.Set as Set
import qualified Data.Text as T
import           Distribution.Types.PackageName ( mkPackageName )
import           Options.Applicative
                   ( CommandFields, Mod, Parser, auto, command, help, idm, info
                   , long, metavar, option, progDesc, showDefault, strOption
                   , subparser, switch, value
                   )
import           Options.Applicative.Builder.Extra ( boolFlags, textOption )
import           Stack.Dot
                   ( DotOpts (..), ListDepsFormat (..), ListDepsFormatOpts (..)
                   , ListDepsOpts (..)
                   )
import           Stack.Options.BuildParser ( flagsParser, targetsParser )
import           Stack.Prelude

-- | Parser for arguments to `stack dot`

dotOptsParser :: Bool -> Parser DotOpts
dotOptsParser :: Bool -> Parser DotOpts
dotOptsParser Bool
externalDefault = Bool
-> Bool
-> Maybe Int
-> Set PackageName
-> [Text]
-> Map ApplyCLIFlag (Map FlagName Bool)
-> Bool
-> Bool
-> Bool
-> DotOpts
DotOpts
  (Bool
 -> Bool
 -> Maybe Int
 -> Set PackageName
 -> [Text]
 -> Map ApplyCLIFlag (Map FlagName Bool)
 -> Bool
 -> Bool
 -> Bool
 -> DotOpts)
-> Parser Bool
-> Parser
     (Bool
      -> Maybe Int
      -> Set PackageName
      -> [Text]
      -> Map ApplyCLIFlag (Map FlagName Bool)
      -> Bool
      -> Bool
      -> Bool
      -> DotOpts)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Bool
includeExternal
  Parser
  (Bool
   -> Maybe Int
   -> Set PackageName
   -> [Text]
   -> Map ApplyCLIFlag (Map FlagName Bool)
   -> Bool
   -> Bool
   -> Bool
   -> DotOpts)
-> Parser Bool
-> Parser
     (Maybe Int
      -> Set PackageName
      -> [Text]
      -> Map ApplyCLIFlag (Map FlagName Bool)
      -> Bool
      -> Bool
      -> Bool
      -> DotOpts)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Bool
includeBase
  Parser
  (Maybe Int
   -> Set PackageName
   -> [Text]
   -> Map ApplyCLIFlag (Map FlagName Bool)
   -> Bool
   -> Bool
   -> Bool
   -> DotOpts)
-> Parser (Maybe Int)
-> Parser
     (Set PackageName
      -> [Text]
      -> Map ApplyCLIFlag (Map FlagName Bool)
      -> Bool
      -> Bool
      -> Bool
      -> DotOpts)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser (Maybe Int)
depthLimit
  Parser
  (Set PackageName
   -> [Text]
   -> Map ApplyCLIFlag (Map FlagName Bool)
   -> Bool
   -> Bool
   -> Bool
   -> DotOpts)
-> Parser (Set PackageName)
-> Parser
     ([Text]
      -> Map ApplyCLIFlag (Map FlagName Bool)
      -> Bool
      -> Bool
      -> Bool
      -> DotOpts)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Maybe [Char] -> Set PackageName)
-> Parser (Maybe [Char]) -> Parser (Set PackageName)
forall a b. (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Set PackageName
-> ([Char] -> Set PackageName) -> Maybe [Char] -> Set PackageName
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Set PackageName
forall a. Set a
Set.empty (([Char] -> Set PackageName) -> Maybe [Char] -> Set PackageName)
-> ([Char] -> Set PackageName) -> Maybe [Char] -> Set PackageName
forall a b. (a -> b) -> a -> b
$ [PackageName] -> Set PackageName
forall a. Ord a => [a] -> Set a
Set.fromList ([PackageName] -> Set PackageName)
-> ([Char] -> [PackageName]) -> [Char] -> Set PackageName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [PackageName]
splitNames) Parser (Maybe [Char])
prunedPkgs
  Parser
  ([Text]
   -> Map ApplyCLIFlag (Map FlagName Bool)
   -> Bool
   -> Bool
   -> Bool
   -> DotOpts)
-> Parser [Text]
-> Parser
     (Map ApplyCLIFlag (Map FlagName Bool)
      -> Bool -> Bool -> Bool -> DotOpts)
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]
targetsParser
  Parser
  (Map ApplyCLIFlag (Map FlagName Bool)
   -> Bool -> Bool -> Bool -> DotOpts)
-> Parser (Map ApplyCLIFlag (Map FlagName Bool))
-> Parser (Bool -> Bool -> Bool -> DotOpts)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser (Map ApplyCLIFlag (Map FlagName Bool))
flagsParser
  Parser (Bool -> Bool -> Bool -> DotOpts)
-> Parser Bool -> Parser (Bool -> Bool -> DotOpts)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Bool
testTargets
  Parser (Bool -> Bool -> DotOpts)
-> Parser Bool -> Parser (Bool -> DotOpts)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Bool
benchTargets
  Parser (Bool -> DotOpts) -> Parser Bool -> Parser DotOpts
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Bool
globalHints
 where
  includeExternal :: Parser Bool
includeExternal = Bool -> [Char] -> [Char] -> Mod FlagFields Bool -> Parser Bool
boolFlags Bool
externalDefault
    [Char]
"external"
    [Char]
"inclusion of external dependencies."
    Mod FlagFields Bool
forall m. Monoid m => m
idm
  includeBase :: Parser Bool
includeBase = Bool -> [Char] -> [Char] -> Mod FlagFields Bool -> Parser Bool
boolFlags Bool
True
    [Char]
"include-base"
    [Char]
"inclusion of dependencies on base."
    Mod FlagFields Bool
forall m. Monoid m => m
idm
  depthLimit :: Parser (Maybe Int)
depthLimit = Parser Int -> Parser (Maybe Int)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (ReadM Int -> Mod OptionFields Int -> Parser Int
forall a. ReadM a -> Mod OptionFields a -> Parser a
option ReadM Int
forall a. Read a => ReadM a
auto
    (  [Char] -> Mod OptionFields Int
forall (f :: * -> *) a. HasName f => [Char] -> Mod f a
long [Char]
"depth"
    Mod OptionFields Int
-> Mod OptionFields Int -> Mod OptionFields Int
forall a. Semigroup a => a -> a -> a
<> [Char] -> Mod OptionFields Int
forall (f :: * -> *) a. HasMetavar f => [Char] -> Mod f a
metavar [Char]
"DEPTH"
    Mod OptionFields Int
-> Mod OptionFields Int -> Mod OptionFields Int
forall a. Semigroup a => a -> a -> a
<> [Char] -> Mod OptionFields Int
forall (f :: * -> *) a. [Char] -> Mod f a
help [Char]
"Limit the depth of dependency resolution. (default: no limit)"
    ))
  prunedPkgs :: Parser (Maybe [Char])
prunedPkgs = Parser [Char] -> Parser (Maybe [Char])
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Mod OptionFields [Char] -> Parser [Char]
forall s. IsString s => Mod OptionFields s -> Parser s
strOption
    (  [Char] -> Mod OptionFields [Char]
forall (f :: * -> *) a. HasName f => [Char] -> Mod f a
long [Char]
"prune"
    Mod OptionFields [Char]
-> Mod OptionFields [Char] -> Mod OptionFields [Char]
forall a. Semigroup a => a -> a -> a
<> [Char] -> Mod OptionFields [Char]
forall (f :: * -> *) a. HasMetavar f => [Char] -> Mod f a
metavar [Char]
"PACKAGES"
    Mod OptionFields [Char]
-> Mod OptionFields [Char] -> Mod OptionFields [Char]
forall a. Semigroup a => a -> a -> a
<> [Char] -> Mod OptionFields [Char]
forall (f :: * -> *) a. [Char] -> Mod f a
help [Char]
"Prune specified package(s). PACKAGES is a comma-separated list of \
            \package names."
    ))
  testTargets :: Parser Bool
testTargets = Mod FlagFields Bool -> Parser Bool
switch
    (  [Char] -> Mod FlagFields Bool
forall (f :: * -> *) a. HasName f => [Char] -> Mod f a
long [Char]
"test"
    Mod FlagFields Bool -> Mod FlagFields Bool -> Mod FlagFields Bool
forall a. Semigroup a => a -> a -> a
<> [Char] -> Mod FlagFields Bool
forall (f :: * -> *) a. [Char] -> Mod f a
help [Char]
"Consider dependencies of test components."
    )
  benchTargets :: Parser Bool
benchTargets = Mod FlagFields Bool -> Parser Bool
switch
    (  [Char] -> Mod FlagFields Bool
forall (f :: * -> *) a. HasName f => [Char] -> Mod f a
long [Char]
"bench"
    Mod FlagFields Bool -> Mod FlagFields Bool -> Mod FlagFields Bool
forall a. Semigroup a => a -> a -> a
<> [Char] -> Mod FlagFields Bool
forall (f :: * -> *) a. [Char] -> Mod f a
help [Char]
"Consider dependencies of benchmark components."
    )

  splitNames :: String -> [PackageName]
  splitNames :: [Char] -> [PackageName]
splitNames = ([Char] -> PackageName) -> [[Char]] -> [PackageName]
forall a b. (a -> b) -> [a] -> [b]
map
      ( [Char] -> PackageName
mkPackageName
      ([Char] -> PackageName)
-> ([Char] -> [Char]) -> [Char] -> PackageName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Bool) -> [Char] -> [Char]
forall a. (a -> Bool) -> [a] -> [a]
takeWhile (Bool -> Bool
not (Bool -> Bool) -> (Char -> Bool) -> Char -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Bool
isSpace)
      ([Char] -> [Char]) -> ([Char] -> [Char]) -> [Char] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Bool) -> [Char] -> [Char]
forall a. (a -> Bool) -> [a] -> [a]
dropWhile Char -> Bool
isSpace
      )
    ([[Char]] -> [PackageName])
-> ([Char] -> [[Char]]) -> [Char] -> [PackageName]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [Char] -> [[Char]]
forall a. Eq a => [a] -> [a] -> [[a]]
splitOn [Char]
","

  globalHints :: Parser Bool
globalHints = Mod FlagFields Bool -> Parser Bool
switch
    (  [Char] -> Mod FlagFields Bool
forall (f :: * -> *) a. HasName f => [Char] -> Mod f a
long [Char]
"global-hints"
    Mod FlagFields Bool -> Mod FlagFields Bool -> Mod FlagFields Bool
forall a. Semigroup a => a -> a -> a
<> [Char] -> Mod FlagFields Bool
forall (f :: * -> *) a. [Char] -> Mod f a
help [Char]
"Do not require an install GHC; instead, use a hints file for \
            \global packages."
    )

separatorParser :: Parser Text
separatorParser :: Parser Text
separatorParser = (Text -> Text) -> Parser Text -> Parser Text
forall a b. (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap
  Text -> Text
escapeSep
  ( Mod OptionFields Text -> Parser Text
textOption
      (  [Char] -> Mod OptionFields Text
forall (f :: * -> *) a. HasName f => [Char] -> Mod f a
long [Char]
"separator"
      Mod OptionFields Text
-> Mod OptionFields Text -> Mod OptionFields Text
forall a. Semigroup a => a -> a -> a
<> [Char] -> Mod OptionFields Text
forall (f :: * -> *) a. HasMetavar f => [Char] -> Mod f a
metavar [Char]
"SEP"
      Mod OptionFields Text
-> Mod OptionFields Text -> Mod OptionFields Text
forall a. Semigroup a => a -> a -> a
<> [Char] -> Mod OptionFields Text
forall (f :: * -> *) a. [Char] -> Mod f a
help [Char]
"Separator between package name and package version."
      Mod OptionFields Text
-> Mod OptionFields Text -> Mod OptionFields Text
forall a. Semigroup a => a -> a -> a
<> Text -> Mod OptionFields Text
forall (f :: * -> *) a. HasValue f => a -> Mod f a
value Text
" "
      Mod OptionFields Text
-> Mod OptionFields Text -> Mod OptionFields Text
forall a. Semigroup a => a -> a -> a
<> Mod OptionFields Text
forall a (f :: * -> *). Show a => Mod f a
showDefault
      )
  )
 where
  escapeSep :: Text -> Text
escapeSep Text
s = HasCallStack => Text -> Text -> Text -> Text
Text -> Text -> Text -> Text
T.replace Text
"\\t" Text
"\t" (HasCallStack => Text -> Text -> Text -> Text
Text -> Text -> Text -> Text
T.replace Text
"\\n" Text
"\n" Text
s)

licenseParser :: Parser Bool
licenseParser :: Parser Bool
licenseParser = Bool -> [Char] -> [Char] -> Mod FlagFields Bool -> Parser Bool
boolFlags Bool
False
  [Char]
"license"
  [Char]
"printing of dependency licenses instead of versions."
  Mod FlagFields Bool
forall m. Monoid m => m
idm

listDepsFormatOptsParser :: Parser ListDepsFormatOpts
listDepsFormatOptsParser :: Parser ListDepsFormatOpts
listDepsFormatOptsParser = Text -> Bool -> ListDepsFormatOpts
ListDepsFormatOpts
  (Text -> Bool -> ListDepsFormatOpts)
-> Parser Text -> Parser (Bool -> ListDepsFormatOpts)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Text
separatorParser
  Parser (Bool -> ListDepsFormatOpts)
-> Parser Bool -> Parser ListDepsFormatOpts
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Bool
licenseParser

listDepsTreeParser :: Parser ListDepsFormat
listDepsTreeParser :: Parser ListDepsFormat
listDepsTreeParser =  ListDepsFormatOpts -> ListDepsFormat
ListDepsTree (ListDepsFormatOpts -> ListDepsFormat)
-> Parser ListDepsFormatOpts -> Parser ListDepsFormat
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ListDepsFormatOpts
listDepsFormatOptsParser

listDepsTextParser :: Parser ListDepsFormat
listDepsTextParser :: Parser ListDepsFormat
listDepsTextParser = ListDepsFormatOpts -> ListDepsFormat
ListDepsText (ListDepsFormatOpts -> ListDepsFormat)
-> Parser ListDepsFormatOpts -> Parser ListDepsFormat
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ListDepsFormatOpts
listDepsFormatOptsParser

listDepsJsonParser :: Parser ListDepsFormat
listDepsJsonParser :: Parser ListDepsFormat
listDepsJsonParser = ListDepsFormat -> Parser ListDepsFormat
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ListDepsFormat
ListDepsJSON

listDepsConstraintsParser :: Parser ListDepsFormat
listDepsConstraintsParser :: Parser ListDepsFormat
listDepsConstraintsParser = ListDepsFormat -> Parser ListDepsFormat
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ListDepsFormat
ListDepsConstraints

toListDepsOptsParser :: Parser ListDepsFormat -> Parser ListDepsOpts
toListDepsOptsParser :: Parser ListDepsFormat -> Parser ListDepsOpts
toListDepsOptsParser Parser ListDepsFormat
formatParser = ListDepsFormat -> DotOpts -> ListDepsOpts
ListDepsOpts
  (ListDepsFormat -> DotOpts -> ListDepsOpts)
-> Parser ListDepsFormat -> Parser (DotOpts -> ListDepsOpts)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ListDepsFormat
formatParser
  Parser (DotOpts -> ListDepsOpts)
-> Parser DotOpts -> Parser ListDepsOpts
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Bool -> Parser DotOpts
dotOptsParser Bool
True

formatSubCommand ::
     String
  -> String
  -> Parser ListDepsFormat
  -> Mod CommandFields ListDepsOpts
formatSubCommand :: [Char]
-> [Char]
-> Parser ListDepsFormat
-> Mod CommandFields ListDepsOpts
formatSubCommand [Char]
cmd [Char]
desc Parser ListDepsFormat
formatParser =
  [Char] -> ParserInfo ListDepsOpts -> Mod CommandFields ListDepsOpts
forall a. [Char] -> ParserInfo a -> Mod CommandFields a
command [Char]
cmd (Parser ListDepsOpts
-> InfoMod ListDepsOpts -> ParserInfo ListDepsOpts
forall a. Parser a -> InfoMod a -> ParserInfo a
info (Parser ListDepsFormat -> Parser ListDepsOpts
toListDepsOptsParser Parser ListDepsFormat
formatParser) ([Char] -> InfoMod ListDepsOpts
forall a. [Char] -> InfoMod a
progDesc [Char]
desc))

-- | Parser for arguments to `stack ls dependencies`.

listDepsOptsParser :: Parser ListDepsOpts
listDepsOptsParser :: Parser ListDepsOpts
listDepsOptsParser = Mod CommandFields ListDepsOpts -> Parser ListDepsOpts
forall a. Mod CommandFields a -> Parser a
subparser
      (  [Char]
-> [Char]
-> Parser ListDepsFormat
-> Mod CommandFields ListDepsOpts
formatSubCommand
           [Char]
"text"
           [Char]
"Print dependencies as text (default)."
           Parser ListDepsFormat
listDepsTextParser
      Mod CommandFields ListDepsOpts
-> Mod CommandFields ListDepsOpts -> Mod CommandFields ListDepsOpts
forall a. Semigroup a => a -> a -> a
<> [Char]
-> [Char]
-> Parser ListDepsFormat
-> Mod CommandFields ListDepsOpts
formatSubCommand
           [Char]
"cabal"
           [Char]
"Print dependencies as exact Cabal constraints."
           Parser ListDepsFormat
listDepsConstraintsParser
      Mod CommandFields ListDepsOpts
-> Mod CommandFields ListDepsOpts -> Mod CommandFields ListDepsOpts
forall a. Semigroup a => a -> a -> a
<> [Char]
-> [Char]
-> Parser ListDepsFormat
-> Mod CommandFields ListDepsOpts
formatSubCommand
           [Char]
"tree"
           [Char]
"Print dependencies as tree."
           Parser ListDepsFormat
listDepsTreeParser
      Mod CommandFields ListDepsOpts
-> Mod CommandFields ListDepsOpts -> Mod CommandFields ListDepsOpts
forall a. Semigroup a => a -> a -> a
<> [Char]
-> [Char]
-> Parser ListDepsFormat
-> Mod CommandFields ListDepsOpts
formatSubCommand
           [Char]
"json"
           [Char]
"Print dependencies as JSON."
           Parser ListDepsFormat
listDepsJsonParser
      )
  Parser ListDepsOpts -> Parser ListDepsOpts -> Parser ListDepsOpts
forall a. Parser a -> Parser a -> Parser a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser ListDepsFormat -> Parser ListDepsOpts
toListDepsOptsParser Parser ListDepsFormat
listDepsTextParser