internal override void ExecuteInternal() { string originalFilePath = GetFilePath(); if (!File.Exists(originalFilePath)) { HandleFileNotFound(originalFilePath, MergeConfiguration.Suffix); return; } var source = File.ReadAllLines(originalFilePath).ToList(); var merge = File.ReadAllLines(Config.FilePath).ToList(); IEnumerable <string> result = source.Merge(merge, out string errorLine); if (errorLine != string.Empty) { HandleLineNotFound(originalFilePath, errorLine); return; } Fs.EnsureFileEditable(originalFilePath); File.WriteAllLines(originalFilePath, result, Encoding.UTF8); File.Delete(Config.FilePath); }
public void EnsureFileEditable_FilePathNullOrEmpty_ShouldLogError(string filePath) { _logDate = DateTime.Now; Fs.EnsureFileEditable(filePath); Assert.True(_fixture.IsErrorMessageInLogFile(_logDate, ErrorLevel, ErrorMessage)); }
public void EnsureFileEditable_FileDoesNotExist_ShouldLogError() { var testScenarioName = "FileDoesNotExist"; string filePath = $"{_testFolder}\\{testScenarioName}"; _logDate = DateTime.Now; Fs.EnsureFileEditable(filePath); Assert.True(_fixture.IsErrorMessageInLogFile(_logDate, ErrorLevel, ErrorMessage)); }
public override void Execute() { string originalFilePath = GetFilePath(); if (!File.Exists(originalFilePath)) { if (_config.FailOnError) { throw new FileNotFoundException(string.Format(StringRes.MergeFileNotFoundExceptionMessage, _config.FilePath)); } else { AddFailedMergePostActionsFileNotFound(originalFilePath); File.Delete(_config.FilePath); return; } } var source = File.ReadAllLines(originalFilePath).ToList(); var merge = File.ReadAllLines(_config.FilePath).ToList(); IEnumerable <string> result = source.HandleRemovals(merge); result = result.Merge(merge.RemoveRemovals(), out string errorLine); if (errorLine != string.Empty) { if (_config.FailOnError) { throw new InvalidDataException(string.Format(StringRes.MergeLineNotFoundExceptionMessage, errorLine, originalFilePath)); } else { AddFailedMergePostActionsAddLineNotFound(originalFilePath, errorLine); } } else { Fs.EnsureFileEditable(originalFilePath); File.WriteAllLines(originalFilePath, result, Encoding.UTF8); // REFRESH PROJECT TO UN-DIRTY IT if ((Path.GetExtension(_config.FilePath).Equals(".csproj", StringComparison.OrdinalIgnoreCase) || Path.GetExtension(_config.FilePath).Equals(".vbproj", StringComparison.OrdinalIgnoreCase)) && (GenContext.Current.OutputPath == GenContext.Current.ProjectPath)) { Gen.GenContext.ToolBox.Shell.RefreshProject(); } } File.Delete(_config.FilePath); }
internal override void ExecuteInternal() { string originalFilePath = GetFilePath(); if (!File.Exists(originalFilePath)) { if (Config.FailOnError) { throw new FileNotFoundException(string.Format(StringRes.MergeFileNotFoundExceptionMessage, Config.FilePath, RelatedTemplate)); } else { AddFailedMergePostActionsFileNotFound(originalFilePath); File.Delete(Config.FilePath); return; } } var source = File.ReadAllLines(originalFilePath).ToList(); var merge = File.ReadAllLines(Config.FilePath).ToList(); IEnumerable <string> result = source.Merge(merge, out string errorLine); if (errorLine != string.Empty) { if (Config.FailOnError) { throw new InvalidDataException(string.Format(StringRes.MergeLineNotFoundExceptionMessage, errorLine, originalFilePath, RelatedTemplate)); } else { AddFailedMergePostActionsAddLineNotFound(originalFilePath, errorLine); } } else { Fs.EnsureFileEditable(originalFilePath); File.WriteAllLines(originalFilePath, result, Encoding.UTF8); // REFRESH PROJECT TO UN-DIRTY IT if (Path.GetExtension(Config.FilePath).EndsWith("proj", StringComparison.OrdinalIgnoreCase) && GenContext.Current.OutputPath == GenContext.Current.DestinationPath) { Gen.GenContext.ToolBox.Shell.RefreshProject(); } } File.Delete(Config.FilePath); }
public void EnsureFileEditable_FileIsNotReadOnly_ShouldNotModifyIsReadOnly() { var testScenarioName = "FileIsNotReadOnly"; string filePath = $"{_testFolder}\\{testScenarioName}"; FSTestsFixture.CreateFiles(_testFolder, new List <string> { testScenarioName }); var originalFileInfo = new FileInfo(filePath); Fs.EnsureFileEditable(filePath); var newFileInfo = new FileInfo(filePath); Assert.False(originalFileInfo.IsReadOnly); Assert.False(newFileInfo.IsReadOnly); }
internal override void ExecuteInternal() { string originalFilePath = Regex.Replace(Config.FilePath, MergeConfiguration.PostactionRegex, "."); if (!File.Exists(originalFilePath)) { HandleFileNotFound(originalFilePath, MergeConfiguration.Suffix); return; } var source = File.ReadAllLines(originalFilePath).ToList(); var merge = File.ReadAllLines(Config.FilePath).ToList(); var originalEncoding = GetEncoding(originalFilePath); // Only check encoding on new project, might have changed on right click if (GenContext.Current.GenerationOutputPath == GenContext.Current.DestinationPath) { var otherEncoding = GetEncoding(Config.FilePath); if (originalEncoding.EncodingName != otherEncoding.EncodingName || !Enumerable.SequenceEqual(originalEncoding.GetPreamble(), otherEncoding.GetPreamble())) { HandleMismatchedEncodings(originalFilePath, Config.FilePath, originalEncoding, otherEncoding); return; } } var mergeHandler = new MergeHandler(Config.CodeStyleProvider); var result = mergeHandler.Merge(source, merge); if (!result.Success) { HandleLineNotFound(originalFilePath, result.ErrorLine); return; } Fs.EnsureFileEditable(originalFilePath); File.WriteAllLines(originalFilePath, result.Result, originalEncoding); File.Delete(Config.FilePath); }
internal override void ExecuteInternal() { string originalFilePath = GetFilePath(); if (!File.Exists(originalFilePath)) { HandleFileNotFound(originalFilePath, MergeConfiguration.Suffix); return; } var source = File.ReadAllLines(originalFilePath).ToList(); var merge = File.ReadAllLines(Config.FilePath).ToList(); var originalEncoding = GetEncoding(originalFilePath); // Only check encoding on new project, might have changed on right click if (GenContext.Current.GenerationOutputPath == GenContext.Current.DestinationPath) { var otherEncoding = GetEncoding(Config.FilePath); if (originalEncoding.EncodingName != otherEncoding.EncodingName || !Enumerable.SequenceEqual(originalEncoding.GetPreamble(), otherEncoding.GetPreamble())) { HandleMismatchedEncodings(originalFilePath, Config.FilePath, originalEncoding, otherEncoding); return; } } IEnumerable <string> result = source.Merge(merge, out string errorLine); if (errorLine != string.Empty) { HandleLineNotFound(originalFilePath, errorLine); return; } Fs.EnsureFileEditable(originalFilePath); File.WriteAllLines(originalFilePath, result, originalEncoding); File.Delete(Config.FilePath); }
public void EnsureFileEditable_FileIsReadOnly_ShouldChangeToReadOnly() { var testScenarioName = "FileIsReadOnly"; var fileToEdit = $"{_testFolder}\\{testScenarioName}"; try { FSTestsFixture.CreateFiles(_testFolder, new List <string> { testScenarioName }, true); Fs.EnsureFileEditable(fileToEdit); var newFileInfo = new FileInfo(fileToEdit); Assert.False(newFileInfo.IsReadOnly); } finally { _ = new FileInfo(fileToEdit) { IsReadOnly = false, }; } }