Exemple #1
0
        public override RenameResult Rename(RenamerInfo info, string oldName, string newName)
        {
            var message  = new StringBuilder($"{info.Path}: ");
            var hasError = false;

            if (info.IsIncluded)
            {
                try
                {
                    // TODO: centralize?
                    var newDirectoryName = ReplaceFilename(info.Path, oldName, newName);
                    if (CanReplacePath(info.Path, newDirectoryName))
                    {
                        Directory.Move(info.Path, newDirectoryName);
                        message.Append($"Renamed '{info.Path}' directory to '{newDirectoryName}'. ");
                        info.Path = newDirectoryName;
                    }
                    else
                    {
                        message.Append("Directory unchanged.");
                    }
                }
                catch (Exception e)
                {
                    message.Append($"Failed to rename '{info.Path}'. {e.ToString()}. ");
                    hasError = true;
                }
            }
            else
            {
                message.Append("Directory skipped.");
            }

            return(new RenameResult(message.ToString(), !hasError));
        }
        public bool IsSatisfiedBy(RenamerInfo info, IFileSystem fileSystem)
        {
            if (Next != null)
            {
                return(Next.IsSatisfiedBy(info, fileSystem) && _rule.Invoke(info, fileSystem));
            }

            return(_rule.Invoke(info, fileSystem));
        }
Exemple #3
0
        public void GetZipStartPath_GivenAFile_ReturnsParentDirectoryPath()
        {
            var info = new RenamerInfo {
                Path = Path.GetTempFileName(), FileType = FileType.Other
            };

            var result = FileSystemWrapper.GetZipStartPath(info);

            Assert.AreEqual(Path.GetTempPath(), result, "The parent directory was supposed to be returned.");
        }
Exemple #4
0
        public void GetItemCount_WithNoChildren_NotIncludingAllItems_WhenItemNotIncluded_ReturnsZero()
        {
            var root = new RenamerInfo {
                IsIncluded = false
            };

            var result = root.GetItemCount(false);

            Assert.AreEqual(0, result, "The count returned was incorrect.");
        }
        public void True_EnsureDefaultRuleAlwaysReturnsTrue()
        {
            var info       = new RenamerInfo {
            };
            var fileSystem = new Mock <IFileSystem>().Object;

            var result = RenamerRule.IsTrue.IsSatisfiedBy(info, fileSystem);

            Assert.IsTrue(result, "This rule is always meant to return true.");
        }
Exemple #6
0
        public void GetItemCount_WithNoChildren_AndIncludingAllItems_ReturnsCountOfItself()
        {
            var root = new RenamerInfo {
                IsIncluded = true
            };

            var result = root.GetItemCount(true);

            Assert.AreEqual(1, result, "The count returned was incorrect.");
        }
Exemple #7
0
        public void GetZipStartPath_GivenADirectoryPath_ReturnsParentDirectoryPath()
        {
            var info = new RenamerInfo {
                Path = Path.GetTempPath(), FileType = FileType.Directory
            };
            var expected = Path.GetDirectoryName(Path.GetTempPath()) + "\\";
            var result   = FileSystemWrapper.GetZipStartPath(info);

            Assert.AreEqual(expected, result, "The parent directory was supposed to be returned.");
        }
        public void IsSatisfiedBy_ForSingleRule_WithFalse_ReturnsFalse()
        {
            var info       = new RenamerInfo();
            var fileSystem = new Mock <IFileSystem>().Object;
            var rule1      = new RenamerRule("rule 1", (a, b) => false);

            var result = rule1.IsSatisfiedBy(info, fileSystem);

            Assert.IsFalse(result, "False rule is meant to be false");
        }
        public void NotBinOrObj_PathsWithPackagesShouldReturnFalse()
        {
            var info = new RenamerInfo {
                Path = @"c:\packages\asdf.exe"
            };
            var fileSystem = new Mock <IFileSystem>().Object;

            var result = RenamerRule.IsNotDll.IsSatisfiedBy(info, fileSystem);

            Assert.IsFalse(result, "Expected obj match to return false.");
        }
        public void NotGit_PathsWithGitShouldReturnFalse()
        {
            var info = new RenamerInfo {
                Path = @"c:\.git\"
            };
            var fileSystem = new Mock <IFileSystem>().Object;

            var result = RenamerRule.IsNotGit.IsSatisfiedBy(info, fileSystem);

            Assert.IsFalse(result, "Expected .Git match to return false.");
        }
        public void NotDll_PathsWithDllShouldReturnFalse()
        {
            var info = new RenamerInfo {
                Path = @"c:\asdf.dll"
            };
            var fileSystem = new Mock <IFileSystem>().Object;

            var result = RenamerRule.IsNotDll.IsSatisfiedBy(info, fileSystem);

            Assert.IsFalse(result, "Expected .dll match to return false.");
        }
        public void IsSatisfiedBy_ForMultipleRules_WithTrueAndTrue_ReturnsTrue()
        {
            var info       = new RenamerInfo();
            var fileSystem = new Mock <IFileSystem>().Object;
            var rule1      = new RenamerRule("rule 1", (a, b) => true);
            var rule2      = new RenamerRule("rule 2", (a, b) => true);

            rule1.Next = rule2;

            var result = rule1.IsSatisfiedBy(info, fileSystem);

            Assert.IsTrue(result, "True and true rules are meant to be true");
        }
        public void IsSatisfiedBy_ForMultipleRules_WithFalseAndFalse_ReturnsFalse()
        {
            var info       = new RenamerInfo();
            var fileSystem = new Mock <IFileSystem>().Object;
            var rule1      = new RenamerRule("rule 1", (a, b) => false);
            var rule2      = new RenamerRule("rule 2", (a, b) => false);

            rule1.Next = rule2;

            var result = rule1.IsSatisfiedBy(info, fileSystem);

            Assert.IsFalse(result, "False and false rules are meant to be false");
        }
