-- SPDX-FileCopyrightText: 2026 Alexandra de Wit
--
-- SPDX-License-Identifier: MIT

{- | npm dependency specs a client fetches from outside the registry. Only registry ranges and tags,
@npm:@ aliases, @file:@, @link:@ and @workspace:@ specs, and local paths pass, so any other scheme
fails closed. Aliases and local specs stay exempt on npm-package-arg's terms, by its POSIX path
grammar, the narrower of its two. Git detection follows hosted-git-info, widened to what another
client or URL parsing could fetch.
-}
module Ecluse.Core.Registry.Npm.DependencySource (
    -- * One spec
    ExternalSource (..),
    externalSource,

    -- * A release's installable dependencies
    InstallableField (..),
    installableFieldName,
    ExternalDependency (..),
    externalDependencyOf,
    earlierDependency,
    renderExternalDependency,
) where

import Data.Aeson (Value (Object, String))
import Data.Aeson.Key qualified as Key
import Data.Aeson.KeyMap (KeyMap)
import Data.Aeson.KeyMap qualified as KeyMap
import Data.Char (isAsciiLower, isAsciiUpper, isDigit, isSpace)
import Data.Text qualified as T
import Data.Universe.Class (Universe (..))
import Data.Universe.Class qualified as Universe
import Data.Universe.Generic (universeGeneric)

-- | Where a client fetches a dependency from instead of the registry.
data ExternalSource
    = -- | A git repository, by URL, hosted shortcut, GitHub shorthand or scp-like address.
      GitSource
    | -- | A tarball at an @http@ or @https@ URL.
      RemoteUrl
    | -- | Any other scheme, such as pnpm's @jsr:@, which this registry does not resolve.
      UnsupportedScheme
    deriving stock (ExternalSource -> ExternalSource -> Bool
(ExternalSource -> ExternalSource -> Bool)
-> (ExternalSource -> ExternalSource -> Bool) -> Eq ExternalSource
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ExternalSource -> ExternalSource -> Bool
== :: ExternalSource -> ExternalSource -> Bool
$c/= :: ExternalSource -> ExternalSource -> Bool
/= :: ExternalSource -> ExternalSource -> Bool
Eq, (forall x. ExternalSource -> Rep ExternalSource x)
-> (forall x. Rep ExternalSource x -> ExternalSource)
-> Generic ExternalSource
forall x. Rep ExternalSource x -> ExternalSource
forall x. ExternalSource -> Rep ExternalSource x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ExternalSource -> Rep ExternalSource x
from :: forall x. ExternalSource -> Rep ExternalSource x
$cto :: forall x. Rep ExternalSource x -> ExternalSource
to :: forall x. Rep ExternalSource x -> ExternalSource
Generic, Int -> ExternalSource -> ShowS
[ExternalSource] -> ShowS
ExternalSource -> String
(Int -> ExternalSource -> ShowS)
-> (ExternalSource -> String)
-> ([ExternalSource] -> ShowS)
-> Show ExternalSource
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ExternalSource -> ShowS
showsPrec :: Int -> ExternalSource -> ShowS
$cshow :: ExternalSource -> String
show :: ExternalSource -> String
$cshowList :: [ExternalSource] -> ShowS
showList :: [ExternalSource] -> ShowS
Show)

instance Universe ExternalSource where universe :: [ExternalSource]
universe = [ExternalSource]
forall a. (Generic a, GUniverse (Rep a)) => [a]
universeGeneric

