{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric      #-}
module Distribution.Types.MungedPackageName
  ( MungedPackageName (..)
  , decodeCompatPackageName
  , encodeCompatPackageName
  ) where

import Distribution.Compat.Prelude
import Prelude ()

import Distribution.Parsec
import Distribution.Pretty
import Distribution.Types.LibraryName
import Distribution.Types.PackageName
import Distribution.Types.UnqualComponentName

import qualified Distribution.Compat.CharParsing as P
import qualified Text.PrettyPrint as Disp

-- | A combination of a package and component name used in various legacy
-- interfaces, chiefly bundled with a version as 'MungedPackageId'. It's generally
-- better to use a 'UnitId' to opaquely refer to some compilation/packing unit,
-- but that doesn't always work, e.g. where a "name" is needed, in which case
-- this can be used as a fallback.
--
-- Use 'mkMungedPackageName' and 'unMungedPackageName' to convert from/to a 'String'.
--
-- In @3.0.0.0@ representation was changed from opaque (string) to semantic representation.
--
-- @since 2.0.0.2
--
data MungedPackageName = MungedPackageName !PackageName !LibraryName
  deriving (forall x. Rep MungedPackageName x -> MungedPackageName
forall x. MungedPackageName -> Rep MungedPackageName x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep MungedPackageName x -> MungedPackageName
$cfrom :: forall x. MungedPackageName -> Rep MungedPackageName x
Generic, ReadPrec [MungedPackageName]
ReadPrec MungedPackageName
Int -> ReadS MungedPackageName
ReadS [MungedPackageName]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [MungedPackageName]
$creadListPrec :: ReadPrec [MungedPackageName]
readPrec :: ReadPrec MungedPackageName
$creadPrec :: ReadPrec MungedPackageName
readList :: ReadS [MungedPackageName]
$creadList :: ReadS [MungedPackageName]
readsPrec :: Int -> ReadS MungedPackageName
$creadsPrec :: Int -> ReadS MungedPackageName
Read, Int -> MungedPackageName -> ShowS
[MungedPackageName] -> ShowS
MungedPackageName -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [MungedPackageName] -> ShowS
$cshowList :: [MungedPackageName] -> ShowS
show :: MungedPackageName -> String
$cshow :: MungedPackageName -> String
showsPrec :: Int -> MungedPackageName -> ShowS
$cshowsPrec :: Int -> MungedPackageName -> ShowS
Show, MungedPackageName -> MungedPackageName -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: MungedPackageName -> MungedPackageName -> Bool
$c/= :: MungedPackageName -> MungedPackageName -> Bool
== :: MungedPackageName -> MungedPackageName -> Bool
$c== :: MungedPackageName -> MungedPackageName -> Bool
Eq, Eq MungedPackageName
MungedPackageName -> MungedPackageName -> Bool
MungedPackageName -> MungedPackageName -> Ordering
MungedPackageName -> MungedPackageName -> MungedPackageName
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
min :: MungedPackageName -> MungedPackageName -> MungedPackageName
$cmin :: MungedPackageName -> MungedPackageName -> MungedPackageName
max :: MungedPackageName -> MungedPackageName -> MungedPackageName
$cmax :: MungedPackageName -> MungedPackageName -> MungedPackageName
>= :: MungedPackageName -> MungedPackageName -> Bool
$c>= :: MungedPackageName -> MungedPackageName -> Bool
> :: MungedPackageName -> MungedPackageName -> Bool
$c> :: MungedPackageName -> MungedPackageName -> Bool
<= :: MungedPackageName -> MungedPackageName -> Bool
$c<= :: MungedPackageName -> MungedPackageName -> Bool
< :: MungedPackageName -> MungedPackageName -> Bool
$c< :: MungedPackageName -> MungedPackageName -> Bool
compare :: MungedPackageName -> MungedPackageName -> Ordering
$ccompare :: MungedPackageName -> MungedPackageName -> Ordering
Ord, Typeable, Typeable MungedPackageName
MungedPackageName -> DataType
MungedPackageName -> Constr
(forall b. Data b => b -> b)
-> MungedPackageName -> MungedPackageName
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u.
Int -> (forall d. Data d => d -> u) -> MungedPackageName -> u
forall u. (forall d. Data d => d -> u) -> MungedPackageName -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> MungedPackageName -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> MungedPackageName -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> MungedPackageName -> m MungedPackageName
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> MungedPackageName -> m MungedPackageName
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c MungedPackageName
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> MungedPackageName -> c MungedPackageName
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c MungedPackageName)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c MungedPackageName)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> MungedPackageName -> m MungedPackageName
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> MungedPackageName -> m MungedPackageName
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> MungedPackageName -> m MungedPackageName
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> MungedPackageName -> m MungedPackageName
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> MungedPackageName -> m MungedPackageName
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> MungedPackageName -> m MungedPackageName
gmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> MungedPackageName -> u
$cgmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> MungedPackageName -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> MungedPackageName -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> MungedPackageName -> [u]
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> MungedPackageName -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> MungedPackageName -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> MungedPackageName -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> MungedPackageName -> r
gmapT :: (forall b. Data b => b -> b)
-> MungedPackageName -> MungedPackageName
$cgmapT :: (forall b. Data b => b -> b)
-> MungedPackageName -> MungedPackageName
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c MungedPackageName)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c MungedPackageName)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c MungedPackageName)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c MungedPackageName)
dataTypeOf :: MungedPackageName -> DataType
$cdataTypeOf :: MungedPackageName -> DataType
toConstr :: MungedPackageName -> Constr
$ctoConstr :: MungedPackageName -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c MungedPackageName
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c MungedPackageName
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> MungedPackageName -> c MungedPackageName
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> MungedPackageName -> c MungedPackageName
Data)

