slackbuilder/lib/SlackBuilder/Package.hs

83 lines
2.3 KiB
Haskell
Raw Normal View History

2023-08-28 21:05:47 +02:00
module SlackBuilder.Package
( DownloadPlaceholder(..)
, Download(..)
2023-09-03 10:26:43 +02:00
, DownloadTemplate(..)
2023-10-05 19:24:42 +02:00
, Package(..)
2023-08-28 21:05:47 +02:00
, PackageInfo(..)
, Maintainer(..)
, Updater(..)
2023-09-03 10:26:43 +02:00
, renderDownloadWithVersion
2023-08-28 21:05:47 +02:00
) where
2023-09-03 10:26:43 +02:00
import Data.List.NonEmpty (NonEmpty(..))
2023-08-28 21:05:47 +02:00
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
2023-09-03 10:26:43 +02:00
import Control.Monad.Catch (MonadThrow)
2023-08-28 21:05:47 +02:00
2023-10-05 19:24:42 +02:00
-- | Contains information how a package can be updated.
data Package = Package
{ latest :: Updater
, category :: Text
, name :: Text
, homepage :: URI
, requires :: [Text]
, reupload :: Bool
}
2023-08-28 21:05:47 +02:00
-- | Download URI with the MD5 checksum of the target.
data Download = Download
{ download :: URI
, md5sum :: Digest MD5
, is64 :: Bool
} deriving (Eq, Show)
-- | Data used to generate an .info file.
data PackageInfo = PackageInfo
{ path :: FilePath
, version :: Text
, homepage :: Text
, requires :: [Text]
, maintainer :: Maintainer
} 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.
2023-09-03 10:26:43 +02:00
renderDownloadWithVersion :: MonadThrow m => DownloadTemplate -> Text -> m URI
renderDownloadWithVersion (DownloadTemplate components) version =
URI.mkURI $ foldr f "" components
where
f (StaticPlaceholder staticPlaceholder) = (staticPlaceholder <>)
f VersionPlaceholder = (version <>)
2023-09-03 10:26:43 +02:00
2023-08-28 21:05:47 +02:00
-- | Function used to get the latest version of a source.
2023-09-03 10:26:43 +02:00
data Updater = Updater (SlackBuilderT (Maybe Text)) DownloadTemplate