@@ -77,8 +77,8 @@ data NewOpts = NewOpts
7777-- | Create a new project with the given options.
7878new
7979 :: (HasConfig r , MonadReader r m , MonadLogger m , MonadCatch m , MonadThrow m , MonadIO m , HasHttpManager r , Functor m , Applicative m )
80- => NewOpts -> m (Path Abs Dir )
81- new opts = do
80+ => NewOpts -> Bool -> m (Path Abs Dir )
81+ new opts forceOverwrite = do
8282 pwd <- getCurrentDir
8383 absDir <- if bare then return pwd
8484 else do relDir <- parseRelDir (packageNameString project)
@@ -99,6 +99,7 @@ new opts = do
9999 (newOptsNonceParams opts)
100100 absDir
101101 templateText
102+ when (not forceOverwrite && bare) $ checkForOverwrite (M. keys files)
102103 writeTemplateFiles files
103104 runTemplateInits absDir
104105 return absDir
@@ -194,7 +195,7 @@ applyTemplate project template nonceParams dir templateText = do
194195 templateText
195196 (mkStrContextM (contextFunction context)))
196197 unless (S. null missingKeys)
197- ($ logInfo (T. pack (show (MissingParameters project template missingKeys (configUserConfigPath config)))))
198+ ($ logInfo (" \n " <> T. pack (show (MissingParameters project template missingKeys (configUserConfigPath config))) <> " \n " ))
198199 files :: Map FilePath LB. ByteString <-
199200 catch (execWriterT $
200201 yield (T. encodeUtf8 (LT. toStrict applied)) $$
@@ -230,6 +231,12 @@ applyTemplate project template nonceParams dir templateText = do
230231 return MuNothing
231232 Just value -> return (MuVariable value)
232233
234+ -- | Check if we're going to overwrite any existing files.
235+ checkForOverwrite :: (MonadIO m , MonadThrow m ) => [Path Abs File ] -> m ()
236+ checkForOverwrite files = do
237+ overwrites <- filterM doesFileExist files
238+ unless (null overwrites) $ throwM (AttemptedOverwrites overwrites)
239+
233240-- | Write files to the new project directory.
234241writeTemplateFiles
235242 :: MonadIO m
@@ -335,6 +342,7 @@ data NewException
335342 | AlreadyExists ! (Path Abs Dir )
336343 | MissingParameters ! PackageName ! TemplateName ! (Set String ) ! (Path Abs File )
337344 | InvalidTemplate ! TemplateName ! String
345+ | AttemptedOverwrites [Path Abs File ]
338346 deriving (Typeable )
339347
340348instance Exception NewException
@@ -393,3 +401,7 @@ instance Show NewException where
393401 " The template \" " <> T. unpack (templateName name) <>
394402 " \" is invalid and could not be used. " <>
395403 " The error was: \" " <> why <> " \" "
404+ show (AttemptedOverwrites fps) =
405+ " The template would create the following files, but they already exist:\n " <>
406+ unlines (map ((" " ++ ) . toFilePath) fps) <>
407+ " Use --force to ignore this, and overwite these files."
0 commit comments