コード例 #1
0
        public VirtualInputDirectory(IReadOnlyFileSystem fileSystem, NormalizedPath path)
        {
            path.ThrowIfNull(nameof(path));

            if (!path.IsRelative)
            {
                throw new ArgumentException("Virtual input paths should always be relative", nameof(path));
            }

            _fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
            Path        = path;
        }
コード例 #2
0
 /// <inheritdoc />
 public IDocument Clone(
     NormalizedPath source,
     NormalizedPath destination,
     IEnumerable <KeyValuePair <string, object> > items,
     IContentProvider contentProvider) =>
 new ObjectDocument <T>(
     Id,
     Object,
     Source.IsNull ? source : Source,
     destination.IsNull ? Destination : destination,
     items == null ? _metadata : new Metadata(_metadata, items),
     contentProvider ?? ContentProvider);
コード例 #3
0
 public TDocument CreateDocument <TDocument>(
     NormalizedPath source,
     NormalizedPath destination,
     IEnumerable <KeyValuePair <string, object> > items,
     IContentProvider contentProvider)
     where TDocument : FactoryDocument, IDocument, new() =>
 (TDocument)Factory <TDocument> .Instance.CreateDocument(
     _baseMetadata,
     source,
     destination,
     new Metadata(_executionState, items),
     contentProvider);
コード例 #4
0
        // Construct an ObjectDocument<T> from the actual type of the document
        private static IDocument GetObjectDocument <T>(
            T obj,
            NormalizedPath source,
            NormalizedPath destination,
            IEnumerable <KeyValuePair <string, object> > items,
            IContentProvider contentProvider = null)
        {
            _ = obj ?? throw new ArgumentNullException();

            // Check if this is already an IDocument
            if (obj is IDocument document)
            {
                // It's already a document so return or clone it instead of wrapping
                if (source.IsNull && destination.IsNull && items == null && contentProvider == null)
                {
                    // Not setting anything new so return the same document
                    return(document);
                }

                // Clone the document with the new values
                return(document.Clone(source, destination, items, contentProvider));
            }

            // Check the actual type of the document
            Type objType = obj.GetType();

            if (typeof(T).Equals(objType))
            {
                // Fast track if the type is the same
                return(new ObjectDocument <T>(
                           obj,
                           source,
                           destination,
                           items,
                           contentProvider));
            }

            // The generic type isn't the same as the actual type so use reflection to get it right
            return((IDocument)Activator.CreateInstance(
                       typeof(ObjectDocument <>).MakeGenericType(objType),
                       new object[]
            {
                obj,
                source,
                destination,
                items,
                contentProvider
            }));
        }
コード例 #5
0
        public static IFile CreateZipFile(IExecutionContext context, NormalizedPath directory)
        {
            directory.ThrowIfNull(nameof(directory));

            IDirectory sourceDirectory = context.FileSystem.GetRootDirectory(directory);

            if (!sourceDirectory.Exists)
            {
                throw new DirectoryNotFoundException("Source zip directory does not exist");
            }
            IFile zipFile = context.FileSystem.GetTempFile();

            context.LogDebug($"Creating zip file from {sourceDirectory.Path.FullPath} at {zipFile.Path.FullPath}");
            ZipFile.CreateFromDirectory(sourceDirectory.Path.FullPath, zipFile.Path.FullPath);
            return(zipFile);
        }
コード例 #6
0
        /// <inheritdoc />
        public IDocument Clone(
            NormalizedPath source,
            NormalizedPath destination,
            IEnumerable <KeyValuePair <string, object> > items,
            IContentProvider contentProvider)
        {
            _initialized = true;
            TDocument document = Clone();

            document._initialized = false; // The newly cloned document would have copied over our value for initialized
            document.Id           = Id;    // Make sure it's got the same ID in case implementation overrode Clone()
            return(document.Initialize(
                       _baseMetadata,
                       _source.IsNull ? source : _source,
                       destination.IsNull ? _destination : destination,
                       items == null ? _metadata : new Metadata(_metadata, items),
                       contentProvider ?? _contentProvider));
        }
