Skip to content

Commit d183505

Browse files
committed
release.hs: fix GitHub uploads, and support setting asset label
1 parent f108f53 commit d183505

File tree

1 file changed

+36
-18
lines changed

1 file changed

+36
-18
lines changed

etc/scripts/release.hs

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ main =
6060
Platform arch _ = buildPlatform
6161
gArch = arch
6262
gBinarySuffix = ""
63+
gUploadLabel = Nothing
6364
gLocalInstallRoot = "" -- Set to real value below.
6465
gProjectRoot = "" -- Set to real value velow.
6566
global0 = foldl (flip id) Global{..} flags
@@ -100,7 +101,10 @@ options =
100101
"Architecture to build (e.g. 'i386' or 'x86_64')."
101102
, Option "" [binaryVariantOptName]
102103
(ReqArg (\v -> Right $ \g -> g{gBinarySuffix = v}) "SUFFIX")
103-
"Extra suffix to add to binary executable archive filename." ]
104+
"Extra suffix to add to binary executable archive filename."
105+
, Option "" [uploadLabelOptName]
106+
(ReqArg (\v -> Right $ \g -> g{gUploadLabel = Just v}) "LABEL")
107+
"Label to give the uploaded release asset" ]
104108

105109
-- | Shake rules.
106110
rules :: Global -> [String] -> Rules ()
@@ -133,9 +137,13 @@ rules global@Global{..} args = do
133137
phony archBuildPhony $ need [archDir </> archPackageFileName]
134138

135139
releaseDir </> "*" <.> uploadExt %> \out -> do
136-
need [dropExtension out]
137-
uploadToGithubRelease global (dropExtension out)
138-
copyFileChanged (dropExtension out) out
140+
let srcFile = dropExtension out
141+
mUploadLabel =
142+
if takeExtension srcFile == ascExt
143+
then fmap (++ " (GPG signature)") gUploadLabel
144+
else gUploadLabel
145+
uploadToGithubRelease global srcFile mUploadLabel
146+
copyFileChanged srcFile out
139147

140148
releaseCheckDir </> binaryExeFileName %> \out -> do
141149
need [installBinDir </> stackExeFileName]
@@ -396,15 +404,6 @@ rules global@Global{..} args = do
396404
binaryExeFileName = binaryName global <.> exe
397405
stackExeFileName = stackProgName <.> exe
398406

399-
zipExt = "zip"
400-
tarGzExt = tarExt <.> gzExt
401-
gzExt = "gz"
402-
tarExt = "tar"
403-
ascExt = "asc"
404-
uploadExt = "upload"
405-
debExt = "deb"
406-
rpmExt = "rpm"
407-
408407
debStagedDocDir dv = debStagingDir dv </> "usr/share/doc" </> stackProgName
409408
debStagedBashCompletionFile dv = debStagingDir dv </> "etc/bash_completion.d/stack"
410409
debStagedExeFile dv = debStagingDir dv </> "usr/bin/stack"
@@ -453,25 +452,39 @@ rules global@Global{..} args = do
453452

454453
anyDistroVersion distro = DistroVersion distro "*" "*"
455454

455+
zipExt = ".zip"
456+
tarGzExt = tarExt <.> gzExt
457+
gzExt = ".gz"
458+
tarExt = ".tar"
459+
ascExt = ".asc"
460+
uploadExt = ".upload"
461+
debExt = ".deb"
462+
rpmExt = ".rpm"
463+
456464

457465
-- | Upload file to Github release.
458-
uploadToGithubRelease :: Global -> FilePath -> Action ()
459-
uploadToGithubRelease global@Global{..} file = do
466+
uploadToGithubRelease :: Global -> FilePath -> Maybe String -> Action ()
467+
uploadToGithubRelease global@Global{..} file mUploadLabel = do
460468
putNormal $ "Uploading to Github: " ++ file
469+
need [file]
461470
GithubRelease{..} <- getGithubRelease
462471
resp <- liftIO $ callGithubApi global
463472
[(CI.mk $ S8.pack "Content-Type", defaultMimeLookup (T.pack file))]
464473
(Just file)
465474
(replace
466-
"{?name}"
467-
("?name=" ++ S8.unpack (urlEncode True (S8.pack (takeFileName file))))
475+
"{?name,label}"
476+
("?name=" ++ urlEncodeStr (takeFileName file) ++
477+
(case mUploadLabel of
478+
Nothing -> ""
479+
Just uploadLabel -> "&label=" ++ urlEncodeStr uploadLabel))
468480
relUploadUrl)
469481
case eitherDecode resp of
470482
Left e -> error ("Could not parse Github asset upload response (" ++ e ++ "):\n" ++ L8.unpack resp ++ "\n")
471483
Right (GithubReleaseAsset{..}) ->
472484
when (assetState /= "uploaded") $
473485
error ("Invalid asset state after Github asset upload: " ++ assetState)
474486
where
487+
urlEncodeStr = S8.unpack . urlEncode True . S8.pack
475488
getGithubRelease = do
476489
releases <- getGithubReleases
477490
let tag = fromMaybe ("v" ++ stackVersionStr global) gGithubReleaseTag
@@ -600,6 +613,10 @@ archOptName = "arch"
600613
binaryVariantOptName :: String
601614
binaryVariantOptName = "binary-variant"
602615

616+
-- | @--upload-label@ command-line option name.
617+
uploadLabelOptName :: String
618+
uploadLabelOptName = "upload-label"
619+
603620
-- | Arguments to pass to all 'stack' invocations.
604621
stackArgs :: Global -> [String]
605622
stackArgs Global{..} = ["--arch=" ++ display gArch]
@@ -647,5 +664,6 @@ data Global = Global
647664
, gProjectRoot :: !FilePath
648665
, gHomeDir :: !FilePath
649666
, gArch :: !Arch
650-
, gBinarySuffix :: !String }
667+
, gBinarySuffix :: !String
668+
, gUploadLabel ::(Maybe String)}
651669
deriving (Show)

0 commit comments

Comments
 (0)