themplate 0.1 → 1.1
raw patch · 4 files changed
+28/−14 lines, 4 files
Files
- LICENSE +1/−1
- README.md +0/−1
- src/Main.hs +25/−10
- themplate.cabal +2/−2
LICENSE view
@@ -1,4 +1,4 @@-Copyright 2013 Benno Fünfstück+Copyright 2013-2014 Benno Fünfstück All rights reserved.
README.md view
@@ -40,7 +40,6 @@ ``` This will initialize the template with the given name in a new directory with the name of the project.- To list all available templates, run: ```
src/Main.hs view
@@ -3,7 +3,7 @@ module Main where import Control.Error-import Control.Exception+import qualified Control.Exception as E import Control.Monad import Control.Monad.Trans.Class import Data.Configurator@@ -64,10 +64,10 @@ contents <- lift $ filter (not . flip elem [".",".."]) <$> getDirectoryContents temp dirs <- lift $ filterM (doesDirectoryExist . (temp </>)) contents files <- lift $ filterM (doesFileExist . (temp </>)) contents- + forM_ files $ \file -> do let lookupVar "project.name" = return $ Just $ T.pack proj- lookupVar x = lookup c x + lookupVar x = lookup c x let substitute = fmapLT (mappend $ T.pack file <> ":Error: Undeclared variable ") . substituteBetween "{{" "}}" (evalPattern lookupVar) content <- lift $ T.readFile (temp </> file)@@ -76,12 +76,27 @@ e <- lift $ doesFileExist (target </> file') when (e && not amend) $ left $ ":Error: Target file " <> T.pack (target </> file') <> " does already exist."- unless e $ lift $ T.writeFile (target </> file') content'- - forM_ dirs $ \dir -> do - lift $ createDirectoryIfMissing True $ target </> dir + unless e $ lift $ do+ T.writeFile (target </> file') content'+ perm <- getPermissions (temp </> file)+ perm' <- getPermissions (target </> file')+ setPermissions (target </> file') $ perm' { executable = executable perm }++ forM_ dirs $ \dir -> do+ lift $ createDirectoryIfMissing True $ target </> dir instantiateTemplate proj amend c (temp </> dir) (target </> dir) +-- | Copy the contents of a directory to another directory+copyDirContents :: String -> String -> IO ()+copyDirContents source target = do+ contents <- filter (not . flip elem ["..","."]) <$> getDirectoryContents source+ dirs <- filterM (doesDirectoryExist . (source </>)) contents+ files <- filterM (doesFileExist . (source </>)) contents+ forM_ files $ \file -> copyFile (source </> file) (target </> file)+ forM_ dirs $ \dir -> do+ createDirectoryIfMissing False (target </> dir)+ copyDirContents (source </> dir) (target </> dir)+ -- | Top level subcommand handler. Switches on the subcommand and performs the given action. -- Takes 3 arguments: The configuration, the path to the appUserDataDirectory and the command. handleCmd :: Config -> String -> Cmd -> IO ()@@ -94,12 +109,12 @@ targetExists <- doesDirectoryExist proj unless targetExists $ createDirectory proj projPath <- canonicalizePath proj- res <- runEitherT (instantiateTemplate proj amend c (appData </> temp) projPath) `catch` \(exc :: SomeException) ->- unless targetExists (removeDirectoryRecursive projPath) >> throw exc+ res <- runEitherT (instantiateTemplate proj amend c (appData </> temp) projPath) `E.catch` \(exc :: E.SomeException) ->+ unless targetExists (removeDirectoryRecursive projPath) >> E.throwIO exc case res of Left m -> do T.putStrLn m- removeDirectoryRecursive projPath+ unless targetExists $ removeDirectoryRecursive projPath exitFailure Right () -> exitSuccess
themplate.cabal view
@@ -1,5 +1,5 @@ name: themplate-version: 0.1+version: 1.1 license: BSD3 cabal-version: >= 1.10 license-file: LICENSE@@ -8,7 +8,7 @@ stability: experimental homepage: http://github.com/bennofs/themplate/ bug-reports: http://github.com/bennofs/themplate/issues-copyright: Copyright (C) 2013 Benno Fünfstück+copyright: Copyright (C) 2013-2014 Benno Fünfstück synopsis: themplate description: themplate build-type: Simple