コード例 #7
0
 public ActionFileSystemWatcher(NormalizedPath outputDirectory, IEnumerable <NormalizedPath> inputDirectories, bool includeSubdirectories, string filter, Action <string> callback)
 {
     foreach (string inputDirectory in inputDirectories.Select(x => x.FullPath).Where(Directory.Exists))
     {
         FileSystemWatcher watcher = new FileSystemWatcher
         {
             Path = inputDirectory,
             IncludeSubdirectories = includeSubdirectories,
             Filter = filter,
             EnableRaisingEvents = true
         };
         watcher.Changed += OnChanged;
         watcher.Created += OnChanged;
         _watchers.Add(watcher);
     }
     _outputPath = outputDirectory.FullPath;
     _callback   = callback;
 }
コード例 #8
0
 /// <summary>
 /// Creates a file provider for a sequence of documents.
 /// </summary>
 /// <param name="documents">The documents to provide virtual directories and files for.</param>
 /// <param name="source">
 /// <c>true</c> to use <see cref="IDocument.Source"/> as the basis for paths,
 /// <c>false</c> to use <see cref="IDocument.Destination"/>.
 /// </param>
 public DocumentFileProvider(IEnumerable <IDocument> documents, bool source)
 {
     if (documents != null)
     {
         foreach (IDocument document in documents)
         {
             NormalizedPath path = source ? document.Source : NormalizedPath.AbsoluteRoot.Combine(document.Destination);
             if (!path.IsNull)
             {
                 Files[path] = document;
                 NormalizedPath directory = path.Parent;
                 while (!directory.IsNullOrEmpty)
                 {
                     Directories.Add(directory);
                     directory = directory.Parent;
                 }
             }
         }
     }
 }
コード例 #9
0
 /// <summary>
 /// Creates a file provider for a sequence of documents.
 /// </summary>
 /// <remarks>
 /// This constructor does not flatten the documents. Only the top-level sequence
 /// (usually the parent-most documents) will be part of the file provider.
 /// </remarks>
 /// <param name="documents">The documents to provide virtual directories and files for.</param>
 /// <param name="source">
 /// <c>true</c> to use <see cref="IDocument.Source"/> as the basis for paths,
 /// <c>false</c> to use <see cref="IDocument.Destination"/>.
 /// </param>
 /// <param name="flatten">
 /// <c>true</c> to flatten the documents, <c>false</c> otherwise.
 /// If <c>false</c> only the top-level sequence (usually the parent-most documents) will be part of the file provider.
 /// </param>
 /// <param name="childrenKey">
 /// The metadata key that contains the children or <c>null</c> to flatten documents in all metadata keys.
 /// This parameter has no effect if <paramref name="flatten"/> is <c>false</c>.
 /// </param>
 public DocumentFileProvider(IEnumerable <IDocument> documents, bool source, bool flatten = true, string childrenKey = Keys.Children)
 {
     if (documents != null)
     {
         foreach (IDocument document in flatten ? documents.Flatten(childrenKey) : documents)
         {
             NormalizedPath path = source ? document.Source : NormalizedPath.AbsoluteRoot.Combine(document.Destination);
             if (!path.IsNull)
             {
                 Files[path] = document;
                 NormalizedPath directory = path.Parent;
                 while (!directory.IsNullOrEmpty)
                 {
                     Directories.Add(directory);
                     directory = directory.Parent;
                 }
             }
         }
     }
 }
コード例 #10
0
 public static IDocument CreateDocument(
     this IExecutionContext context,
     NormalizedPath destination,
     Stream stream,
     string mediaType = null) =>
 context.CreateDocument(null, destination, null, context.GetContentProvider(stream, mediaType));
コード例 #11
0
 public Document(
     NormalizedPath destination,
     IContentProvider contentProvider = null)
     : base(destination, contentProvider)
 {
 }
コード例 #12
0
 /// <summary>
 /// Gets a directory representing an input.
 /// </summary>
 /// <param name="fileSystem">The file system.</param>
 /// <param name="path">
 /// The path of the input directory. If this is an absolute path,
 /// then a directory representing the specified path is returned.
 /// If it's a relative path, then the returned directory will
 /// be a virtual directory that aggregates all input
 /// paths. If this is <c>null</c> then a virtual
 /// directory aggregating all input paths is returned.
 /// </param>
 /// <returns>An input directory.</returns>
 public static IDirectory GetInputDirectory(this IReadOnlyFileSystem fileSystem, NormalizedPath path)
 {
     _ = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
     return(path.IsNull
         ? new VirtualInputDirectory(fileSystem, NormalizedPath.Dot)
         : (path.IsRelative ? new VirtualInputDirectory(fileSystem, path) : fileSystem.GetDirectory(path)));
 }
