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(..) , DownloadTemplate(..)
, PackageDescription(..) , PackageDescription(..)
, PackageUpdateData(..) , PackageUpdateData(..)
, Maintainer(..)
, Updater(..) , Updater(..)
, renderDownloadWithVersion , renderDownloadWithVersion
) where ) where
import Data.List.NonEmpty (NonEmpty(..))
import Data.Text (Text) import Data.Text (Text)
import qualified Data.Text as Text import qualified Data.Text as Text
import Text.URI (URI(..)) import Text.URI (URI(..))
@ -43,12 +41,6 @@ data Download = Download
, md5sum :: Digest MD5 , md5sum :: Digest MD5
} deriving (Eq, Show) } 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 -- | Appears in the download URI template and specifies which part of the URI
-- should be replaced with the package version. -- should be replaced with the package version.
data DownloadPlaceholder data DownloadPlaceholder
@ -62,20 +54,18 @@ instance Show DownloadPlaceholder
show VersionPlaceholder = "{version}" show VersionPlaceholder = "{version}"
-- | List of URI components, including version placeholders. -- | List of URI components, including version placeholders.
newtype DownloadTemplate = DownloadTemplate (NonEmpty DownloadPlaceholder) newtype DownloadTemplate = DownloadTemplate
deriving Eq { unDownloadTemplate :: Text
} deriving Eq
instance Show DownloadTemplate instance Show DownloadTemplate
where where
show (DownloadTemplate components) = concatMap show components show = Text.unpack . unDownloadTemplate
-- | Replaces placeholders in the URL template with the given version. -- | Replaces placeholders in the URL template with the given version.
renderDownloadWithVersion :: MonadThrow m => DownloadTemplate -> Text -> m URI renderDownloadWithVersion :: MonadThrow m => DownloadTemplate -> Text -> m URI
renderDownloadWithVersion (DownloadTemplate components) version = renderDownloadWithVersion (DownloadTemplate template) version =
URI.mkURI $ foldr f "" components URI.mkURI $ Text.replace "{version}" version template
where
f (StaticPlaceholder staticPlaceholder) = (staticPlaceholder <>)
f VersionPlaceholder = (version <>)
-- | Function used to get the latest version of a source. -- | Function used to get the latest version of a source.
data Updater = Updater data Updater = Updater

View File

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

View File

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