Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for binary files to to-directory-tree #2618

Merged
merged 1 commit into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions dhall/src/Dhall/DirectoryTree.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import System.PosixCompat.Types (FileMode, GroupID, UserID)

import qualified Control.Exception as Exception
import qualified Data.ByteString as ByteString
import qualified Data.Foldable as Foldable
import qualified Data.Text as Text
import qualified Data.Text.IO as Text.IO
Expand Down Expand Up @@ -268,7 +269,7 @@
-- | Resolve a `User` to a numerical id.
getUser :: User -> IO UserID
getUser (UserId uid) = return uid
getUser (UserName name) =

Check warning on line 272 in dhall/src/Dhall/DirectoryTree.hs

View workflow job for this annotation

GitHub Actions / windows-latest - stack.yaml

Defined but not used: `name'
#ifdef mingw32_HOST_OS
ioError $ mkIOError illegalOperationErrorType x Nothing Nothing
where x = "System.Posix.User.getUserEntryForName: not supported"
Expand All @@ -279,7 +280,7 @@
-- | Resolve a `Group` to a numerical id.
getGroup :: Group -> IO GroupID
getGroup (GroupId gid) = return gid
getGroup (GroupName name) =
getGroup (GroupName name) =

Check warning on line 283 in dhall/src/Dhall/DirectoryTree.hs

View workflow job for this annotation

GitHub Actions / windows-latest - stack.yaml

Defined but not used: `name'
#ifdef mingw32_HOST_OS
ioError $ mkIOError illegalOperationErrorType x Nothing Nothing
where x = "System.Posix.User.getGroupEntryForName: not supported"
Expand All @@ -290,21 +291,29 @@
-- | Process a `FilesystemEntry`. Writes the content to disk and apply the
-- metadata to the newly created item.
processFilesystemEntry :: Bool -> FilePath -> FilesystemEntry -> IO ()
processFilesystemEntry allowSeparators path (DirectoryEntry entry) = do
let path' = path </> entryName entry
when (hasMetadata entry && not isMetadataSupported) $
Exception.throwIO $ MetadataUnsupportedError path'
Directory.createDirectoryIfMissing allowSeparators path'
processFilesystemEntryList allowSeparators path' $ entryContent entry
-- It is important that we write the metadata after we wrote the content of
-- the directories/files below this directory as we might lock ourself out
-- by changing ownership or permissions.
applyMetadata entry path'
processFilesystemEntry _ path (FileEntry entry) = do
processFilesystemEntry allowSeparators path (DirectoryEntry entry) =
processEntryWith path entry $ \path' content -> do
Directory.createDirectoryIfMissing allowSeparators path'
processFilesystemEntryList allowSeparators path' content
processFilesystemEntry allowSeparators path (FileEntry entry) = do
Util.printWarning "`file` is deprecated and will be removed eventually. Please use `text-file` instead."
processFilesystemEntry allowSeparators path (TextFileEntry entry)
processFilesystemEntry _ path (BinaryFileEntry entry) =
processEntryWith path entry ByteString.writeFile
processFilesystemEntry _ path (TextFileEntry entry) =
processEntryWith path entry Text.IO.writeFile

-- | A helper function used by 'processFilesystemEntry'.
processEntryWith
:: FilePath
-> Entry a
-> (FilePath -> a -> IO ())
-> IO ()
processEntryWith path entry f = do
let path' = path </> entryName entry
when (hasMetadata entry && not isMetadataSupported) $
Exception.throwIO $ MetadataUnsupportedError path'
Text.IO.writeFile path' $ entryContent entry
Exception.throwIO (MetadataUnsupportedError path')
f path' (entryContent entry)
-- It is important that we write the metadata after we wrote the content of
-- the file as we might lock ourself out by changing ownership or
-- permissions.
Expand Down
7 changes: 7 additions & 0 deletions dhall/src/Dhall/DirectoryTree/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module Dhall.DirectoryTree.Types
, isMetadataSupported
) where

import Data.ByteString (ByteString)
import Data.Functor.Identity (Identity (..))
import Data.Sequence (Seq)
import Data.Text (Text)
Expand Down Expand Up @@ -72,6 +73,8 @@ type FileEntry = Entry Text
data FilesystemEntry
= DirectoryEntry (Entry (Seq FilesystemEntry))
| FileEntry (Entry Text)
| BinaryFileEntry (Entry ByteString)
| TextFileEntry (Entry Text)
deriving (Eq, Generic, Ord, Show)

instance FromDhall FilesystemEntry where
Expand All @@ -82,6 +85,10 @@ instance FromDhall FilesystemEntry where
DirectoryEntry <$> extract (autoWith normalizer) entry
Make "file" entry ->
FileEntry <$> extract (autoWith normalizer) entry
Make "binary-file" entry ->
BinaryFileEntry <$> extract (autoWith normalizer) entry
Make "text-file" entry ->
TextFileEntry <$> extract (autoWith normalizer) entry
expr -> Decode.typeError (expected (Decode.autoWith normalizer :: Decoder FilesystemEntry)) expr
}

Expand Down
10 changes: 1 addition & 9 deletions dhall/src/Dhall/Import.hs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ import Data.Text (Text)
import Data.Typeable (Typeable)
import Data.Void (Void, absurd)
import Dhall.TypeCheck (TypeError)
import Dhall.Util (printWarning)

import Dhall.Syntax
( Chunks (..)
Expand Down Expand Up @@ -1280,15 +1281,6 @@ loadWithManager newManager =
(makeEmptyStatus newManager defaultOriginHeaders ".")
UseSemanticCache

printWarning :: (MonadIO m) => String -> m ()
printWarning message = do
let warning =
"\n"
<> "\ESC[1;33mWarning\ESC[0m: "
<> message

liftIO $ System.IO.hPutStrLn System.IO.stderr warning

-- | Resolve all imports within an expression, importing relative to the given
-- directory.
loadRelativeTo :: FilePath -> SemanticCacheMode -> Expr Src Import -> IO (Expr Src Void)
Expand Down
17 changes: 17 additions & 0 deletions dhall/src/Dhall/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ module Dhall.Util
, snipDoc
, insert
, _ERROR
, _WARNING
, printWarning
, Censor(..)
, Input(..)
, Transitivity(..)
Expand Down Expand Up @@ -111,6 +113,21 @@ insert expression =
_ERROR :: IsString string => string
_ERROR = "\ESC[1;31mError\ESC[0m"

-- | Prefix used for error messages
_WARNING :: IsString string => string
_WARNING = "\ESC[1;33mWarning\ESC[0m"

-- | Output a warning message on stderr.
printWarning :: (MonadIO m) => String -> m ()
printWarning message = do
let warning =
"\n"
<> _WARNING
<> ": "
<> message

liftIO $ IO.hPutStrLn IO.stderr warning

get
:: (String -> Text -> Either ParseError a)
-> Censor
Expand Down
Loading