Skip to content

Commit

Permalink
feat(install): check if git hooks exist
Browse files Browse the repository at this point in the history
Allows user to back up git hooks

Closes #4
  • Loading branch information
Shane Wilson committed Jan 15, 2015
1 parent f81a8bb commit 7267f11
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/CGTools/Install.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import Prelude hiding (readFile, writeFile, unlines)
import Data.Text (Text, unpack, pack, append, strip)
import Data.Text.IO (readFile)
import Data.Text.Lazy.IO (writeFile)
import System.Directory (getPermissions, setPermissions, executable)
import Control.Monad (when)
import System.Directory (getPermissions, setPermissions, executable, doesFileExist, renameFile)
import System.Process (readProcess)
import System.FilePath (splitFileName, (</>), (<.>))
import Text.Hastache (MuContext, hastacheStr, defaultConfig)
import Text.Hastache.Context (mkGenericContext)
import Data.Data (Data, Typeable)
import Paths_cgtools
import System.IO (hFlush, stdout)

data Ctx = Ctx {
path :: Text
Expand All @@ -33,6 +36,19 @@ getPrepareCommitPath g = unpack $ strip g `append` "/hooks/prepare-commit-msg"

genFile :: MuContext IO -> FilePath -> FilePath -> IO ()
genFile context input output = do
exists <- doesFileExist output
when exists $ do
let (d,f) = splitFileName output
putStr $ "backup " ++ f ++ "? (y/n [y]): "
hFlush stdout
bkup <- getChar
when (bkup /= 'n') $ do
putStr $ "name of backup ["++ f ++".bak]: "
hFlush stdout
backup <- getLine
let backupName = if backup == "" then d </> f <.> "bak" else d </> backup
renameFile output backupName

pkgfileName <- getDataFileName ("scaffold/" ++ input)
template <- readFile pkgfileName
transformedFile <- hastacheStr defaultConfig template context
Expand Down

0 comments on commit 7267f11

Please sign in to comment.