summaryrefslogtreecommitdiff
path: root/lib/SlackBuilder/Package.hs
blob: 43710400bf12bc1b746ac482eec560123e848b64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
module SlackBuilder.Package
    ( DownloadPlaceholder(..)
    , Download(..)
    , DownloadTemplate(..)
    , Package(..)
    , PackageInfo(..)
    , 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)

-- | Contains information how a package can be updated.
data Package = Package
    { latest :: Updater
    , category :: Text
    , name :: Text
    , homepage :: URI
    , requires :: [Text]
    , reupload :: Bool
    }

-- | 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.
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 (SlackBuilderT (Maybe Text)) DownloadTemplate