Exemple #14
0
        public void ContainsSolutionFolder_WhenNoChildrenAreASolutionFile_ReturnsFalse()
        {
            var info = new RenamerInfo
            {
                Children = new List <RenamerInfo>
                {
                    new RenamerInfo {
                        Path = "path", FileType = FileType.Other
                    }
                }
            };

            Assert.IsFalse(info.ContainsSolutionFile, "The renamer info is not meant to contain a solution file.");
        }
Exemple #15
0
        public void ContainsSolutionFolder_WhenChildIsASolutionFile_ReturnsTrue()
        {
            var info = new RenamerInfo
            {
                Children = new List <RenamerInfo>
                {
                    new RenamerInfo {
                        Path = "path", FileType = FileType.Solution
                    }
                }
            };

            Assert.IsTrue(info.ContainsSolutionFile, "The renamer info is meant to contain a solution file.");
        }
Exemple #16
0
        public void GetItemCount_WithChildren_NotIncludingAllItems_WhenItemIncluded_ReturnsOneItemCount()
        {
            var root = new RenamerInfo
            {
                IsIncluded = true,
                Children   = new List <RenamerInfo>
                {
                    new RenamerInfo {
                        IsIncluded = false
                    }
                }
            };

            var result = root.GetItemCount(false);

            Assert.AreEqual(1, result, "The count returned was incorrect.");
        }
Exemple #17
0
        public void ContainsSolutionFolder_WhenMultipleSolutionFilesPresent_ReturnsFalse()
        {
            var info = new RenamerInfo
            {
                Children = new List <RenamerInfo>
                {
                    new RenamerInfo {
                        Path = "path1", FileType = FileType.Solution
                    },
                    new RenamerInfo {
                        Path = "path2", FileType = FileType.Solution
                    }
                }
            };

            Assert.IsFalse(info.ContainsSolutionFile, "The renamer info is not meant to contain multiple solution files.");
        }
Exemple #18
0
        public void GetItemCount_WithChildren_AndIncludingAllItems_ReturnsCountWithChildren()
        {
            var root = new RenamerInfo
            {
                IsIncluded = true,
                Children   = new List <RenamerInfo>
                {
                    new RenamerInfo {
                        IsIncluded = true
                    }
                }
            };

            var result = root.GetItemCount(true);

            Assert.AreEqual(2, result, "The count returned was incorrect.");
        }
Exemple #19
0
        private void RecursiveShowFiles(RenamerInfo root, ItemCollection items)
        {
            var label = new Label {
                Content = root.ToString()
            };

            label.Foreground = root.IsIncluded ? new SolidColorBrush(Colors.Black) : new SolidColorBrush(Colors.Gray);
            var treeViewItem = new TreeViewItem
            {
                Header     = label,
                IsExpanded = root.IsIncluded
            };

            items.Add(treeViewItem);

            foreach (var child in root.Children.OrderBy(i => i.FileType != FileType.Directory))
            {
                RecursiveShowFiles(child, treeViewItem.Items);
            }
        }
Exemple #20
0
 public abstract RenameResult Rename(RenamerInfo info, string oldName, string newName);
        public override RenameResult Rename(RenamerInfo info, string oldName, string newName)
        {
            var message     = new StringBuilder($"{info.Path}: ");
            var renameCount = 0;
            var hasError    = false;

            if (info.IsIncluded)
            {
                try
                {
                    // read every line, replacing the oldname with the new name
                    var fileContents = File.ReadAllLines(info.Path);
                    for (int i = 0; i < fileContents.Length; i++)
                    {
                        // TODO: fix up logic to be more precise
                        var line = fileContents[i];
                        renameCount    += Regex.Matches(line, oldName).Count;
                        fileContents[i] = Regex.Replace(fileContents[i], oldName, newName, RegexOptions.IgnoreCase);
                    }

                    if (renameCount > 0)
                    {
                        var pluralized = "occurrence" + (renameCount != 1 ? "s" : string.Empty);
                        message.Append($"Replaced {renameCount} {pluralized} of '{oldName}'. ");
                        File.WriteAllLines(info.Path, fileContents);
                    }
                    else
                    {
                        message.Append("Unchanged. ");
                    }
                }
                catch (Exception e)
                {
                    message.Append($"Failed to replace contents. {e.ToString()} ");
                    hasError = true;
                }

                if (!hasError)
                {
                    try
                    {
                        // TODO: centralize?
                        var newFilename = ReplaceFilename(info.Path, oldName, newName);
                        if (CanReplacePath(info.Path, newFilename))
                        {
                            File.Move(info.Path, newFilename);
                            message.Append($"Renamed '{info.Path}' file to '{newFilename}' ");
                            info.Path = newFilename;
                        }
                    }
                    catch (Exception e)
                    {
                        message.Append($"Failed to rename '{info.Path}'. {e.ToString()} ");
                        hasError = true;
                    }
                }
            }
            else
            {
                message.Append("skipped. ");
            }

            return(new RenameResult(message.ToString(), !hasError));
        }