Example #1
0
        /// <exception cref="ArgumentNullException"><paramref name="env"/> or <paramref name="path"/> is
        ///  <see langword="null"/></exception>
        /// <exception cref="InvalidOperationException">Cannot clean directory when <paramref name="env.FS"/> is null.
        /// </exception>
        public static void Clean(IFileSystemEnvironment env, DirectoryPath path, Func <IFileSystemInfo, bool> predicate)
        {
            if (env == null)
            {
                throw new ArgumentNullException(nameof(env));
            }
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            if (env.FS == null)
            {
                throw new InvalidOperationException(
                          $"Cannot clean directory when {nameof(env)}.{nameof(env.FS)} is null");
            }

            if (path.IsRelative)
            {
                path = path.MakeAbsolute(env);
            }

            // Get the root directory.
            var root = env.FS.GetDirectory(path);

            if (!root.Exists)
            {
                root.Create();
                return;
            }

            predicate = predicate ?? (info => true);
            CleanDirectory(root, predicate, 0);
        }
Example #2
0
        /// <exception cref="ArgumentNullException"><paramref name="env"/> or <paramref name="path"/> is
        ///  <see langword="null"/></exception>
        /// <exception cref="InvalidOperationException">Cannot create directory when <paramref name="env.FS"/> is null.
        /// </exception>
        public static void Create(IFileSystemEnvironment env, DirectoryPath path)
        {
            if (env == null)
            {
                throw new ArgumentNullException(nameof(env));
            }
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            if (env.FS == null)
            {
                throw new InvalidOperationException(
                          $"Cannot create directory when {nameof(env)}.{nameof(env.FS)} is null");
            }

            if (path.IsRelative)
            {
                path = path.MakeAbsolute(env);
            }

            var directory = env.FS.GetDirectory(path);

            if (!directory.Exists)
            {
                directory.Create();
            }
        }
Example #3
0
        /// <summary>
        /// Makes the path absolute (if relative) using the current working directory.
        /// </summary>
        /// <example>
        /// <code>
        /// var path = MakeAbsolute(Directory("./resources"));
        /// </code>
        /// </example>
        /// <param name="env">The context.</param>
        /// <param name="path">The path.</param>
        /// <returns>An absolute directory path.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="env"/> or <paramref name="path"/> is
        ///  <see langword="null"/></exception>
        public static DirectoryPath MakeAbsolute(this IFileSystemEnvironment env, DirectoryPath path)
        {
            if (env == null)
            {
                throw new ArgumentNullException(nameof(env));
            }

            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            return(path.MakeAbsolute(env));
        }
Example #4
0
        /// <exception cref="IOException">The directory <paramref name="path.FullPath"/> does not exist. -or-
        ///  Cannot delete directory <paramref name="path.FullPath"/> without recursion since it's not empty.
        /// </exception>
        /// <exception cref="ArgumentNullException"><paramref name="env"/> is <see langword="null"/></exception>
        /// <exception cref="InvalidOperationException">Cannot delete directory when <paramref name="env.FS"/> is null.
        /// </exception>
        public static void Delete(IFileSystemEnvironment env, DirectoryPath path, bool recursive)
        {
            if (env == null)
            {
                throw new ArgumentNullException(nameof(env));
            }
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            if (env.FS == null)
            {
                throw new InvalidOperationException(
                          $"Cannot delete directory when {nameof(env)}.{nameof(env.FS)} is null");
            }

            if (path.IsRelative)
            {
                path = path.MakeAbsolute(env);
            }

            var directory = env.FS.GetDirectory(path);

            if (!directory.Exists)
            {
                throw new IOException($"The directory '{path.FullPath}' does not exist.");
            }

            var hasDirectories = directory.GetDirectories("*", SearchScope.Current).Any();
            var hasFiles       = directory.GetFiles("*", SearchScope.Current).Any();

            if (!recursive && (hasDirectories || hasFiles))
            {
                throw new IOException($"Cannot delete directory '{path.FullPath}' " +
                                      "without recursion since it's not empty.");
            }

            directory.Delete(recursive);
        }
