コード例 #1
0
        /// <summary>
        /// Gets the size of a file in bytes.
        /// </summary>
        /// <param name="env">The context.</param>
        /// <param name="filePath">The path.</param>
        /// <returns>Size of file in bytes or -1 if file doesn't exist.</returns>
        /// <example>
        /// <code>
        /// Information("File size: {0}", FileSize("./build.cake"));
        /// </code>
        /// </example>
        /// <exception cref="FileNotFoundException">Unable to find the specified file.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="env"/> or <paramref name="filePath"/>
        ///  is <see langword="null"/></exception>
        /// <exception cref="InvalidOperationException">Cannot get file size when
        ///  <paramref name="env.FS"/> is null.</exception>
        public static long FileSize(this IFileSystemEnvironment env, FilePath filePath)
        {
            if (env == null)
            {
                throw new ArgumentNullException(nameof(env));
            }

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

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

            var file = env.FS.GetFile(filePath.MakeAbsolute(env));

            if (!file.Exists)
            {
                throw new FileNotFoundException("Unable to find the specified file.", filePath.FullPath);
            }

            return(file.Length);
        }
コード例 #2
0
        /// <exception cref="ArgumentNullException"><paramref name="env"/> or <paramref name="filePath"/> or
        ///  <paramref name="targetFilePath"/> 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 CopyFile(IFileSystemEnvironment env, FilePath filePath, FilePath targetFilePath)
        {
            if (env == null)
            {
                throw new ArgumentNullException(nameof(env));
            }
            if (filePath == null)
            {
                throw new ArgumentNullException(nameof(filePath));
            }
            if (targetFilePath == null)
            {
                throw new ArgumentNullException(nameof(targetFilePath));
            }

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

            var targetDirectoryPath = targetFilePath.GetDirectory().MakeAbsolute(env);

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

            CopyFileCore(env, filePath, targetFilePath);
        }
コード例 #3
0
ファイル: FileDeleter.cs プロジェクト: EnoughTea/essentions
        /// <exception cref="ArgumentNullException"><paramref name="env"/> or <paramref name="filePath"/>
        ///  is <see langword="null"/></exception>
        /// <exception cref="FileNotFoundException">The file <paramref name="filePath"/> does not exist.</exception>
        /// <exception cref="InvalidOperationException">Cannot delete files when <paramref name="env.FS"/> is null.
        /// </exception>
        public static void DeleteFile(IFileSystemEnvironment env, FilePath filePath)
        {
            if (env == null)
            {
                throw new ArgumentNullException(nameof(env));
            }
            if (filePath == null)
            {
                throw new ArgumentNullException(nameof(filePath));
            }

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

            filePath = filePath.MakeAbsolute(env);

            var file = env.FS.GetFile(filePath);

            if (!file.Exists)
            {
                throw new FileNotFoundException($"The file '{filePath.FullPath}' does not exist.", filePath.FullPath);
            }

            file.Delete();
        }
コード例 #4
0
        private void PrepareUnixFixture()
        {
            Environment    = FakeEnvironment.CreateUnixEnvironment();
            Environment.FS = new FakeFileSystem(Environment);

            // Directories
            FileSystem.CreateDirectory("/Working");
            FileSystem.CreateDirectory("/Working/Foo");
            FileSystem.CreateDirectory("/Working/Foo/Bar");
            FileSystem.CreateDirectory("/Working/Bar");
            FileSystem.CreateDirectory("/Foo/Bar");
            FileSystem.CreateDirectory("/Foo (Bar)");

            // Files
            FileSystem.CreateFile("/Working/Foo/Bar/Qux.c");
            FileSystem.CreateFile("/Working/Foo/Bar/Qex.c");
            FileSystem.CreateFile("/Working/Foo/Bar/Qux.h");
            FileSystem.CreateFile("/Working/Foo/Baz/Qux.c");
            FileSystem.CreateFile("/Working/Foo/Bar/Baz/Qux.c");
            FileSystem.CreateFile("/Working/Bar/Qux.c");
            FileSystem.CreateFile("/Working/Bar/Qux.h");
            FileSystem.CreateFile("/Working/Foo.Bar.Test.dll");
            FileSystem.CreateFile("/Working/Bar.Qux.Test.dll");
            FileSystem.CreateFile("/Working/Quz.FooTest.dll");
            FileSystem.CreateFile("/Foo/Bar.baz");
            FileSystem.CreateFile("/Foo (Bar)/Baz.c");
        }
