-- |
-- Module: Staversion.Internal.Command
-- Description: Command from the user.
-- Maintainer: Toshio Ito <debug.ito@gmail.com>
--
-- __This is an internal module. End-users should not use it.__
module Staversion.Internal.Command
       ( Command(..),
         parseCommandArgs,
         defFormatConfig,
         _parseCommandStrings
       ) where

import Control.Applicative ((<$>), (<*>), optional, some, (<|>), many)
import Data.Function (on)
import Data.Monoid (mconcat, (<>))
import Data.Text (pack)
import qualified Options.Applicative as Opt
import qualified Paths_staversion as MyInfo
import System.Directory (getHomeDirectory)
import System.FilePath ((</>))
import qualified Text.PrettyPrint.ANSI.Leijen as Pretty

import Staversion.Internal.Aggregate (Aggregator)
import qualified Staversion.Internal.Aggregate as Agg
import Staversion.Internal.Format (FormatConfig(..), FormatVersion)
import qualified Staversion.Internal.Format as Format
import Staversion.Internal.Log
  ( LogLevel(..), Logger(loggerThreshold), defaultLogger
  )
import Staversion.Internal.Query
  ( Resolver,
    PackageName,
    Query(..),
    parseQuery,
    PackageSource(..)
  )
import Staversion.Internal.Version (showBaseVersion)

-- | Command from the user.
data Command =
  Command { Command -> [Char]
commBuildPlanDir :: FilePath,
            -- ^ path to the directory where build plan files are stored.
            Command -> [Char]
commStackCommand :: String,
            -- ^ shell command to invoke @stack@ tool.
            Command -> Logger
commLogger :: Logger,
            -- ^ the logger
            Command -> [PackageSource]
commSources :: [PackageSource],
            -- ^ package sources to search
            Command -> [Query]
commQueries :: [Query],
            -- ^ package queries
            Command -> Bool
commAllowNetwork :: Bool,
            -- ^ if 'True', it accesses the Internet to query build plans etc.
            Command -> Maybe Aggregator
commAggregator :: Maybe Aggregator,
            -- ^ if 'Just', do aggregation over the results.
            Command -> FormatConfig
commFormatConfig :: FormatConfig
            -- ^ config for the formatter
          }

-- | Default values for 'Command'.
data DefCommand = DefCommand { DefCommand -> [Char]
defBuildPlanDir :: FilePath
                             } deriving (Int -> DefCommand -> ShowS
[DefCommand] -> ShowS
DefCommand -> [Char]
(Int -> DefCommand -> ShowS)
-> (DefCommand -> [Char])
-> ([DefCommand] -> ShowS)
-> Show DefCommand
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DefCommand -> ShowS
showsPrec :: Int -> DefCommand -> ShowS
$cshow :: DefCommand -> [Char]
show :: DefCommand -> [Char]
$cshowList :: [DefCommand] -> ShowS
showList :: [DefCommand] -> ShowS
Show,DefCommand -> DefCommand -> Bool
(DefCommand -> DefCommand -> Bool)
-> (DefCommand -> DefCommand -> Bool) -> Eq DefCommand
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DefCommand -> DefCommand -> Bool
== :: DefCommand -> DefCommand -> Bool
$c/= :: DefCommand -> DefCommand -> Bool
/= :: DefCommand -> DefCommand -> Bool
Eq,Eq DefCommand
Eq DefCommand =>
(DefCommand -> DefCommand -> Ordering)
-> (DefCommand -> DefCommand -> Bool)
-> (DefCommand -> DefCommand -> Bool)
-> (DefCommand -> DefCommand -> Bool)
-> (DefCommand -> DefCommand -> Bool)
-> (DefCommand -> DefCommand -> DefCommand)
-> (DefCommand -> DefCommand -> DefCommand)
-> Ord DefCommand
DefCommand -> DefCommand -> Bool
DefCommand -> DefCommand -> Ordering
DefCommand -> DefCommand -> DefCommand
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
$ccompare :: DefCommand -> DefCommand -> Ordering
compare :: DefCommand -> DefCommand -> Ordering
$c< :: DefCommand -> DefCommand -> Bool
< :: DefCommand -> DefCommand -> Bool
$c<= :: DefCommand -> DefCommand -> Bool
<= :: DefCommand -> DefCommand -> Bool
$c> :: DefCommand -> DefCommand -> Bool
> :: DefCommand -> DefCommand -> Bool
$c>= :: DefCommand -> DefCommand -> Bool
>= :: DefCommand -> DefCommand -> Bool
$cmax :: DefCommand -> DefCommand -> DefCommand
max :: DefCommand -> DefCommand -> DefCommand
$cmin :: DefCommand -> DefCommand -> DefCommand
min :: DefCommand -> DefCommand -> DefCommand
Ord)

defCommand :: IO DefCommand
defCommand :: IO DefCommand
defCommand = [Char] -> DefCommand
DefCommand ([Char] -> DefCommand) -> IO [Char] -> IO DefCommand
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO [Char]
def_build_plan_dir where
  def_build_plan_dir :: IO [Char]
def_build_plan_dir = do
    [Char]