コード例 #13
0
 /// <summary>
 /// Gets an absolute directory.
 /// </summary>
 /// <param name="fileSystem">The file system.</param>
 /// <param name="path">
 /// The absolute path of the directory.
 /// </param>
 /// <returns>A directory.</returns>
 public static IDirectory GetDirectory(this IReadOnlyFileSystem fileSystem, NormalizedPath path)
 {
     _ = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
     return(fileSystem.FileProvider.GetDirectory(path));
 }
コード例 #14
0
 /// <summary>
 /// Gets a directory representing a root directory.
 /// </summary>
 /// <param name="fileSystem">The file system.</param>
 /// <param name="path">
 /// The path of the root directory. If this is an absolute path,
 /// then a directory representing the specified path is returned.
 /// If it's a relative path, then it will be combined with the
 /// current root path. If this is <c>null</c> then the base
 /// root directory is returned.
 /// </param>
 /// <returns>A root directory.</returns>
 public static IDirectory GetRootDirectory(this IReadOnlyFileSystem fileSystem, NormalizedPath path)
 {
     _ = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
     return(path.IsNull
         ? fileSystem.GetDirectory(fileSystem.RootPath)
         : fileSystem.GetDirectory(fileSystem.RootPath.Combine(path)));
 }
コード例 #15
0
 /// <summary>
 /// Gets a file representing a root file.
 /// </summary>
 /// <param name="fileSystem">The file system.</param>
 /// <param name="path">
 /// The path of the root file. If this is an absolute path,
 /// then a file representing the specified path is returned.
 /// If it's a relative path, then it will be combined with the
 /// current root path.
 /// </param>
 /// <returns>A root file.</returns>
 public static IFile GetRootFile(this IReadOnlyFileSystem fileSystem, NormalizedPath path)
 {
     _ = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
     return(fileSystem.GetFile(fileSystem.RootPath.Combine(path)));
 }
コード例 #16
0
 public IFile GetFile(NormalizedPath path) => new DocumentFile(this, path);
コード例 #17
0
        internal static NormalizedPath GetContainingInputPathForAbsolutePath(this IReadOnlyFileSystem fileSystem, NormalizedPath path)
        {
            _ = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
            path.ThrowIfNull(nameof(path));

            if (!path.IsAbsolute)
            {
                throw new ArgumentException("Path must be absolute");
            }

            return(fileSystem.InputPaths
                   .Reverse()
                   .Select(x => fileSystem.RootPath.Combine(x))
                   .FirstOrDefault(x => path.Segments.StartsWith(x.Segments)));
        }
コード例 #18
0
 /// <summary>
 /// Gets the value for the specified key converted to a <see cref="NormalizedPath"/>. This method never throws an exception.
 /// It will return the specified default value if the key is not found.
 /// </summary>
 /// <param name="metadata">The metadata instance.</param>
 /// <param name="key">The key of the value to get.</param>
 /// <param name="defaultValue">The default value to use if the key is not found.</param>
 /// <returns>The value for the specified key converted to a <see cref="NormalizedPath"/> or the specified default value.</returns>
 public static NormalizedPath GetPath(this IMetadata metadata, string key, NormalizedPath defaultValue = default) => metadata.Get(key, defaultValue);
コード例 #19
0
 public IDirectory GetDirectory(NormalizedPath path) => new DocumentDirectory(this, path);
コード例 #20
0
        // ToDocument

        public static IDocument ToDocument <T>(
            this T obj,
            NormalizedPath destination,
            IEnumerable <KeyValuePair <string, object> > items,
            IContentProvider contentProvider = null) =>
        GetObjectDocument(obj, null, destination, new Metadata(items), contentProvider);
コード例 #21
0
 /// <summary>
 /// Clones this document.
 /// </summary>
 /// <param name="document">The document.</param>
 /// <param name="destination">The new destination or <c>null</c> to keep the existing destination.</param>
 /// <param name="contentProvider">The new content provider or <c>null</c> to keep the existing content provider.</param>
 /// <returns>A new document of the same type as this document.</returns>
 public static IDocument Clone(
     this IDocument document,
     NormalizedPath destination,
     IContentProvider contentProvider = null) =>
 document.Clone(null, destination, null, contentProvider);
