slackbuilder/lib/SlackBuilder/Package.hs
Eugen Wissner 6a063b2cc4
All checks were successful
Build / audit (push) Successful in 16m10s
Build / test (push) Successful in 16m35s
Accept up2date package parameter
2023-12-11 08:14:55 +01:00

75 lines
2.1 KiB
Haskell

module SlackBuilder.Package
( DownloadPlaceholder(..)
, Download(..)
, DownloadTemplate(..)
, Package(..)
, Maintainer(..)
, Updater(..)
, renderDownloadWithVersion
) where
import Data.List.NonEmpty (NonEmpty(..))
import Data.Text (Text)
import qualified Data.Text as Text
import Text.URI (URI(..))
import qualified Text.URI as URI
import Crypto.Hash (Digest, MD5)
import SlackBuilder.Trans
import Control.Monad.Catch (MonadThrow)
import Data.Map (Map)
-- | Contains information how a package can be updated.
data Package = Package
{ latest :: Updater
, downloaders :: Map Text Updater
, category :: Text
, name :: Text
}
-- | Download URI with the MD5 checksum of the target.
data Download = Download
{ download :: URI
, md5sum :: Digest MD5
, is64 :: Bool
} deriving (Eq, Show)
-- | Package maintainer information.
data Maintainer = Maintainer
{ name :: Text
, email :: Text
} deriving (Eq, Show)
-- | Appears in the download URI template and specifies which part of the URI
-- should be replaced with the package version.
data DownloadPlaceholder
= StaticPlaceholder Text
| VersionPlaceholder
deriving Eq
instance Show DownloadPlaceholder
where
show (StaticPlaceholder staticPlaceholder) = Text.unpack staticPlaceholder
show VersionPlaceholder = "{version}"
-- | List of URI components, including version placeholders.
newtype DownloadTemplate = DownloadTemplate (NonEmpty DownloadPlaceholder)
deriving Eq
instance Show DownloadTemplate
where
show (DownloadTemplate components) = concatMap show components
-- | Replaces placeholders in the URL template with the given version.
renderDownloadWithVersion :: MonadThrow m => DownloadTemplate -> Text -> m URI
renderDownloadWithVersion (DownloadTemplate components) version =
URI.mkURI $ foldr f "" components
where
f (StaticPlaceholder staticPlaceholder) = (staticPlaceholder <>)
f VersionPlaceholder = (version <>)
-- | Function used to get the latest version of a source.
data Updater = Updater
{ detectLatest :: SlackBuilderT (Maybe Text)
, getVersion :: Text -> Text -> SlackBuilderT Download
}