Support text based placeholders
All checks were successful
Build / audit (push) Successful in 9s
Build / test (push) Successful in 14m17s

This commit is contained in:
Eugen Wissner 2024-09-01 17:34:24 +02:00
parent 14cc805dcf
commit 4f74c2ec10
Signed by: belka
GPG Key ID: A27FDC1E8EE902C0
3 changed files with 21 additions and 80 deletions

View File

@ -9,12 +9,10 @@ module SlackBuilder.Package
, DownloadTemplate(..)
, PackageDescription(..)
, PackageUpdateData(..)
, Maintainer(..)
, Updater(..)
, renderDownloadWithVersion
) where
import Data.List.NonEmpty (NonEmpty(..))
import Data.Text (Text)
import qualified Data.Text as Text
import Text.URI (URI(..))
@ -43,12 +41,6 @@ data Download = Download
, md5sum :: Digest MD5
} 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
@ -62,20 +54,18 @@ instance Show DownloadPlaceholder
show VersionPlaceholder = "{version}"
-- | List of URI components, including version placeholders.
newtype DownloadTemplate = DownloadTemplate (NonEmpty DownloadPlaceholder)
deriving Eq
newtype DownloadTemplate = DownloadTemplate
{ unDownloadTemplate :: Text
} deriving Eq
instance Show DownloadTemplate
where
show (DownloadTemplate components) = concatMap show components
show = Text.unpack . unDownloadTemplate
-- | 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 <>)
renderDownloadWithVersion (DownloadTemplate template) version =
URI.mkURI $ Text.replace "{version}" version template
-- | Function used to get the latest version of a source.
data Updater = Updater

View File

