2022-03-16 18:00:18 +03:00
#!/usr/bin/env cabal
{- cabal:
build-depends:
base == 4.*
, turtle == 1.5.*
, regex-tdfa == 1.3.*
, text == 1.2.*
2020-10-31 13:08:33 +03:00
-}
{-# LANGUAGE OverloadedStrings #-}
2022-11-22 21:13:36 +03:00
import Control.Arrow (second)
import Control.Monad (unless, when)
2022-11-07 15:24:35 +03:00
import qualified Data.Text as T
import qualified Data.Text.IO as T.IO
import System.Environment (getArgs)
2022-11-22 21:13:36 +03:00
import System.Exit (ExitCode (..), die)
2022-11-07 15:24:35 +03:00
import qualified Text.Regex.TDFA as TR
import Turtle (empty, shell, shellStrict)
2020-10-31 13:08:33 +03:00
main = do
2022-11-07 15:24:35 +03:00
args <- getArgs
case args of
[version] -> makeNewRelease version
_ -> putStrLn "Usage: new-release <version>"
2020-10-31 13:08:33 +03:00
makeNewRelease :: String -> IO ()
makeNewRelease newVersion = do
2022-11-22 21:13:36 +03:00
(ExitSuccess, branchName) <- second T.strip <$> shellStrict "git rev-parse --abbrev-ref HEAD" empty
case branchName of
"release" -> return ()
_ -> do
putStrLn ""
putStrLn $ "Are you sure you want to publish new release directly from the '" ++ T.unpack branchName ++ "' branch?"
putStrLn "Such release will likely be out of sync with published documentation, examples and similar."
putStrLn "Don't do this if you want to publish latest release, instead do it from the 'release' branch."
putStrLn "If you instead want to publish a release candidate, or re-publish old release, or smth like that, then please continue by typing the current branch and release version as a confirmation that you know what you are doing."
putStrLn "Branch you are on right now: "
branchName' <- getLine
putStrLn "Repeat the release version: "
newVersion' <- getLine
when (newVersion' /= newVersion) $ error "You typed incorrect release version."
when (branchName' /= T.unpack branchName) $ error "You typed incorrect branch name."
2022-02-25 15:40:28 +03:00
2022-11-07 15:24:35 +03:00
let cabalFileName = "waspc.cabal"
2022-11-22 21:13:36 +03:00
versionFromCabalFile <- getVersionFromCabalFile . T.unpack <$> T.IO.readFile cabalFileName
when (versionFromCabalFile /= newVersion) $ do
putStrLn "\n"
putStrLn $ "Version from " ++ cabalFileName ++ " file (" ++ versionFromCabalFile ++ ") does not match the release version (" ++ newVersion ++ ")."
putStrLn "If you are doing a regular release, you will want these to match, so abort this script and go update the version in cabal file first."
putStrLn "Or maybe you know what you are doing and want to continue? [Y/N]:"
confirmation <- getLine
when (confirmation /= "Y") $ die "Aborting"
2020-10-31 13:08:33 +03:00
2022-11-22 21:13:36 +03:00
unlessM isWorkingTreeClean $ do
putStrLn "\n"
putStrLn "Your working tree is not clean! Run 'git status' to check it out."
putStrLn "Only commited changes will become part of the release, so make sure you commited everything you wanted."
putStrLn "Do you want to continue regardless? [Y/N]:"
confirmation <- getLine
when (confirmation /= "Y") $ die "Aborting"
2020-10-31 13:08:33 +03:00
2022-11-22 21:13:36 +03:00
let tag = "v" ++ newVersion
2020-10-31 13:08:33 +03:00
2022-11-07 15:24:35 +03:00
putStrLn $
unlines
2022-11-22 21:31:43 +03:00
[ "",
"This will release wasp version " ++ versionFromCabalFile ++ ".",
2022-11-22 21:13:36 +03:00
"tag " ++ tag ++ " will be created and pushed, triggering CI to create new release on Github.",
"Are you sure you want to proceed? [Y/N]:"
2022-11-07 15:24:35 +03:00
]
confirmation <- getLine
2022-11-22 21:13:36 +03:00
when (confirmation /= "Y") $ die "Aborting"
2020-10-31 13:08:33 +03:00
2022-11-07 15:24:35 +03:00
putStrLn $ "\nCreating tag " ++ tag ++ " and pushing it.\n"
ExitSuccess <- shell (T.pack $ "git tag " ++ tag) empty
ExitSuccess <- shell (T.pack $ "git push origin " ++ tag) empty
2020-10-31 13:08:33 +03:00
2022-11-22 21:13:36 +03:00
putStrLn $ "Success! Check out Actions on Github to see how your new release is being built. Once it is built, draft release on Github will be created, with binaries attached. Then it is up to you to manually publish it from draft to proper release."
2022-11-07 15:24:35 +03:00
return ()
2020-10-31 13:08:33 +03:00
2022-11-22 21:13:36 +03:00
getVersionFromCabalFile :: String -> String -- Returns version currently written in the cabal file.
getVersionFromCabalFile cabalFileContents = do
2022-11-07 15:24:35 +03:00
let (beforeMatch, match, afterMatch, submatches) =
2022-11-22 21:13:36 +03:00
cabalFileContents TR.=~ ("^(version: *)(.+)$" :: String) :: (String, String, String, [String])
match' = trim match
in if (null match')
then error "Couldn't locate version in cabal file!?"
else submatches !! 1
isWorkingTreeClean :: IO Bool
isWorkingTreeClean = do
(exitCode, _) <- shellStrict "git status | grep 'nothing to commit, working tree clean'" empty
return $ exitCode == ExitSuccess
unlessM ma perform = ma >>= (`unless` perform)
trim :: String -> String
trim = T.unpack . T.strip . T.pack