module Ecluse.Core.Registry.RubyGems.Archive.Metadata (
GemMetadata (..),
parseGemMetadata,
) where
import Data.Map.Strict qualified as Map
import Data.Set qualified as Set
import Data.Text qualified as T
import Text.Libyaml qualified as Y
import Ecluse.Core.Ecosystem (Ecosystem (RubyGems))
import Ecluse.Core.Registry (ParseError (..), parseRefusal)
import Ecluse.Core.Registry.Archive.Path (checkPath)
import Ecluse.Core.Registry.RubyGems.Archive.Yaml
import Ecluse.Core.Registry.RubyGems.CompactSpec (GemSpecification (..))
import Ecluse.Core.Registry.RubyGems.Document
import Ecluse.Core.Registry.RubyGems.Project
import Ecluse.Core.Version (Version, mkVersion, renderVersion)
import Ecluse.Core.Version.Gem (isGemRequirementOperator, parseGem)
data GemMetadata = GemMetadata
{ GemMetadata -> GemSpecification
metadataSpecification :: GemSpecification
, GemMetadata -> [Text]
metadataFiles :: [Text]
, GemMetadata -> [Text]
metadataExtensions :: [Text]
, GemMetadata -> YamlValue
metadataDocument :: YamlValue
}
deriving stock (GemMetadata -> GemMetadata -> Bool
(GemMetadata -> GemMetadata -> Bool)
-> (GemMetadata -> GemMetadata -> Bool) -> Eq GemMetadata
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: GemMetadata -> GemMetadata -> Bool
== :: GemMetadata -> GemMetadata -> Bool
$c/= :: GemMetadata -> GemMetadata -> Bool
/= :: GemMetadata -> GemMetadata -> Bool
Eq, Int -> GemMetadata -> ShowS
[GemMetadata] -> ShowS
GemMetadata -> String
(Int -> GemMetadata -> ShowS)
-> (GemMetadata -> String)
-> ([GemMetadata] -> ShowS)
-> Show GemMetadata
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> GemMetadata -> ShowS
showsPrec :: Int -> GemMetadata -> ShowS
$cshow :: GemMetadata -> String
show :: GemMetadata -> String
$cshowList :: [GemMetadata] -> ShowS
showList :: [GemMetadata] -> ShowS
Show)
parseGemMetadata :: YamlValue -> Either ParseError GemMetadata
parseGemMetadata :: YamlValue -> Either ParseError GemMetadata
parseGemMetadata YamlValue
document = do
fields <- Text -> YamlValue -> Either ParseError (Map Text YamlValue)
tagged Text
"Gem::Specification" YamlValue
document
name <- yamlField "name" fields >>= yamlNativeText >>= projectName
version <- yamlField "version" fields >>= nativeVersion
platform <- yamlField "platform" fields >>= nativePlatform
specificationVersion <- yamlField "specification_version" fields >>= yamlText
unless (specificationVersion `elem` ["1", "2", "3", "4"]) (parseRefusal "unsupported gem metadata specification version")
dependencies <- yamlField "dependencies" fields >>= yamlSequence >>= traverse nativeDependency
ruby <- yamlField "required_ruby_version" fields >>= nativeRequirement
gems <- yamlField "required_rubygems_version" fields >>= nativeRequirement
metadata <- case Map.lookup "metadata" fields of
Maybe YamlValue
Nothing -> Map Text Text -> Either ParseError (Map Text Text)
forall a b. b -> Either a b
Right Map Text Text
forall k a. Map k a
Map.empty
Just YamlValue
raw -> YamlValue -> Either ParseError (Map Text YamlValue)
yamlMapping YamlValue
raw Either ParseError (Map Text YamlValue)
-> (Map Text YamlValue -> Either ParseError (Map Text Text))
-> Either ParseError (Map Text Text)
forall a b.
Either ParseError a
-> (a -> Either ParseError b) -> Either ParseError b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (YamlValue -> Either ParseError Text)
-> Map Text YamlValue -> Either ParseError (Map Text Text)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Map Text a -> f (Map Text b)
traverse YamlValue -> Either ParseError Text
yamlText
files <- yamlField "files" fields >>= paths
extensions <- maybe (Right []) paths (Map.lookup "extensions" fields)
let inventory = [Text] -> Set Text
forall a. Ord a => [a] -> Set a
Set.fromList [Text]
files
unless (Set.size inventory == length files) (parseRefusal "duplicate gem metadata file")
unless (all (`Set.member` inventory) extensions) (parseRefusal "gem extension is absent from metadata file inventory")
let specification = PackageName
-> Version
-> GemPlatform
-> [GemDependency]
-> GemRequirement
-> GemRequirement
-> Map Text Text
-> GemSpecification
GemSpecification PackageName
name Version
version GemPlatform
platform ([Maybe GemDependency] -> [GemDependency]
forall a. [Maybe a] -> [a]
catMaybes [Maybe GemDependency]
dependencies) GemRequirement
ruby GemRequirement
gems Map Text Text
metadata
pure (GemMetadata specification files extensions document)
paths :: YamlValue -> Either ParseError [Text]
paths :: YamlValue -> Either ParseError [Text]
paths YamlValue
value =
YamlValue -> Either ParseError [YamlValue]
yamlSequence YamlValue
value
Either ParseError [YamlValue]
-> ([YamlValue] -> Either ParseError [Text])
-> Either ParseError [Text]
forall a b.
Either ParseError a
-> (a -> Either ParseError b) -> Either ParseError b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (YamlValue -> Either ParseError Text)
-> [YamlValue] -> Either ParseError [Text]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse
( \YamlValue
item -> do
raw <- YamlValue -> Either ParseError Text
yamlText YamlValue
item
parts <- checkPath raw
unless (T.intercalate "/" parts == raw) (parseRefusal "non-canonical gem metadata path")
pure raw
)
tagged :: Text -> YamlValue -> Either ParseError (Map Text YamlValue)
tagged :: Text -> YamlValue -> Either ParseError (Map Text YamlValue)
tagged Text
className YamlValue
value = do
Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (YamlValue -> Tag
yamlTag YamlValue
value Tag -> Tag -> Bool
forall a. Eq a => a -> a -> Bool
== String -> Tag
Y.UriTag (Text -> String
forall a. ToString a => a -> String
toString (Text
"!ruby/object:" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
className))) (Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"incorrect native gem YAML tag")
YamlValue -> Either ParseError (Map Text YamlValue)
yamlMapping YamlValue
value
nativeVersion :: YamlValue -> Either ParseError Version
nativeVersion :: YamlValue -> Either ParseError Version
nativeVersion YamlValue
value = do
fields <- Text -> YamlValue -> Either ParseError (Map Text YamlValue)
tagged Text
"Gem::Version" YamlValue
value
unless (Map.keys fields == ["version"]) (parseRefusal "invalid Gem::Version YAML fields")
raw <- yamlField "version" fields >>= yamlNativeText
unless (isJust (parseGem raw)) (parseRefusal "invalid gem metadata version")
pure (mkVersion RubyGems raw)
nativeRequirement :: YamlValue -> Either ParseError GemRequirement
nativeRequirement :: YamlValue -> Either ParseError GemRequirement
nativeRequirement YamlValue
value = do
fields <- Text -> YamlValue -> Either ParseError (Map Text YamlValue)
tagged Text
"Gem::Requirement" YamlValue
value
unless (Map.keys fields == ["requirements"]) (parseRefusal "invalid Gem::Requirement YAML fields")
clauses <- yamlField "requirements" fields >>= yamlSequence >>= traverse clause
maybeToRight (ParseError "empty gem metadata requirement") (nonEmpty clauses)
where
clause :: YamlValue -> Either ParseError Text
clause YamlValue
raw =
YamlValue -> Either ParseError [YamlValue]
yamlSequence YamlValue
raw Either ParseError [YamlValue]
-> ([YamlValue] -> Either ParseError Text)
-> Either ParseError Text
forall a b.
Either ParseError a
-> (a -> Either ParseError b) -> Either ParseError b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
[YamlValue
operator, YamlValue
version] -> do
op <- YamlValue -> Either ParseError Text
yamlText YamlValue
operator
unless (isGemRequirementOperator op) (parseRefusal "invalid gem metadata requirement operator")
text <- renderVersion <$> nativeVersion version
pure (op <> " " <> text)
[YamlValue]
_ -> Text -> Either ParseError Text
forall a. Text -> Either ParseError a
parseRefusal Text
"invalid gem metadata requirement clause"
nativeDependency :: YamlValue -> Either ParseError (Maybe GemDependency)
nativeDependency :: YamlValue -> Either ParseError (Maybe GemDependency)
nativeDependency YamlValue
value = do
fields <- Text -> YamlValue -> Either ParseError (Map Text YamlValue)
tagged Text
"Gem::Dependency" YamlValue
value
unless (all (`elem` ["name", "requirement", "type", "prerelease", "version_requirements"]) (Map.keys fields)) (parseRefusal "unsupported Gem::Dependency YAML field")
name <- yamlField "name" fields >>= yamlNativeText >>= projectName
wanted <- yamlField "requirement" fields >>= nativeRequirement
for_ (Map.lookup "version_requirements" fields) $ \YamlValue
other -> do
old <- YamlValue -> Either ParseError GemRequirement
nativeRequirement YamlValue
other
unless (sort (toList wanted) == sort (toList old)) (parseRefusal "conflicting gem metadata requirements")
kind <- yamlField "type" fields >>= yamlText
case kind of
Text
":runtime" -> Maybe GemDependency -> Either ParseError (Maybe GemDependency)
forall a b. b -> Either a b
Right (GemDependency -> Maybe GemDependency
forall a. a -> Maybe a
Just (PackageName -> GemRequirement -> GemDependency
GemDependency PackageName
name GemRequirement
wanted))
Text
"runtime" -> Maybe GemDependency -> Either ParseError (Maybe GemDependency)
forall a b. b -> Either a b
Right (GemDependency -> Maybe GemDependency
forall a. a -> Maybe a
Just (PackageName -> GemRequirement -> GemDependency
GemDependency PackageName
name GemRequirement
wanted))
Text
":development" -> Maybe GemDependency -> Either ParseError (Maybe GemDependency)
forall a b. b -> Either a b
Right Maybe GemDependency
forall a. Maybe a
Nothing
Text
"development" -> Maybe GemDependency -> Either ParseError (Maybe GemDependency)
forall a b. b -> Either a b
Right Maybe GemDependency
forall a. Maybe a
Nothing
Text
_ -> Text -> Either ParseError (Maybe GemDependency)
forall a. Text -> Either ParseError a
parseRefusal Text
"unsupported gem metadata dependency type"
nativePlatform :: YamlValue -> Either ParseError GemPlatform
nativePlatform :: YamlValue -> Either ParseError GemPlatform
nativePlatform YamlValue
value = case YamlValue
value of
YamlScalar{} -> YamlValue -> Either ParseError Text
yamlNativeText YamlValue
value Either ParseError Text
-> (Text -> Either ParseError GemPlatform)
-> Either ParseError GemPlatform
forall a b.
Either ParseError a
-> (a -> Either ParseError b) -> Either ParseError b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Text -> Either ParseError GemPlatform
mkGemPlatform
YamlValue
_ -> do
fields <- Text -> YamlValue -> Either ParseError (Map Text YamlValue)
tagged Text
"Gem::Platform" YamlValue
value
unless (Map.keys fields == ["cpu", "os", "version"]) (parseRefusal "invalid Gem::Platform YAML fields")
cpu <- yamlField "cpu" fields >>= optionalPart
os <- yamlField "os" fields >>= yamlNativeText
version <- yamlField "version" fields >>= optionalPart
mkGemPlatform (T.intercalate "-" (catMaybes [cpu, Just os, version]))
where
optionalPart :: YamlValue -> Either ParseError (Maybe Text)
optionalPart YamlValue
raw = do
text <- YamlValue -> Either ParseError (Maybe Text)
yamlNativeString YamlValue
raw
pure (text >>= \Text
part -> Text
part Text -> Maybe () -> Maybe Text
forall a b. a -> Maybe b -> Maybe a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> Bool
not (Text -> Bool
T.null Text
part)))