summaryrefslogtreecommitdiff
path: root/app/SlackBuilder/Download.hs
diff options
context:
space:
mode:
Diffstat (limited to 'app/SlackBuilder/Download.hs')
-rw-r--r--app/SlackBuilder/Download.hs29
1 files changed, 24 insertions, 5 deletions
diff --git a/app/SlackBuilder/Download.hs b/app/SlackBuilder/Download.hs
index 842f4ae..c6516d4 100644
--- a/app/SlackBuilder/Download.hs
+++ b/app/SlackBuilder/Download.hs
@@ -1,5 +1,6 @@
module SlackBuilder.Download
- ( cloneAndArchive
+ ( clone
+ , cloneAndArchive
, commit
, download
, hostedSources
@@ -17,7 +18,7 @@ import SlackBuilder.Trans
import Control.Monad.Trans.Reader (asks)
import Control.Monad.IO.Class (MonadIO(liftIO))
import System.IO (IOMode(..), withFile)
-import System.FilePath ((</>), (<.>))
+import System.FilePath ((</>), (<.>), takeBaseName, splitPath, joinPath)
import System.Process
( CreateProcess(..)
, StdStream(..)
@@ -51,6 +52,7 @@ import Conduit
, (.|)
, ZipSink(..)
, await
+ , sourceFile
)
import Crypto.Hash (Digest, MD5, hashInit, hashFinalize, hashUpdate)
import Data.Void (Void)
@@ -159,12 +161,29 @@ sinkHash = sink hashInit
sink ctx = await
>>= maybe (pure $ hashFinalize ctx) (sink . hashUpdate ctx)
-download :: Text -> FilePath -> SlackBuilderT (Maybe (Digest MD5))
-download uri target = SlackBuilderT (liftIO $ mkURI uri)
- >>= traverse (runReq defaultHttpConfig . go . fst) . useHttpsURI
+download :: URI -> FilePath -> SlackBuilderT (Maybe (Digest MD5))
+download uri target = traverse (runReq defaultHttpConfig . go . fst)
+ $ useHttpsURI uri
where
go uri' = reqBr GET uri' NoReqBody mempty readResponse
readResponse :: Response BodyReader -> IO (Digest MD5)
readResponse response = runConduitRes
$ responseBodySource response
.| getZipSink (ZipSink (sinkFile target) *> ZipSink sinkHash)
+
+clone :: Text -> Text -> Text -> SlackBuilderT (Maybe (Digest MD5))
+clone repo tarball tagPrefix = do
+ let tarballPath = Text.unpack tarball
+ nameVersion = Text.pack $ takeBaseName tarballPath
+ remotePath = Text.pack $ joinPath $ ("/" :) $ drop 1 $ splitPath tarballPath
+ localPath = "slackbuilds" </> tarballPath
+ remoteFileExists' <- remoteFileExists remotePath
+
+ if remoteFileExists'
+ then
+ hostedSources remotePath >>= flip download localPath
+ else
+ let go = sourceFile localPath .| sinkHash
+ in cloneAndArchive repo nameVersion tarballPath tagPrefix
+ >> uploadCommand tarball remotePath
+ >> liftIO (runConduitRes go) <&> Just