//
//
// PL/.NET (pldotnet) - PostgreSQL support for .NET C# and F# as
// procedural languages (PL)
//
//
// Copyright (c) 2023 Brick Abode
//
// This code is subject to the terms of the PostgreSQL License.
// The full text of the license can be found in the LICENSE file
// at the top level of the pldotnet repository.
//
//
#if INTERACTIVE
#r "FSharp.Compiler.Service.dll"
#endif
namespace PlDotNET.FSharp
open System
open System.IO
open System.Text
open System.Collections.Generic
open FSharp.Compiler.CodeAnalysis
open FSharp.Compiler.IO
///
/// A virtual file system that allows reading and writing to files in memory.
///
///
/// This class implements the `IFileSystem` interface and provides a virtual file system
/// that allows reading and writing to files stored in memory. It also delegates to a
/// default file system for all other file system operations.
///
/// A list of tuples representing the initial files in the virtual file system.
/// Each tuple consists of the file name and its contents.
/// The default file system to delegate to for all other file system operations.
type VirtualFileSystem(container: (string * string) [], defaultFileSystem: IFileSystem) =
///
/// A dictionary of the virtual files in the file system, keyed by file name.
///
///
/// The dictionary stores the contents of the virtual files as strings.
///
let files = dict container
///
/// A dictionary of the virtual files in the file system, keyed by file name.
///
///
/// The dictionary stores the contents of the virtual files as `MemoryStream` objects.
///
let streams =
let d = new Dictionary()
for (key, value) in container do
d.Add(key, new MemoryStream(Encoding.UTF8.GetBytes(value)))
d
///
/// An interface for file system operations.
///
///
/// This interface provides methods for performing various file system operations,
/// such as reading and writing to files, checking file existence, and deleting files.
///
interface IFileSystem with
///
/// Opens a file for reading.
///
/// The name of the file to open.
/// An optional flag indicating whether to use a memory-mapped file.
/// An optional flag indicating whether to shadow copy the file.
/// A stream representing the opened file.
member _.OpenFileForReadShim(fileName, ?useMemoryMappedFile: bool, ?shouldShadowCopy: bool) =
match files.TryGetValue fileName with
| true, text -> streams.[fileName] :> Stream
| _ ->
defaultFileSystem.OpenFileForReadShim(
fileName,
?useMemoryMappedFile = useMemoryMappedFile,
?shouldShadowCopy = shouldShadowCopy
)
///
/// Opens a file for writing.
///
/// The name of the file to open.
/// An optional flag indicating the mode to open the file in.
/// An optional flag indicating the access to grant to the file.
/// An optional flag indicating the type of sharing to allow for the file.
/// A stream representing the opened file.
member _.OpenFileForWriteShim(fileName, ?fileMode: FileMode, ?fileAccess: FileAccess, ?fileShare: FileShare) =
defaultFileSystem.OpenFileForWriteShim(
fileName,
?fileMode = fileMode,
?fileAccess = fileAccess,
?fileShare = fileShare
)
///
/// Check if a file exists.
///
/// The name of the file to check.
/// `true` if the file exists, `false` otherwise.
member _.FileExistsShim(fileName) =
files.ContainsKey(fileName)
|| defaultFileSystem.FileExistsShim(fileName)
///
/// Get the path to the system's temporary directory.
///
/// The path to the temporary directory.
member _.GetTempPathShim() = defaultFileSystem.GetTempPathShim()
///
/// Get the last write time of a file.
///
/// The name of the file to check.
/// The last write time of the file.
member _.GetLastWriteTimeShim(fileName) =
defaultFileSystem.GetLastWriteTimeShim(fileName)
///
/// Get the full path to a file.
///
/// The name of the file.
/// The full path to the file.
member _.GetFullPathShim(fileName) =
defaultFileSystem.GetFullPathShim(fileName)
///
/// Check if a path is invalid.
///
/// The path to check.
/// `true` if the path is invalid, `false` otherwise.
member _.IsInvalidPathShim(fileName) =
defaultFileSystem.IsInvalidPathShim(fileName)
///
/// Check if a path is rooted.
///
/// The path to check.
/// `true` if the path is rooted, `false` otherwise.
member _.IsPathRootedShim(fileName) =
defaultFileSystem.IsPathRootedShim(fileName)
///
/// Delete a file.
///
/// The name of the file to delete.
member _.FileDeleteShim(fileName) =
defaultFileSystem.FileDeleteShim(fileName)
///
/// Get the assembly loader to use.
///
member _.AssemblyLoader = defaultFileSystem.AssemblyLoader
///
/// Get the full path to a file in a directory.
///
/// The directory containing the file.
/// The name of the file.
/// The full path to the file in the directory.
member _.GetFullFilePathInDirectoryShim dir fileName =
defaultFileSystem.GetFullFilePathInDirectoryShim dir fileName
///
/// Normalize a path.
///
/// The path to normalize.
/// The normalized path.
member _.NormalizePathShim(path) =
defaultFileSystem.NormalizePathShim(path)
///
/// Get the directory name from a path.
///
/// The path to get the directory name from.
/// The directory name from the path.
member _.GetDirectoryNameShim(path) =
defaultFileSystem.GetDirectoryNameShim(path)
///
/// Get the creation time of a file.
///
/// The path to the file.
/// The creation time of the file.
member _.GetCreationTimeShim(path) =
defaultFileSystem.GetCreationTimeShim(path)
///
/// Copy a file.
///
/// The path to the source file.
/// The path to the destination file.
/// A flag indicating whether to overwrite the destination file if it exists.
member _.CopyShim(src, dest, overwrite) =
defaultFileSystem.CopyShim(src, dest, overwrite)
///
/// Create a directory.
///
/// The path to the directory to create.
member _.DirectoryCreateShim(path) =
defaultFileSystem.DirectoryCreateShim(path)
///
/// Check if a directory exists.
///
/// The path to the directory to check.
/// `true` if the directory exists, `false` otherwise.
member _.DirectoryExistsShim(path) =
defaultFileSystem.DirectoryExistsShim(path)
///
/// Delete a directory.
///
/// The path to the directory to delete.
member _.DirectoryDeleteShim(path) =
defaultFileSystem.DirectoryDeleteShim(path)
///
/// Enumerate the files in a directory.
///
/// The path to the directory to enumerate.
/// The search pattern to use for the enumeration.
/// An enumerable containing the names of the files in the directory.
member _.EnumerateFilesShim(path, pattern) =
defaultFileSystem.EnumerateFilesShim(path, pattern)
///
/// Enumerate the directories in a directory.
///
/// The path to the directory to enumerate.
/// An enumerable containing the names of the directories in the directory.
member _.EnumerateDirectoriesShim(path) =
defaultFileSystem.EnumerateDirectoriesShim(path)
///
/// Check if a file is stable using a heuristic.
///
/// The path to the file to check.
/// `true` if the file is stable according to the heuristic, `false` otherwise.
member _.IsStableFileHeuristic(path) =
defaultFileSystem.IsStableFileHeuristic(path)