Allow updater save a function for each download

There are source that can be downloaded as archive, for example cloning
repositories with submodules. So how source are downloaded should be
changable per download.
This commit is contained in:
Eugen Wissner 2023-10-28 03:31:10 +02:00
parent 6c0e2c2d24
commit fa6d93c5ca
2 changed files with 45 additions and 44 deletions

View File

@ -64,10 +64,9 @@ autoUpdatable =
template = Package.DownloadTemplate template = Package.DownloadTemplate
$ Package.StaticPlaceholder "https://github.com/universal-ctags/ctags/archive/" $ Package.StaticPlaceholder "https://github.com/universal-ctags/ctags/archive/"
:| templateTail :| templateTail
in Package.Updater latest' template in Package.Updater latest' $ reuploadWithTemplate template []
, category = "development" , category = "development"
, name = "universal-ctags" , name = "universal-ctags"
, reupload = Just []
, downloaders = mempty , downloaders = mempty
} }
, Package , Package
@ -77,10 +76,9 @@ autoUpdatable =
template = Package.DownloadTemplate template = Package.DownloadTemplate
$ Package.StaticPlaceholder "https://getcomposer.org/download/" $ Package.StaticPlaceholder "https://getcomposer.org/download/"
:| [Package.VersionPlaceholder, Package.StaticPlaceholder "/composer.phar"] :| [Package.VersionPlaceholder, Package.StaticPlaceholder "/composer.phar"]
in Package.Updater latest' template in Package.Updater latest' $ downloadWithTemplate template
, category = "development" , category = "development"
, name = "composer" , name = "composer"
, reupload = Nothing
, downloaders = mempty , downloaders = mempty
} }
, Package , Package
@ -95,10 +93,9 @@ autoUpdatable =
$ Package.StaticPlaceholder "https://github.com/jitsi/jitsi-meet-electron/releases/download/v" $ Package.StaticPlaceholder "https://github.com/jitsi/jitsi-meet-electron/releases/download/v"
:| Package.VersionPlaceholder :| Package.VersionPlaceholder
: [Package.StaticPlaceholder "/jitsi-meet-x86_64.AppImage"] : [Package.StaticPlaceholder "/jitsi-meet-x86_64.AppImage"]
in Package.Updater latest' template in Package.Updater latest' $ downloadWithTemplate template
, category = "network" , category = "network"
, name = "jitsi-meet-desktop" , name = "jitsi-meet-desktop"
, reupload = Nothing
, downloaders = mempty , downloaders = mempty
} }
, Package , Package
@ -117,10 +114,9 @@ autoUpdatable =
$ Package.StaticPlaceholder "https://www.php.net/distributions/php-" $ Package.StaticPlaceholder "https://www.php.net/distributions/php-"
:| Package.VersionPlaceholder :| Package.VersionPlaceholder
: [Package.StaticPlaceholder ".tar.xz"] : [Package.StaticPlaceholder ".tar.xz"]
in Package.Updater latest' template in Package.Updater latest' $ downloadWithTemplate template
, category = "development" , category = "development"
, name = "php82" , name = "php82"
, reupload = Nothing
, downloaders = mempty , downloaders = mempty
} }
, Package , Package
@ -140,10 +136,9 @@ autoUpdatable =
$ Package.StaticPlaceholder "https://github.com/kovidgoyal/kitty/releases/download/v" $ Package.StaticPlaceholder "https://github.com/kovidgoyal/kitty/releases/download/v"
:| Package.VersionPlaceholder :| Package.VersionPlaceholder
: templateTail : templateTail
in Package.Updater latest' template in Package.Updater latest' $ reuploadWithTemplate template [RawCommand "go" ["mod", "vendor"]]
, category = "system" , category = "system"
, name = "kitty" , name = "kitty"
, reupload = Just [RawCommand "go" ["mod", "vendor"]]
, downloaders = mempty , downloaders = mempty
} }
, Package , Package
@ -160,10 +155,9 @@ autoUpdatable =
: Package.StaticPlaceholder "/rdiff-backup-" : Package.StaticPlaceholder "/rdiff-backup-"
: Package.VersionPlaceholder : Package.VersionPlaceholder
: [Package.StaticPlaceholder ".tar.gz"] : [Package.StaticPlaceholder ".tar.gz"]
in Package.Updater latest' template in Package.Updater latest' $ reuploadWithTemplate template []
, category = "system" , category = "system"
, name = "rdiff-backup" , name = "rdiff-backup"
, reupload = Just mempty
, downloaders = mempty , downloaders = mempty
} }
, Package , Package
@ -180,10 +174,9 @@ autoUpdatable =
template = Package.DownloadTemplate $ pure template = Package.DownloadTemplate $ pure
$ Package.StaticPlaceholder $ 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 latest' template in Package.Updater latest' $ downloadWithTemplate template
, category = "network" , category = "network"
, name = "webex" , name = "webex"
, reupload = Nothing
, downloaders = mempty , downloaders = mempty
} }
, Package , Package
@ -200,10 +193,9 @@ autoUpdatable =
: Package.StaticPlaceholder "/librsync-" : Package.StaticPlaceholder "/librsync-"
: Package.VersionPlaceholder : Package.VersionPlaceholder
: [Package.StaticPlaceholder ".tar.gz"] : [Package.StaticPlaceholder ".tar.gz"]
in Package.Updater latest' template in Package.Updater latest' $ reuploadWithTemplate template []
, category = "libraries" , category = "libraries"
, name = "librsync" , name = "librsync"
, reupload = Just mempty
, downloaders = [] , downloaders = []
} }
, Package , Package
@ -219,10 +211,9 @@ autoUpdatable =
: Package.StaticPlaceholder "/dmd." : Package.StaticPlaceholder "/dmd."
: Package.VersionPlaceholder : Package.VersionPlaceholder
: [Package.StaticPlaceholder ".linux.tar.xz"] : [Package.StaticPlaceholder ".linux.tar.xz"]
in Package.Updater latest' template in Package.Updater latest' $ downloadWithTemplate template
, category = "development" , category = "development"
, name = "dmd" , name = "dmd"
, reupload = Nothing
, downloaders = mempty , downloaders = mempty
} }
, Package , Package
@ -235,17 +226,16 @@ autoUpdatable =
template = Package.DownloadTemplate template = Package.DownloadTemplate
$ Package.StaticPlaceholder "https://codeload.github.com/dlang/tools/tar.gz/v" $ Package.StaticPlaceholder "https://codeload.github.com/dlang/tools/tar.gz/v"
:| [Package.VersionPlaceholder] :| [Package.VersionPlaceholder]
in Package.Updater latest' template in Package.Updater latest' $ reuploadWithTemplate template []
, category = "development" , category = "development"
, name = "d-tools" , name = "d-tools"
, reupload = Just []
, downloaders = , downloaders =
let dubArguments = GhArguments{ owner = "dlang", name = "dub", transform = Nothing} let dubArguments = GhArguments{ owner = "dlang", name = "dub", transform = Nothing}
latestDub = latestGitHub dubArguments pure latestDub = latestGitHub dubArguments pure
dubTemplate = Package.DownloadTemplate dubTemplate = Package.DownloadTemplate
$ Package.StaticPlaceholder "https://codeload.github.com/dlang/dub/tar.gz/v" $ Package.StaticPlaceholder "https://codeload.github.com/dlang/dub/tar.gz/v"
:| [Package.VersionPlaceholder] :| [Package.VersionPlaceholder]
in [Package.Updater latestDub dubTemplate] in [Package.Updater latestDub $ downloadWithTemplate dubTemplate]
} }
] ]
@ -255,9 +245,9 @@ up2Date = for_ autoUpdatable go
go package = getAndLogLatest package go package = getAndLogLatest package
>>= mapM_ (updatePackageIfRequired package) >>= mapM_ (updatePackageIfRequired package)
>> liftIO (putStrLn "") >> liftIO (putStrLn "")
getAndLogLatest Package{ latest = Package.Updater getLatest _, name } getAndLogLatest Package{ latest = Package.Updater{ detectLatest }, name }
= liftIO (putStrLn $ Text.unpack name <> ": Retreiving the latest version.") = liftIO (putStrLn $ Text.unpack name <> ": Retreiving the latest version.")
>> getLatest >> detectLatest
updatePackageIfRequired :: Package -> Text -> SlackBuilderT () updatePackageIfRequired :: Package -> Text -> SlackBuilderT ()
updatePackageIfRequired package@Package{..} version = do updatePackageIfRequired package@Package{..} version = do
@ -285,42 +275,46 @@ updatePackageIfRequired package@Package{..} version = do
Left errorBundle -> liftIO $ putStr $ errorBundlePretty errorBundle Left errorBundle -> liftIO $ putStr $ errorBundlePretty errorBundle
updateDownload :: Package -> Package.Updater -> SlackBuilderT (URI, Digest MD5) updateDownload :: Package -> Package.Updater -> SlackBuilderT (URI, Digest MD5)
updateDownload package (Package.Updater updater downloadTemplate) = updater updateDownload package Package.Updater{ detectLatest } = detectLatest
>>= renderAndDownload package downloadTemplate . fromJust >>= renderAndDownload package . fromJust
renderAndDownload :: Package -> Package.DownloadTemplate -> Text -> SlackBuilderT (URI, Digest MD5)
renderAndDownload Package{..} downloadTemplate version = do
let packagePath = category <> "/" <> name
downloadWithTemplate :: Package.DownloadTemplate -> Text -> Text -> SlackBuilderT (URI, Digest MD5)
downloadWithTemplate downloadTemplate packagePath version = do
repository' <- SlackBuilderT $ asks repository repository' <- SlackBuilderT $ asks repository
uri' <- liftIO $ Package.renderDownloadWithVersion downloadTemplate version uri' <- liftIO $ Package.renderDownloadWithVersion downloadTemplate version
let downloadFileName = URI.unRText let downloadFileName = URI.unRText
$ NonEmpty.last $ snd $ fromJust $ URI.uriPath uri' $ NonEmpty.last $ snd $ fromJust $ URI.uriPath uri'
relativeTarball = packagePath <> "/" <> downloadFileName relativeTarball = packagePath <> "/" <> downloadFileName
tarball = repository' </> Text.unpack relativeTarball tarball = repository' </> Text.unpack relativeTarball
liftIO $ putStrLn
$ "Downloading " <> Text.unpack (URI.render uri') <> " to " <> tarball <> "."
checksum <- fromJust <$> download uri' tarball checksum <- fromJust <$> download uri' tarball
download' <- handleReupload uri' relativeTarball downloadFileName pure (uri', checksum)
reuploadWithTemplate :: Package.DownloadTemplate -> [CmdSpec] -> Text -> Text -> SlackBuilderT (URI, Digest MD5)
reuploadWithTemplate downloadTemplate commands packagePath version = do
(uri', checksum) <- downloadWithTemplate downloadTemplate packagePath version
let downloadFileName = URI.unRText
$ NonEmpty.last $ snd $ fromJust $ URI.uriPath uri'
relativeTarball = packagePath <> "/" <> downloadFileName
download' <- handleReupload relativeTarball downloadFileName
pure (download', checksum) pure (download', checksum)
where where
handleReupload uri' relativeTarball downloadFileName = do name' = Text.pack $ takeBaseName $ Text.unpack packagePath
handleReupload relativeTarball downloadFileName = do
repository' <- SlackBuilderT $ asks repository repository' <- SlackBuilderT $ asks repository
case reupload of case commands of
Just [] -> uploadTarball relativeTarball downloadFileName [] -> uploadTarball relativeTarball downloadFileName
Just commands -> _ ->
let tarballPath = repository' </> Text.unpack relativeTarball let tarballPath = repository' </> Text.unpack relativeTarball
packedDirectory = takeBaseName $ dropExtension tarballPath packedDirectory = takeBaseName $ dropExtension tarballPath
in liftIO (callProcess "tar" ["xvf", tarballPath]) in liftIO (callProcess "tar" ["xvf", tarballPath])
>> liftIO (traverse (defaultCreateProcess packedDirectory) commands) >> liftIO (traverse (defaultCreateProcess packedDirectory) commands)
>> liftIO (callProcess "tar" ["Jcvf", tarballPath, packedDirectory]) >> liftIO (callProcess "tar" ["Jcvf", tarballPath, packedDirectory])
>> uploadTarball relativeTarball downloadFileName >> uploadTarball relativeTarball downloadFileName
Nothing -> pure uri'
uploadTarball relativeTarball downloadFileName uploadTarball relativeTarball downloadFileName
= liftIO (putStrLn $ "Upload the source tarball " <> Text.unpack relativeTarball) = liftIO (putStrLn $ "Upload the source tarball " <> Text.unpack relativeTarball)
>> uploadCommand relativeTarball ("/" <> name) >> uploadCommand relativeTarball ("/" <> name')
>> liftIO (mkURI $ "https://download.dlackware.com/hosted-sources/" <> name <> "/" <> downloadFileName) >> liftIO (mkURI $ "https://download.dlackware.com/hosted-sources/" <> name' <> "/" <> downloadFileName)
defaultCreateProcess cwd' cmdSpec defaultCreateProcess cwd' cmdSpec
= flip withCreateProcess (const . const . const waitForProcess) = flip withCreateProcess (const . const . const waitForProcess)
$ CreateProcess $ CreateProcess
@ -341,13 +335,19 @@ renderAndDownload Package{..} downloadTemplate version = do
, child_group = Nothing , child_group = Nothing
} }
renderAndDownload :: Package -> Text -> SlackBuilderT (URI, Digest MD5)
renderAndDownload Package{..} version = do
let packagePath = category <> "/" <> name
Package.Updater _ getVersion = latest
getVersion packagePath version
updatePackage :: Package -> PackageInfo -> Text -> SlackBuilderT () updatePackage :: Package -> PackageInfo -> Text -> SlackBuilderT ()
updatePackage package@Package{..} info version = do updatePackage package@Package{..} info version = do
let packagePath = category <> "/" <> name let packagePath = category <> "/" <> name
Package.Updater _ downloadTemplate = latest
repository' <- SlackBuilderT $ asks repository repository' <- SlackBuilderT $ asks repository
mainDownload <- renderAndDownload package downloadTemplate version mainDownload <- renderAndDownload package version
moreDownloads <- traverse (updateDownload package) downloaders moreDownloads <- traverse (updateDownload package) downloaders
let (allDownloads, allChecksums) = unzip $ mainDownload : moreDownloads let (allDownloads, allChecksums) = unzip $ mainDownload : moreDownloads
let infoFilePath = repository' </> Text.unpack packagePath let infoFilePath = repository' </> Text.unpack packagePath

View File

@ -17,7 +17,6 @@ import qualified Text.URI as URI
import Crypto.Hash (Digest, MD5) import Crypto.Hash (Digest, MD5)
import SlackBuilder.Trans import SlackBuilder.Trans
import Control.Monad.Catch (MonadThrow) import Control.Monad.Catch (MonadThrow)
import System.Process (CmdSpec(..))
-- | Contains information how a package can be updated. -- | Contains information how a package can be updated.
data Package = Package data Package = Package
@ -25,7 +24,6 @@ data Package = Package
, downloaders :: [Updater] , downloaders :: [Updater]
, category :: Text , category :: Text
, name :: Text , name :: Text
, reupload :: Maybe [CmdSpec]
} }
-- | Download URI with the MD5 checksum of the target. -- | Download URI with the MD5 checksum of the target.
@ -79,4 +77,7 @@ renderDownloadWithVersion (DownloadTemplate components) version =
f VersionPlaceholder = (version <>) 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 (SlackBuilderT (Maybe Text)) DownloadTemplate data Updater = Updater
{ detectLatest :: SlackBuilderT (Maybe Text)
, getVersion :: Text -> Text -> SlackBuilderT (URI, Digest MD5)
}