コード例 #5
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();
            }
        }
コード例 #6
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);
        }
コード例 #7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PathComparer"/> class.
        /// </summary>
        /// <param name="environment">The environment.</param>
        /// <exception cref="ArgumentNullException"><paramref name="environment"/> is <see langword="null"/></exception>
        public PathComparer(IFileSystemEnvironment environment)
        {
            if (environment == null)
            {
                throw new ArgumentNullException(nameof(environment));
            }

            IsCaseSensitive = environment.IsUnix();
        }
コード例 #8
0
ファイル: DirectoryPath.cs プロジェクト: EnoughTea/essentions
 /// <summary>
 /// Makes the path absolute (if relative) using the current working directory.
 /// </summary>
 /// <param name="environment">The environment.</param>
 /// <returns>An absolute path.</returns>
 /// <exception cref="ArgumentNullException"><paramref name="environment"/> is <see langword="null"/></exception>
 public DirectoryPath MakeAbsolute(IFileSystemEnvironment environment)
 {
     if (environment == null)
     {
         throw new ArgumentNullException(nameof(environment));
     }
     return(IsRelative
         ? environment.WorkingDirectory.Combine(this).Collapse()
         : new DirectoryPath(FullPath));
 }
コード例 #9
0
 public GlobVisitorContext(
     IFileSystem fileSystem,
     IFileSystemEnvironment environment,
     Func <IDirectory, bool> predicate)
 {
     FileSystem  = fileSystem;
     Environment = environment;
     _predicate  = predicate;
     Results     = new List <IFileSystemInfo>();
     _pathParts  = new LinkedList <string>();
 }
コード例 #10
0
 /// <summary>
 /// Cleans the specified directories.
 /// Cleaning a directory will remove all it's content but not the directory itself.
 /// </summary>
 /// <example>
 /// <code>
 /// var directoriesToClean = GetDirectories("./src/**/bin/");
 /// CleanDirectories(directoriesToClean);
 /// </code>
 /// </example>
 /// <param name="env">The context.</param>
 /// <param name="directories">The directory paths.</param>
 /// <exception cref="ArgumentNullException"><paramref name="directories"/> is <see langword="null"/>
 /// </exception>
 /// <exception cref="InvalidOperationException">Cannot clean directory when <paramref name="env.FS"/> is null.
 /// </exception>
 public static void CleanDirectories(this IFileSystemEnvironment env, IEnumerable <DirectoryPath> directories)
 {
     if (directories == null)
     {
         throw new ArgumentNullException(nameof(directories));
     }
     foreach (var directory in directories)
     {
         CleanDirectory(env, directory);
     }
 }
コード例 #11
0
        /// <summary>
        /// Cleans the directories matching the specified pattern.
        /// Cleaning the directory will remove all it's content but not the directory itself.
        /// </summary>
        /// <example>
        /// <code>
        /// CleanDirectories("./src/**/bin/debug");
        /// </code>
        /// </example>
        /// <param name="env">The context.</param>
        /// <param name="pattern">The pattern to match.</param>
        /// <exception cref="InvalidOperationException">Cannot clean directory when <paramref name="env.FS"/> is null.
        /// </exception>
        public static void CleanDirectories(this IFileSystemEnvironment env, string pattern)
        {
            var directories = env.GetDirectories(pattern);

            if (directories.Count == 0)
            {
                return;
            }

            CleanDirectories(env, directories);
        }
コード例 #12
0
        /// <summary>
        /// Cleans the directories matching the specified pattern.
        /// Cleaning the directory will remove all it's content but not the directory itself.
        /// </summary>
        /// <example>
        /// <code>
        /// Func&lt;IFileSystemInfo, bool&gt; exclude_node_modules =
        /// fileSystemInfo=>!fileSystemInfo.Path.FullPath.EndsWith(
        ///                 "node_modules",
        ///                 StringComparison.OrdinalIgnoreCase);
        /// CleanDirectories("./src/**/bin/debug", exclude_node_modules);
        /// </code>
        /// </example>
        /// <param name="env">The context.</param>
        /// <param name="pattern">The pattern to match.</param>
        /// <param name="predicate">The predicate used to filter directories based on file system information.</param>
        /// <exception cref="InvalidOperationException">Cannot clean directory when <paramref name="env.FS"/> is null.
        /// </exception>
        public static void CleanDirectories(this IFileSystemEnvironment env, string pattern,
                                            Func <IFileSystemInfo, bool> predicate)
        {
            var directories = env.GetDirectories(pattern, predicate);

            if (directories.Count == 0)
            {
                return;
            }

            CleanDirectories(env, directories);
        }
