Better code organization

This commit is contained in:
Yann Esposito (Yogsototh) 2018-09-01 19:35:30 +02:00
parent ed5d5c6b85
commit b32dff64a1
Signed by untrusted user who does not match committer: yogsototh
GPG key ID: 7B19A4C650D59646
6 changed files with 115 additions and 33 deletions

View file

@ -23,7 +23,7 @@ gpm = do
subcmd <- options "Git Project Manager" parser subcmd <- options "Git Project Manager" parser
case subcmd of case subcmd of
Init -> Init.init Init -> Init.init
NewIssue -> inGPM Issue.newIssue NewIssue -> inGPM Issue.handleNewIssue
Review reviewCmd -> inGPM (Review.handleReview reviewCmd) Review reviewCmd -> inGPM (Review.handleReview reviewCmd)
data Command = Init data Command = Init

View file

@ -16,10 +16,10 @@ import Turtle
import Data.FileEmbed (embedStringFile) import Data.FileEmbed (embedStringFile)
import GPM.Helpers (debug) import GPM.Helpers (debug_)
init :: IO () init :: IO ()
init = do init = do
echo "* wiki.org" echo "* wiki.org"
output "wiki.org" $(embedStringFile "templates/wiki.org") output "wiki.org" $(embedStringFile "templates/wiki.org")
debug "git add wiki.org" debug_ "git add wiki.org"

View file

@ -1,6 +1,5 @@
{-# LANGUAGE NoImplicitPrelude #-} {-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-| {-|
module : GPM.Helpers module : GPM.Helpers
Description : GPM helper functions Description : GPM helper functions
@ -8,32 +7,44 @@ License : Public Domain
Maintainer : yann.esposito@gmail.com Maintainer : yann.esposito@gmail.com
-} -}
module GPM.Helpers module GPM.Helpers
(debug,inGPM) (debug,debug_,inGPM,getCurrentGitBranch,getGitUser)
where where
import Protolude hiding (stdout,fold,die) import Protolude hiding (die)
import Turtle import Turtle
import qualified Control.Foldl as Fold import qualified Control.Foldl as Fold
debug :: Text -> IO () -- | execute a shell script and return the last line as text
-- but also log the command to the console to minimize surprise
debug :: Text -> IO (Maybe Text)
debug cmd = do debug cmd = do
putText cmd putText cmd
stdout $ inshell cmd empty fmap lineToText <$> _foldIO (inshell cmd empty) (Fold.generalize Fold.last)
-- | execute a shell script without stdin and without handling output
debug_ :: Text -> IO ()
debug_ = void . debug
getCurrentGitBranch :: IO (Maybe Text)
getCurrentGitBranch = debug "git rev-parse --abbrev-ref HEAD"
getGitUser :: IO (Maybe Text)
getGitUser = debug "git config user.name"
-- | Ensure actions occurs in the @gpm@ branch -- | Ensure actions occurs in the @gpm@ branch
-- and returns to current branch with also all untracked files -- and returns to current branch with also all untracked files
inGPM :: MonadIO io => IO a -> io () inGPM :: IO a -> IO a
inGPM actions = sh $ do inGPM actions = bracket safeChangeBranch safeReturnBranch (const actions)
res <- fold (inshell "git rev-parse --abbrev-ref HEAD" empty) Fold.head where
oldbr <- case res of safeChangeBranch = do
Nothing -> die "Cannot retrieve current branch" res <- getCurrentGitBranch
Just br -> do case res of
void $ inshell "git stash --all" empty Nothing -> die "Cannot retrieve current branch"
void $ inshell "git checkout gpm" empty Just br -> do
return br debug_ "git stash --all"
liftIO $ bracket (return ()) debug_ "git checkout gpm"
(const $ sh $ do return br
void $ inshell ("git checkout " <> lineToText oldbr) empty safeReturnBranch oldbr = do
void $ inshell "git stash pop" empty) debug_ ("git checkout " <> oldbr)
(const actions) debug_ "git stash pop"

View file

