module Ecluse.Core.Registry.RubyGems.Project (
projectName,
rubyGemsNameChar,
rubyGemsNameChars,
unscopedRubyGems,
gemFileStem,
projectVersion,
GemPlatform,
mkGemPlatform,
unGemPlatform,
GemCoordinate,
mkCoordinate,
coordinateName,
coordinateVersion,
coordinatePlatform,
coordinateFilename,
coordinateStem,
candidateCoordinates,
) where
import Data.ByteString qualified as BS
import Data.Char (isDigit)
import Data.Text qualified as T
import Ecluse.Core.Ecosystem (Ecosystem (RubyGems))
import Ecluse.Core.Package (PackageName, mkPackageName, pkgEcosystem, pkgNamespace, renderPackageName)
import Ecluse.Core.Registry (ParseError (ParseError))
import Ecluse.Core.Registry.WireSupport (nameComponentWith)
import Ecluse.Core.Server.Path (Filename, mkFilename, unFilename)
import Ecluse.Core.Text (isAsciiAlphaNum)
import Ecluse.Core.Version (Version, mkVersion, renderVersion)
import Ecluse.Core.Version.Gem (parseGem)
projectName :: Text -> Either ParseError PackageName
projectName :: Text -> Either ParseError PackageName
projectName Text
raw = Ecosystem -> Maybe Scope -> Text -> PackageName
mkPackageName Ecosystem
RubyGems Maybe Scope
forall a. Maybe a
Nothing (Text -> PackageName)
-> Either ParseError Text -> Either ParseError PackageName
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> (Text -> Bool) -> Text -> Either ParseError Text
nameComponentWith Text
"RubyGems package name" ((Char -> Bool) -> Text -> Bool
T.all Char -> Bool
rubyGemsNameChar) Text
raw
rubyGemsNameChar :: Char -> Bool
rubyGemsNameChar :: Char -> Bool
rubyGemsNameChar Char
c = Char -> Bool
isAsciiAlphaNum Char
c Bool -> Bool -> Bool
|| Char
c Char -> [Char] -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` [Char
'.', Char
'-', Char
'_']
rubyGemsNameChars :: [Char]
rubyGemsNameChars :: [Char]
rubyGemsNameChars = [Char
'A' .. Char
'Z'] [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char
'a' .. Char
'z'] [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char
'0' .. Char
'9'] [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
"._-"
unscopedRubyGems :: PackageName -> Bool
unscopedRubyGems :: PackageName -> Bool
unscopedRubyGems PackageName
name = PackageName -> Ecosystem
pkgEcosystem PackageName
name Ecosystem -> Ecosystem -> Bool
forall a. Eq a => a -> a -> Bool
== Ecosystem
RubyGems Bool -> Bool -> Bool
&& Maybe Scope -> Bool
forall a. Maybe a -> Bool
isNothing (PackageName -> Maybe Scope
pkgNamespace PackageName
name)
gemFileStem :: Text -> Maybe Text
gemFileStem :: Text -> Maybe Text
gemFileStem = Text -> Text -> Maybe Text
T.stripSuffix Text
".gem"
projectVersion :: Text -> Either ParseError Version
projectVersion :: Text -> Either ParseError Version
projectVersion Text
raw = do
Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Text -> Text
T.strip Text
raw Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
raw Bool -> Bool -> Bool
&& Bool -> ((Char, Text) -> Bool) -> Maybe (Char, Text) -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False (Char -> Bool
isDigit (Char -> Bool) -> ((Char, Text) -> Char) -> (Char, Text) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char, Text) -> Char
forall a b. (a, b) -> a
fst) (Text -> Maybe (Char, Text)
T.uncons Text
raw) Bool -> Bool -> Bool
&& Maybe GemKey -> Bool
forall a. Maybe a -> Bool
isJust (Text -> Maybe GemKey
parseGem Text
raw)) (ParseError -> Either ParseError ()
forall a b. a -> Either a b
Left (Text -> ParseError
ParseError Text
"invalid RubyGems version"))
Version -> Either ParseError Version
forall a. a -> Either ParseError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Ecosystem -> Text -> Version
mkVersion Ecosystem
RubyGems (Text -> Text
T.copy Text
raw))
newtype GemPlatform = GemPlatform Text
deriving stock (GemPlatform -> GemPlatform -> Bool
(GemPlatform -> GemPlatform -> Bool)
-> (GemPlatform -> GemPlatform -> Bool) -> Eq GemPlatform
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: GemPlatform -> GemPlatform -> Bool
== :: GemPlatform -> GemPlatform -> Bool
$c/= :: GemPlatform -> GemPlatform -> Bool
/= :: GemPlatform -> GemPlatform -> Bool
Eq, Eq GemPlatform
Eq GemPlatform =>
(GemPlatform -> GemPlatform -> Ordering)
-> (GemPlatform -> GemPlatform -> Bool)
-> (GemPlatform -> GemPlatform -> Bool)
-> (GemPlatform -> GemPlatform -> Bool)
-> (GemPlatform -> GemPlatform -> Bool)
-> (GemPlatform -> GemPlatform -> GemPlatform)
-> (GemPlatform -> GemPlatform -> GemPlatform)
-> Ord GemPlatform
GemPlatform -> GemPlatform -> Bool
GemPlatform -> GemPlatform -> Ordering
GemPlatform -> GemPlatform -> GemPlatform
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 :: GemPlatform -> GemPlatform -> Ordering
compare :: GemPlatform -> GemPlatform -> Ordering
$c< :: GemPlatform -> GemPlatform -> Bool
< :: GemPlatform -> GemPlatform -> Bool
$c<= :: GemPlatform -> GemPlatform -> Bool
<= :: GemPlatform -> GemPlatform -> Bool
$c> :: GemPlatform -> GemPlatform -> Bool
> :: GemPlatform -> GemPlatform -> Bool
$c>= :: GemPlatform -> GemPlatform -> Bool
>= :: GemPlatform -> GemPlatform -> Bool
$cmax :: GemPlatform -> GemPlatform -> GemPlatform
max :: GemPlatform -> GemPlatform -> GemPlatform
$cmin :: GemPlatform -> GemPlatform -> GemPlatform
min :: GemPlatform -> GemPlatform -> GemPlatform
Ord, Int -> GemPlatform -> [Char] -> [Char]
[GemPlatform] -> [Char] -> [Char]
GemPlatform -> [Char]
(Int -> GemPlatform -> [Char] -> [Char])
-> (GemPlatform -> [Char])
-> ([GemPlatform] -> [Char] -> [Char])
-> Show GemPlatform
forall a.
(Int -> a -> [Char] -> [Char])
-> (a -> [Char]) -> ([a] -> [Char] -> [Char]) -> Show a
$cshowsPrec :: Int -> GemPlatform -> [Char] -> [Char]
showsPrec :: Int -> GemPlatform -> [Char] -> [Char]
$cshow :: GemPlatform -> [Char]
show :: GemPlatform -> [Char]
$cshowList :: [GemPlatform] -> [Char] -> [Char]
showList :: [GemPlatform] -> [Char] -> [Char]
Show)
mkGemPlatform :: Text -> Either ParseError GemPlatform
mkGemPlatform :: Text -> Either ParseError GemPlatform
mkGemPlatform Text
raw = do
checked <- Text -> (Text -> Bool) -> Text -> Either ParseError Text
nameComponentWith Text
"RubyGems platform" ((Char -> Bool) -> Text -> Bool
T.all Char -> Bool
rubyGemsNameChar) Text
raw
let (_, separated) = T.breakOn "-" checked
os = if Text -> Bool
T.null Text
separated then Text
checked else Int -> Text -> Text
T.drop Int
1 Text
separated
unless (checked == "ruby" || knownOs checked || knownOs os) (Left (ParseError "unsupported RubyGems platform"))
pure (GemPlatform (T.copy checked))
where
knownOs :: Text -> Bool
knownOs Text
value = (Text -> Bool) -> [Text] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (Text -> Text -> Bool
family Text
value) [Text
"aix", Text
"cygwin", Text
"darwin", Text
"macruby", Text
"freebsd", Text
"java", Text
"jruby", Text
"dalvik", Text
"dotnet", Text
"linux", Text
"mingw32", Text
"mingw", Text
"mswin32", Text
"mswin64", Text
"netbsdelf", Text
"openbsd", Text
"solaris", Text
"wasi"]
family :: Text -> Text -> Bool
family Text
value Text
prefix = case Text -> Text -> Maybe Text
T.stripPrefix Text
prefix Text
value of
Just Text
rest -> Text -> Bool
T.null Text
rest Bool -> Bool -> Bool
|| Bool -> ((Char, Text) -> Bool) -> Maybe (Char, Text) -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False (\(Char
c, Text
_) -> 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 -> Bool
isDigit Char
c) (Text -> Maybe (Char, Text)
T.uncons Text
rest)
Maybe Text
Nothing -> Bool
False
unGemPlatform :: GemPlatform -> Text
unGemPlatform :: GemPlatform -> Text
unGemPlatform (GemPlatform Text
raw) = Text
raw
data GemCoordinate = GemCoordinate
{ GemCoordinate -> PackageName
coordinateName :: PackageName
, GemCoordinate -> Version
coordinateVersion :: Version
, GemCoordinate -> GemPlatform
coordinatePlatform :: GemPlatform
, GemCoordinate -> Filename
coordinateFilename :: Filename
}
deriving stock (GemCoordinate -> GemCoordinate -> Bool
(GemCoordinate -> GemCoordinate -> Bool)
-> (GemCoordinate -> GemCoordinate -> Bool) -> Eq GemCoordinate
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: GemCoordinate -> GemCoordinate -> Bool
== :: GemCoordinate -> GemCoordinate -> Bool
$c/= :: GemCoordinate -> GemCoordinate -> Bool
/= :: GemCoordinate -> GemCoordinate -> Bool
Eq, Int -> GemCoordinate -> [Char] -> [Char]
[GemCoordinate] -> [Char] -> [Char]
GemCoordinate -> [Char]
(Int -> GemCoordinate -> [Char] -> [Char])
-> (GemCoordinate -> [Char])
-> ([GemCoordinate] -> [Char] -> [Char])
-> Show GemCoordinate
forall a.
(Int -> a -> [Char] -> [Char])
-> (a -> [Char]) -> ([a] -> [Char] -> [Char]) -> Show a
$cshowsPrec :: Int -> GemCoordinate -> [Char] -> [Char]
showsPrec :: Int -> GemCoordinate -> [Char] -> [Char]
$cshow :: GemCoordinate -> [Char]
show :: GemCoordinate -> [Char]
$cshowList :: [GemCoordinate] -> [Char] -> [Char]
showList :: [GemCoordinate] -> [Char] -> [Char]
Show)
mkCoordinate :: PackageName -> Version -> GemPlatform -> Either ParseError GemCoordinate
mkCoordinate :: PackageName
-> Version -> GemPlatform -> Either ParseError GemCoordinate
mkCoordinate PackageName
name Version
version GemPlatform
platform = do
Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (PackageName -> Bool
unscopedRubyGems PackageName
name) (ParseError -> Either ParseError ()
forall a b. a -> Either a b
Left (Text -> ParseError
ParseError Text
"not an unscoped RubyGems package"))
checkedName <- Text -> Either ParseError PackageName
projectName (PackageName -> Text
renderPackageName PackageName
name)
checkedVersion <- projectVersion (renderVersion version)
let raw = Version -> Text
renderVersion Version
checkedVersion
let suffix = if GemPlatform -> Text
unGemPlatform GemPlatform
platform Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"ruby" then Text
"" else Text
"-" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> GemPlatform -> Text
unGemPlatform GemPlatform
platform
filename = PackageName -> Text
renderPackageName PackageName
checkedName Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"-" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
raw Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
suffix Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
".gem"
file <- maybeToRight (ParseError "invalid RubyGems artifact filename") (mkFilename filename)
pure (GemCoordinate checkedName checkedVersion platform file)
coordinateStem :: GemCoordinate -> Text
coordinateStem :: GemCoordinate -> Text
coordinateStem GemCoordinate
coordinate = let raw :: Text
raw = Filename -> Text
unFilename (GemCoordinate -> Filename
coordinateFilename GemCoordinate
coordinate) in Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe Text
raw (Text -> Maybe Text
gemFileStem Text
raw)
candidateCoordinates :: Int -> Filename -> Either ParseError [GemCoordinate]
candidateCoordinates :: Int -> Filename -> Either ParseError [GemCoordinate]
candidateCoordinates Int
cap Filename
filename = do
let raw :: Text
raw = Filename -> Text
unFilename Filename
filename
Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (ByteString -> Int
BS.length (Text -> ByteString
forall a b. ConvertUtf8 a b => a -> b
encodeUtf8 Text
raw) Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
cap) (ParseError -> Either ParseError ()
forall a b. a -> Either a b
Left (Text -> ParseError
ParseError Text
"RubyGems coordinate capture exceeds its bound"))
stem <- ParseError -> Maybe Text -> Either ParseError Text
forall l r. l -> Maybe r -> Either l r
maybeToRight (Text -> ParseError
ParseError Text
"RubyGems artifact capture has no .gem extension") (Text -> Maybe Text
gemFileStem Text
raw)
when (T.count "-" stem > 100) (Left (ParseError "RubyGems coordinate capture has too many separators"))
when (toInteger (BS.length (encodeUtf8 raw)) * (1 + toInteger (T.count "-" stem)) > toInteger cap) (Left (ParseError "RubyGems coordinate work exceeds its bound"))
let candidates = Int -> [GemCoordinate] -> [GemCoordinate]
forall a. Int -> [a] -> [a]
take Int
101 (((Text, Text) -> [GemCoordinate])
-> [(Text, Text)] -> [GemCoordinate]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Text, Text) -> [GemCoordinate]
splitVersion (Text -> [(Text, Text)]
splits Text
stem))
when (length candidates > 100) (Left (ParseError "RubyGems coordinate capture has too many candidates"))
when (null candidates) (Left (ParseError "invalid RubyGems artifact coordinate"))
pure candidates
where
splits :: Text -> [(Text, Text)]
splits Text
text = [(Int -> Text -> Text
T.take Int
at Text
text, Int -> Text -> Text
T.drop (Int
at Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Text
text) | (Int
at, Char
char) <- [Int] -> [Char] -> [(Int, Char)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0 ..] (Text -> [Char]
T.unpack Text
text), Char
char Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'-']
splitVersion :: (Text, Text) -> [GemCoordinate]
splitVersion (Text
nameText, Text
tailText) = case Text -> Either ParseError PackageName
projectName Text
nameText of
Left ParseError
_ -> []
Right PackageName
name -> ((Text, Text) -> Maybe GemCoordinate)
-> [(Text, Text)] -> [GemCoordinate]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (PackageName -> (Text, Text) -> Maybe GemCoordinate
build PackageName
name) ((Text
tailText, Text
"ruby") (Text, Text) -> [(Text, Text)] -> [(Text, Text)]
forall a. a -> [a] -> [a]
: Text -> [(Text, Text)]
splits Text
tailText)
build :: PackageName -> (Text, Text) -> Maybe GemCoordinate
build PackageName
name (Text
version, Text
platform) = Either ParseError GemCoordinate -> Maybe GemCoordinate
forall l r. Either l r -> Maybe r
rightToMaybe (Either ParseError GemCoordinate -> Maybe GemCoordinate)
-> Either ParseError GemCoordinate -> Maybe GemCoordinate
forall a b. (a -> b) -> a -> b
$ do
parsed <- Text -> Either ParseError GemPlatform
mkGemPlatform Text
platform
mkCoordinate name (mkVersion RubyGems version) parsed