{-# LANGUAGE CPP #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ViewPatterns #-}

-----------------------------------------------------------------------------

-- |
-- Module      :  Distribution.Simple.Setup.Copy
-- Copyright   :  Isaac Jones 2003-2004
--                Duncan Coutts 2007
-- License     :  BSD3
--
-- Maintainer  :  cabal-devel@haskell.org
-- Portability :  portable
--
-- Definition of the copy command-line options.
-- See: @Distribution.Simple.Setup@
module Distribution.Simple.Setup.Copy
  ( CopyFlags
      ( CopyCommonFlags
      , copyVerbosity
      , copyDistPref
      , copyCabalFilePath
      , copyWorkingDir
      , copyTargets
      , ..
      )
  , emptyCopyFlags
  , defaultCopyFlags
  , copyCommand
  ) where

import Distribution.Compat.Prelude hiding (get)
import Prelude ()

import Distribution.ReadE
import Distribution.Simple.Command hiding (boolOpt, boolOpt')
import Distribution.Simple.Flag
import Distribution.Simple.InstallDirs
import Distribution.Simple.Setup.Common
import Distribution.Simple.Utils
import Distribution.Utils.Path
import Distribution.Verbosity

-- ------------------------------------------------------------

-- * Copy flags

-- ------------------------------------------------------------

-- | Flags to @copy@: (destdir, copy-prefix (backwards compat), verbosity)
data CopyFlags = CopyFlags
  { CopyFlags -> CommonSetupFlags
copyCommonFlags :: !CommonSetupFlags
  , CopyFlags -> Flag CopyDest
copyDest :: Flag CopyDest
  }
  deriving (Int -> CopyFlags -> ShowS
[CopyFlags] -> ShowS
CopyFlags -> String
(Int -> CopyFlags -> ShowS)
-> (CopyFlags -> String)
-> ([CopyFlags] -> ShowS)
-> Show CopyFlags
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CopyFlags -> ShowS
showsPrec :: Int -> CopyFlags -> ShowS
$cshow :: CopyFlags -> String
show :: CopyFlags -> String
$cshowList :: [CopyFlags] -> ShowS
showList :: [CopyFlags] -> ShowS
Show, (forall x. CopyFlags -> Rep CopyFlags x)
-> (forall x. Rep CopyFlags x -> CopyFlags) -> Generic CopyFlags
forall x. Rep CopyFlags x -> CopyFlags
forall x. CopyFlags -> Rep CopyFlags x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. CopyFlags -> Rep CopyFlags x
from :: forall x. CopyFlags -> Rep CopyFlags x
$cto :: forall x. Rep CopyFlags x -> CopyFlags
to :: forall x. Rep CopyFlags x -> CopyFlags
Generic)

pattern CopyCommonFlags
  :: Flag Verbosity
  -> Flag (SymbolicPath Pkg (Dir Dist))
  -> Flag (SymbolicPath CWD (Dir Pkg))
  -> Flag (SymbolicPath Pkg File)
  -> [String]
  -> CopyFlags
pattern $mCopyCommonFlags :: forall {r}.
CopyFlags
-> (Flag Verbosity
    -> Flag (SymbolicPath Pkg ('Dir Dist))
    -> Flag (SymbolicPath CWD ('Dir Pkg))
    -> Flag (SymbolicPath Pkg 'File)
    -> [String]
    -> r)
-> ((# #) -> r)
-> r
CopyCommonFlags
  { CopyFlags -> Flag Verbosity
copyVerbosity
  , CopyFlags -> Flag (SymbolicPath Pkg ('Dir Dist))
copyDistPref
  , CopyFlags -> Flag (SymbolicPath CWD ('Dir Pkg))
copyWorkingDir
  , CopyFlags -> Flag (SymbolicPath Pkg 'File)
copyCabalFilePath
  , CopyFlags -> [String]
copyTargets
  } <-
  ( copyCommonFlags ->
      CommonSetupFlags
        { setupVerbosity = copyVerbosity
        , setupDistPref = copyDistPref
        , setupWorkingDir = copyWorkingDir
        , setupCabalFilePath = copyCabalFilePath
        , setupTargets = copyTargets
        }
    )

instance Binary CopyFlags
instance Structured CopyFlags

defaultCopyFlags :: CopyFlags
defaultCopyFlags :: CopyFlags
defaultCopyFlags =
  CopyFlags
    { copyCommonFlags :: CommonSetupFlags
copyCommonFlags = CommonSetupFlags
defaultCommonSetupFlags
    , copyDest :: Flag CopyDest
copyDest = CopyDest -> Flag CopyDest
forall a. a -> Flag a
Flag CopyDest
NoCopyDest
    }

copyCommand :: CommandUI CopyFlags
copyCommand :: CommandUI CopyFlags
copyCommand =
  CommandUI
    { commandName :: String
commandName = String
"copy"
    , commandSynopsis :: String
commandSynopsis = String
"Copy the files of all/specific components to install locations."
    , commandDescription :: Maybe ShowS
commandDescription = ShowS -> Maybe ShowS
forall a. a -> Maybe a
Just (ShowS -> Maybe ShowS) -> ShowS -> Maybe ShowS
forall a b. (a -> b) -> a -> b
$ \String
_ ->
        ShowS
wrapText ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$
          String
"Components encompass executables and libraries. "
            String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"Does not call register, and allows a prefix at install time. "
            String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"Without the --destdir flag, configure determines location.\n"
    , commandNotes :: Maybe ShowS
commandNotes = ShowS -> Maybe ShowS
forall a. a -> Maybe a
Just (ShowS -> Maybe ShowS) -> ShowS -> Maybe ShowS
forall a b. (a -> b) -> a -> b
$ \String
pname ->
        String
"Examples:\n"
          String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"  "
          String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
pname
          String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" copy           "
          String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"    All the components in the package\n"
          String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"  "
          String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
pname
          String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" copy foo       "
          String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"    A component (i.e. lib, exe, test suite)"
    , commandUsage :: ShowS
commandUsage =
        String -> [String] -> ShowS
usageAlternatives String
"copy" ([String] -> ShowS) -> [String] -> ShowS
forall a b. (a -> b) -> a -> b
$
          [ String
"[FLAGS]"
          , String
"COMPONENTS [FLAGS]"
          ]
    , commandDefaultFlags :: CopyFlags
commandDefaultFlags = CopyFlags
defaultCopyFlags
    , commandOptions :: ShowOrParseArgs -> [OptionField CopyFlags]
commandOptions = \ShowOrParseArgs
showOrParseArgs -> case ShowOrParseArgs
showOrParseArgs of
        ShowOrParseArgs
ShowArgs ->
          (OptionField CopyFlags -> Bool)
-> [OptionField CopyFlags] -> [OptionField CopyFlags]
forall a. (a -> Bool) -> [a] -> [a]
filter
            ( (String -> [String] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [String
"target-package-db"])
                (String -> Bool)
-> (OptionField CopyFlags -> String)
-> OptionField CopyFlags
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. OptionField CopyFlags -> String
forall a. OptionField a -> String
optionName
            )
            ([OptionField CopyFlags] -> [OptionField CopyFlags])
-> [OptionField CopyFlags] -> [OptionField CopyFlags]
forall a b. (a -> b) -> a -> b
$ ShowOrParseArgs -> [OptionField CopyFlags]
copyOptions ShowOrParseArgs
ShowArgs
        ShowOrParseArgs
ParseArgs -> ShowOrParseArgs -> [OptionField CopyFlags]
copyOptions ShowOrParseArgs
ParseArgs
    }

copyOptions :: ShowOrParseArgs -> [OptionField CopyFlags]
copyOptions :: ShowOrParseArgs -> [OptionField CopyFlags]
copyOptions ShowOrParseArgs
showOrParseArgs =
  (CopyFlags -> CommonSetupFlags)
-> (CommonSetupFlags -> CopyFlags -> CopyFlags)
-> ShowOrParseArgs
-> [OptionField CopyFlags]
-> [OptionField CopyFlags]
forall flags.
(flags -> CommonSetupFlags)
-> (CommonSetupFlags -> flags -> flags)
-> ShowOrParseArgs
-> [OptionField flags]
-> [OptionField flags]
withCommonSetupOptions
    CopyFlags -> CommonSetupFlags
copyCommonFlags
    (\CommonSetupFlags
c CopyFlags
f -> CopyFlags
f{copyCommonFlags = c})
    ShowOrParseArgs
showOrParseArgs
    [ String
-> [String]
-> String
-> (CopyFlags -> Flag CopyDest)
-> (Flag CopyDest -> CopyFlags -> CopyFlags)
-> MkOptDescr
     (CopyFlags -> Flag CopyDest)
     (Flag CopyDest -> CopyFlags -> CopyFlags)
     CopyFlags
-> OptionField CopyFlags
forall get set a.
String
-> [String]
-> String
-> get
-> set
-> MkOptDescr get set a
-> OptionField a
option
        String
""
        [String
"destdir"]
        String
"directory to copy files to, prepended to installation directories"
        CopyFlags -> Flag CopyDest
copyDest
        ( \Flag CopyDest
v CopyFlags
flags -> case CopyFlags -> Flag CopyDest
copyDest CopyFlags
flags of
            Flag (CopyToDb String
_) -> String -> CopyFlags
forall a. HasCallStack => String -> a
error String
"Use either 'destdir' or 'target-package-db'."
            Flag CopyDest
_ -> CopyFlags
flags{copyDest = v}
        )
        ( String
-> ReadE (Flag CopyDest)
-> (Flag CopyDest -> [String])
-> MkOptDescr
     (CopyFlags -> Flag CopyDest)
     (Flag CopyDest -> CopyFlags -> CopyFlags)
     CopyFlags
forall b a.
Monoid b =>
String
-> ReadE b
-> (b -> [String])
-> MkOptDescr (a -> b) (b -> a -> a) a
reqArg
            String
"DIR"
            ((String -> Flag CopyDest) -> ReadE (Flag CopyDest)
forall a. (String -> a) -> ReadE a
succeedReadE (CopyDest -> Flag CopyDest
forall a. a -> Flag a
Flag (CopyDest -> Flag CopyDest)
-> (String -> CopyDest) -> String -> Flag CopyDest
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> CopyDest
CopyTo))
            (\Flag CopyDest
f -> case Flag CopyDest
f of Flag (CopyTo String
p) -> [String
p]; Flag CopyDest
_ -> [])
        )
    , String
-> [String]
-> String
-> (CopyFlags -> Flag CopyDest)
-> (Flag CopyDest -> CopyFlags -> CopyFlags)
-> MkOptDescr
     (CopyFlags -> Flag CopyDest)
     (Flag CopyDest -> CopyFlags -> CopyFlags)
     CopyFlags
-> OptionField CopyFlags
forall get set a.
String
-> [String]
-> String
-> get
-> set
-> MkOptDescr get set a
-> OptionField a
option
        String
""
        [String
"target-package-db"]
        String
"package database to copy files into. Required when using ${pkgroot} prefix."
        CopyFlags -> Flag CopyDest
copyDest
        ( \Flag CopyDest
v CopyFlags
flags -> case CopyFlags -> Flag CopyDest
copyDest CopyFlags
flags of
            Flag CopyDest
NoFlag -> CopyFlags
flags{copyDest = v}
            Flag CopyDest
NoCopyDest -> CopyFlags
flags{copyDest = v}
            Flag CopyDest
_ -> String -> CopyFlags
forall a. HasCallStack => String -> a
error String
"Use either 'destdir' or 'target-package-db'."
        )
        ( String
-> ReadE (Flag CopyDest)
-> (Flag CopyDest -> [String])
-> MkOptDescr
     (CopyFlags -> Flag CopyDest)
     (Flag CopyDest -> CopyFlags -> CopyFlags)
     CopyFlags
forall b a.
Monoid b =>
String
-> ReadE b
-> (b -> [String])
-> MkOptDescr (a -> b) (b -> a -> a) a
reqArg
            String
"DATABASE"
            ((String -> Flag CopyDest) -> ReadE (Flag CopyDest)
forall a. (String -> a) -> ReadE a
succeedReadE (CopyDest -> Flag CopyDest
forall a. a -> Flag a
Flag (CopyDest -> Flag CopyDest)
-> (String -> CopyDest) -> String -> Flag CopyDest
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> CopyDest
CopyToDb))
            (\Flag CopyDest
f -> case Flag CopyDest
f of Flag (CopyToDb String
p) -> [String
p]; Flag CopyDest
_ -> [])
        )
    ]

emptyCopyFlags :: CopyFlags
emptyCopyFlags :: CopyFlags
emptyCopyFlags = CopyFlags
forall a. Monoid a => a
mempty

instance Monoid CopyFlags where
  mempty :: CopyFlags
mempty = CopyFlags
forall a. (Generic a, GMonoid (Rep a)) => a
gmempty
  mappend :: CopyFlags -> CopyFlags -> CopyFlags
mappend = CopyFlags -> CopyFlags -> CopyFlags
forall a. Semigroup a => a -> a -> a
(<>)

instance Semigroup CopyFlags where
  <> :: CopyFlags -> CopyFlags -> CopyFlags
(<>) = CopyFlags -> CopyFlags -> CopyFlags
forall a. (Generic a, GSemigroup (Rep a)) => a -> a -> a
gmappend