From 2f46303a6d18b98d1c970a2c18d3907bba826bb0 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Thu, 14 Nov 2024 22:58:14 +0100 Subject: [PATCH] Support VCS clone configuration --- lib/SlackBuilder/Config.hs | 26 +++++++++++++--- src/Main.hs | 63 +++++++++++++++++++++----------------- 2 files changed, 56 insertions(+), 33 deletions(-) diff --git a/lib/SlackBuilder/Config.hs b/lib/SlackBuilder/Config.hs index 88d2253..cb4b4cf 100644 --- a/lib/SlackBuilder/Config.hs +++ b/lib/SlackBuilder/Config.hs @@ -4,7 +4,8 @@ -- | Configuration file. module SlackBuilder.Config - ( DownloaderSettings(..) + ( CloneSettings(..) + , DownloaderSettings(..) , Settings(..) , MaintainerSettings(..) , PackageSettings(..) @@ -33,17 +34,24 @@ newtype MaintainerSettings = MaintainerSettings data DownloaderSettings = DownloaderSettings { name :: Text - , template :: Text , is64 :: Bool , version :: Text + , template :: Maybe Text + , clone :: Maybe CloneSettings , github :: Maybe (Text, Text) , packagist :: Maybe (Text, Text) , text :: Maybe (Text, [String]) , repackage :: Maybe [String] } deriving (Eq, Show) -newtype PackageSettings = PackageSettings +data PackageSettings = PackageSettings { downloader :: DownloaderSettings + , downloaders :: [DownloaderSettings] + } deriving (Eq, Show) + +data CloneSettings = CloneSettings + { remote :: Text + , tagTemplate :: Text } deriving (Eq, Show) settingsCodec :: Toml.TomlCodec Settings @@ -63,9 +71,10 @@ maintainerSettingsCodec = MaintainerSettings downloaderSettingsCodec :: Toml.TomlCodec DownloaderSettings downloaderSettingsCodec = DownloaderSettings <$> Toml.text "name" .= name - <*> Toml.text "template" .= template <*> Toml.bool "is64" .= is64 <*> Toml.text "version" .= version + <*> Toml.dioptional (Toml.text "template") .= template + <*> Toml.dioptional (Toml.table cloneSettingsCodec "clone") .= clone <*> Toml.dioptional (Toml.table githubCodec "github") .= github <*> Toml.dioptional (Toml.table packagistCodec "packagist") .= packagist <*> Toml.dioptional (Toml.table textCodec "text") .= text @@ -76,4 +85,11 @@ downloaderSettingsCodec = DownloaderSettings textCodec = Toml.pair (Toml.text "url") (Toml.arrayOf Toml._String "picker") packageSettingsCodec :: Toml.TomlCodec PackageSettings -packageSettingsCodec = Toml.dimap (getField @"downloader") PackageSettings downloaderSettingsCodec +packageSettingsCodec = PackageSettings + <$> downloaderSettingsCodec .= getField @"downloader" + <*> Toml.list downloaderSettingsCodec "downloader" .= downloaders + +cloneSettingsCodec :: Toml.TomlCodec CloneSettings +cloneSettingsCodec = CloneSettings + <$> Toml.text "remote" .= remote + <*> Toml.text "tag_template" .= tagTemplate diff --git a/src/Main.hs b/src/Main.hs index 9c28456..5e2b0ca 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -32,6 +32,7 @@ import System.Console.ANSI , ConsoleLayer(..) ) import Data.Maybe (fromJust, mapMaybe) +import qualified Text.URI as URI findUpdatable :: Text -> [PackageSettings] -> DownloaderSettings findUpdatable packageName = getField @"downloader" . fromJust @@ -40,33 +41,39 @@ findUpdatable packageName = getField @"downloader" . fromJust githubAutoUpdatables :: [PackageSettings] -> [PackageDescription] githubAutoUpdatables = mapMaybe go where - go PackageSettings{ downloader = setting } - | Just githubSettings <- getField @"github" setting = Just - $ PackageDescription - { latest = - let ghArguments = uncurry PackageOwner githubSettings - template = Package.DownloadTemplate $ getField @"template" setting - in Package.Updater - { detectLatest = latestGitHub ghArguments (getField @"version" setting) - , getVersion = repackageWithTemplate (getField @"repackage" setting) template - , is64 = getField @"is64" setting - } + go PackageSettings{ downloader = setting, downloaders } = do + latest' <- packageUpdaterFromSettings setting + pure $ PackageDescription + { latest = latest' , name = getField @"name" setting - , downloaders = mempty - } - | Just packagistSettings <- getField @"packagist" setting = Just - $ PackageDescription - { latest = - let packagistArguments = uncurry PackageOwner packagistSettings - template = Package.DownloadTemplate $ getField @"template" setting - in Package.Updater - { detectLatest = latestPackagist packagistArguments - , getVersion = repackageWithTemplate (getField @"repackage" setting) template - , is64 = getField @"is64" setting - } - , name = getField @"name" setting - , downloaders = mempty + , downloaders = Map.fromList $ mapMaybe forDownloader downloaders } + forDownloader downloaderSettings@DownloaderSettings{ name } = + (name,) <$> packageUpdaterFromSettings downloaderSettings + +packageUpdaterFromSettings :: DownloaderSettings -> Maybe Package.Updater +packageUpdaterFromSettings DownloaderSettings{..} = do + getVersion' <- getVersionSettings + detectLatest' <- detectLatestSettings + Just Package.Updater + { detectLatest = detectLatest' + , getVersion = getVersion' + , is64 = is64 + } + where + detectLatestSettings + | Just githubSettings <- github = + let ghArguments = uncurry PackageOwner githubSettings + in Just $ latestGitHub ghArguments version + | Just packagistSettings <- packagist = + let packagistArguments = uncurry PackageOwner packagistSettings + in Just $ latestPackagist packagistArguments + | otherwise = Nothing + getVersionSettings + | Just template' <- template = + Just $ repackageWithTemplate repackage $ Package.DownloadTemplate template' + | Just CloneSettings{..} <- clone = + flip cloneFromGit tagTemplate <$> URI.mkURI remote | otherwise = Nothing autoUpdatable :: [PackageSettings] -> [PackageDescription] @@ -75,7 +82,7 @@ autoUpdatable packageSettings = githubAutoUpdatables packageSettings ++ { latest = let setting = findUpdatable "webex" packageSettings textArguments = uncurry TextArguments $ fromJust $ getField @"text" setting - template = Package.DownloadTemplate $ getField @"template" setting + template = Package.DownloadTemplate $ fromJust $ getField @"template" setting in Package.Updater { detectLatest = latestText textArguments (getField @"version" setting) , getVersion = repackageWithTemplate (getField @"repackage" setting) template @@ -88,7 +95,7 @@ autoUpdatable packageSettings = githubAutoUpdatables packageSettings ++ { latest = let setting = findUpdatable "dmd" packageSettings textArguments = uncurry TextArguments $ fromJust $ getField @"text" setting - template = Package.DownloadTemplate $ getField @"template" setting + template = Package.DownloadTemplate $ fromJust $ getField @"template" setting in Package.Updater { detectLatest = latestText textArguments (getField @"version" setting) , getVersion = repackageWithTemplate (getField @"repackage" setting) template @@ -101,7 +108,7 @@ autoUpdatable packageSettings = githubAutoUpdatables packageSettings ++ { latest = let setting = findUpdatable "d-tools" packageSettings textArguments = uncurry TextArguments $ fromJust $ getField @"text" setting - template = Package.DownloadTemplate $ getField @"template" setting + template = Package.DownloadTemplate $ fromJust $ getField @"template" setting in Package.Updater { detectLatest = latestText textArguments (getField @"version" setting) , getVersion = repackageWithTemplate (getField @"repackage" setting) template