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

DirectoryTree: Add binary-file to support creating files with Bytes content #2641

Merged
merged 3 commits into from
Feb 21, 2025
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
6 changes: 2 additions & 4 deletions dhall/src/Dhall/DirectoryTree.hs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,8 @@
makeType :: Expector (Expr Src Void)
makeType = Record . Map.fromList <$> sequenceA
[ makeConstructor "directory" (Decode.auto :: Decoder DirectoryEntry)
, makeConstructor "file" (Decode.auto :: Decoder FileEntry)
, makeConstructor "file" (Decode.auto :: Decoder TextFileEntry)
, makeConstructor "binary-file" (Decode.auto :: Decoder BinaryFileEntry)
]
where
makeConstructor :: Text -> Decoder b -> Expector (Text, RecordField Src Void)
Expand All @@ -269,7 +270,7 @@
-- | Resolve a `User` to a numerical id.
getUser :: User -> IO UserID
getUser (UserId uid) = return uid
getUser (UserName name) =

Check warning on line 273 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 @@ -280,7 +281,7 @@
-- | Resolve a `Group` to a numerical id.
getGroup :: Group -> IO GroupID
getGroup (GroupId gid) = return gid
getGroup (GroupName name) =

Check warning on line 284 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 @@ -295,9 +296,6 @@
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) =
Expand Down
23 changes: 16 additions & 7 deletions dhall/src/Dhall/DirectoryTree/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@

-- | Types used by the implementation of the @to-directory-tree@ subcommand
module Dhall.DirectoryTree.Types
( FilesystemEntry(..)
( FilesystemEntry(DirectoryEntry, BinaryFileEntry, TextFileEntry, FileEntry)
, DirectoryEntry
, FileEntry
, BinaryFileEntry
, TextFileEntry
, Entry(..)
, User(..)
, Group(..)
Expand Down Expand Up @@ -67,28 +69,35 @@ pattern Make label entry <- App (Field (Var (V "_" 0)) (fieldSelectionLabel -> l
type DirectoryEntry = Entry (Seq FilesystemEntry)

-- | A file in the filesystem.
{-# DEPRECATED FileEntry "`FileEntry` is deprecated and will be removed eventually. Please use `TextFileEntry` instead." #-}
type FileEntry = Entry Text

-- | A binary file in the filesystem.
type BinaryFileEntry = Entry ByteString

-- | A text file in the filesystem.
type TextFileEntry = Entry Text

-- | A filesystem entry.
data FilesystemEntry
= DirectoryEntry (Entry (Seq FilesystemEntry))
| FileEntry (Entry Text)
| BinaryFileEntry (Entry ByteString)
| TextFileEntry (Entry Text)
| BinaryFileEntry BinaryFileEntry
| TextFileEntry TextFileEntry
deriving (Eq, Generic, Ord, Show)

pattern FileEntry :: Entry Text -> FilesystemEntry
pattern FileEntry entry = TextFileEntry entry

instance FromDhall FilesystemEntry where
autoWith normalizer = Decoder
{ expected = pure $ Var (V "tree" 0)
, extract = \case
Make "directory" entry ->
DirectoryEntry <$> extract (autoWith normalizer) entry
Make "file" entry ->
FileEntry <$> extract (autoWith normalizer) entry
TextFileEntry <$> 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
4 changes: 2 additions & 2 deletions dhall/tests/Dhall/Test/DirectoryTree.hs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
but got: 438
-}
fixpointedPermissions :: TestTree
fixpointedPermissions = testCase "permissions" $ do

Check warning on line 69 in dhall/tests/Dhall/Test/DirectoryTree.hs

View workflow job for this annotation

GitHub Actions / windows-latest - stack.yaml

Defined but not used: `fixpointedPermissions'
let outDir = "./tests/to-directory-tree/fixpoint-permissions.out"
path = "./tests/to-directory-tree/fixpoint-permissions.dhall"
entries <- runDirectoryTree False outDir path
Expand All @@ -79,19 +79,19 @@
prettyFileMode mode @?= prettyFileMode Files.ownerModes

fixpointedUserGroup :: TestTree
fixpointedUserGroup = testCase "user and group" $ do

Check warning on line 82 in dhall/tests/Dhall/Test/DirectoryTree.hs

View workflow job for this annotation

GitHub Actions / windows-latest - stack.yaml

Defined but not used: `fixpointedUserGroup'
let file = "./tests/to-directory-tree/fixpoint-usergroup.dhall"
expr <- Dhall.inputExpr file
entries <- decodeDirectoryTree expr
entries @?=
[ FileEntry $ Entry
[ TextFileEntry $ Entry
{ entryName = "ids"
, entryContent = ""
, entryUser = Just (UserId 0)
, entryGroup = Just (GroupId 0)
, entryMode = Nothing
}
, FileEntry $ Entry
, TextFileEntry $ Entry
{ entryName = "names"
, entryContent = ""
, entryUser = Just (UserName "user")
Expand Down
6 changes: 5 additions & 1 deletion dhall/tests/to-directory-tree/fixpoint-helper.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ let Entry =
}

let Make =
\(r : Type) -> { directory : Entry (List r) -> r, file : Entry Text -> r }
\(r : Type) ->
{ directory : Entry (List r) -> r
, binary-file : Entry Bytes -> r
, file : Entry Text -> r
}

in { User, Group, Access, Mode, Entry, Make }
30 changes: 15 additions & 15 deletions dhall/tests/to-directory-tree/fixpoint-simple.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ let Make = (./fixpoint-helper.dhall).Make

in \(r : Type) ->
\(make : Make r) ->
[ make.file
{ name = "file"
, content = ""
, user = None User
, group = None Group
, mode = None Mode
}
, make.directory
{ name = "directory"
, content = [] : List r
, user = None User
, group = None Group
, mode = None Mode
}
]
[ make.file
{ name = "file"
, content = ""
, user = None User
, group = None Group
, mode = None Mode
}
, make.directory
{ name = "directory"
, content = [] : List r
, user = None User
, group = None Group
, mode = None Mode
}
]
9 changes: 7 additions & 2 deletions dhall/tests/to-directory-tree/type.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,14 @@ let Entry =
in forall (result : Type) ->
let DirectoryEntry = Entry (List result)

let FileEntry = Entry Text
let BinaryFileEntry = Entry Bytes

let TextFileEntry = Entry Text

let Make =
{ directory : DirectoryEntry -> result, file : FileEntry -> result }
{ directory : DirectoryEntry -> result
, binary-file : BinaryFileEntry -> result
, file : TextFileEntry -> result
}

in forall (make : Make) -> List result
Loading