instance Binary MungedPackageName
instance Structured MungedPackageName
instance NFData MungedPackageName where rnf :: MungedPackageName -> ()
rnf = forall a. (Generic a, GNFData (Rep a)) => a -> ()
genericRnf

-- | Computes the package name for a library.  If this is the public
-- library, it will just be the original package name; otherwise,
-- it will be a munged package name recording the original package
-- name as well as the name of the internal library.
--
-- A lot of tooling in the Haskell ecosystem assumes that if something
-- is installed to the package database with the package name 'foo',
-- then it actually is an entry for the (only public) library in package
-- 'foo'.  With internal packages, this is not necessarily true:
-- a public library as well as arbitrarily many internal libraries may
-- come from the same package.  To prevent tools from getting confused
-- in this case, the package name of these internal libraries is munged
-- so that they do not conflict the public library proper.  A particular
-- case where this matters is ghc-pkg: if we don't munge the package
-- name, the inplace registration will OVERRIDE a different internal
-- library.
--
-- We munge into a reserved namespace, "z-", and encode both the
-- component name and the package name of an internal library using the
-- following format:
--
--      compat-pkg-name ::= "z-" package-name "-z-" library-name
--
-- where package-name and library-name have "-" ( "z" + ) "-"
-- segments encoded by adding an extra "z".
--
-- When we have the public library, the compat-pkg-name is just the
-- package-name, no surprises there!
--
-- >>> prettyShow $ MungedPackageName "servant" LMainLibName
-- "servant"
--
-- >>> prettyShow $ MungedPackageName "servant" (LSubLibName "lackey")
-- "z-servant-z-lackey"
--
instance Pretty MungedPackageName where
    -- First handle the cases where we can just use the original 'PackageName'.
    -- This is for the PRIMARY library, and it is non-Backpack, or the
    -- indefinite package for us.
    pretty :: MungedPackageName -> Doc
pretty = String -> Doc
Disp.text forall b c a. (b -> c) -> (a -> b) -> a -> c
. MungedPackageName -> String
encodeCompatPackageName'

-- |
--
-- >>> simpleParsec "servant" :: Maybe MungedPackageName
-- Just (MungedPackageName (PackageName "servant") LMainLibName)
--
-- >>> simpleParsec "z-servant-z-lackey" :: Maybe MungedPackageName
-- Just (MungedPackageName (PackageName "servant") (LSubLibName (UnqualComponentName "lackey")))
--
-- >>> simpleParsec "z-servant-zz" :: Maybe MungedPackageName
-- Just (MungedPackageName (PackageName "z-servant-zz") LMainLibName)
--
instance Parsec MungedPackageName where
    parsec :: forall (m :: * -> *). CabalParsing m => m MungedPackageName
parsec = String -> MungedPackageName
decodeCompatPackageName' forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). CabalParsing m => m String
parsecUnqualComponentName

-------------------------------------------------------------------------------
-- ZDashCode conversions
-------------------------------------------------------------------------------

-- | Intended for internal use only
--
-- >>> decodeCompatPackageName "z-servant-z-lackey"
-- MungedPackageName (PackageName "servant") (LSubLibName (UnqualComponentName "lackey"))
--
decodeCompatPackageName :: PackageName -> MungedPackageName
decodeCompatPackageName :: PackageName -> MungedPackageName
decodeCompatPackageName = String -> MungedPackageName
decodeCompatPackageName' forall b c a. (b -> c) -> (a -> b) -> a -> c
. PackageName -> String
unPackageName

-- | Intended for internal use only
--
-- >>> encodeCompatPackageName $ MungedPackageName "servant" (LSubLibName "lackey")
-- PackageName "z-servant-z-lackey"
--
-- This is used in @cabal-install@ in the Solver.
-- May become obsolete as solver moves to per-component solving.
--
encodeCompatPackageName :: MungedPackageName -> PackageName
encodeCompatPackageName :: MungedPackageName -> PackageName
encodeCompatPackageName = String -> PackageName
mkPackageName forall b c a. (b -> c) -> (a -> b) -> a -> c
. MungedPackageName -> String
encodeCompatPackageName'

decodeCompatPackageName' :: String -> MungedPackageName
decodeCompatPackageName' :: String -> MungedPackageName
decodeCompatPackageName' String
m =
    case String