コード例 #22
0
 /// <summary>
 /// Clones this document.
 /// </summary>
 /// <param name="document">The document.</param>
 /// <param name="destination">The new destination or <c>null</c> to keep the existing destination.</param>
 /// <param name="items">New metadata items.</param>
 /// <param name="contentProvider">The new content provider or <c>null</c> to keep the existing content provider.</param>
 /// <returns>A new document of the same type as this document.</returns>
 public static IDocument Clone(
     this IDocument document,
     NormalizedPath destination,
     IEnumerable <KeyValuePair <string, object> > items,
     IContentProvider contentProvider = null) =>
 document.Clone(null, destination, items, contentProvider);
コード例 #23
0
        // ToDocument

        public static ObjectDocument <T> ToDocument <T>(
            this T obj,
            NormalizedPath destination,
            IEnumerable <KeyValuePair <string, object> > items,
            IContentProvider contentProvider = null) =>
        new ObjectDocument <T>(obj, null, destination, items, contentProvider);
コード例 #24
0
 /// <summary>
 /// Gets a temp path by combining it with the root path and temp path.
 /// </summary>
 /// <param name="fileSystem">The file system.</param>
 /// <param name="path">The path to combine with the root path and temp path.
 /// If this is <see cref="NormalizedPath.Null"/>, returns the root path combined with the temp path.</param>
 /// <returns>The temp path.</returns>
 public static NormalizedPath GetTempPath(this IReadOnlyFileSystem fileSystem, NormalizedPath path)
 {
     _ = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
     return(path.IsNull
         ? fileSystem.RootPath.Combine(fileSystem.TempPath)
         : fileSystem.RootPath.Combine(fileSystem.TempPath).Combine(path));
 }
コード例 #25
0
 public static ObjectDocument <T> ToDocument <T>(
     this T obj,
     NormalizedPath destination,
     IContentProvider contentProvider = null) =>
 new ObjectDocument <T>(obj, null, destination, null, contentProvider);
コード例 #26
0
 public IDocument GetDocument(NormalizedPath path) =>
 Files.TryGetValue(path, out IDocument document) ? document : throw new KeyNotFoundException();
コード例 #27
0
 public IDocument Clone(
     NormalizedPath source,
     NormalizedPath destination,
     IEnumerable <KeyValuePair <string, object> > items,
     IContentProvider contentProvider = null) =>
 _document.Clone(source, destination, items, contentProvider);
コード例 #28
0
 public static IDocument ToDocument <T>(
     this T obj,
     NormalizedPath source,
     NormalizedPath destination,
     IContentProvider contentProvider = null) =>
 GetObjectDocument(obj, source, destination, null, contentProvider);
コード例 #29
0
 protected Document(
     NormalizedPath destination,
     IContentProvider contentProvider = null)
     : this(null, null, destination, null, contentProvider)
 {
 }
コード例 #30
0
        /// <summary>
        /// Gets the absolute input path that contains the specified file or directory. If the provided
        /// file or directory path is absolute, this returns the input path that contains the specified
        /// path (note that the specified file or directory does not need to exist and this just returns
        /// the input path that would contain the file or directory based only on path information). If
        /// the provided path is relative, this checks all input paths for the existence of the file
        /// or directory and returns the first one where it exists.
        /// </summary>
        /// <param name="fileSystem">The file system.</param>
        /// <param name="path">The file path.</param>
        /// <returns>The input path that contains the specified file,
        /// or <c>null</c> if no input path does.</returns>
        public static NormalizedPath GetContainingInputPath(this IReadOnlyFileSystem fileSystem, NormalizedPath path)
        {
            _ = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
            path.ThrowIfNull(nameof(path));

            if (path.IsAbsolute)
            {
                return(fileSystem.GetContainingInputPathForAbsolutePath(path));
            }

            // Try to find a file first
            IFile file = fileSystem.GetInputFile(path);

            if (file.Exists)
            {
                return(fileSystem.GetContainingInputPath(file.Path));
            }

            // Then try to find a directory
            IEnumerable <(NormalizedPath x, IDirectory)> rootDirectories =
                fileSystem.InputPaths
                .Reverse()
                .Select(x => (x, fileSystem.GetRootDirectory(x.Combine(path))));
            IEnumerable <(NormalizedPath x, IDirectory)> existingRootDirectories = rootDirectories.Where(x => x.Item2.Exists);

            return(existingRootDirectories.Select(x => fileSystem.RootPath.Combine(x.Item1)).FirstOrDefault());
        }