Skip to content

Commit be23e4c

Browse files
committed
"stack new --bare" complains for overwrites #1597
1 parent 5f41d21 commit be23e4c

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/Stack/New.hs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ data NewOpts = NewOpts
7777
-- | Create a new project with the given options.
7878
new
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.
234241
writeTemplateFiles
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

340348
instance 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."

src/main/Main.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1180,7 +1180,7 @@ initCmd initOpts go = do
11801180
newCmd :: (NewOpts,InitOpts) -> GlobalOpts -> IO ()
11811181
newCmd (newOpts,initOpts) go@GlobalOpts{..} = do
11821182
withMiniConfigAndLock go $ do
1183-
dir <- new newOpts
1183+
dir <- new newOpts (forceOverwrite initOpts)
11841184
initProject dir initOpts globalResolver
11851185

11861186
-- | List the available templates.

0 commit comments

Comments
 (0)