コード例 #1
0
ファイル: FakeFileSystemTree.cs プロジェクト: stgwilli/cake
        public void DeleteDirectory(FakeDirectory fakeDirectory, bool recursive)
        {
            var root   = new Stack <FakeDirectory>();
            var result = new Stack <FakeDirectory>();

            if (fakeDirectory.Exists)
            {
                root.Push(fakeDirectory);
            }

            while (root.Count > 0)
            {
                var node = root.Pop();
                result.Push(node);

                foreach (var child in node.Content.Directories)
                {
                    root.Push(child.Value);
                }
            }

            while (result.Count > 0)
            {
                var directory = result.Pop();
                foreach (var file in directory.Content.Files.Select(x => x).ToArray())
                {
                    // Delete the file.
                    DeleteFile(file.Value);
                }

                // Delete the directory.
                directory.Parent.Content.Remove(directory);
                directory.Exists = false;
            }
        }
コード例 #2
0
ファイル: FakeFileSystemTree.cs プロジェクト: samvik/cake
        public FakeDirectory FindDirectory(DirectoryPath path)
        {
            // Want the root?
            if (path.Segments.Length == 0)
            {
                return(_root);
            }

            var queue = new Queue <string>(path.Segments);

            FakeDirectory current  = null;
            var           children = _root.Content;

            while (queue.Count > 0)
            {
                // Set the parent.
                var parent = current;

                // Calculate the current path.
                var segment = queue.Dequeue();
                path = parent != null?parent.Path.Combine(segment) : new DirectoryPath(segment);

                // Find the current path.
                if (!children.Directories.ContainsKey(path))
                {
                    return(null);
                }

                current  = children.Directories[path];
                children = current.Content;
            }

            return(current);
        }
コード例 #3
0
ファイル: FakeFileSystemTree.cs プロジェクト: samvik/cake
        public void DeleteDirectory(FakeDirectory fakeDirectory, bool recursive)
        {
            var root   = new Stack <FakeDirectory>();
            var result = new Stack <FakeDirectory>();

            if (fakeDirectory.Exists)
            {
                root.Push(fakeDirectory);
            }

            while (root.Count > 0)
            {
                var node = root.Pop();
                result.Push(node);

                var directories = node.Content.Directories;

                if (directories.Count > 0 && !recursive)
                {
                    throw new IOException("The directory is not empty.");
                }

                foreach (var child in directories)
                {
                    root.Push(child.Value);
                }
            }

            while (result.Count > 0)
            {
                var directory = result.Pop();

                var files = directory.Content.Files.Select(x => x).ToArray();
                if (files.Length > 0 && !recursive)
                {
                    throw new IOException("The directory is not empty.");
                }

                foreach (var file in files)
                {
                    // Delete the file.
                    DeleteFile(file.Value);
                }

                // Delete the directory.
                directory.Parent.Content.Remove(directory);
                directory.Exists = false;
            }
        }
コード例 #4
0
        private static IEnumerable <FakeFile> GetFiles(FakeDirectory current, Regex exression)
        {
            var result = new List <FakeFile>();

            foreach (var file in current.Content.Files)
            {
                if (file.Value.Exists)
                {
                    // Is this a match? In that case, add it to the result.
                    if (exression.IsMatch(file.Key.GetFilename().FullPath))
                    {
                        result.Add(current.Content.Files[file.Key]);
                    }
                }
            }
            return(result);
        }
コード例 #5
0
ファイル: FakeFileSystemTree.cs プロジェクト: samvik/cake
        public FakeDirectory CreateDirectory(FakeDirectory directory)
        {
            var path  = directory.Path;
            var queue = new Queue <string>(path.Segments);

            FakeDirectory current  = null;
            var           children = _root.Content;

            while (queue.Count > 0)
            {
                // Get the segment.
                var currentSegment = queue.Dequeue();
                var parent         = current;

                // Calculate the current path.
                path = parent != null?parent.Path.Combine(currentSegment) : new DirectoryPath(currentSegment);

                if (!children.Directories.ContainsKey(path))
                {
                    if (queue.Count == 0)
                    {
                        // Use the provided directory.
                        current = directory;
                    }
                    else
                    {
                        current = new FakeDirectory(this, path);
                    }

                    current.Parent = parent;
                    current.Hidden = false;
                    children.Add(current);
                }
                else
                {
                    current = children.Directories[path];
                }

                current.Exists = true;
                children       = current.Content;
            }

            return(directory);
        }
コード例 #6
0
ファイル: FakeFileSystemTree.cs プロジェクト: samvik/cake
        public FakeFileSystemTree(ICakeEnvironment 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();
        }
コード例 #7
0
 public void Remove(FakeDirectory directory)
 {
     _directories.Remove(directory.Path);
 }
コード例 #8
0
 public void Add(FakeDirectory directory)
 {
     _directories.Add(directory.Path, directory);
 }
コード例 #9
0
 public FakeDirectoryContent(FakeDirectory owner, PathComparer comparer)
 {
     _owner       = owner;
     _directories = new Dictionary <DirectoryPath, FakeDirectory>(comparer);
     _files       = new Dictionary <FilePath, FakeFile>(comparer);
 }