Example #5
0
        /// <exception cref="ArgumentNullException"><paramref name="env"/> or <paramref name="filePaths"/> or
        ///  <paramref name="targetDirectoryPath"/> is <see langword="null"/></exception>
        /// <exception cref="InvalidOperationException">The target directory does not exist.</exception>
        /// <exception cref="FileNotFoundException">The file does not exist.</exception>
        public static void CopyFiles(IFileSystemEnvironment env, IEnumerable <FilePath> filePaths,
                                     DirectoryPath targetDirectoryPath)
        {
            if (env == null)
            {
                throw new ArgumentNullException(nameof(env));
            }
            if (filePaths == null)
            {
                throw new ArgumentNullException(nameof(filePaths));
            }
            if (targetDirectoryPath == null)
            {
                throw new ArgumentNullException(nameof(targetDirectoryPath));
            }

            if (env.FS == null)
            {
                throw new InvalidOperationException(
                          $"Cannot copy files when {nameof(env)}.{nameof(env.FS)} is null");
            }

            var absTargetDirectoryPath = targetDirectoryPath.MakeAbsolute(env);

            // Make sure the target directory exist.
            if (!env.FS.Exists(absTargetDirectoryPath))
            {
                throw new InvalidOperationException($"The directory '{absTargetDirectoryPath.FullPath}' does not exist.");
            }

            // Iterate all files and copy them.
            foreach (var filePath in filePaths)
            {
                CopyFileCore(env, filePath, absTargetDirectoryPath.GetFilePath(filePath));
            }
        }
Example #6
0
        /// <summary>
        /// Copies the contents of a directory to the specified location.
        /// </summary>
        /// <example>
        /// <code>
        /// CopyDirectory("source_path", "destination_path");
        /// </code>
        /// </example>
        /// <param name="env">The context.</param>
        /// <param name="source">The source directory path.</param>
        /// <param name="destination">The destination directory path.</param>
        /// <exception cref="ArgumentNullException"><paramref name="env"/> or <paramref name="source"/>
        ///  or <paramref name="destination"/> is <see langword="null"/></exception>
        /// <exception cref="InvalidOperationException">Source directory does not exist or could not be found.</exception>
        public static void CopyDirectory(this IFileSystemEnvironment env, DirectoryPath source,
                                         DirectoryPath destination)
        {
            if (env == null)
            {
                throw new ArgumentNullException(nameof(env));
            }

            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (destination == null)
            {
                throw new ArgumentNullException(nameof(destination));
            }

            if (env.FS == null)
            {
                throw new InvalidOperationException(
                          $"Cannot check if directory exists when {nameof(env)}.{nameof(env.FS)} is null");
            }

            if (source.IsRelative)
            {
                source = source.MakeAbsolute(env);
            }

            // Get the subdirectories for the specified directory.
            var sourceDir = env.FS.GetDirectory(source);

            if (!sourceDir.Exists)
            {
                throw new InvalidOperationException("Source directory does not exist or could not be found: "
                                                    + source.FullPath);
            }

            var dirs = sourceDir.GetDirectories("*", SearchScope.Current);

            var destinationDir = env.FS.GetDirectory(destination);

            if (!destinationDir.Exists)
            {
                destinationDir.Create();
            }

            // Get the files in the directory and copy them to the new location.
            var files = sourceDir.GetFiles("*", SearchScope.Current);

            foreach (var file in files)
            {
                var temppath = destinationDir.Path.CombineWithFilePath(file.Path.GetFilename());
                file.Copy(temppath, true);
            }

            // Copy all of the subdirectories
            foreach (var subdir in dirs)
            {
                var temppath = destination.Combine(subdir.Path.GetDirectoryName());
                CopyDirectory(env, subdir.Path, temppath);
            }
        }