Skip to content

Commit 063357f

Browse files
authored
Added support for binary files to to-directory-tree (#2618)
1 parent cd46573 commit 063357f

File tree

4 files changed

+48
-23
lines changed

4 files changed

+48
-23
lines changed

dhall/src/Dhall/DirectoryTree.hs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import System.FilePath ((</>))
4242
import System.PosixCompat.Types (FileMode, GroupID, UserID)
4343

4444
import qualified Control.Exception as Exception
45+
import qualified Data.ByteString as ByteString
4546
import qualified Data.Foldable as Foldable
4647
import qualified Data.Text as Text
4748
import qualified Data.Text.IO as Text.IO
@@ -279,7 +280,7 @@ getUser (UserName name) =
279280
-- | Resolve a `Group` to a numerical id.
280281
getGroup :: Group -> IO GroupID
281282
getGroup (GroupId gid) = return gid
282-
getGroup (GroupName name) =
283+
getGroup (GroupName name) =
283284
#ifdef mingw32_HOST_OS
284285
ioError $ mkIOError illegalOperationErrorType x Nothing Nothing
285286
where x = "System.Posix.User.getGroupEntryForName: not supported"
@@ -290,21 +291,29 @@ getGroup (GroupName name) =
290291
-- | Process a `FilesystemEntry`. Writes the content to disk and apply the
291292
-- metadata to the newly created item.
292293
processFilesystemEntry :: Bool -> FilePath -> FilesystemEntry -> IO ()
293-
processFilesystemEntry allowSeparators path (DirectoryEntry entry) = do
294-
let path' = path </> entryName entry
295-
when (hasMetadata entry && not isMetadataSupported) $
296-
Exception.throwIO $ MetadataUnsupportedError path'
297-
Directory.createDirectoryIfMissing allowSeparators path'
298-
processFilesystemEntryList allowSeparators path' $ entryContent entry
299-
-- It is important that we write the metadata after we wrote the content of
300-
-- the directories/files below this directory as we might lock ourself out
301-
-- by changing ownership or permissions.
302-
applyMetadata entry path'
303-
processFilesystemEntry _ path (FileEntry entry) = do
294+
processFilesystemEntry allowSeparators path (DirectoryEntry entry) =
295+
processEntryWith path entry $ \path' content -> do
296+
Directory.createDirectoryIfMissing allowSeparators path'
297+
processFilesystemEntryList allowSeparators path' content
298+
processFilesystemEntry allowSeparators path (FileEntry entry) = do
299+
Util.printWarning "`file` is deprecated and will be removed eventually. Please use `text-file` instead."
300+
processFilesystemEntry allowSeparators path (TextFileEntry entry)
301+
processFilesystemEntry _ path (BinaryFileEntry entry) =
302+
processEntryWith path entry ByteString.writeFile
303+
processFilesystemEntry _ path (TextFileEntry entry) =
304+
processEntryWith path entry Text.IO.writeFile
305+
306+
-- | A helper function used by 'processFilesystemEntry'.
307+
processEntryWith
308+
:: FilePath
309+
-> Entry a
310+
-> (FilePath -> a -> IO ())
311+
-> IO ()
312+
processEntryWith path entry f = do
304313
let path' = path </> entryName entry
305314
when (hasMetadata entry && not isMetadataSupported) $
306-
Exception.throwIO $ MetadataUnsupportedError path'
307-
Text.IO.writeFile path' $ entryContent entry
315+
Exception.throwIO (MetadataUnsupportedError path')
316+
f path' (entryContent entry)
308317
-- It is important that we write the metadata after we wrote the content of
309318
-- the file as we might lock ourself out by changing ownership or
310319
-- permissions.

dhall/src/Dhall/DirectoryTree/Types.hs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ module Dhall.DirectoryTree.Types
2727
, isMetadataSupported
2828
) where
2929

30+
import Data.ByteString (ByteString)
3031
import Data.Functor.Identity (Identity (..))
3132
import Data.Sequence (Seq)
3233
import Data.Text (Text)
@@ -72,6 +73,8 @@ type FileEntry = Entry Text
7273
data FilesystemEntry
7374
= DirectoryEntry (Entry (Seq FilesystemEntry))
7475
| FileEntry (Entry Text)
76+
| BinaryFileEntry (Entry ByteString)
77+
| TextFileEntry (Entry Text)
7578
deriving (Eq, Generic, Ord, Show)
7679

7780
instance FromDhall FilesystemEntry where
@@ -82,6 +85,10 @@ instance FromDhall FilesystemEntry where
8285
DirectoryEntry <$> extract (autoWith normalizer) entry
8386
Make "file" entry ->
8487
FileEntry <$> extract (autoWith normalizer) entry
88+
Make "binary-file" entry ->
89+
BinaryFileEntry <$> extract (autoWith normalizer) entry
90+
Make "text-file" entry ->
91+
TextFileEntry <$> extract (autoWith normalizer) entry
8592
expr -> Decode.typeError (expected (Decode.autoWith normalizer :: Decoder FilesystemEntry)) expr
8693
}
8794

dhall/src/Dhall/Import.hs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ import Data.Text (Text)
172172
import Data.Typeable (Typeable)
173173
import Data.Void (Void, absurd)
174174
import Dhall.TypeCheck (TypeError)
175+
import Dhall.Util (printWarning)
175176

176177
import Dhall.Syntax
177178
( Chunks (..)
@@ -1280,15 +1281,6 @@ loadWithManager newManager =
12801281
(makeEmptyStatus newManager defaultOriginHeaders ".")
12811282
UseSemanticCache
12821283

1283-
printWarning :: (MonadIO m) => String -> m ()
1284-
printWarning message = do
1285-
let warning =
1286-
"\n"
1287-
<> "\ESC[1;33mWarning\ESC[0m: "
1288-
<> message
1289-
1290-
liftIO $ System.IO.hPutStrLn System.IO.stderr warning
1291-
12921284
-- | Resolve all imports within an expression, importing relative to the given
12931285
-- directory.
12941286
loadRelativeTo :: FilePath -> SemanticCacheMode -> Expr Src Import -> IO (Expr Src Void)

dhall/src/Dhall/Util.hs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ module Dhall.Util
99
, snipDoc
1010
, insert
1111
, _ERROR
12+
, _WARNING
13+
, printWarning
1214
, Censor(..)
1315
, Input(..)
1416
, Transitivity(..)
@@ -111,6 +113,21 @@ insert expression =
111113
_ERROR :: IsString string => string
112114
_ERROR = "\ESC[1;31mError\ESC[0m"
113115

116+
-- | Prefix used for error messages
117+
_WARNING :: IsString string => string
118+
_WARNING = "\ESC[1;33mWarning\ESC[0m"
119+
120+
-- | Output a warning message on stderr.
121+
printWarning :: (MonadIO m) => String -> m ()
122+
printWarning message = do
123+
let warning =
124+
"\n"
125+
<> _WARNING
126+
<> ": "
127+
<> message
128+
129+
liftIO $ IO.hPutStrLn IO.stderr warning
130+
114131
get
115132
:: (String -> Text -> Either ParseError a)
116133
-> Censor

0 commit comments

Comments
 (0)