コード例 #13
0
 /// <summary>
 /// Gets a file path from string.
 /// </summary>
 /// <example>
 /// <code>
 /// // Get the temp file.
 /// var root = Directory("./");
 /// var temp = root + File("temp");
 /// // Delete the file.
 /// CleanDirectory(temp);
 /// </code>
 /// </example>
 /// <param name="env">The context.</param>
 /// <param name="path">The path.</param>
 /// <returns>A file path.</returns>
 /// <exception cref="ArgumentNullException"><paramref name="env"/> or <paramref name="path"/>
 ///  is <see langword="null"/></exception>
 public static ConvertableFilePath File(this IFileSystemEnvironment env, string path)
 {
     if (env == null)
     {
         throw new ArgumentNullException(nameof(env));
     }
     if (path == null)
     {
         throw new ArgumentNullException(nameof(path));
     }
     return(new ConvertableFilePath(new FilePath(path)));
 }
コード例 #14
0
ファイル: Globber.cs プロジェクト: EnoughTea/essentions
        /// <summary>
        /// Initializes a new instance of the <see cref="Globber"/> class.
        /// </summary>
        /// <param name="env">The environment.</param>
        /// <exception cref="ArgumentNullException"><paramref name="env"/> is <see langword="null"/></exception>
        public Globber(IFileSystemEnvironment env)
        {
            if (env == null)
            {
                throw new ArgumentNullException(nameof(env));
            }

            _env      = env;
            _parser   = new GlobParser(env);
            _visitor  = new GlobVisitor(env);
            _comparer = new PathComparer(env.IsUnix());
        }
コード例 #15
0
        /// <summary>
        /// Copies existing files to a new location.
        /// </summary>
        /// <param name="env">The context.</param>
        /// <param name="filePaths">The file paths.</param>
        /// <param name="targetDirectoryPath">The target directory path.</param>
        /// <example>
        /// <code>
        /// CreateDirectory("destination");
        /// var files = new [] {
        ///     "Cake.exe",
        ///     "Cake.pdb"
        /// };
        /// CopyFiles(files, "destination");
        /// </code>
        /// </example>
        /// <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(this IFileSystemEnvironment env, IEnumerable <string> filePaths,
                                     DirectoryPath targetDirectoryPath)
        {
            if (filePaths == null)
            {
                throw new ArgumentNullException(nameof(filePaths));
            }

            var paths = filePaths.Select(p => new FilePath(p));

            FileCopier.CopyFiles(env, paths, targetDirectoryPath);
        }
コード例 #16
0
        /// <summary>
        /// Cleans the specified directories.
        /// Cleaning a directory will remove all it's content but not the directory itself.
        /// </summary>
        /// <example>
        /// <code>
        /// var directoriesToClean = new []{
        ///     "./src/Cake/obj",
        ///     "./src/Cake.Common/obj"
        /// };
        /// CleanDirectories(directoriesToClean);
        /// </code>
        /// </example>
        /// <param name="env">The context.</param>
        /// <param name="directories">The directory paths.</param>
        /// <exception cref="ArgumentNullException"><paramref name="directories"/> is <see langword="null"/></exception>
        public static void CleanDirectories(this IFileSystemEnvironment env, IEnumerable <string> directories)
        {
            if (directories == null)
            {
                throw new ArgumentNullException(nameof(directories));
            }
            var paths = directories.Select(p => new DirectoryPath(p));

            foreach (var directory in paths)
            {
                CleanDirectory(env, directory);
            }
        }