-- | The dependency maps a consumer's install reads, in the order a refusal names them.
data InstallableField
    = Dependencies
    | OptionalDependencies
    | PeerDependencies
    deriving stock (InstallableField -> InstallableField -> Bool
(InstallableField -> InstallableField -> Bool)
-> (InstallableField -> InstallableField -> Bool)
-> Eq InstallableField
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: InstallableField -> InstallableField -> Bool
== :: InstallableField -> InstallableField -> Bool
$c/= :: InstallableField -> InstallableField -> Bool
/= :: InstallableField -> InstallableField -> Bool
Eq, (forall x. InstallableField -> Rep InstallableField x)
-> (forall x. Rep InstallableField x -> InstallableField)
-> Generic InstallableField
forall x. Rep InstallableField x -> InstallableField
forall x. InstallableField -> Rep InstallableField x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. InstallableField -> Rep InstallableField x
from :: forall x. InstallableField -> Rep InstallableField x
$cto :: forall x. Rep InstallableField x -> InstallableField
to :: forall x. Rep InstallableField x -> InstallableField
Generic, Eq InstallableField
Eq InstallableField =>
(InstallableField -> InstallableField -> Ordering)
-> (InstallableField -> InstallableField -> Bool)
-> (InstallableField -> InstallableField -> Bool)
-> (InstallableField -> InstallableField -> Bool)
-> (InstallableField -> InstallableField -> Bool)
-> (InstallableField -> InstallableField -> InstallableField)
-> (InstallableField -> InstallableField -> InstallableField)
-> Ord InstallableField
InstallableField -> InstallableField -> Bool
InstallableField -> InstallableField -> Ordering
InstallableField -> InstallableField -> InstallableField
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 :: InstallableField -> InstallableField -> Ordering
compare :: InstallableField -> InstallableField -> Ordering
$c< :: InstallableField -> InstallableField -> Bool
< :: InstallableField -> InstallableField -> Bool
$c<= :: InstallableField -> InstallableField -> Bool
<= :: InstallableField -> InstallableField -> Bool
$c> :: InstallableField -> InstallableField -> Bool
> :: InstallableField -> InstallableField -> Bool
$c>= :: InstallableField -> InstallableField -> Bool
>= :: InstallableField -> InstallableField -> Bool
$cmax :: InstallableField -> InstallableField -> InstallableField
max :: InstallableField -> InstallableField -> InstallableField
$cmin :: InstallableField -> InstallableField -> InstallableField
min :: InstallableField -> InstallableField -> InstallableField
Ord, Int -> InstallableField -> ShowS
[InstallableField] -> ShowS
InstallableField -> String
(Int -> InstallableField -> ShowS)
-> (InstallableField -> String)
-> ([InstallableField] -> ShowS)
-> Show InstallableField
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> InstallableField -> ShowS
showsPrec :: Int -> InstallableField -> ShowS
$cshow :: InstallableField -> String
show :: InstallableField -> String
$cshowList :: [InstallableField] -> ShowS
showList :: [InstallableField] -> ShowS
Show)

instance Universe InstallableField where universe :: [InstallableField]
universe = [InstallableField]
forall a. (Generic a, GUniverse (Rep a)) => [a]
universeGeneric

-- | The member name of the field in a release object.
installableFieldName :: InstallableField -> Text
installableFieldName :: InstallableField -> Text
installableFieldName = \case
    InstallableField
Dependencies -> Text
"dependencies"
    InstallableField
OptionalDependencies -> Text
"optionalDependencies"
    InstallableField
PeerDependencies -> Text
"peerDependencies"

-- | One installable dependency whose spec fetches from outside the registry.
data ExternalDependency = ExternalDependency
    { ExternalDependency -> InstallableField
edField :: InstallableField
    , ExternalDependency -> Text
edName :: Text
    -- ^ The dependency's name as the map keys it. The spec itself is never kept, since it can carry a credential.
    , ExternalDependency -> ExternalSource
edSource :: ExternalSource
    }
    deriving stock (ExternalDependency -> ExternalDependency -> Bool
(ExternalDependency -> ExternalDependency -> Bool)
-> (ExternalDependency -> ExternalDependency -> Bool)
-> Eq ExternalDependency
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ExternalDependency -> ExternalDependency -> Bool
== :: ExternalDependency -> ExternalDependency -> Bool
$c/= :: ExternalDependency -> ExternalDependency -> Bool
/= :: ExternalDependency -> ExternalDependency -> Bool
Eq, Int -> ExternalDependency -> ShowS
[ExternalDependency] -> ShowS
ExternalDependency -> String
(Int -> ExternalDependency -> ShowS)
-> (ExternalDependency -> String)
-> ([ExternalDependency] -> ShowS)
-> Show ExternalDependency
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ExternalDependency -> ShowS
showsPrec :: Int -> ExternalDependency -> ShowS
$cshow :: ExternalDependency -> String
show :: ExternalDependency -> String
$cshowList :: [ExternalDependency] -> ShowS
showList :: [ExternalDependency] -> ShowS
Show)

{- | Classify one spec. A spec with neither a colon nor a slash is a registry range, a tag or a file
name, so the common case reads the text once and allocates nothing.
-}
externalSource :: Text -> Maybe ExternalSource
externalSource :: Text -> Maybe ExternalSource
externalSource Text
spec
    | Bool -> Bool
