#!/usr/bin/env stack {- stack script --resolver lts-16.14 --package turtle --package regex-tdfa --package text -} {-# LANGUAGE OverloadedStrings #-} import Control.Monad (when) import qualified Data.Text as T import qualified Data.Text.IO as T.IO import System.Environment (getArgs) import System.Exit (ExitCode (..)) import qualified Text.Regex.TDFA as TR import Turtle (empty, shell, shellStrict) main = do args <- getArgs case args of [version] -> makeNewRelease version _ -> putStrLn "Usage: new-release " makeNewRelease :: String -> IO () makeNewRelease newVersion = do (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!" oldPackageYamlContents <- T.unpack <$> T.IO.readFile "package.yaml" (newPackageYamlContents, oldVersion) <- updatePackageYaml oldPackageYamlContents newVersion 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 , "package.yaml will be updated, and this change will be commited and pushed." , "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" T.IO.writeFile "package.yaml" $ T.pack newPackageYamlContents -- TODO: Print what I am doing for each step. putStrLn "\nCreating new commit with updated package.yaml and pushing it.\n" ExitSuccess <- shell "git add package.yaml" empty 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." 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 return () updatePackageYaml :: String -> String -> IO (String, String) -- Returns (update package yaml contents, old version) updatePackageYaml packageYamlContents newVersion = do let (beforeMatch, match, afterMatch, submatches) = packageYamlContents TR.=~ ("(version: *)([^ #]+)( *# *%WASP_VERSION%)" :: String) :: (String, String, String, [String]) when (null match) $ error ("Couldn't locate version in package.yaml, make sure it is annotated" ++ " with line comment starting with %WASP_VERSION% (in the same line).") let [matchBeforeVersion, oldVersion, matchAfterVersion] = submatches let newContents = beforeMatch ++ matchBeforeVersion ++ newVersion ++ matchAfterVersion ++ afterMatch return (newContents, oldVersion)