m of
        Char
'z':Char
'-':String
rest | Right [String
pn, String
cn] <- forall a. ParsecParser a -> String -> Either String a
explicitEitherParsec forall (m :: * -> *). CabalParsing m => m [String]
parseZDashCode String
rest
            -> PackageName -> LibraryName -> MungedPackageName
MungedPackageName (String -> PackageName
mkPackageName String
pn) (UnqualComponentName -> LibraryName
LSubLibName (String -> UnqualComponentName
mkUnqualComponentName String
cn))
        String
s   -> PackageName -> LibraryName -> MungedPackageName
MungedPackageName (String -> PackageName
mkPackageName String
s) LibraryName
LMainLibName

encodeCompatPackageName' :: MungedPackageName -> String
encodeCompatPackageName' :: MungedPackageName -> String
encodeCompatPackageName' (MungedPackageName PackageName
pn LibraryName
LMainLibName)      = PackageName -> String
unPackageName PackageName
pn
encodeCompatPackageName' (MungedPackageName PackageName
pn (LSubLibName UnqualComponentName
uqn)) =
     String
"z-" forall a. [a] -> [a] -> [a]
++ ShowS
zdashcode (PackageName -> String
unPackageName PackageName
pn) forall a. [a] -> [a] -> [a]
++
    String
"-z-" forall a. [a] -> [a] -> [a]
++ ShowS
zdashcode (UnqualComponentName -> String
unUnqualComponentName UnqualComponentName
uqn)

zdashcode :: String -> String
zdashcode :: ShowS
zdashcode String
s = forall {a}. (Ord a, Num a) => String -> Maybe a -> ShowS
go String
s (forall a. Maybe a
Nothing :: Maybe Int) []
    where go :: String -> Maybe a -> ShowS
go [] Maybe a
_ String
r = forall a. [a] -> [a]
reverse String
r
          go (Char
'-':String
z) (Just a
n) String
r | a
n forall a. Ord a => a -> a -> Bool
> a
0 = String -> Maybe a -> ShowS
go String
z (forall a. a -> Maybe a
Just a
0) (Char
'-'forall a. a -> [a] -> [a]
:Char
'z'forall a. a -> [a] -> [a]
:String
r)
          go (Char
'-':String
z) Maybe a
_        String
r = String -> Maybe a -> ShowS
go String
z (forall a. a -> Maybe a
Just a
0) (Char
'-'forall a. a -> [a] -> [a]
:String
r)
          go (Char
'z':String
z) (Just a
n) String
r = String -> Maybe a -> ShowS
go String
z (forall a. a -> Maybe a
Just (a
nforall a. Num a => a -> a -> a
+a
1)) (Char
'z'forall a. a -> [a] -> [a]
:String
r)
          go (Char
c:String
z)   Maybe a
_        String
r = String -> Maybe a -> ShowS
go String
z forall a. Maybe a
Nothing (Char
cforall a. a -> [a] -> [a]
:String
r)

parseZDashCode :: CabalParsing m => m [String]
parseZDashCode :: forall (m :: * -> *). CabalParsing m => m [String]
parseZDashCode = do
    [String]
ns <- forall (t :: * -> *) a. Foldable t => t a -> [a]
toList forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a sep.
Alternative m =>
m a -> m sep -> m (NonEmpty a)
P.sepByNonEmpty (forall (f :: * -> *) a. Alternative f => f a -> f [a]
some (forall (m :: * -> *). CharParsing m => (Char -> Bool) -> m Char
P.satisfy (forall a. Eq a => a -> a -> Bool
/= Char
'-'))) (forall (m :: * -> *). CharParsing m => Char -> m Char
P.char Char
'-')
    forall (m :: * -> *) a. Monad m => a -> m a
return ([String] -> [String]
go [String]
ns)
  where
    go :: [String] -> [String]
go [String]
ns = case forall a. (a -> Bool) -> [a] -> ([a], [a])
break (forall a. Eq a => a -> a -> Bool
==String
"z") [String]
ns of
                ([String]
_, []) -> [[String] -> String
paste [String]
ns]
                ([String]
as, String
"z":[String]
bs) -> [String] -> String
paste [String]
as forall a. a -> [a] -> [a]
: [String] -> [String]
go [String]
bs
                ([String], [String])
_ -> forall a. HasCallStack => String -> a
error String
"parseZDashCode: go"
    unZ :: String -> String
    unZ :: ShowS
unZ String
"" = forall a. HasCallStack => String -> a
error String
"parseZDashCode: unZ"
    unZ r :: String
r@(Char
'z':String
zs) | forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (forall a. Eq a => a -> a -> Bool
==Char
'z') String
zs = String
zs
                   | Bool
otherwise      = String
r
    unZ String
r = String
r
    paste :: [String] -> String
    paste :: [String] -> String
paste = forall a. [a] -> [[a]] -> [a]
intercalate String
"-" forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map ShowS
unZ

-- $setup
-- >>> :seti -XOverloadedStrings