/// <summary> /// Applies the document actions to the given set of documents. /// </summary> public async Task ApplyDocumentActionsAsync(IEnumerable <string> paths) { if (paths == null) { throw new ArgumentNullException(nameof(paths)); } var remainingPaths = new HashSet <string>(paths); try { var activeWindow = _dte.ActiveWindow; foreach (var path in paths) { await ApplyDocumentActionsAsync(path); remainingPaths.Remove(path); } if (activeWindow != null) { activeWindow.Activate(); } } finally { if (remainingPaths.Any()) { string ErrorMessageFormat = @"The following files were not formatted: " + Environment.NewLine + "{0}"; LoggerUtilities.LogError(string.Format(CultureInfo.CurrentCulture, ErrorMessageFormat, string.Join(Environment.NewLine, remainingPaths))); } } }
/// <summary> /// Returns the given pending change if it should be undone, otherwise null. /// </summary> private Task <PendingChange> ShouldUndoPendingChangeAsync(PendingChange pendingChange) { return(Task.Run(() => { if (pendingChange.IsAdd || pendingChange.IsDelete || pendingChange.IsLocalItemDelete || pendingChange.IsUndelete) { return null; } byte[] baseItemHashCode; try { using (var baseFileStream = pendingChange.DownloadBaseFile()) { using (var hashAlgorithem = new SHA1Cng()) { baseItemHashCode = hashAlgorithem.ComputeHash(baseFileStream); } } } catch (Exception ex) { const string ErrorMessageFormat = "Error occurred during computing hash for the base item of {0}: {1}"; LoggerUtilities.LogError(string.Format(CultureInfo.CurrentCulture, ErrorMessageFormat, pendingChange.ServerItem, ex.ToString())); return null; } byte[] localItemHashCode; try { using (var localFileStream = new FileStream(Path.GetFullPath(pendingChange.LocalItem), FileMode.Open, FileAccess.Read)) { using (var hashAlgorithem = new SHA1Cng()) { localItemHashCode = hashAlgorithem.ComputeHash(localFileStream); } } } catch (Exception ex) { const string ErrorMessageFormat = "Error occurred during computing hash for the local item of {0}: {1}"; LoggerUtilities.LogError(string.Format(CultureInfo.CurrentCulture, ErrorMessageFormat, pendingChange.ServerItem, ex.ToString())); return null; } return Enumerable.SequenceEqual(baseItemHashCode, localItemHashCode) ? pendingChange : null; })); }
public sealed override async Task ExecuteAsync(ProjectItem projectItem) { const int MAX_RETRY = 5; const int E_FAIL = unchecked ((int)0x80004005); const int RPC_E_CALL_REJECTED = unchecked ((int)0x80010001); if (projectItem != null) { var statusBar = _serviceProvider.GetService(typeof(SVsStatusbar)) as IVsStatusbar; statusBar.SetText("Updating " + projectItem.get_FileNames(1)); for (int retry = 0; retry < MAX_RETRY; retry++) { try { projectItem.Document.DTE.ExecuteCommand(_command); break; } catch (Exception ex) { if (ex is COMException && (((COMException)ex).ErrorCode == E_FAIL || ((COMException)ex).ErrorCode == RPC_E_CALL_REJECTED) && retry < MAX_RETRY - 1) { await Task.Delay(TimeSpan.FromSeconds(1)); continue; } else { const string ErrorMessageFormat = "Error occurred during the command {0} running on the item {1}: {2}"; LoggerUtilities.LogError(string.Format(CultureInfo.CurrentCulture, ErrorMessageFormat, _command, projectItem.get_FileNames(1), ex.ToString())); break; } } } } }