@ -8,7 +8,6 @@ module Main
import Control.Exception (Exception(..), handle)
import Data.Char (isNumber)
import Data.List.NonEmpty (NonEmpty(..))
import Control.Monad.Catch (MonadThrow(..))
import Control.Monad.IO.Class (MonadIO(..))
import qualified Data.Map as Map
@ -41,15 +40,8 @@ autoUpdatable =
[ PackageDescription
{ latest =
let ghArguments = PackageOwner{ owner = "universal-ctags", name = "ctags" }
templateTail =
[ Package.VersionPlaceholder
, Package.StaticPlaceholder "/ctags-"
, Package.VersionPlaceholder
, Package.StaticPlaceholder ".tar.gz"
]
template = Package.DownloadTemplate
$ Package.StaticPlaceholder "https://github.com/universal-ctags/ctags/archive/"
:| templateTail
"https://github.com/universal-ctags/ctags/archive/{version}/ctags-{version}.tar.gz"
in Package.Updater
{ detectLatest = latestGitHub ghArguments "(v)\\."
, getVersion = reuploadWithTemplate template []
@ -62,8 +54,7 @@ autoUpdatable =
{ latest =
let packagistArguments = PackageOwner{ owner = "composer", name = "composer" }
template = Package.DownloadTemplate
$ Package.StaticPlaceholder "https://getcomposer.org/download/"
:| [Package.VersionPlaceholder, Package.StaticPlaceholder "/composer.phar"]
"https://getcomposer.org/download/{version}/composer.phar"
in Package.Updater
{ detectLatest = latestPackagist packagistArguments
, getVersion = downloadWithTemplate template
@ -79,9 +70,7 @@ autoUpdatable =
, name = "jitsi-meet-electron"
}
template = Package.DownloadTemplate
$ Package.StaticPlaceholder "https://github.com/jitsi/jitsi-meet-electron/releases/download/v"
:| Package.VersionPlaceholder
: [Package.StaticPlaceholder "/jitsi-meet-x86_64.AppImage"]
"https://github.com/jitsi/jitsi-meet-electron/releases/download/v{version}/jitsi-meet-x86_64.AppImage"
in Package.Updater
{ detectLatest = latestGitHub ghArguments "(v)*"
, getVersion = downloadWithTemplate template
@ -97,9 +86,7 @@ autoUpdatable =
, name = "php-src"
}
template = Package.DownloadTemplate
$ Package.StaticPlaceholder "https://www.php.net/distributions/php-"
:| Package.VersionPlaceholder
: [Package.StaticPlaceholder ".tar.xz"]
"https://www.php.net/distributions/php-{version}.tar.xz"
in Package.Updater
{ detectLatest = latestGitHub ghArguments "(php-)8.2.\\d"
, getVersion = downloadWithTemplate template
@ -114,15 +101,8 @@ autoUpdatable =
{ owner = "kovidgoyal"
, name = "kitty"
}
templateTail =
[ Package.StaticPlaceholder "/kitty-"
, Package.VersionPlaceholder
, Package.StaticPlaceholder ".tar.xz"
]
template = Package.DownloadTemplate
$ Package.StaticPlaceholder "https://github.com/kovidgoyal/kitty/releases/download/v"
:| Package.VersionPlaceholder
: templateTail
"https://github.com/kovidgoyal/kitty/releases/download/v{version}/kitty-{version}.tar.xz"
in Package.Updater
{ detectLatest = latestGitHub ghArguments "(v)\\."
, getVersion = reuploadWithTemplate template [RawCommand "go" ["mod", "vendor"]]
@ -138,11 +118,7 @@ autoUpdatable =
, name = "rdiff-backup"
}
template = Package.DownloadTemplate
$ Package.StaticPlaceholder "https://github.com/rdiff-backup/rdiff-backup/releases/download/v"
:| Package.VersionPlaceholder
: Package.StaticPlaceholder "/rdiff-backup-"
: Package.VersionPlaceholder
: [Package.StaticPlaceholder ".tar.gz"]
"https://github.com/rdiff-backup/rdiff-backup/releases/download/v{version}/rdiff-backup-{version}.tar.gz"
in Package.Updater
{ detectLatest = latestGitHub ghArguments "(v)\\."
, getVersion = reuploadWithTemplate template []
@ -161,8 +137,7 @@ autoUpdatable =
. snd
. Text.breakOn needle
}
template = Package.DownloadTemplate $ pure
$ Package.StaticPlaceholder
template = Package.DownloadTemplate
"https://binaries.webex.com/WebexDesktop-Ubuntu-Official-Package/Webex.deb"
in Package.Updater
{ detectLatest = latestText textArguments
@ -179,11 +154,7 @@ autoUpdatable =
, name = "librsync"
}
template = Package.DownloadTemplate
$ Package.StaticPlaceholder "https://github.com/librsync/librsync/archive/v"
:| Package.VersionPlaceholder
: Package.StaticPlaceholder "/librsync-"
: Package.VersionPlaceholder
: [Package.StaticPlaceholder ".tar.gz"]
"https://github.com/librsync/librsync/archive/v{version}/librsync-{version}.tar.gz"
in Package.Updater
{ detectLatest = latestGitHub ghArguments "(v)\\."
, getVersion = reuploadWithTemplate template []
@ -199,11 +170,7 @@ autoUpdatable =
, versionPicker = Text.strip
}
template = Package.DownloadTemplate
$ Package.StaticPlaceholder "https://downloads.dlang.org/releases/2.x/"
:| Package.VersionPlaceholder
: Package.StaticPlaceholder "/dmd."
: Package.VersionPlaceholder
: [Package.StaticPlaceholder ".linux.tar.xz"]
"https://downloads.dlang.org/releases/2.x/{version}/dmd.{version}.linux.tar.xz"
in Package.Updater
{ detectLatest = latestText textArguments
, getVersion = downloadWithTemplate template
@ -218,9 +185,7 @@ autoUpdatable =
{ textURL = "https://downloads.dlang.org/releases/LATEST"
, versionPicker = Text.strip
}
template = Package.DownloadTemplate
$ Package.StaticPlaceholder "https://codeload.github.com/dlang/tools/tar.gz/v"
:| [Package.VersionPlaceholder]
template = Package.DownloadTemplate "https://codeload.github.com/dlang/tools/tar.gz/v{version}"
in Package.Updater
{ detectLatest = latestText textArguments
, getVersion = reuploadWithTemplate template []
@ -247,9 +212,7 @@ autoUpdatable =
, getVersion = cloneFromGit dcdURI "v"
, is64 = False
}
dubTemplate = Package.DownloadTemplate
$ Package.StaticPlaceholder "https://codeload.github.com/dlang/dub/tar.gz/v"
:| [Package.VersionPlaceholder]
dubTemplate = Package.DownloadTemplate "https://codeload.github.com/dlang/dub/tar.gz/v{version}"
dscannerURI = [uri|https://github.com/dlang-community/D-Scanner.git|]
in Map.fromList
[ ("DUB", latestDub)
@ -263,15 +226,8 @@ autoUpdatable =
{ owner = "simd-everywhere"
, name = "simde"
}
templateTail =
[ Package.StaticPlaceholder "/simde-amalgamated-"
, Package.VersionPlaceholder
, Package.StaticPlaceholder ".tar.xz"
]
template = Package.DownloadTemplate
$ Package.StaticPlaceholder "https://github.com/simd-everywhere/simde/releases/download/v"
:| Package.VersionPlaceholder
: templateTail
"https://github.com/simd-everywhere/simde/releases/download/v{version}/simde-amalgamated-{version}.tar.xz"
in Package.Updater
{ detectLatest = latestGitHub ghArguments "(v)\\."
, getVersion = downloadWithTemplate template

View File

@ -6,7 +6,6 @@ module SlackBuilder.PackageSpec
( spec
) where
import Data.List.NonEmpty (NonEmpty(..))
import SlackBuilder.Package
import Test.Hspec (Spec, describe, it, shouldBe)
import Text.URI.QQ (uri)
@ -15,17 +14,13 @@ spec :: Spec
spec = do
describe "renderDownloadWithVersion" $ do
it "renders text as URL" $
let given = DownloadTemplate
$ pure
$ StaticPlaceholder "https://example.com"
let given = DownloadTemplate "https://example.com"
actual = renderDownloadWithVersion given "1.2"
expected = Just [uri|https://example.com|]
in actual `shouldBe` expected
it "renders the components in order" $
let given = DownloadTemplate
$ StaticPlaceholder "https://example.com/"
:| [VersionPlaceholder, StaticPlaceholder "/segment"]
let given = DownloadTemplate "https://example.com/{version}/segment"
actual = renderDownloadWithVersion given "1.2"
expected = Just [uri|https://example.com/1.2/segment|]
in actual `shouldBe` expected