Skip to content

Commit

Permalink
Fixes #187 - copying files from ext that do not exist. (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
matijaSos authored Feb 22, 2021
1 parent 19bba53 commit 3eb926e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
14 changes: 12 additions & 2 deletions waspc/src/ExternalCode.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ module ExternalCode
, SourceExternalCodeDir
) where

import Control.Exception (catch)
import System.IO.Error (isDoesNotExistError)
import qualified Data.Text.Lazy as TextL
import qualified Data.Text.Lazy.IO as TextL.IO
import Data.Maybe (catMaybes)
import Data.Text (Text)

import qualified Util.IO
Expand Down Expand Up @@ -60,7 +63,14 @@ readFiles extCodeDirPath = do
-- or create new file draft that will support that.
-- In generator, when creating TextFileDraft, give it function/logic for text transformation,
-- and it will be taken care of when draft will be written to the disk.
fileTexts <- mapM (TextL.IO.readFile . SP.toFilePath) absFilePaths
fileTexts <- catMaybes <$> mapM (tryReadFile . SP.toFilePath) absFilePaths
let files = map (\(path, text) -> File path extCodeDirPath text) (zip relFilePaths fileTexts)
return files

where
-- NOTE(matija): we had cases (e.g. tmp Vim files) where a file initially existed
-- but then got deleted before actual reading was invoked.
-- That would make this function crash, so we just ignore those errors.
tryReadFile :: FilePath -> IO (Maybe TextL.Text)
tryReadFile fp = (Just <$> (TextL.IO.readFile fp)) `catch` (\e -> if isDoesNotExistError e
then return Nothing
else ioError e)
13 changes: 12 additions & 1 deletion waspc/src/Generator/FileDraft/WriteableMonad.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module Generator.FileDraft.WriteableMonad
) where


import Control.Exception (catch)
import System.IO.Error (isDoesNotExistError)
import qualified System.Directory
import qualified Data.Text.IO
import Data.Aeson as Aeson
Expand Down Expand Up @@ -48,7 +50,16 @@ class (Monad m) => WriteableMonad m where

instance WriteableMonad IO where
createDirectoryIfMissing = System.Directory.createDirectoryIfMissing
copyFile = System.Directory.copyFile
-- TODO(matija): we should rename this function to make it clear it won't throw an exception when
-- a file does not exist.
copyFile src dst = do
-- NOTE(matija): we had cases (e.g. tmp Vim files) where a file initially existed
-- when the filedraft was created but then got deleted before actual copying was invoked.
-- That would make this function crash, so we just ignore those errors.
(System.Directory.copyFile src dst) `catch` (\e -> if isDoesNotExistError e
then return ()
else ioError e)

writeFileFromText = Data.Text.IO.writeFile
getTemplateFileAbsPath = Templates.getTemplateFileAbsPath
getTemplatesDirAbsPath = Templates.getTemplatesDirAbsPath
Expand Down

0 comments on commit 3eb926e

Please sign in to comment.