コード例 #17
0
        /// <summary>
        /// Deletes the specified directories.
        /// </summary>
        /// <example>
        /// <code>
        /// var directoriesToDelete = new DirectoryPath[]{
        ///     Directory("be"),
        ///     Directory("gone")
        /// };
        /// DeleteDirectories(directoriesToDelete, recursive:true);
        /// </code>
        /// </example>
        /// <param name="env">The context.</param>
        /// <param name="directories">The directory paths.</param>
        /// <param name="recursive">Will perform a recursive delete if set to <c>true</c>.</param>
        /// <exception cref="ArgumentNullException"><paramref name="directories"/> is <see langword="null"/></exception>
        /// <exception cref="System.IO.IOException">The directory does not exist. -or-
        ///  Cannot delete directory without recursion since it's not empty.
        /// </exception>
        public static void DeleteDirectories(this IFileSystemEnvironment env,
                                             IEnumerable <DirectoryPath> directories, bool recursive = false)
        {
            if (directories == null)
            {
                throw new ArgumentNullException(nameof(directories));
            }

            foreach (var directory in directories)
            {
                DeleteDirectory(env, directory, recursive);
            }
        }
コード例 #18
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));
        }
コード例 #19
0
        /// <summary>
        /// Makes the path absolute (if relative) using the current working directory.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="path">The path.</param>
        /// <returns>An absolute file path.</returns>
        /// <example>
        /// <code>
        /// var path = MakeAbsolute(File("./resources"));
        /// </code>
        /// </example>
        /// <exception cref="ArgumentNullException"><paramref name="context"/> or <paramref name="path"/>
        ///  is <see langword="null"/></exception>
        public static FilePath MakeAbsolute(this IFileSystemEnvironment context, FilePath path)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

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

            return(path.MakeAbsolute(context));
        }
コード例 #20
0
        /// <summary>
        /// Deletes the specified directories.
        /// </summary>
        /// <example>
        /// <code>
        /// var directoriesToDelete = new []{
        ///     "be",
        ///     "gone"
        /// };
        /// DeleteDirectories(directoriesToDelete, recursive:true);
        /// </code>
        /// </example>
        /// <param name="env">The context.</param>
        /// <param name="directories">The directory paths.</param>
        /// <param name="recursive">Will perform a recursive delete if set to <c>true</c>.</param>
        /// <exception cref="ArgumentNullException"><paramref name="directories"/> is <see langword="null"/></exception>
        /// <exception cref="System.IO.IOException">The directory does not exist. -or-
        ///  Cannot delete directory without recursion since it's not empty.
        /// </exception>
        public static void DeleteDirectories(this IFileSystemEnvironment env, IEnumerable <string> directories,
                                             bool recursive = false)
        {
            if (directories == null)
            {
                throw new ArgumentNullException(nameof(directories));
            }

            var paths = directories.Select(p => new DirectoryPath(p));

            foreach (var directory in paths)
            {
                DeleteDirectory(env, directory, recursive);
            }
        }
コード例 #21
0
ファイル: FileDeleter.cs プロジェクト: EnoughTea/essentions
        /// <exception cref="ArgumentNullException"><paramref name="env"/> or <paramref name="filePaths"/>
        ///  is <see langword="null"/></exception>
        /// <exception cref="FileNotFoundException">The file does not exist.</exception>
        /// <exception cref="InvalidOperationException">Cannot delete files when <paramref name="env.FS"/> is null.
        /// </exception>
        public static void DeleteFiles(IFileSystemEnvironment env, IEnumerable <FilePath> filePaths)
        {
            if (env == null)
            {
                throw new ArgumentNullException(nameof(env));
            }
            if (filePaths == null)
            {
                throw new ArgumentNullException(nameof(filePaths));
            }

            foreach (var filePath in filePaths)
            {
                DeleteFile(env, filePath);
            }
        }