home <- IO [Char]
getHomeDirectory
    [Char] -> IO [Char]
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Char] -> IO [Char]) -> [Char] -> IO [Char]
forall a b. (a -> b) -> a -> b
$ [Char]
home [Char] -> ShowS
</> [Char]
".stack" [Char] -> ShowS
</> [Char]
"build-plan"

commandParser :: DefCommand -> Opt.Parser Command
commandParser :: DefCommand -> Parser Command
commandParser DefCommand
def_comm = [Char]
-> [Char]
-> Logger
-> [PackageSource]
-> [Query]
-> Bool
-> Maybe Aggregator
-> FormatConfig
-> Command
Command ([Char]
 -> [Char]
 -> Logger
 -> [PackageSource]
 -> [Query]
 -> Bool
 -> Maybe Aggregator
 -> FormatConfig
 -> Command)
-> Parser [Char]
-> Parser
     ([Char]
      -> Logger
      -> [PackageSource]
      -> [Query]
      -> Bool
      -> Maybe Aggregator
      -> FormatConfig
      -> Command)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [Char]
build_plan_dir Parser
  ([Char]
   -> Logger
   -> [PackageSource]
   -> [Query]
   -> Bool
   -> Maybe Aggregator
   -> FormatConfig
   -> Command)
-> Parser [Char]
-> Parser
     (Logger
      -> [PackageSource]
      -> [Query]
      -> Bool
      -> Maybe Aggregator
      -> FormatConfig
      -> Command)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser [Char]
stack_command Parser
  (Logger
   -> [PackageSource]
   -> [Query]
   -> Bool
   -> Maybe Aggregator
   -> FormatConfig
   -> Command)
-> Parser Logger
-> Parser
     ([PackageSource]
      -> [Query] -> Bool -> Maybe Aggregator -> FormatConfig -> Command)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Logger
logger Parser
  ([PackageSource]
   -> [Query] -> Bool -> Maybe Aggregator -> FormatConfig -> Command)
-> Parser [PackageSource]
-> Parser
     ([Query] -> Bool -> Maybe Aggregator -> FormatConfig -> Command)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser [PackageSource]
sources
                         Parser
  ([Query] -> Bool -> Maybe Aggregator -> FormatConfig -> Command)
-> Parser [Query]
-> Parser (Bool -> Maybe Aggregator -> FormatConfig -> Command)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser [Query]
queries Parser (Bool -> Maybe Aggregator -> FormatConfig -> Command)
-> Parser Bool
-> Parser (Maybe Aggregator -> FormatConfig -> Command)
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
network Parser (Maybe Aggregator -> FormatConfig -> Command)
-> Parser (Maybe Aggregator) -> Parser (FormatConfig -> Command)
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 Aggregator)
aggregate Parser (FormatConfig -> Command)
-> Parser FormatConfig -> Parser Command
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser FormatConfig
format_config where
  logger :: Parser Logger
logger = Bool -> Logger
makeLogger (Bool -> Logger) -> Parser Bool -> Parser Logger
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Bool
is_verbose
  makeLogger :: Bool -> Logger
makeLogger Bool
True = Logger
defaultLogger { loggerThreshold = Just LogDebug }
  makeLogger Bool
False = Logger
defaultLogger
  is_verbose :: Parser Bool
is_verbose = Mod FlagFields Bool -> Parser Bool
Opt.switch (Mod FlagFields Bool -> Parser Bool)
-> Mod FlagFields Bool -> Parser Bool
forall a b. (a -> b) -> a -> b
$ [Mod FlagFields Bool] -> Mod FlagFields Bool
forall a. Monoid a => [a] -> a
mconcat [ [Char] -> Mod FlagFields Bool
forall (f :: * -> *) a. HasName f => [Char] -> Mod f a
Opt.long [Char]
"verbose",
                                      Char -> Mod FlagFields Bool
forall (f :: * -> *) a. HasName f => Char -> Mod f a
Opt.short Char
'v',
                                      [Char] -> Mod FlagFields Bool
forall (f :: * -> *) a. [Char] -> Mod f a
Opt.help [Char]
"Verbose messages."
                                    ]
  build_plan_dir :: Parser [Char]
build_plan_dir = Mod OptionFields [Char] -> Parser [Char]
forall s. IsString s => Mod OptionFields s -> Parser s
Opt.strOption
                   (Mod OptionFields [Char] -> Parser [Char])
-> Mod OptionFields [Char] -> Parser [Char]
forall a b. (a -> b) -> a -> b
$ [Mod OptionFields [Char]] -> Mod OptionFields [Char]
forall a. Monoid a => [a] -> a
mconcat [ [Char] -> Mod OptionFields [Char]
forall (f :: * -> *) a. HasName f => [Char] -> Mod f a
Opt.long [Char]
"build-plan-dir",
                               [Char] -> Mod OptionFields [Char]
forall (f :: * -> *) a. [Char] -> Mod f a
Opt.help [Char]
"Directory where build plan YAML files are stored.",
                               [Char] -> Mod OptionFields [Char]
forall (f :: * -> *) a. HasMetavar f => [Char] -> Mod f a
Opt.metavar [Char]
"DIR",
                               [Char] -> Mod OptionFields [Char]
forall (f :: * -> *) a. HasValue f => a -> Mod f a
Opt.value (DefCommand -> [Char]
defBuildPlanDir DefCommand
def_comm),
                               Mod OptionFields [Char]
forall a (f :: * -> *). Show a => Mod f a
Opt.showDefault
                             ]
  withDefault :: Functor m => [a] -> m [a] -> m [a]
  withDefault :: forall (m :: * -> *) a. Functor m => [a] -> m [a] -> m [a]
