public async Task <AddLicenseHeaderToAllFilesResult> RemoveOrReplaceHeadersAsync(object projectOrProjectItem) { await _licenseHeaderExtension.JoinableTaskFactory.SwitchToMainThreadAsync(); var project = projectOrProjectItem as Project; var projectItem = projectOrProjectItem as ProjectItem; var replacerInput = new List <LicenseHeaderContentInput>(); var countSubLicenseHeadersFound = 0; IDictionary <string, string[]> headers; var linkedItems = new List <ProjectItem>(); if (project == null && projectItem == null) { return(new AddLicenseHeaderToAllFilesResult(countSubLicenseHeadersFound, true, linkedItems)); } ProjectItems projectItems; var fileOpenedStatus = new Dictionary <string, bool>(); if (project != null) { headers = LicenseHeaderFinder.GetHeaderDefinitionForProjectWithFallback(project); projectItems = project.ProjectItems; } else { headers = LicenseHeaderFinder.GetHeaderDefinitionForItem(projectItem); projectItems = projectItem.ProjectItems; } foreach (ProjectItem item in projectItems) { if (ProjectItemInspection.IsPhysicalFile(item) && ProjectItemInspection.IsLink(item)) { linkedItems.Add(item); } else { var inputFiles = CoreHelpers.GetFilesToProcess(item, headers, out var subLicenseHeaders, out var subFileOpenedStatus); replacerInput.AddRange(inputFiles); foreach (var status in subFileOpenedStatus) { fileOpenedStatus[status.Key] = status.Value; } countSubLicenseHeadersFound = subLicenseHeaders; } } var result = await _licenseHeaderExtension.LicenseHeaderReplacer.RemoveOrReplaceHeader( replacerInput, CoreHelpers.CreateProgress(_baseUpdateViewModel, project?.Name, fileOpenedStatus, _cancellationToken), _cancellationToken); await CoreHelpers.HandleResultAsync(result, _licenseHeaderExtension, _baseUpdateViewModel, project?.Name, fileOpenedStatus, _cancellationToken); return(new AddLicenseHeaderToAllFilesResult(countSubLicenseHeadersFound, headers == null, linkedItems)); }
public void TestProjectItem_WithoutIsLinkProperty() { ProjectItem linkedProjectItem = MockRepository.GenerateMock <ProjectItem> (); linkedProjectItem.Stub(x => x.Properties).Return(MockRepository.GenerateStub <Properties> ()); linkedProjectItem.Properties.Stub(x => x.Item("IsLink")).Throw(new ArgumentException()); Assert.IsFalse(ProjectItemInspection.IsLink(linkedProjectItem)); }
public async Task IsLink_GivenNonLinkedItem_ReturnsFalse() { await VisualStudioTestContext.SwitchToMainThread(); var linkedProjectItem = MockRepository.GenerateMock <ProjectItem>(); linkedProjectItem.Stub(x => x.Properties).Return(MockRepository.GenerateStub <Properties>()); linkedProjectItem.Properties.Stub(x => x.Item("IsLink")).Throw(new ArgumentException()); Assert.That(ProjectItemInspection.IsLink(linkedProjectItem), Is.False); }
public void TestIsLink() { ProjectItem linkedProjectItem = MockRepository.GenerateMock <ProjectItem>(); Property propertyStub = MockRepository.GenerateStub <Property> (); propertyStub.Value = true; linkedProjectItem.Stub(x => x.Properties).Return(MockRepository.GenerateStub <Properties> ()); linkedProjectItem.Properties.Stub(x => x.Item("IsLink")).Return(propertyStub); Assert.IsTrue(ProjectItemInspection.IsLink(linkedProjectItem)); }
public async Task IsLink_GivenLinkedItem_ReturnsTrue() { await VisualStudioTestContext.SwitchToMainThread(); var linkedProjectItem = MockRepository.GenerateMock <ProjectItem>(); var propertyStub = MockRepository.GenerateStub <Property>(); propertyStub.Value = true; linkedProjectItem.Stub(x => x.Properties).Return(MockRepository.GenerateStub <Properties>()); linkedProjectItem.Properties.Stub(x => x.Item("IsLink")).Return(propertyStub); Assert.That(ProjectItemInspection.IsLink(linkedProjectItem), Is.True); }
public AddLicenseHeaderToAllFilesReturn Execute(object projectOrProjectItem) { var project = projectOrProjectItem as Project; var projectItem = projectOrProjectItem as ProjectItem; int countSubLicenseHeadersFound = 0; IDictionary <string, string[]> headers = null; List <ProjectItem> linkedItems = new List <ProjectItem> (); if (project != null || projectItem != null) { licenseReplacer.ResetExtensionsWithInvalidHeaders(); ProjectItems projectItems; if (project != null) { headers = LicenseHeaderFinder.GetHeader(project); projectItems = project.ProjectItems; } else { headers = LicenseHeaderFinder.GetHeaderRecursive(projectItem); projectItems = projectItem.ProjectItems; } foreach (ProjectItem item in projectItems) { if (ProjectItemInspection.IsPhysicalFile(item) && ProjectItemInspection.IsLink(item)) { linkedItems.Add(item); } else { countSubLicenseHeadersFound = licenseReplacer.RemoveOrReplaceHeaderRecursive(item, headers); } } } return(new AddLicenseHeaderToAllFilesReturn(countSubLicenseHeadersFound, headers == null, linkedItems)); }
/// <summary> /// Tries to open a given project item as a Document which can be used to add or remove headers. /// </summary> /// <param name="item">The project item.</param> /// <param name="document">The document which was created or null if an error occured (see return value).</param> /// <param name="headers">A dictionary of headers using the file extension as key and the header as value or null if headers should only be removed.</param> /// <returns>A value indicating the result of the operation. Document will be null unless DocumentCreated is returned.</returns> public CreateDocumentResult TryCreateDocument(ProjectItem item, out Document document, out bool wasOpen, IDictionary <string, string[]> headers = null) { document = null; wasOpen = true; if (!ProjectItemInspection.IsPhysicalFile(item)) { return(CreateDocumentResult.NoPhysicalFile); } if (ProjectItemInspection.IsLicenseHeader(item)) { return(CreateDocumentResult.LicenseHeaderDocument); } if (ProjectItemInspection.IsLink(item)) { return(CreateDocumentResult.LinkedFile); } var language = _licenseHeaderExtension.LanguagesPage.Languages .Where(x => x.Extensions.Any(y => item.Name.EndsWith(y, StringComparison.OrdinalIgnoreCase))) .FirstOrDefault(); if (language == null) { return(CreateDocumentResult.LanguageNotFound); } Window window = null; //try to open the document as a text document try { if (!item.IsOpen[Constants.vsViewKindTextView]) { window = item.Open(Constants.vsViewKindTextView); wasOpen = false; } } catch (COMException) { return(CreateDocumentResult.NoTextDocument); } catch (IOException) { return(CreateDocumentResult.NoPhysicalFile); } var itemDocument = item.Document; if (item.Document == null) { return(CreateDocumentResult.NoPhysicalFile); } var textDocument = itemDocument.Object("TextDocument") as TextDocument; if (textDocument == null) { return(CreateDocumentResult.NoTextDocument); } string[] header = null; if (headers != null) { var extension = headers.Keys .OrderByDescending(x => x.Length) .Where(x => item.Name.EndsWith(x, StringComparison.OrdinalIgnoreCase)) .FirstOrDefault(); if (extension == null) { return(CreateDocumentResult.NoHeaderFound); } header = headers[extension]; if (header.All(string.IsNullOrEmpty)) { return(CreateDocumentResult.EmptyHeader); } } var optionsPage = _licenseHeaderExtension.OptionsPage; document = new Document( document: textDocument, language: language, lines: header, projectItem: item, keywords: (optionsPage.UseRequiredKeywords ? optionsPage.RequiredKeywords.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(k => k.Trim()) : null), blacklist: (optionsPage.UseBlacklistedKeywords ? optionsPage.BlacklistedKeywords.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(b => b.Trim()) : null)); return(CreateDocumentResult.DocumentCreated); }