コード例 #22
0
        /// <summary>
        /// Gets all files matching the specified pattern.
        /// </summary>
        /// <example>
        /// <code>
        /// Func&lt;IFileSystemInfo, bool&gt; exclude_node_modules =
        /// fileSystemInfo=>!fileSystemInfo.Path.FullPath.EndsWith(
        ///                 "node_modules",
        ///                 StringComparison.OrdinalIgnoreCase);
        /// var files = GetFiles("./**/Cake.*.dll", exclude_node_modules);
        /// foreach(var file in files)
        /// {
        ///     Information("File: {0}", file);
        /// }
        /// </code>
        /// </example>
        /// <param name="env">The context.</param>
        /// <param name="pattern">The glob pattern to match.</param>
        /// <param name="predicate">The predicate used to filter files based on file system information.</param>
        /// <returns>A <see cref="FilePathCollection" />.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="env"/> is <see langword="null"/></exception>
        /// <exception cref="InvalidOperationException">Cannot get files by pattern when
        ///  <paramref name="env.Globber"/> is null.</exception>
        public static FilePathCollection GetFiles(this IFileSystemEnvironment env, string pattern, Func <IFileSystemInfo, bool> predicate)
        {
            if (env == null)
            {
                throw new ArgumentNullException(nameof(env));
            }

            if (env.Globber == null)
            {
                throw new InvalidOperationException(
                          $"Cannot get files by pattern when {nameof(env)}.{nameof(env.Globber)} is null.");
            }

            return(new FilePathCollection(env.Globber.Match(pattern, predicate).OfType <FilePath>(),
                                          new PathComparer(env.IsUnix())));
        }
コード例 #23
0
        /// <summary>
        /// Gets all directory matching the specified pattern.
        /// </summary>
        /// <example>
        /// <code>
        /// var directories = GetDirectories("./src/**/obj/*");
        /// foreach(var directory in directories)
        /// {
        ///     Information("Directory: {0}", directory);
        /// }
        /// </code>
        /// </example>
        /// <param name="env">The context.</param>
        /// <param name="pattern">The glob pattern to match.</param>
        /// <returns>A <see cref="DirectoryPathCollection" />.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="env"/> is <see langword="null"/></exception>
        /// <exception cref="InvalidOperationException">Cannot get files by pattern when
        ///  <paramref name="env.Globber"/> is null.</exception>
        public static DirectoryPathCollection GetDirectories(this IFileSystemEnvironment env, string pattern)
        {
            if (env == null)
            {
                throw new ArgumentNullException(nameof(env));
            }

            if (env.Globber == null)
            {
                throw new InvalidOperationException(
                          $"Cannot get files by pattern when {nameof(env)}.{nameof(env.Globber)} is null.");
            }

            return(new DirectoryPathCollection(env.Globber.Match(pattern).OfType <DirectoryPath>(),
                                               new PathComparer(env.IsUnix())));
        }
コード例 #24
0
        /// <exception cref="ArgumentNullException"><paramref name="env"/> or <paramref name="filePath"/> or
        ///  <paramref name="targetDirectoryPath"/>is <see langword="null"/></exception>
        /// <exception cref="FileNotFoundException">The target directory do not exist..</exception>
        public static void MoveFileToDirectory(IFileSystemEnvironment env, FilePath filePath, DirectoryPath targetDirectoryPath)
        {
            if (env == null)
            {
                throw new ArgumentNullException(nameof(env));
            }
            if (filePath == null)
            {
                throw new ArgumentNullException(nameof(filePath));
            }
            if (targetDirectoryPath == null)
            {
                throw new ArgumentNullException(nameof(targetDirectoryPath));
            }

            MoveFile(env, filePath, targetDirectoryPath.GetFilePath(filePath));
        }
コード例 #25
0
        /// <exception cref="ArgumentNullException"><paramref name="env"/> or <paramref name="pattern"/> or
        ///  is <see langword="null"/></exception>
        /// <exception cref="FileNotFoundException">The file does not exist.</exception>
        /// <exception cref="InvalidOperationException">The target directory does not exist.</exception>
        public static void CopyFiles(IFileSystemEnvironment env, string pattern, DirectoryPath targetDirectoryPath)
        {
            if (env == null)
            {
                throw new ArgumentNullException(nameof(env));
            }
            if (pattern == null)
            {
                throw new ArgumentNullException(nameof(pattern));
            }
            var files = env.GetFiles(pattern);

            if (files.Count == 0)
            {
                return;
            }

            CopyFiles(env, files, targetDirectoryPath);
        }
コード例 #26
0
        public FakeFileSystemTree(IFileSystemEnvironment environment)
        {
            if (environment == null)
            {
                throw new ArgumentNullException("environment");
            }
            if (environment.WorkingDirectory == null)
            {
                throw new ArgumentException("Working directory not set.");
            }
            if (environment.WorkingDirectory.IsRelative)
            {
                throw new ArgumentException("Working directory cannot be relative.");
            }
            Comparer = new PathComparer(environment.IsUnix());

            _root = new FakeDirectory(this, "/");
            _root.Create();
        }
