/// <summary> /// Creates a solution that will be used as parent for the sources that need to be checked. /// </summary> /// <param name="projectId">The project identifier to use.</param> /// <param name="language">The language for which the solution is being created.</param> /// <returns>The created solution.</returns> protected virtual Solution CreateSolution(ProjectId projectId, string language) { var compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true); Solution solution = new AdhocWorkspace() .CurrentSolution .AddProject(projectId, TestProjectName, TestProjectName, language) .WithProjectCompilationOptions(projectId, compilationOptions) .AddMetadataReference(projectId, MetadataReferences.CorlibReference) .AddMetadataReference(projectId, MetadataReferences.SystemReference) .AddMetadataReference(projectId, MetadataReferences.SystemCoreReference) .AddMetadataReference(projectId, MetadataReferences.CSharpSymbolsReference) .AddMetadataReference(projectId, MetadataReferences.CodeAnalysisReference); var publicApi = this.GetUnshippedPublicApi(); if (publicApi != null) { var documentId = DocumentId.CreateNewId(projectId); solution = solution.AddAdditionalDocument(documentId, DeclarePublicAPIAnalyzer.UnshippedFileName, publicApi, filePath: this.GetUnshippedPublicApiFilePath()); } publicApi = this.GetShippedPublicApi(); if (publicApi != null) { var documentId = DocumentId.CreateNewId(projectId); solution = solution.AddAdditionalDocument(documentId, DeclarePublicAPIAnalyzer.ShippedFileName, publicApi, filePath: this.GetShippedPublicApiFilePath()); } ParseOptions parseOptions = solution.GetProject(projectId).ParseOptions; return(solution.WithProjectParseOptions(projectId, parseOptions.WithDocumentationMode(DocumentationMode.Diagnose))); }
/// <summary> /// Creates a solution that will be used as parent for the sources that need to be checked. /// </summary> /// <param name="projectId">The project identifier to use.</param> /// <param name="language">The language for which the solution is being created.</param> /// <returns>The created solution.</returns> protected virtual Solution CreateSolution(ProjectId projectId, string language) { var compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true); Solution solution = new AdhocWorkspace() .CurrentSolution .AddProject(projectId, TestProjectName, TestProjectName, language) .WithProjectCompilationOptions(projectId, compilationOptions) .AddMetadataReference(projectId, MetadataReferences.CorlibReference) .AddMetadataReference(projectId, MetadataReferences.SystemReference) .AddMetadataReference(projectId, MetadataReferences.SystemCoreReference) .AddMetadataReference(projectId, MetadataReferences.CSharpSymbolsReference) .AddMetadataReference(projectId, MetadataReferences.CodeAnalysisReference); var settings = this.GetSettings(); if (!string.IsNullOrEmpty(settings)) { var documentId = DocumentId.CreateNewId(projectId); solution = solution.AddAdditionalDocument(documentId, SettingsHelper.PublicApiFileName, settings); } ParseOptions parseOptions = solution.GetProject(projectId).ParseOptions; return(solution.WithProjectParseOptions(projectId, parseOptions.WithDocumentationMode(DocumentationMode.Diagnose))); }
/// <summary> /// Creates a solution that will be used as parent for the sources that need to be checked. /// </summary> /// <param name="projectId">The project identifier to use.</param> /// <param name="language">The language for which the solution is being created.</param> /// <returns>The created solution.</returns> protected virtual Solution CreateSolution(ProjectId projectId, string language) { var compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true); var additionalDiagnosticOptions = this.GetDisabledDiagnostics().Select(id => new KeyValuePair <string, ReportDiagnostic>(id, ReportDiagnostic.Suppress)); var newSpecificOptions = compilationOptions.SpecificDiagnosticOptions.AddRange(additionalDiagnosticOptions); compilationOptions = compilationOptions.WithSpecificDiagnosticOptions(newSpecificOptions); Solution solution = new AdhocWorkspace() .CurrentSolution .AddProject(projectId, TestProjectName, TestProjectName, language) .WithProjectCompilationOptions(projectId, compilationOptions) .AddMetadataReference(projectId, CorlibReference) .AddMetadataReference(projectId, SystemReference) .AddMetadataReference(projectId, SystemCoreReference) .AddMetadataReference(projectId, CSharpSymbolsReference) .AddMetadataReference(projectId, CodeAnalysisReference); var settings = this.GetSettings(); if (!string.IsNullOrEmpty(settings)) { var documentId = DocumentId.CreateNewId(projectId); solution = solution.AddAdditionalDocument(documentId, SettingsFileName, settings); } ParseOptions parseOptions = solution.GetProject(projectId).ParseOptions; return(solution.WithProjectParseOptions(projectId, parseOptions.WithDocumentationMode(DocumentationMode.Diagnose))); }
/// <summary> /// Create a project using the inputted strings as sources. /// </summary> /// <param name="sources">Classes in the form of strings</param> /// <param name="language">The language the source code is in</param> /// <param name="projectOptions">Additional project options.</param> /// <returns>A Project created out of the Documents created from the source strings</returns> private static Project CreateProject(string[] sources, string language = LanguageNames.CSharp, ProjectOptions projectOptions = null) { string fileNamePrefix = DefaultFilePathPrefix; string fileExt = language == LanguageNames.CSharp ? CSharpDefaultFileExt : VisualBasicDefaultExt; var projectId = ProjectId.CreateNewId(debugName: TestProjectName); var solution = new AdhocWorkspace() .CurrentSolution .AddProject(projectId, TestProjectName, TestProjectName, language) .AddMetadataReferences(projectId, projectOptions?.MetadataReferences == null ? References : projectOptions.MetadataReferences.Concat(References)); if (projectOptions?.AdditionalDocuments != null) { foreach (var additionalDocument in projectOptions.AdditionalDocuments) { var documentId = DocumentId.CreateNewId(projectId, debugName: additionalDocument.Name); solution = solution.AddAdditionalDocument(documentId, additionalDocument.Name, additionalDocument.Content); } } int count = 0; foreach (var source in sources) { var newFileName = fileNamePrefix + count + "." + fileExt; var documentId = DocumentId.CreateNewId(projectId, debugName: newFileName); solution = solution.AddDocument(documentId, newFileName, SourceText.From(source)); count++; } return(solution.GetProject(projectId)); }
/// <summary> /// Create a project using the inputted strings as sources. /// </summary> /// <param name="sources">Classes in the form of strings</param> /// <param name="language">The language the source code is in</param> /// <returns>A Project created out of the Documents created from the source strings</returns> private Project CreateProject(string[] sources, string language = LanguageNames.CSharp, bool usePortableReference = false) { string fileNamePrefix = DefaultFilePathPrefix; string fileExt = language == LanguageNames.CSharp ? CSharpDefaultFileExt : VisualBasicDefaultExt; var compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true); var projectId = ProjectId.CreateNewId(debugName: TestProjectName); var corlibReference = usePortableReference ? PortableCorlibReference : CorlibReference; var solution = new AdhocWorkspace() .CurrentSolution .AddProject(projectId, TestProjectName, TestProjectName, language) .WithProjectCompilationOptions(projectId, compilationOptions) .AddMetadataReference(projectId, corlibReference) .AddMetadataReference(projectId, SystemCoreReference) .AddMetadataReference(projectId, CSharpSymbolsReference) .AddMetadataReference(projectId, CodeAnalysisReference) .AddMetadataReference(projectId, ThisProjectReference) .AddMetadataReference(projectId, MsTestReference) .AddMetadataReference(projectId, XunitReference); var settings = this.GetSettings(); if (!string.IsNullOrEmpty(settings)) { var documentId = DocumentId.CreateNewId(projectId); solution = solution.AddAdditionalDocument(documentId, SettingsFileName, settings); } ParseOptions parseOptions = solution.GetProject(projectId).ParseOptions; solution.WithProjectParseOptions(projectId, parseOptions.WithDocumentationMode(DocumentationMode.Diagnose)); int count = 0; foreach (var source in sources) { var newFileName = fileNamePrefix + count + "." + fileExt; var documentId = DocumentId.CreateNewId(projectId, debugName: newFileName); solution = solution.AddDocument(documentId, newFileName, SourceText.From(source)); count++; } return(solution.GetProject(projectId)); }
/// <summary> /// Create a project using the inputted strings as sources. /// </summary> /// <param name="sources">Classes in the form of strings</param> /// <param name="language">The language the source code is in</param> /// <returns>A Project created out of the Documents created from the source strings</returns> private Project CreateProject(string[] sources, string language = LanguageNames.CSharp) { string fileNamePrefix = DefaultFilePathPrefix; string fileExt = language == LanguageNames.CSharp ? CSharpDefaultFileExt : VisualBasicDefaultExt; var projectId = ProjectId.CreateNewId(debugName: TestProjectName); var solution = new AdhocWorkspace() .CurrentSolution .AddProject(projectId, TestProjectName, TestProjectName, language) .AddMetadataReference(projectId, CorlibReference) .AddMetadataReference(projectId, SystemCoreReference) .AddMetadataReference(projectId, CSharpSymbolsReference) .AddMetadataReference(projectId, CodeAnalysisReference); int count = 0; foreach (var source in sources) { var newFileName = fileNamePrefix + count + "." + fileExt; var documentId = DocumentId.CreateNewId(projectId, debugName: newFileName); solution = solution.AddDocument(documentId, newFileName, SourceText.From(source)); count++; } var settings = this.GetSettings(); if (string.IsNullOrEmpty(settings)) { settings = JsonConvert.SerializeObject(DependencyCopSettings.Default); } if (!string.IsNullOrEmpty(settings)) { var documentId = DocumentId.CreateNewId(projectId); solution = solution.AddAdditionalDocument(documentId, this.GetSettingsFileName(), settings); } return(solution.GetProject(projectId)); }
/// <summary> /// Creates a solution that will be used as parent for the sources that need to be checked. /// </summary> /// <param name="projectId">The project identifier to use.</param> /// <param name="language">The language for which the solution is being created.</param> /// <returns>The created solution.</returns> protected virtual Solution CreateSolution(ProjectId projectId, string language) { var compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true); var solution = new AdhocWorkspace() .CurrentSolution .AddProject(projectId, TestProjectName, TestProjectName, language) .WithProjectCompilationOptions(projectId, compilationOptions) .AddMetadataReference(projectId, MetadataReferences.CorlibReference) .AddMetadataReference(projectId, MetadataReferences.SystemReference) .AddMetadataReference(projectId, MetadataReferences.SystemCoreReference) .AddMetadataReference(projectId, MetadataReferences.CSharpSymbolsReference) .AddMetadataReference(projectId, MetadataReferences.CodeAnalysisReference); solution.Workspace.Options = solution.Workspace.Options .WithChangedOption(FormattingOptions.IndentationSize, language, IndentationSize) .WithChangedOption(FormattingOptions.TabSize, language, TabSize) .WithChangedOption(FormattingOptions.UseTabs, language, UseTabs); var settings = GetSettings(); var defaultSettings = new StyleCopSettings(); if (IndentationSize != defaultSettings.Indentation.IndentationSize || UseTabs != defaultSettings.Indentation.UseTabs || TabSize != defaultSettings.Indentation.TabSize) { var indentationSettings = $@" {{ ""settings"": {{ ""indentation"": {{ ""indentationSize"": {IndentationSize}, ""useTabs"": {UseTabs.ToString().ToLowerInvariant()}, ""tabSize"": {TabSize} }} }} }} "; if (string.IsNullOrEmpty(settings)) { settings = indentationSettings; } else { var mergedSettings = JsonConvert.DeserializeObject <JObject>(settings); mergedSettings.Merge(JsonConvert.DeserializeObject <JObject>(indentationSettings)); settings = JsonConvert.SerializeObject(mergedSettings); } } if (!string.IsNullOrEmpty(settings)) { var documentId = DocumentId.CreateNewId(projectId); solution = solution.AddAdditionalDocument(documentId, SettingsHelper.SettingsFileName, settings); } var parseOptions = solution.GetProject(projectId).ParseOptions; return(solution.WithProjectParseOptions(projectId, parseOptions.WithDocumentationMode(DocumentationMode.Diagnose))); }
/// <summary> /// Creates a solution that will be used as parent for the sources that need to be checked. /// </summary> /// <param name="projectId">The project identifier to use.</param> /// <param name="language">The language for which the solution is being created.</param> /// <returns>The created solution.</returns> protected virtual Solution CreateSolution(ProjectId projectId, string language) { var compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true); Solution solution = new AdhocWorkspace() .CurrentSolution .AddProject(projectId, TestProjectName, TestProjectName, language) .WithProjectCompilationOptions(projectId, compilationOptions) .AddMetadataReference(projectId, MetadataReferences.CorlibReference) .AddMetadataReference(projectId, MetadataReferences.SystemReference) .AddMetadataReference(projectId, MetadataReferences.SystemCoreReference) .AddMetadataReference(projectId, MetadataReferences.CSharpSymbolsReference) .AddMetadataReference(projectId, MetadataReferences.CodeAnalysisReference); if (MetadataReferences.SystemRuntimeReference != null) { solution = solution.AddMetadataReference(projectId, MetadataReferences.SystemRuntimeReference); } if (MetadataReferences.SystemValueTupleReference != null) { solution = solution.AddMetadataReference(projectId, MetadataReferences.SystemValueTupleReference); } solution.Workspace.Options = solution.Workspace.Options .WithChangedOption(FormattingOptions.IndentationSize, language, this.IndentationSize) .WithChangedOption(FormattingOptions.TabSize, language, this.TabSize) .WithChangedOption(FormattingOptions.UseTabs, language, this.UseTabs); var settings = this.GetSettings(); StyleCopSettings defaultSettings = new StyleCopSettings(); if (this.IndentationSize != defaultSettings.Indentation.IndentationSize || this.UseTabs != defaultSettings.Indentation.UseTabs || this.TabSize != defaultSettings.Indentation.TabSize) { var indentationSettings = $@" {{ ""settings"": {{ ""indentation"": {{ ""indentationSize"": {this.IndentationSize}, ""useTabs"": {this.UseTabs.ToString().ToLowerInvariant()}, ""tabSize"": {this.TabSize} }} }} }} "; if (string.IsNullOrEmpty(settings)) { settings = indentationSettings; } else { JsonObject indentationObject = JsonReader.Parse(indentationSettings).AsJsonObject; JsonObject settingsObject = JsonReader.Parse(settings).AsJsonObject; JsonObject mergedSettings = MergeJsonObjects(settingsObject, indentationObject); using (var writer = new JsonWriter(pretty: true)) { settings = writer.Serialize(mergedSettings); } } } if (!string.IsNullOrEmpty(settings)) { var documentId = DocumentId.CreateNewId(projectId); solution = solution.AddAdditionalDocument(documentId, this.GetSettingsFileName(), settings); } ParseOptions parseOptions = solution.GetProject(projectId).ParseOptions; return(solution.WithProjectParseOptions(projectId, parseOptions.WithDocumentationMode(DocumentationMode.Diagnose))); }
/// <summary> /// Create a project using the inputted strings as sources. /// </summary> /// <param name="sources">Classes in the form of strings</param> /// <param name="language">The language the source code is in</param> /// <param name="framework">The targed unit test framework.</param> /// <returns>A Project created out of the Documents created from the source strings</returns> private Project CreateProject(string[] sources, string language, UnitTestFramework framework) { string fileNamePrefix = DefaultFilePathPrefix; string fileExt = language == LanguageNames.CSharp ? CSharpDefaultFileExt : VisualBasicDefaultExt; var compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true); var projectId = ProjectId.CreateNewId(debugName: TestProjectName); MetadataReference corlibReference; MetadataReference unitTestFrameworkReference; switch (framework) { case UnitTestFramework.MSTest: unitTestFrameworkReference = MsTestReference; corlibReference = CorlibReference; break; case UnitTestFramework.Xunit: unitTestFrameworkReference = XunitReference; corlibReference = PortableCorlibReference; break; case UnitTestFramework.NUnit: unitTestFrameworkReference = NUnitReference; corlibReference = CorlibReference; break; default: throw new ArgumentException("The provided value is not supported", nameof(framework)); } var solution = new AdhocWorkspace() .CurrentSolution .AddProject(projectId, TestProjectName, TestProjectName, language) .WithProjectCompilationOptions(projectId, compilationOptions) .AddMetadataReference(projectId, corlibReference) .AddMetadataReference(projectId, SystemCoreReference) .AddMetadataReference(projectId, CSharpSymbolsReference) .AddMetadataReference(projectId, CodeAnalysisReference) .AddMetadataReference(projectId, ThisProjectReference) .AddMetadataReference(projectId, unitTestFrameworkReference); var settings = this.GetSettings(); if (!string.IsNullOrEmpty(settings)) { var documentId = DocumentId.CreateNewId(projectId); solution = solution.AddAdditionalDocument(documentId, SettingsFileName, settings); } ParseOptions parseOptions = solution.GetProject(projectId).ParseOptions; #pragma warning disable RS1014 // Do not ignore values returned by methods on immutable objects. solution.WithProjectParseOptions(projectId, parseOptions.WithDocumentationMode(DocumentationMode.Diagnose)); #pragma warning restore RS1014 // Do not ignore values returned by methods on immutable objects. int count = 0; foreach (var source in sources) { var newFileName = fileNamePrefix + count + "." + fileExt; var documentId = DocumentId.CreateNewId(projectId, debugName: newFileName); solution = solution.AddDocument(documentId, newFileName, SourceText.From(source)); count++; } return(solution.GetProject(projectId)); }