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 #-}
|
|
|
|
|
|
|
|
import Control.Monad (when)
|
|
|
|
import qualified Data.Text as T
|
|
|
|
import qualified Data.Text.IO as T.IO
|
|
|
|
import System.Environment (getArgs)
|
2020-11-05 18:27:16 +03:00
|
|
|
import System.Exit (ExitCode (..))
|
2020-10-31 13:08:33 +03:00
|
|
|
import qualified Text.Regex.TDFA as TR
|
2022-02-25 15:40:28 +03:00
|
|
|
import Turtle (empty, shell, shellStrict)
|
2020-10-31 13:08:33 +03:00
|
|
|
|
|
|
|
main = do
|
|
|
|
args <- getArgs
|
|
|
|
case args of
|
|
|
|
[version] -> makeNewRelease version
|
2020-11-05 18:02:45 +03:00
|
|
|
_ -> putStrLn "Usage: new-release <version>"
|
2020-10-31 13:08:33 +03:00
|
|
|
|
|
|
|
makeNewRelease :: String -> IO ()
|
|
|
|
makeNewRelease newVersion = do
|
2022-02-25 15:40:28 +03:00
|
|
|
(ExitSuccess, branchName) <- shellStrict "git rev-parse --abbrev-ref HEAD" empty
|
|
|
|
when (T.strip branchName /= "main") $ error "You must run this script from the main branch!"
|
|
|
|
|
2022-03-16 18:00:18 +03:00
|
|
|
let cabalFileName = "waspc.cabal"
|
|
|
|
oldCabalFileContents <- T.unpack <$> T.IO.readFile cabalFileName
|
2020-10-31 13:08:33 +03:00
|
|
|
|
2022-03-16 18:00:18 +03:00
|
|
|
(newCabalFileContents, oldVersion) <- updateCabalFile oldCabalFileContents newVersion
|
2020-10-31 13:08:33 +03:00
|
|
|
let tag = "v" ++ newVersion
|
|
|
|
|
|
|
|
when (newVersion == oldVersion) $ error "Version you provided is the current version!"
|
|
|
|
|
|
|
|
putStrLn $ unlines
|
|
|
|
[ "This will update wasp version from " ++ oldVersion ++ " to " ++ newVersion
|
2022-03-16 18:00:18 +03:00
|
|
|
, cabalFileName ++ " will be updated, and this change will be commited and pushed."
|
2020-10-31 13:08:33 +03:00
|
|
|
, "Also, tag " ++ tag ++ " will be created and pushed, triggering CI to"
|
|
|
|
++ " create new release on Github."
|
|
|
|
, "Are you sure you want to proceed (y/n)?"
|
|
|
|
]
|
|
|
|
confirmation <- getLine
|
|
|
|
when (confirmation /= "y") $ error "Aborting"
|
|
|
|
|
2022-03-16 18:00:18 +03:00
|
|
|
T.IO.writeFile cabalFileName $ T.pack newCabalFileContents
|
2020-10-31 13:08:33 +03:00
|
|
|
|
|
|
|
-- TODO: Print what I am doing for each step.
|
|
|
|
|
2022-03-16 18:00:18 +03:00
|
|
|
putStrLn $ "\nCreating new commit with updated " ++ cabalFileName ++ " and pushing it.\n"
|
|
|
|
ExitSuccess <- shell (T.pack $ "git add " ++ cabalFileName) empty
|
2020-11-05 18:27:16 +03:00
|
|
|
ExitSuccess <- shell (T.pack $ "git commit -m \"Updated wasp version to " ++ newVersion ++ ".\"") empty
|
|
|
|
gitPushExitCode <- shell "git push" empty
|
|
|
|
case gitPushExitCode of
|
|
|
|
ExitSuccess -> return ()
|
|
|
|
ExitFailure _ -> do
|
|
|
|
ExitSuccess <- shell "git reset --hard HEAD~1" empty
|
|
|
|
error "Failed to do `git push`, reverted changes."
|
2020-10-31 13:08:33 +03:00
|
|
|
|
|
|
|
putStrLn $ "\nCreating tag " ++ tag ++ " and pushing it.\n"
|
2020-11-05 18:27:16 +03:00
|
|
|
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
|
|
|
|
|
|
|
return ()
|
|
|
|
|
2022-03-16 18:00:18 +03:00
|
|
|
updateCabalFile :: String -> String -> IO (String, String) -- Returns (updated cabal file contents, old version)
|
|
|
|
updateCabalFile cabalFileContents newVersion = do
|
2020-10-31 13:08:33 +03:00
|
|
|
let (beforeMatch, match, afterMatch, submatches) =
|
2022-03-16 18:00:18 +03:00
|
|
|
cabalFileContents TR.=~ ("^(version: *)([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+)" :: String) :: (String, String, String, [String])
|
|
|
|
when (null match) $ error "Couldn't locate version in cabal file!?"
|
|
|
|
let [matchBeforeVersion, oldVersion] = submatches
|
|
|
|
let newContents = beforeMatch ++ matchBeforeVersion ++ newVersion ++ afterMatch
|
2020-10-31 13:08:33 +03:00
|
|
|
return (newContents, oldVersion)
|