@ -1,6 +1,5 @@
{-# LANGUAGE NoImplicitPrelude #-} {-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-| {-|
module : GPM.Init module : GPM.Init
@ -15,7 +14,7 @@ where
import Protolude import Protolude
import Turtle import Turtle
import GPM.Helpers (debug) import GPM.Helpers (debug_)
import qualified GPM.Docs as Docs import qualified GPM.Docs as Docs
import qualified GPM.Issue as Issue import qualified GPM.Issue as Issue
import qualified GPM.Review as Review import qualified GPM.Review as Review
@ -27,13 +26,13 @@ init = do
Issue.init Issue.init
Docs.init Docs.init
Review.init Review.init
debug "git commit -m 'gpm initialized'" debug_ "git commit -m 'gpm initialized'"
debug "git checkout master" debug_ "git checkout master"
mkNewEmptyBranch :: Text -> IO () mkNewEmptyBranch :: Text -> IO ()
mkNewEmptyBranch br = do mkNewEmptyBranch br = do
putText $ "create a new branch " <> br <> " (be sure the branch " <> br <> " doesn't already exists)" putText $ "create a new branch " <> br <> " (be sure the branch " <> br <> " doesn't already exists)"
debug $ "git checkout --orphan " <> br debug_ $ "git checkout --orphan " <> br
echo "cleanup the branch" echo "cleanup the branch"
debug "git rm --cached -r ." debug_ "git rm --cached -r ."

View file

@ -1,5 +1,6 @@
{-# LANGUAGE NoImplicitPrelude #-} {-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TemplateHaskell #-}
{-| {-|
@ -10,7 +11,7 @@ Maintainer : yann.esposito@gmail.com
-} -}
module GPM.Issue module GPM.Issue
( init ( init
, newIssue , handleNewIssue
) )
where where
@ -18,9 +19,80 @@ import Protolude hiding (die)
import Turtle import Turtle
import Data.FileEmbed (embedStringFile) import Data.FileEmbed (embedStringFile)
import GPM.Helpers
import Text.Mustache
newIssue :: IO () data NewIssue =
newIssue = die "TODO" NewIssue { priority :: Priority
, status :: Text
, title :: Text
, user :: User
, branch :: Maybe Text
, tags :: [Text]
, assignee :: Maybe User
, reviewers :: [User]
, description :: Text
}
data Priority = PriorityA | PriorityB | PriorityC
type User = Text
priorityToText :: Priority -> Text
priorityToText PriorityA = "[#A]"
priorityToText PriorityB = "[#B]"
priorityToText PriorityC = "[#C]"
instance ToMustache NewIssue where
toMustache NewIssue{..} = object
[ "priority" ~> priorityToText priority
, "status" ~> status
, "title" ~> title
, "user" ~> user
, "branch" ~> branch
, "tags" ~> tags
, "assignee" ~> assignee
, "reviewers" ~> reviewers
, "description" ~> description
]
createTmpNewIssue :: NewIssue -> IO ()
createTmpNewIssue ni = do
ecompiled <- automaticCompile ["./templates"] "new-issue.org"
case ecompiled of
Left pe -> do
print pe
die "Parse ERROR, check your template ./templates/new-issue.org"
Right compiled -> writeFile ".issues.org.tmp" (substitute compiled ni)
handleNewIssue :: IO ()
handleNewIssue = do
newIssue <- gatherNewIssueInfos
createTmpNewIssue newIssue
validTmpNewIssue
validTmpNewIssue :: IO ()
validTmpNewIssue = do
tmpIssue <- readFile ".issues.org.tmp"
appendFile "issues.org" ("\n\n" <> tmpIssue)
defaultNewIssue :: NewIssue
defaultNewIssue = NewIssue
{ priority = PriorityB
, status = "TODO"
, title = "Issue Title"
, user = "Unknown User"
, branch = Nothing
, tags = []
, assignee = Nothing
, reviewers = []
, description = "Write a comprehensive issue description..."
}
gatherNewIssueInfos :: IO NewIssue
gatherNewIssueInfos = do
user <- getGitUser
branch <- getCurrentGitBranch
return $ defaultNewIssue { user = fromMaybe "Unknown User" user
, branch = branch }
init :: IO () init :: IO ()
init = do init = do

View file

@ -20,7 +20,7 @@ import Protolude hiding (die, (%))
import Turtle import Turtle
import Data.FileEmbed (embedStringFile) import Data.FileEmbed (embedStringFile)
import GPM.Helpers (debug) import GPM.Helpers (debug_)
data ReviewCommand = ReviewStart (Maybe Text) data ReviewCommand = ReviewStart (Maybe Text)
| ReviewStop (Maybe Text) | ReviewStop (Maybe Text)
@ -37,7 +37,7 @@ init = do
mktree "reviews" mktree "reviews"
putText $ format ("* "%fp) fic putText $ format ("* "%fp) fic
output fic $(embedStringFile "templates/review.org") output fic $(embedStringFile "templates/review.org")
debug "git add reviews" debug_ "git add reviews"
parseReviewCmd :: Parser ReviewCommand parseReviewCmd :: Parser ReviewCommand