コード例 #27
0
        /// <summary>
        /// Determines whether the given path refers to an existing directory.
        /// </summary>
        /// <example>
        /// <code>
        /// var dir = "publish";
        /// if (!DirectoryExists(dir))
        /// {
        ///     CreateDirectory(dir);
        /// }
        /// </code>
        /// </example>
        /// <param name="env">The context.</param>
        /// <param name="path">The <see cref="DirectoryPath"/> to check.</param>
        /// <returns><c>true</c> if <paramref name="path"/> refers to an existing directory;
        /// <c>false</c> if the directory does not exist or an error occurs when trying to
        /// determine if the specified path exists.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="env"/> or <paramref name="path"/> is
        ///  <see langword="null"/></exception>
        /// <exception cref="InvalidOperationException">Cannot check if directory exists when
        ///  <paramref name="env.FS"/> is null.</exception>
        public static bool DirectoryExists(this 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 check if directory exists when {nameof(env)}.{nameof(env.FS)} is null");
            }

            return(env.FS.GetDirectory(path).Exists);
        }
コード例 #28
0
        /// <summary>
        /// Determines whether the given path refers to an existing file.
        /// </summary>
        /// <param name="env">The context.</param>
        /// <param name="filePath">The <see cref="FilePath"/> to check.</param>
        /// <returns><c>true</c> if <paramref name="filePath"/> refers to an existing file;
        /// <c>false</c> if the file does not exist or an error occurs when trying to
        /// determine if the specified file exists.</returns>
        /// <example>
        /// <code>
        /// if (FileExists("findme.txt"))
        /// {
        ///     Information("File exists!");
        /// }
        /// </code>
        /// </example>
        /// <exception cref="ArgumentNullException"><paramref name="env"/> or <paramref name="filePath"/>
        ///  is <see langword="null"/></exception>
        /// <exception cref="InvalidOperationException">Cannot check file for existance when
        ///  <paramref name="env.FS"/> is null.</exception>
        public static bool FileExists(this IFileSystemEnvironment env, FilePath filePath)
        {
            if (env == null)
            {
                throw new ArgumentNullException(nameof(env));
            }

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

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

            return(env.FS.GetFile(filePath.MakeAbsolute(env)).Exists);
        }
コード例 #29
0
ファイル: FileDeleter.cs プロジェクト: EnoughTea/essentions
        /// <exception cref="ArgumentNullException"><paramref name="env"/> or <paramref name="pattern"/>
        ///  is <see langword="null"/></exception>
        /// <exception cref="FileNotFoundException">The file does not exist.</exception>
        /// <exception cref="InvalidOperationException">Cannot delete files when <paramref name="env.FS"/> is null.
        /// </exception>
        public static void DeleteFiles(IFileSystemEnvironment env, string pattern)
        {
            if (env == null)
            {
                throw new ArgumentNullException(nameof(env));
            }
            if (pattern == null)
            {
                throw new ArgumentNullException(nameof(pattern));
            }

            var files = env.GetFiles(pattern);

            if (files.Count == 0)
            {
                return;
            }

            DeleteFiles(env, files);
        }
コード例 #30
0
        private void PrepareWindowsFixture()
        {
            Environment    = FakeEnvironment.CreateWindowsEnvironment();
            Environment.FS = new FakeFileSystem(Environment);

            // Directories
            FileSystem.CreateDirectory("C://Working");
            FileSystem.CreateDirectory("C://Working/Foo");
            FileSystem.CreateDirectory("C://Working/Foo/Bar");
            FileSystem.CreateDirectory("C:");
            FileSystem.CreateDirectory("C:/Program Files (x86)");

            // Files
            FileSystem.CreateFile("C:/Working/Foo/Bar/Qux.c");
            FileSystem.CreateFile("C:/Program Files (x86)/Foo.c");
            FileSystem.CreateFile("C:/Working/Project.A.Test.dll");
            FileSystem.CreateFile("C:/Working/Project.B.Test.dll");
            FileSystem.CreateFile("C:/Working/Project.IntegrationTest.dll");
            FileSystem.CreateFile("C:/Tools & Services/MyTool.dll");
            FileSystem.CreateFile("C:/Tools + Services/MyTool.dll");
        }