withDefault [a]
def_vals = ([a] -> [a]) -> m [a] -> m [a]
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [a] -> [a]
applyDef
    where
      applyDef :: [a] -> [a]
applyDef [] = [a]
def_vals
      applyDef [a]
vs = [a]
vs
  sources :: Parser [PackageSource]
sources = [PackageSource] -> Parser [PackageSource] -> Parser [PackageSource]
forall (m :: * -> *) a. Functor m => [a] -> m [a] -> m [a]
withDefault [PackageSource
SourceStackDefault] (Parser [PackageSource] -> Parser [PackageSource])
-> Parser [PackageSource] -> Parser [PackageSource]
forall a b. (a -> b) -> a -> b
$ Parser PackageSource -> Parser [PackageSource]
forall a. Parser a -> Parser [a]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many (Parser PackageSource -> Parser [PackageSource])
-> Parser PackageSource -> Parser [PackageSource]
forall a b. (a -> b) -> a -> b
$ Parser PackageSource
resolver Parser PackageSource
-> Parser PackageSource -> Parser PackageSource
forall a. Parser a -> Parser a -> Parser a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser PackageSource
hackage Parser PackageSource
-> Parser PackageSource -> Parser PackageSource
forall a. Parser a -> Parser a -> Parser a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser PackageSource
stack_explicit Parser PackageSource
-> Parser PackageSource -> Parser PackageSource
forall a. Parser a -> Parser a -> Parser a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser PackageSource
stack_default
  resolver :: Parser PackageSource
resolver = ([Char] -> PackageSource) -> Parser [Char] -> Parser PackageSource
forall a b. (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Char] -> PackageSource
SourceStackage (Parser [Char] -> Parser PackageSource)
-> Parser [Char] -> Parser PackageSource
forall a b. (a -> b) -> a -> b
$ Mod OptionFields [Char] -> Parser [Char]
forall s. IsString s => Mod OptionFields s -> Parser s
Opt.strOption
             (Mod OptionFields [Char] -> Parser [Char])
-> Mod OptionFields [Char] -> Parser [Char]
forall a b. (a -> b) -> a -> b
$ [Mod OptionFields [Char]] -> Mod OptionFields [Char]
forall a. Monoid a => [a] -> a
mconcat [ [Char] -> Mod OptionFields [Char]
forall (f :: * -> *) a. HasName f => [Char] -> Mod f a
Opt.long [Char]
"resolver",
                         Char -> Mod OptionFields [Char]
forall (f :: * -> *) a. HasName f => Char -> Mod f a
Opt.short Char
'r',
                         [Char] -> Mod OptionFields [Char]
forall (f :: * -> *) a. [Char] -> Mod f a
Opt.help [Char]
"Stackage resolver to search. e.g. \"lts-6.15\"",
                         [Char] -> Mod OptionFields [Char]
forall (f :: * -> *) a. HasMetavar f => [Char] -> Mod f a
Opt.metavar [Char]
"RESOLVER_NAME"
                       ]
  hackage :: Parser PackageSource
hackage = PackageSource
-> Mod FlagFields PackageSource -> Parser PackageSource
forall a. a -> Mod FlagFields a -> Parser a
Opt.flag' PackageSource
SourceHackage
            (Mod FlagFields PackageSource -> Parser PackageSource)
-> Mod FlagFields PackageSource -> Parser PackageSource
forall a b. (a -> b) -> a -> b
$ [Mod FlagFields PackageSource] -> Mod FlagFields PackageSource
forall a. Monoid a => [a] -> a
mconcat [ [Char] -> Mod FlagFields PackageSource
forall (f :: * -> *) a. HasName f => [Char] -> Mod f a
Opt.long [Char]
"hackage",
                        Char -> Mod FlagFields PackageSource
forall (f :: * -> *) a. HasName f => Char -> Mod f a
Opt.short Char
'H',
                        [Char] -> Mod FlagFields PackageSource
forall (f :: * -> *) a. [Char] -> Mod f a
Opt.help [Char]
"Search hackage.org for the latest version."
                      ]
  stack_explicit :: Parser PackageSource
stack_explicit = ([Char] -> PackageSource) -> Parser [Char] -> Parser PackageSource
forall a b. (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Char] -> PackageSource
SourceStackYaml (Parser [Char] -> Parser PackageSource)
-> Parser [Char] -> Parser PackageSource
forall a b. (a -> b) -> a -> b
$ Mod OptionFields [Char] -> Parser [Char]
forall s. IsString s => Mod OptionFields s -> Parser s
Opt.strOption
                   (Mod OptionFields [Char] -> Parser [Char])
