Provide a name for additional downloads

This commit is contained in:
Eugen Wissner 2023-11-01 17:05:16 +01:00
parent 396a536b3a
commit 64233c4c63
3 changed files with 27 additions and 23 deletions

View File

@ -8,6 +8,7 @@ import Data.List.NonEmpty (NonEmpty(..))
import qualified Data.List.NonEmpty as NonEmpty
import Control.Monad.IO.Class (MonadIO(..))
import Data.Maybe (fromJust)
import qualified Data.Map as Map
import Options.Applicative (execParser)
import SlackBuilder.CommandLine
import SlackBuilder.Config
@ -24,8 +25,7 @@ import SlackBuilder.Package (Package(..))
import qualified SlackBuilder.Package as Package
import Text.URI (URI(..), mkURI)
import Text.URI.QQ (uri)
import Crypto.Hash (Digest, MD5)
import Data.Foldable (for_, find)
import Data.Foldable (Foldable(..), for_, find)
import qualified Text.URI as URI
import System.FilePath ((</>), (<.>), dropExtension, takeBaseName, makeRelative, splitFileName)
import SlackBuilder.Info
@ -197,7 +197,7 @@ autoUpdatable =
in Package.Updater latest' $ reuploadWithTemplate template []
, category = "libraries"
, name = "librsync"
, downloaders = []
, downloaders = mempty
}
, Package
{ latest =
@ -242,10 +242,10 @@ autoUpdatable =
:| [Package.VersionPlaceholder]
dscannerURI = [uri|https://github.com/dlang-community/D-Scanner.git|]
dcdURI = [uri|https://github.com/dlang-community/DCD.git|]
in
[ Package.Updater latestDub $ downloadWithTemplate dubTemplate
, Package.Updater latestDscanner $ cloneFromGit dscannerURI "v"
, Package.Updater latestDcd $ cloneFromGit dcdURI "v"
in Map.fromList
[ ("DUB", Package.Updater latestDub $ downloadWithTemplate dubTemplate)
, ("DSCANNER", Package.Updater latestDscanner $ cloneFromGit dscannerURI "v")
, ("DCD", Package.Updater latestDcd $ cloneFromGit dcdURI "v")
]
}
]
@ -285,11 +285,13 @@ updatePackageIfRequired package@Package{..} version = do
updatePackage package parsedInfoFile version
Left errorBundle -> liftIO $ putStr $ errorBundlePretty errorBundle
updateDownload :: Package -> Package.Updater -> SlackBuilderT (URI, Digest MD5)
updateDownload Package{..} Package.Updater{..} = detectLatest
>>= getVersion (Text.pack $ Text.unpack category </> Text.unpack name) . fromJust
updateDownload :: Package -> Package.Updater -> SlackBuilderT (Package.Download, Text)
updateDownload Package{..} Package.Updater{..} = do
latestDownloadVersion <- fromJust <$> detectLatest
result <- getVersion (Text.pack $ Text.unpack category </> Text.unpack name) latestDownloadVersion
pure (result, latestDownloadVersion)
cloneFromGit :: URI -> Text -> Text -> Text -> SlackBuilderT (URI, Digest MD5)
cloneFromGit :: URI -> Text -> Text -> Text -> SlackBuilderT Package.Download
cloneFromGit repo tagPrefix packagePath version = do
repository' <- SlackBuilderT $ asks repository
let downloadFileName = URI.unRText
@ -300,10 +302,10 @@ cloneFromGit repo tagPrefix packagePath version = do
name' = Text.pack (takeBaseName $ Text.unpack packagePath)
checksum <- clone (URI.render repo) (Text.pack tarball) tagPrefix
uploadCommand (Text.pack relativeTarball) ("/" <> name')
(, fromJust checksum)
(flip . flip Package.Download) (fromJust checksum) False
<$> liftIO (mkURI $ "https://download.dlackware.com/hosted-sources/" <> name' <> "/" <> downloadFileName)
downloadWithTemplate :: Package.DownloadTemplate -> Text -> Text -> SlackBuilderT (URI, Digest MD5)
downloadWithTemplate :: Package.DownloadTemplate -> Text -> Text -> SlackBuilderT Package.Download
downloadWithTemplate downloadTemplate packagePath version = do
repository' <- SlackBuilderT $ asks repository
uri' <- liftIO $ Package.renderDownloadWithVersion downloadTemplate version
@ -312,17 +314,17 @@ downloadWithTemplate downloadTemplate packagePath version = do
relativeTarball = packagePath <> "/" <> downloadFileName
tarball = repository' </> Text.unpack relativeTarball
checksum <- fromJust <$> download uri' tarball
pure (uri', checksum)
pure $ Package.Download uri' checksum False
reuploadWithTemplate :: Package.DownloadTemplate -> [CmdSpec] -> Text -> Text -> SlackBuilderT (URI, Digest MD5)
reuploadWithTemplate :: Package.DownloadTemplate -> [CmdSpec] -> Text -> Text -> SlackBuilderT Package.Download
reuploadWithTemplate downloadTemplate commands packagePath version = do
(uri', checksum) <- downloadWithTemplate downloadTemplate packagePath version
Package.Download{ download = uri', md5sum = 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 $ Package.Download download' checksum False
where
name' = Text.pack $ takeBaseName $ Text.unpack packagePath
handleReupload relativeTarball downloadFileName = do
@ -360,7 +362,7 @@ reuploadWithTemplate downloadTemplate commands packagePath version = do
, child_group = Nothing
}
renderAndDownload :: Package -> Text -> SlackBuilderT (URI, Digest MD5)
renderAndDownload :: Package -> Text -> SlackBuilderT Package.Download
renderAndDownload Package{..} version = do
let packagePath = category <> "/" <> name
Package.Updater _ getVersion = latest
@ -374,13 +376,13 @@ updatePackage package@Package{..} info version = do
repository' <- SlackBuilderT $ asks repository
mainDownload <- renderAndDownload package version
moreDownloads <- traverse (updateDownload package) downloaders
let (allDownloads, allChecksums) = unzip $ mainDownload : moreDownloads
let allDownloads = mainDownload : toList (fst <$> moreDownloads)
let infoFilePath = repository' </> Text.unpack packagePath
</> (Text.unpack name <.> "info")
package' = info
{ version = version
, downloads = allDownloads
, checksums = allChecksums
, downloads = getField @"download" <$> allDownloads
, checksums = getField @"md5sum" <$> allDownloads
}
liftIO $ Text.IO.writeFile infoFilePath $ generate package'
updateSlackBuildVersion packagePath version

View File

@ -17,11 +17,12 @@ import qualified Text.URI as URI
import Crypto.Hash (Digest, MD5)
import SlackBuilder.Trans
import Control.Monad.Catch (MonadThrow)
import Data.Map (Map)
-- | Contains information how a package can be updated.
data Package = Package
{ latest :: Updater
, downloaders :: [Updater]
, downloaders :: Map Text Updater
, category :: Text
, name :: Text
}
@ -79,5 +80,5 @@ renderDownloadWithVersion (DownloadTemplate components) version =
-- | Function used to get the latest version of a source.
data Updater = Updater
{ detectLatest :: SlackBuilderT (Maybe Text)
, getVersion :: Text -> Text -> SlackBuilderT (URI, Digest MD5)
, getVersion :: Text -> Text -> SlackBuilderT Download
}

View File

@ -19,6 +19,7 @@ common dependencies
build-depends:
base ^>= 4.16.4.0,
bytestring ^>= 0.11.0,
containers ^>= 0.6,
cryptonite >= 0.30,
directory ^>= 1.3.8,
filepath ^>= 1.4.2,