Skip to content

Commit

Permalink
Fixes #187 - copying files from ext that do not exist.
Browse files Browse the repository at this point in the history
  • Loading branch information
matijaSos committed Feb 21, 2021
1 parent 19bba53 commit d72cef7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
12 changes: 11 additions & 1 deletion waspc/src/Generator/FileDraft/CopyFileDraft.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module Generator.FileDraft.CopyFileDraft
( CopyFileDraft(..)
) where

import Control.Monad (when)

import StrongPath (Path, Abs, Rel, File, (</>))
import qualified StrongPath as SP
import Generator.Common (ProjectRootDir)
Expand All @@ -19,6 +21,14 @@ data CopyFileDraft = CopyFileDraft
instance Writeable CopyFileDraft where
write absDstDirPath draft = do
createDirectoryIfMissing True (SP.toFilePath $ SP.parent absDraftDstPath)
copyFile (SP.toFilePath $ _srcPath draft) (SP.toFilePath $ absDraftDstPath)

-- NOTE(matija): we had cases (e.g. tmp Vim files) where file initially existed
-- when the filedraft was created but then got deleted before this function was invoked.
-- That would make this function crash, so here we make a check to make sure the given
-- file exists.
fileExists <- doesFileExist (SP.toFilePath $ _srcPath draft)
when fileExists
(copyFile (SP.toFilePath $ _srcPath draft) (SP.toFilePath $ absDraftDstPath))

where
absDraftDstPath = absDstDirPath </> (_dstPath draft)
5 changes: 5 additions & 0 deletions waspc/src/Generator/FileDraft/WriteableMonad.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,15 @@ class (Monad m) => WriteableMonad m where
-> Aeson.Value -- ^ JSON to be provided as template data.
-> m Text

doesFileExist
:: FilePath
-> m Bool

instance WriteableMonad IO where
createDirectoryIfMissing = System.Directory.createDirectoryIfMissing
copyFile = System.Directory.copyFile
writeFileFromText = Data.Text.IO.writeFile
getTemplateFileAbsPath = Templates.getTemplateFileAbsPath
getTemplatesDirAbsPath = Templates.getTemplatesDirAbsPath
compileAndRenderTemplate = Templates.compileAndRenderTemplate
doesFileExist = System.Directory.doesFileExist
3 changes: 3 additions & 0 deletions waspc/test/Generator/MockWriteableMonad.hs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ instance WriteableMonad MockWriteableMonad where
(_, config) <- get
return $ (compileAndRenderTemplate_impl config) path json

doesFileExist path = MockWriteableMonad (return True)


modifyLogs :: MonadState (a, b) m => (a -> a) -> m ()
modifyLogs f = modify (\(logs, config) -> (f logs, config))

Expand Down

0 comments on commit d72cef7

Please sign in to comment.