-> Mod OptionFields [Char] -> Parser [Char]
forall a b. (a -> b) -> a -> b
$ [Mod OptionFields [Char]] -> Mod OptionFields [Char]
forall a. Monoid a => [a] -> a
mconcat [ [Char] -> Mod OptionFields [Char]
forall (f :: * -> *) a. HasName f => [Char] -> Mod f a
Opt.long [Char]
"stack",
                               [Char] -> Mod OptionFields [Char]
forall (f :: * -> *) a. [Char] -> Mod f a
Opt.help ( [Char]
"Path to stack.yaml file."
                                          [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
" It searches for package versions of the resolver of the specified stack.yaml file."
                                        ),
                               [Char] -> Mod OptionFields [Char]
forall (f :: * -> *) a. HasMetavar f => [Char] -> Mod f a
Opt.metavar [Char]
"FILE"
                             ]
  stack_default :: Parser PackageSource
stack_default = PackageSource
-> Mod FlagFields PackageSource -> Parser PackageSource
forall a. a -> Mod FlagFields a -> Parser a
Opt.flag' PackageSource
SourceStackDefault
                  (Mod FlagFields PackageSource -> Parser PackageSource)
-> Mod FlagFields PackageSource -> Parser PackageSource
forall a b. (a -> b) -> a -> b
$ [Mod FlagFields PackageSource] -> Mod FlagFields PackageSource
forall a. Monoid a => [a] -> a
mconcat [ [Char] -> Mod FlagFields PackageSource
forall (f :: * -> *) a. HasName f => [Char] -> Mod f a
Opt.long [Char]
"stack-default",
                              Char -> Mod FlagFields PackageSource
forall (f :: * -> *) a. HasName f => Char -> Mod f a
Opt.short Char
'S',
                              [Char] -> Mod FlagFields PackageSource
forall (f :: * -> *) a. [Char] -> Mod f a
Opt.help ( [Char]
"Search the resolver that 'stack' command would use by default."
                                         [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
" This option is implied if there is no options about package source (e.g. -r and -H)."
                                       )
                            ]
  queries :: Parser [Query]
queries = [Query] -> Parser [Query] -> Parser [Query]
forall (m :: * -> *) a. Functor m => [a] -> m [a] -> m [a]
withDefault [Query
QueryStackYamlDefault] (Parser [Query] -> Parser [Query])
-> Parser [Query] -> Parser [Query]
forall a b. (a -> b) -> a -> b
$ Parser Query -> Parser [Query]
forall a. Parser a -> Parser [a]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many (Parser Query -> Parser [Query]) -> Parser Query -> Parser [Query]
forall a b. (a -> b) -> a -> b
$ [Char] -> Query
parseQuery ([Char] -> Query) -> Parser [Char] -> Parser Query
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Parser [Char]
query_package Parser [Char] -> Parser [Char] -> Parser [Char]
forall a. Parser a -> Parser a -> Parser a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser [Char]
query_cabal Parser [Char] -> Parser [Char] -> Parser [Char]
forall a. Parser a -> Parser a -> Parser a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser [Char]
query_stack_yaml)
  query_package :: Parser [Char]
query_package = Mod ArgumentFields [Char] -> Parser [Char]
forall s. IsString s => Mod ArgumentFields s -> Parser s
Opt.strArgument
                  (Mod ArgumentFields [Char] -> Parser [Char])
-> Mod ArgumentFields [Char] -> Parser [Char]
forall a b. (a -> b) -> a -> b
$ [Mod ArgumentFields [Char]] -> Mod ArgumentFields [Char]
forall a. Monoid a => [a] -> a
mconcat [ [Char] -> Mod ArgumentFields [Char]
forall (f :: * -> *) a. [Char] -> Mod f a
Opt.help [Char]
"Name of package whose version you want to check.",
                              [Char] -> Mod ArgumentFields [Char]
forall (f :: * -> *) a. HasMetavar f => [Char] -> Mod f a
Opt.metavar [Char]
"PACKAGE_NAME"
                            ]
  query_cabal :: Parser [Char]
query_cabal = Mod ArgumentFields [Char] -> Parser [Char]
forall s. IsString s => Mod ArgumentFields s -> Parser s
Opt.strArgument
                (Mod ArgumentFields [Char] -> Parser [Char])
-> Mod ArgumentFields [Char] -> Parser [Char]
forall a b. (a -> b) -> a -> b
$ [Mod ArgumentFields [Char]] -> Mod ArgumentFields [Char]
forall a. Monoid a => [a] -> a
mconcat [ [Char] -> Mod ArgumentFields [Char]
forall (f :: * -> *) a. [Char] -> Mod f a
Opt.help [Char]
".cabal file name. It checks versions of packages in build-deps lists.",
                            [Char] -> Mod ArgumentFields [Char]
forall (f :: * -> *) a. HasMetavar f => [Char] -> Mod f a
Opt.metavar [Char]
"CABAL_FILEPATH"
                          ]
  query_stack_yaml :: Parser [Char]
query_stack_yaml = Mod ArgumentFields [Char] -> Parser [Char]
forall s. IsString s => Mod ArgumentFields s -> Parser s
Opt.strArgument
                     (Mod ArgumentFields [Char] -> Parser [Char])
-> Mod ArgumentFields [Char] -> Parser [Char]
forall a b. (a -> b) -> a -> b
$ [Mod ArgumentFields [Char]] -> Mod ArgumentFields [Char]
forall a. Monoid a => [a] -> a
mconcat [ [Char] -> Mod ArgumentFields [Char]
forall (f :: * -> *) a. [Char] -> Mod f a
Opt.help ( [Char]
"Path to stack.yaml file."
                                            [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
" It checks versions of packages in build-deps of all cabal projects listed in the stack.yaml."
                                            [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
" If you just type 'stack.yaml',"
                                            [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
" it means the default configuration that 'stack' command would use by default."
                                            [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
" 'stack.yaml' is implied if there is no query argument."
                                          ),
                                 [Char] -> Mod ArgumentFields [Char]
forall (f :: * -> *) a. HasMetavar f => [Char] -> Mod f a
Opt.metavar [Char]
"STACK_YAML_FILEPATH"
                               ]
  network :: Parser Bool
network = Bool -> Bool
not (Bool -> Bool) -> Parser Bool -> Parser Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Bool
no_network
  no_network :: Parser Bool
no_network = Mod FlagFields Bool -> Parser Bool
Opt.switch (Mod FlagFields Bool -> Parser Bool)
-> Mod FlagFields Bool -> Parser Bool
forall a b. (a -> b) -> a -> b
$ [Mod FlagFields Bool] -> Mod FlagFields Bool
forall a. Monoid a => [a] -> a
mconcat [ [Char] -> Mod FlagFields Bool
forall (f :: * -> *) a. HasName f => [Char] -> Mod f a
Opt.long [Char]
"no-network",
                                      [Char] -> Mod FlagFields Bool
forall (f :: * -> *) a. [Char] -> Mod f a
Opt.help [Char]
"Forbid network access."
                                    ]
  aggregate :: Parser (Maybe Aggregator)
aggregate = Parser Aggregator -> Parser (Maybe Aggregator)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Parser Aggregator -> Parser (Maybe Aggregator))
-> Parser Aggregator -> Parser (Maybe Aggregator)
forall a b. (a -> b) -> a -> b
$ ReadM Aggregator
-> Mod OptionFields Aggregator -> Parser Aggregator
forall a. ReadM a -> Mod OptionFields a -> Parser a
Opt.option ([Char] -> ([Char] -> Maybe Aggregator) -> ReadM Aggregator
forall a. [Char] -> ([Char] -> Maybe a) -> ReadM a
maybeReader [Char]
"AGGREGATOR" [Char] -> Maybe Aggregator
parseAggregator)
              (Mod OptionFields Aggregator -> Parser Aggregator)
-> Mod OptionFields Aggregator -> Parser Aggregator
forall a b. (a -> b) -> a -> b
$ [Mod OptionFields Aggregator] -> Mod OptionFields Aggregator
forall a. Monoid a => [a] -> a
mconcat [ [Char] -> Mod OptionFields Aggregator
forall (f :: * -> *) a. HasName f => [Char] -> Mod f a
Opt.long [Char]
"aggregate",
                          Char -> Mod OptionFields Aggregator
forall (f :: * -> *) a. HasName f => Char -> Mod f a
Opt.short Char
'a',
                          [Char] -> Mod OptionFields Aggregator
forall (f :: * -> *) a. HasMetavar f => [Char] -> Mod f a
Opt.metavar [Char]
"AGGREGATOR",
                          Maybe Doc -> Mod OptionFields Aggregator
forall (f :: * -> *) a. Maybe Doc -> Mod f a
Opt.helpDoc (Maybe Doc -> Mod OptionFields Aggregator)
-> Maybe Doc -> Mod OptionFields Aggregator
forall a b. (a -> b) -> a -> b
$ Doc -> Maybe Doc
forall a. a -> Maybe a
Just (Doc -> Maybe Doc) -> Doc -> Maybe Doc
forall a b. (a -> b) -> a -> b
$ [Char] -> Doc
docAggregators [Char]
"AGGREGATOR"
                        ]
  format_config :: Parser FormatConfig
format_config = FormatVersion -> FormatConfig
FormatConfig (FormatVersion -> FormatConfig)
-> Parser FormatVersion -> Parser FormatConfig
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser FormatVersion
format_version
  format_version :: Parser FormatVersion
format_version = ReadM FormatVersion
-> Mod OptionFields FormatVersion -> Parser FormatVersion
forall a. ReadM a -> Mod OptionFields a -> Parser a
Opt.option ([Char] -> ([Char] -> Maybe FormatVersion) -> ReadM FormatVersion
forall a. [Char] -> ([Char] -> Maybe a) -> ReadM a
maybeReader [Char]
"FORMAT" (([Char] -> Maybe FormatVersion) -> ReadM FormatVersion)
-> ([Char] -> Maybe FormatVersion) -> ReadM FormatVersion
forall a b. (a -> b) -> a -> b
$ [SelectSpec FormatVersion] -> [Char] -> Maybe FormatVersion
forall a. [SelectSpec a] -> [Char] -> Maybe a
parseSelect [SelectSpec FormatVersion]
formatVersions)
                   (Mod OptionFields FormatVersion -> Parser FormatVersion)
-> Mod OptionFields FormatVersion -> Parser FormatVersion
forall a b. (a -> b) -> a -> b
$ [Mod OptionFields FormatVersion] -> Mod OptionFields FormatVersion
forall a. Monoid a => [a] -> a
mconcat [ [Char] -> Mod OptionFields FormatVersion
forall (f :: * -> *) a. HasName f => [Char] -> Mod f a
Opt.long [Char]
"format-version",
                               [Char] -> Mod OptionFields FormatVersion
forall (f :: * -> *) a. HasMetavar f => [Char] -> Mod f a
Opt.metavar [Char]
"FORMAT",
                               Maybe Doc -> Mod OptionFields FormatVersion
forall (f :: * -> *) a. Maybe Doc -> Mod f a
Opt.helpDoc (Maybe Doc -> Mod OptionFields FormatVersion)
-> Maybe Doc -> Mod OptionFields FormatVersion
forall a b. (a -> b) -> a -> b
$ Doc -> Maybe Doc
forall a. a -> Maybe a
Just (Doc -> Maybe Doc) -> Doc -> Maybe Doc
forall a b. (a -> b) -> a -> b
$ [Char] -> Doc
docFormatVersions [Char]
"FORMAT",
                               FormatVersion -> Mod OptionFields FormatVersion
forall (f :: * -> *) a. HasValue f => a -> Mod f a
Opt.value (FormatVersion -> Mod OptionFields FormatVersion)
-> FormatVersion -> Mod OptionFields FormatVersion
forall a b. (a -> b) -> a -> b
$ FormatConfig -> FormatVersion
fconfFormatVersion FormatConfig
defFormatConfig
                             ]
  stack_command :: Parser [Char]
stack_command = Mod OptionFields [Char] -> Parser [Char]
forall s. IsString s => Mod OptionFields s -> Parser s
Opt.strOption
                  (Mod OptionFields [Char] -> Parser [Char])
-> Mod OptionFields [Char] -> Parser [Char]
forall a b. (a -> b) -> a -> b
$ [Mod OptionFields [Char]] -> Mod OptionFields [Char]
forall a. Monoid a => [a] -> a
mconcat [ [Char] -> Mod OptionFields [Char]
forall (f :: * -> *) a. HasName f => [Char] -> Mod f a
Opt.long [Char]
"stack-command",
                              [Char] -> Mod OptionFields [Char]
forall (f :: * -> *) a. [Char] -> Mod f a
Opt.help [Char]
"Shell command for stack tool.",
                              [Char] -> Mod OptionFields [Char]
forall (f :: * -> *) a. HasMetavar f => [Char] -> Mod f a
Opt.metavar [Char]
"COMMAND",
                              [Char] -> Mod OptionFields [Char]
forall (f :: * -> *) a. HasValue f => a -> Mod f a
Opt.value [Char]
"stack",
                              Mod OptionFields [Char]
forall a (f :: * -> *). Show a => Mod f a
Opt.showDefault
                            ]

maybeReader :: String -> (String -> Maybe a) -> Opt.ReadM a
maybeReader :: forall a. [Char] -> ([Char] -> Maybe a) -> ReadM a
maybeReader [Char]
metavar [Char] -> Maybe a
mfunc = do
  [Char]
got <- ReadM [Char]
forall s. IsString s => ReadM s
Opt.str
  case [Char] -> Maybe a
mfunc [Char]
got of
   Maybe a
Nothing -> [Char] -> ReadM a
forall a. [Char] -> ReadM a
Opt.readerError ([Char]
"Unknown " [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
metavar [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
": " [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
got)
   Just a
v -> a -> ReadM a
forall a. a -> ReadM a
forall (m :: * -> *) a. Monad m => a -> m a
return a
v


data SelectSpec a = SelectSpec { forall a. SelectSpec a -> a
selectResult :: a,
                                 forall a. SelectSpec a -> [Char]
selectSymbol :: String,
                                 forall a. SelectSpec a -> [Char]
selectDesc :: String
                               }

type AggregatorSpec = SelectSpec Aggregator

aggregators :: [AggregatorSpec]
aggregators :: [AggregatorSpec]
aggregators = [ Aggregator -> [Char] -> [Char] -> AggregatorSpec
forall a. a -> [Char] -> [Char] -> SelectSpec a
SelectSpec Aggregator
Agg.aggOr [Char]
"or" [Char]
"concatenate versions with (||).",
                Aggregator -> [Char] -> [Char] -> AggregatorSpec
forall a. a -> [Char] -> [Char] -> SelectSpec a
SelectSpec Aggregator
Agg.aggPvpMajor [Char]
"pvp-major"
                ( [Char]
"aggregate versions to a range that is supposed to be "
                  [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
"compatible with the given versions "
                  [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
"in terms of PVP (Package Versioning Policy.) "
                  [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
"Major versions are used for upper bounds."
                ),
                Aggregator -> [Char] -> [Char] -> AggregatorSpec
forall a. a -> [Char] -> [Char] -> SelectSpec a
SelectSpec Aggregator
Agg.aggPvpMajor [Char]
"pvp" [Char]
"alias for 'pvp-major'",
                Aggregator -> [Char] -> [Char] -> AggregatorSpec
forall a. a -> [Char] -> [Char] -> SelectSpec a
SelectSpec Aggregator
Agg.aggPvpMinor [Char]
"pvp-minor"
                ( [Char]
"aggregate versions to a range that is supposed to be "
                  [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
"compatible with the given versions "
                  [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
"in terms of PVP. "
                  [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
"Minor versions are used for upper bounds, i.e. this is stricter than 'pvp-major'."
                )
              ]

parseSelect :: [SelectSpec a] -> String -> Maybe a
parseSelect :: forall a. [SelectSpec a] -> [Char] -> Maybe a
parseSelect [SelectSpec a]
specs [Char]
symbol = [SelectSpec a] -> Maybe a
forall {a}. [SelectSpec a] -> Maybe a
toMaybe ([SelectSpec a] -> Maybe a) -> [SelectSpec a] -> Maybe a
forall a b. (a -> b) -> a -> b
$ (SelectSpec a -> Bool) -> [SelectSpec a] -> [SelectSpec a]
forall a. (a -> Bool) -> [a] -> [a]
filter (\SelectSpec a
spec -> SelectSpec a -> [Char]
forall a. SelectSpec a -> [Char]
selectSymbol SelectSpec a
spec [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== [Char]
symbol) [SelectSpec a]
specs where
  toMaybe :: [SelectSpec a] -> Maybe a
toMaybe [] = Maybe a
forall a. Maybe a
Nothing
  toMaybe (SelectSpec a
spec : [SelectSpec a]
_) = a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> a -> Maybe a
forall a b. (a -> b) -> a -> b
$ SelectSpec a -> a
forall a. SelectSpec a -> a
selectResult SelectSpec a
spec

parseAggregator :: String -> Maybe Aggregator
parseAggregator :: [Char] -> Maybe Aggregator
parseAggregator = [AggregatorSpec] -> [Char] -> Maybe Aggregator
forall a. [SelectSpec a] -> [Char] -> Maybe a
parseSelect [AggregatorSpec]
aggregators

wrapped :: String -> Pretty.Doc
wrapped :: [Char] -> Doc
wrapped = [Doc] -> Doc
Pretty.fillSep ([Doc] -> Doc) -> ([Char] -> [Doc]) -> [Char] -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Char] -> Doc) -> [[Char]] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map [Char] -> Doc
Pretty.text ([[Char]] -> [Doc]) -> ([Char] -> [[Char]]) -> [Char] -> [Doc]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [[Char]]
words

docSelect :: [SelectSpec a] -> String -> String -> Pretty.Doc
docSelect :: forall a. [SelectSpec a] -> [Char] -> [Char] -> Doc
docSelect [SelectSpec a]
specs [Char]
foreword_str [Char]
metavar = [Doc] -> Doc
Pretty.vsep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (Doc
foreword  Doc -> [Doc] -> [Doc]
forall a. a -> [a] -> [a]
:) ([Doc] -> [Doc]) -> [Doc] -> [Doc]
forall a b. (a -> b) -> a -> b
$ (SelectSpec a -> Doc) -> [SelectSpec a] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map SelectSpec a -> Doc
forall {a}. SelectSpec a -> Doc
docSpec [SelectSpec a]
specs where
  foreword :: Doc
foreword = [Char] -> Doc
wrapped ( [Char]
foreword_str [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
" Possible " [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
metavar [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
" is:" )
  docSpec :: SelectSpec a -> Doc
docSpec SelectSpec {selectSymbol :: forall a. SelectSpec a -> [Char]
selectSymbol = [Char]
symbol, selectDesc :: forall a. SelectSpec a -> [Char]
selectDesc = [Char]
desc} =
    Int -> Doc -> Doc
Pretty.hang Int
2 (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ [Char] -> Doc
wrapped ([Char]
symbol [Char] -> ShowS
forall a. Semigroup a => a -> a -> a
<> [Char]
": " [Char] -> ShowS
forall a. Semigroup a => a -> a -> a
<> [Char]
desc)

docSelectWithDefault :: [SelectSpec a] -> String -> String -> Pretty.Doc
docSelectWithDefault :: forall a. [SelectSpec a] -> [Char] -> [Char] -> Doc
docSelectWithDefault [] [Char]
foreword [Char]
metavar = [SelectSpec Any] -> [Char] -> [Char] -> Doc
forall a. [SelectSpec a] -> [Char] -> [Char] -> Doc
docSelect [] [Char]
foreword [Char]
metavar where
docSelectWithDefault (SelectSpec a
def_spec : [SelectSpec a]
rest) [Char]
foreword [Char]
metavar = [SelectSpec a] -> [Char] -> [Char] -> Doc
forall a. [SelectSpec a] -> [Char] -> [Char] -> Doc
docSelect (SelectSpec a
def_spec' SelectSpec a -> [SelectSpec a] -> [SelectSpec a]
forall a. a -> [a] -> [a]
: [SelectSpec a]
rest) [Char]
foreword [Char]
metavar where
  def_spec' :: SelectSpec a
def_spec' = SelectSpec a
def_spec { selectSymbol = selectSymbol def_spec <> " [DEFAULT]" }
  

docAggregators :: String -> Pretty.Doc
docAggregators :: [Char] -> Doc
docAggregators = [AggregatorSpec] -> [Char] -> [Char] -> Doc
forall a. [SelectSpec a] -> [Char] -> [Char] -> Doc
docSelect [AggregatorSpec]
aggregators [Char]
"Aggregate version results over different resolvers."


defFormatConfig :: FormatConfig
defFormatConfig :: FormatConfig
defFormatConfig = FormatConfig { fconfFormatVersion :: FormatVersion
fconfFormatVersion = SelectSpec FormatVersion -> FormatVersion
forall a. SelectSpec a -> a
selectResult (SelectSpec FormatVersion -> FormatVersion)
-> SelectSpec FormatVersion -> FormatVersion
forall a b. (a -> b) -> a -> b
$ [SelectSpec FormatVersion] -> SelectSpec FormatVersion
forall a. HasCallStack => [a] -> a
head [SelectSpec FormatVersion]
formatVersions
                               }

formatVersions :: [SelectSpec FormatVersion]
formatVersions :: [SelectSpec FormatVersion]
formatVersions = [ FormatVersion -> [Char] -> [Char] -> SelectSpec FormatVersion
forall a. a -> [Char] -> [Char] -> SelectSpec a
SelectSpec FormatVersion
Format.formatVersionCabal [Char]
"cabal"
                   ( [Char]
"Let Cabal format VersionRanges"
                   ),
                   FormatVersion -> [Char] -> [Char] -> SelectSpec FormatVersion
forall a. a -> [Char] -> [Char] -> SelectSpec a
SelectSpec FormatVersion
Format.formatVersionCabalCaret [Char]
"cabal-caret"
                   ( [Char]
"Similar to 'cabal', but it uses the caret operator (^>=) if possible"
                   )
                 ]


docFormatVersions :: String -> Pretty.Doc
docFormatVersions :: [Char] -> Doc
docFormatVersions = [SelectSpec FormatVersion] -> [Char] -> [Char] -> Doc
forall a. [SelectSpec a] -> [Char] -> [Char] -> Doc
docSelectWithDefault [SelectSpec FormatVersion]
formatVersions [Char]
"Format for package version ranges."

programDescription :: Opt.Parser a -> Opt.ParserInfo a
programDescription :: forall a. Parser a -> ParserInfo a
programDescription Parser a
parser =
  Parser a -> InfoMod a -> ParserInfo a
forall a. Parser a -> InfoMod a -> ParserInfo a
Opt.info (Parser (a -> a)
forall a. Parser (a -> a)
Opt.helper Parser (a -> a) -> Parser a -> Parser a
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser a
parser)
  (InfoMod a -> ParserInfo a) -> InfoMod a -> ParserInfo a
forall a b. (a -> b) -> a -> b
$ [InfoMod a] -> InfoMod a
forall a. Monoid a => [a] -> a
mconcat [ InfoMod a
forall a. InfoMod a
Opt.fullDesc,
              [Char] -> InfoMod a
forall a. [Char] -> InfoMod a
Opt.progDesc ( [Char]
"Look for version numbers for Haskell packages in specific stackage resolvers"
                             [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
" (or possibly other package sources)"
                           ),
              [Char] -> InfoMod a
forall a. [Char] -> InfoMod a
Opt.footer ([Char]
"Version: " [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ (BaseVersion -> [Char]
showBaseVersion BaseVersion
MyInfo.version))
            ]

parseCommandArgs :: IO Command
parseCommandArgs :: IO Command
parseCommandArgs = ParserInfo Command -> IO Command
forall a. ParserInfo a -> IO a
Opt.execParser (ParserInfo Command -> IO Command)
-> (DefCommand -> ParserInfo Command) -> DefCommand -> IO Command
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser Command -> ParserInfo Command
forall a. Parser a -> ParserInfo a
programDescription (Parser Command -> ParserInfo Command)
-> (DefCommand -> Parser Command)
-> DefCommand
-> ParserInfo Command
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DefCommand -> Parser Command
commandParser (DefCommand -> IO Command) -> IO DefCommand -> IO Command
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< IO DefCommand
defCommand

-- | Just for testing.
_parseCommandStrings :: [String] -> IO (Maybe Command)
_parseCommandStrings :: [[Char]] -> IO (Maybe Command)
_parseCommandStrings [[Char]]
args = (DefCommand -> Maybe Command)
-> IO DefCommand -> IO (Maybe Command)
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (ParserInfo Command -> Maybe Command
forall {a}. ParserInfo a -> Maybe a
doParse (ParserInfo Command -> Maybe Command)
-> (DefCommand -> ParserInfo Command)
-> DefCommand
-> Maybe Command
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser Command -> ParserInfo Command
forall a. Parser a -> ParserInfo a
programDescription (Parser Command -> ParserInfo Command)
-> (DefCommand -> Parser Command)
-> DefCommand
-> ParserInfo Command
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DefCommand -> Parser Command
commandParser) IO DefCommand
defCommand
  where
    doParse :: ParserInfo a -> Maybe a
doParse ParserInfo a
pinfo = ParserResult a -> Maybe a
forall a. ParserResult a -> Maybe a
Opt.getParseResult (ParserResult a -> Maybe a) -> ParserResult a -> Maybe a
forall a b. (a -> b) -> a -> b
$ ParserPrefs -> ParserInfo a -> [[Char]] -> ParserResult a
forall a. ParserPrefs -> ParserInfo a -> [[Char]] -> ParserResult a
Opt.execParserPure ParserPrefs
prefs ParserInfo a
pinfo [[Char]]
args
    prefs :: ParserPrefs
prefs = PrefsMod -> ParserPrefs
Opt.prefs PrefsMod
forall a. Monoid a => a
mempty