not ((Char -> Bool) -> Text -> Bool
T.any (\Char
c -> Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
':' Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'/') Text
spec) = Maybe ExternalSource
forall a. Maybe a
Nothing
    | Text -> Bool
aliasOrLocal Text
spec = Maybe ExternalSource
forall a. Maybe a
Nothing
    | Bool
otherwise = Text -> Maybe ExternalSource
detect (Text -> Text
urlInput Text
spec)

-- npm-package-arg's POSIX grammar, on the spec as written. Windows also reads a leading backslash
-- as a path, but elsewhere hosted-git-info resolves \x@github.com:acme/left-pad to GitHub.
aliasOrLocal :: Text -> Bool
aliasOrLocal :: Text -> Bool
aliasOrLocal Text
spec = Bool -> (Text -> Bool) -> Maybe Text -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False (Text -> [Text] -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` [Text
"npm", Text
"file"]) (Text -> Maybe Text
scheme Text
spec) Bool -> Bool -> Bool
|| Bool
pathLike
  where
    pathLike :: Bool
pathLike = case Text -> String
T.unpack (Int -> Text -> Text
T.take Int
2 Text
spec) of
        Char
'.' : String
_ -> Bool
True
        Char
'/' : String
_ -> Bool
True
        [Char
'~', Char
'/'] -> Bool
True
        [Char
letter, Char
':'] -> Char -> Bool
isAsciiLetter Char
letter
        String
_ -> Bool
False

-- URL parsing drops tabs and newlines anywhere, and C0 controls and spaces at either end.
urlInput :: Text -> Text
urlInput :: Text -> Text
urlInput Text
spec = (Char -> Bool) -> Text -> Text
T.dropAround (Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
<= Char
' ') (if (Char -> Bool) -> Text -> Bool
T.any Char -> Bool
tabOrNewline Text
spec then (Char -> Bool) -> Text -> Text
T.filter (Bool -> Bool
not (Bool -> Bool) -> (Char -> Bool) -> Char -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Bool
tabOrNewline) Text
spec else Text
spec)
  where
    tabOrNewline :: Char -> Bool
tabOrNewline Char
c = Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'\t' Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'\n' Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'\r'

detect :: Text -> Maybe ExternalSource
detect :: Text -> Maybe ExternalSource
detect Text
spec = case Text -> Maybe Text
scheme Text
spec of
    Just Text
named
        | Text
named Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"http" Bool -> Bool -> Bool
|| Text
named Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"https" -> ExternalSource -> Maybe ExternalSource
forall a. a -> Maybe a
Just ExternalSource
RemoteUrl
        | Text
named Text -> [Text] -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` [Text]
gitSchemes Bool -> Bool -> Bool
|| Text
"git+" Text -> Text -> Bool
`T.isPrefixOf` Text
named -> ExternalSource -> Maybe ExternalSource
forall a. a -> Maybe a
Just ExternalSource
GitSource
        -- hosted-git-info reads an unknown scheme before an @ as the user of an ssh URL.
        | Text -> Bool
userAtGitHost Text
spec -> ExternalSource -> Maybe ExternalSource
forall a. a -> Maybe a
Just ExternalSource
GitSource
        | Text
named Text -> [Text] -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` [Text]
resolvedSchemes Bool -> Bool -> Bool
|| Text -> Int -> Ordering
T.compareLength Text
named Int
1 Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
== Ordering
EQ -> Maybe ExternalSource
forall a. Maybe a
Nothing
        | Bool
otherwise -> ExternalSource -> Maybe ExternalSource
forall a. a -> Maybe a
Just ExternalSource
UnsupportedScheme
    Maybe Text
Nothing
        | Text -> Bool
gitHubShorthand Text
spec -> ExternalSource -> Maybe ExternalSource
forall a. a -> Maybe a
Just ExternalSource
GitSource
        | Text -> Bool
userAtHost ((Char -> Bool) -> Text -> Text
T.takeWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'#') Text
spec) -> ExternalSource -> Maybe ExternalSource
forall a. a -> Maybe a
Just ExternalSource
GitSource
        | Bool
otherwise -> Maybe ExternalSource
forall a. Maybe a
Nothing

-- An scp-like or ssh git address: a user with no slash, then an @. packages/@acme/widget is a local path.
userAtHost :: Text -> Bool
userAtHost :: Text -> Bool
userAtHost Text
text = case (Char -> Bool) -> Text -> (Text, Text)
T.break (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'@') Text
text of
    (Text
user, Text
rest) -> Bool -> Bool
not (Text -> Bool
T.null Text
user Bool -> Bool -> Bool
|| Text -> Bool
T.null Text
rest Bool -> Bool -> Bool
|| (Char -> Bool) -> Text -> Bool
T.any (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'/') Text
user)

-- Registry aliases and local specs, for a spec npm-package-arg does not read as written.
-- A one-letter scheme is a Windows drive.
resolvedSchemes :: [Text]
resolvedSchemes :: [Text]
resolvedSchemes = [Text
"npm", Text
"file", Text
"link", Text
"workspace"]

gitSchemes :: [Text]
gitSchemes :: [Text]
gitSchemes = [Text
"git", Text
"ssh", Text
"github", Text
"gitlab", Text
"bitbucket", Text
"gist", Text
"sourcehut"]

-- The URL scheme, folded to lower case, when the spec starts with one.
scheme :: Text -> Maybe Text
scheme :: Text -> Maybe Text
scheme Text
spec = case Text -> Maybe (Char, Text)
T.uncons Text
spec of
    Just (Char
lead, Text
_)
        | Char -> Bool
isAsciiLetter Char
lead
        , (Text
named, Text
rest) <- (Char -> Bool) -> Text -> (Text, Text)
T.span Char -> Bool
schemeChar Text
spec
        , Text
":" Text -> Text -> Bool
`T.isPrefixOf` Text
rest ->
            Text -> Maybe Text
forall a. a -> Maybe a
Just (Text -> Text
T.toLower Text
named)
    Maybe (Char, Text)
_ -> Maybe Text
forall a. Maybe a
Nothing
  where
    schemeChar :: Char -> Bool
schemeChar Char
c = Char -> Bool
isAsciiLetter Char
c Bool -> Bool -> Bool
|| Char -> Bool
isDigit Char
c Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'+' Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'-' Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'.'

-- hosted-git-info's isGitHubShorthand, which reads user/repo as github:user/repo.
gitHubShorthand :: Text -> Bool
gitHubShorthand :: Text -> Bool
gitHubShorthand Text
spec =
    Bool -> Bool
not ((Char -> Bool) -> Text -> Bool
T.any (\Char
c -> Char -> Bool
isSpace Char
c Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'@' Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
':') Text
beforeHash)
        Bool -> Bool -> Bool
&& HasCallStack => Text -> Text -> Int
Text -> Text -> Int
T.count Text
"/" Text
beforeHash Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
1
        Bool -> Bool -> Bool
&& Bool -> (Int -> Bool) -> Maybe Int -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False (Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0) ((Char -> Bool) -> Text -> Maybe Int
T.findIndex (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'/') Text
spec)
        Bool -> Bool -> Bool
&& Bool -> Bool
not (Text
"/" Text -> Text -> Bool
`T.isSuffixOf` Text
beforeHash)
        Bool -> Bool -> Bool
&& Bool -> Bool
not (Text
"." Text -> Text -> Bool
`T.isPrefixOf` Text
spec)
  where
    beforeHash :: Text
beforeHash = (Char -> Bool) -> Text -> Text
T.takeWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'#') Text
spec

userAtGitHost :: Text -> Bool
userAtGitHost :: Text -> Bool
userAtGitHost Text
spec = (Text -> Bool) -> [Text] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any Text -> Bool
startsAtGitHost (Int -> [Text] -> [Text]
forall a. Int -> [a] -> [a]
drop Int
1 (HasCallStack => Text -> Text -> [Text]
Text -> Text -> [Text]
T.splitOn Text
"@" (Text -> Text
T.toLower Text
spec)))
  where
    startsAtGitHost :: Text -> Bool
startsAtGitHost Text
after = (Text -> Bool) -> [Text] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (\Text
host -> Text -> Text -> Bool
leads Text
host Text
after Bool -> Bool -> Bool
|| Text -> Text -> Bool
leads (Text
"www." Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
host) Text
after) [Text]
gitHosts
    leads :: Text -> Text -> Bool
leads Text
host Text
after = case Text -> Text -> Maybe Text
T.stripPrefix Text
host Text
after of
        Just Text
rest -> Bool -> ((Char, Text) -> Bool) -> Maybe (Char, Text) -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
True (\(Char
next, Text
_) -> Char
next Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
':' Bool -> Bool -> Bool
|| Char
next Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'/' Bool -> Bool -> Bool
|| Char
next Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'#') (Text -> Maybe (Char, Text)
T.uncons Text
rest)
        Maybe Text
Nothing -> Bool
False

-- The hosts hosted-git-info resolves from a URL's host name.
gitHosts :: [Text]
gitHosts :: [Text]
gitHosts = [Text
"github.com", Text
"gist.github.com", Text
"gitlab.com", Text
"bitbucket.org", Text
"git.sr.ht"]

isAsciiLetter :: Char -> Bool
isAsciiLetter :: Char -> Bool
isAsciiLetter Char
c = Char -> Bool
isAsciiLower Char
c Bool -> Bool -> Bool
|| Char -> Bool
isAsciiUpper Char
c

{- | A release object's external dependency, reading only maps that are objects and specs that are
strings. Several pick the first field, then the smallest name, so every read form reports one.
-}
externalDependencyOf :: KeyMap Value -> Maybe ExternalDependency
externalDependencyOf :: KeyMap Value -> Maybe ExternalDependency
externalDependencyOf KeyMap Value
release = [Maybe ExternalDependency] -> Maybe ExternalDependency
forall (t :: * -> *) (f :: * -> *) a.
(Foldable t, Alternative f) =>
t (f a) -> f a
asum ((InstallableField -> Maybe ExternalDependency)
-> [InstallableField] -> [Maybe ExternalDependency]
forall a b. (a -> b) -> [a] -> [b]
map InstallableField -> Maybe ExternalDependency
inField [InstallableField]
forall a. Universe a => [a]
Universe.universe)
  where
    inField :: InstallableField -> Maybe ExternalDependency
inField InstallableField
field = case Key -> KeyMap Value -> Maybe Value
forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup (Text -> Key
Key.fromText (InstallableField -> Text
installableFieldName InstallableField
field)) KeyMap Value
release of
        Just (Object KeyMap Value
specs) -> [Maybe ExternalDependency] -> Maybe ExternalDependency
forall (t :: * -> *) (f :: * -> *) a.
(Foldable t, Alternative f) =>
t (f a) -> f a
asum [InstallableField -> Text -> ExternalSource -> ExternalDependency
ExternalDependency InstallableField
field (Key -> Text
Key.toText Key
name) (ExternalSource -> ExternalDependency)
-> Maybe ExternalSource -> Maybe ExternalDependency
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Maybe ExternalSource
externalSource Text
spec | (Key
name, String Text
spec) <- KeyMap Value -> [(Key, Value)]
forall v. KeyMap v -> [(Key, v)]
KeyMap.toAscList KeyMap Value
specs]
        Maybe Value
_ -> Maybe ExternalDependency
forall a. Maybe a
Nothing

-- | The dependency 'externalDependencyOf' would report of the two: the earlier field, then the smaller name.
earlierDependency :: ExternalDependency -> ExternalDependency -> ExternalDependency
earlierDependency :: ExternalDependency -> ExternalDependency -> ExternalDependency
earlierDependency ExternalDependency
held ExternalDependency
next
    | (ExternalDependency -> InstallableField
edField ExternalDependency
next, ExternalDependency -> Text
edName ExternalDependency
next) (InstallableField, Text) -> (InstallableField, Text) -> Bool
forall a. Ord a => a -> a -> Bool
< (ExternalDependency -> InstallableField
edField ExternalDependency
held, ExternalDependency -> Text
edName ExternalDependency
held) = ExternalDependency
next
    | Bool
otherwise = ExternalDependency
held

-- | Name the dependency, its field and the kind of source, without the spec.
renderExternalDependency :: ExternalDependency -> Text
renderExternalDependency :: ExternalDependency -> Text
renderExternalDependency ExternalDependency
dependency =
    Text
"dependency '" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> ExternalDependency -> Text
edName ExternalDependency
dependency Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"' in " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> InstallableField -> Text
installableFieldName (ExternalDependency -> InstallableField
edField ExternalDependency
dependency) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" names " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
kind
  where
    kind :: Text
kind = case ExternalDependency -> ExternalSource
edSource ExternalDependency
dependency of
        ExternalSource
GitSource -> Text
"a git source"
        ExternalSource
RemoteUrl -> Text
"a remote URL"
        ExternalSource
UnsupportedScheme -> Text
"an unsupported scheme"