private static async Task RunFormatItemAsync(IFormattingEngine engine, string item, CommandLineOptions options, CancellationToken cancellationToken) { Console.WriteLine(Path.GetFileName(item)); string extension = Path.GetExtension(item); if (StringComparer.OrdinalIgnoreCase.Equals(extension, ".sln")) { using (var workspace = CreateMSBuildWorkspace(options)) { workspace.LoadMetadataForReferencedProjects = true; var solution = await workspace.OpenSolutionAsync(item, cancellationToken : cancellationToken); await engine.FormatSolutionAsync(solution, cancellationToken); } } else { using (var workspace = CreateMSBuildWorkspace(options)) { workspace.LoadMetadataForReferencedProjects = true; var project = await workspace.OpenProjectAsync(item, cancellationToken : cancellationToken); await engine.FormatProjectAsync(project, cancellationToken); } } }
// Expects a list of paths to files or directories of DLLs containing analyzers and adds them to the engine internal static ImmutableArray <DiagnosticAnalyzer> AddCustomAnalyzers(IFormattingEngine engine, ImmutableArray <string> analyzerList) { foreach (var analyzerPath in analyzerList) { if (File.Exists(analyzerPath)) { var newAnalyzers = LoadAnalyzersFromAssembly(analyzerPath, true); engine.AddAnalyzers(newAnalyzers); return(newAnalyzers); } else if (Directory.Exists(analyzerPath)) { var DLLs = Directory.GetFiles(analyzerPath, "*.dll"); foreach (var dll in DLLs) { // allows specifying a folder that contains analyzers as well as non-analyzer DLLs without throwing var newAnalyzers = LoadAnalyzersFromAssembly(dll, false); if (newAnalyzers.Count() > 0) { engine.AddAnalyzers(newAnalyzers); } return(newAnalyzers); } } } return(ImmutableArray <DiagnosticAnalyzer> .Empty); }
private static async Task RunFormatItemAsync(IFormattingEngine engine, string item, string language, CancellationToken cancellationToken) { Console.WriteLine(Path.GetFileName(item)); string extension = Path.GetExtension(item); if (StringComparer.OrdinalIgnoreCase.Equals(extension, ".rsp")) { using (var workspace = ResponseFileWorkspace.Create()) { // Project project = workspace.OpenCommandLineProject(item, language); // await engine.FormatProjectAsync(project, cancellationToken); } } else if (StringComparer.OrdinalIgnoreCase.Equals(extension, ".sln")) { // using (var workspace = MSBuildWorkspace.Create()) // { // workspace.LoadMetadataForReferencedProjects = true; // var solution = await workspace.OpenSolutionAsync(item, cancellationToken); // await engine.FormatSolutionAsync(solution, cancellationToken); // } } else { // using (var workspace = MSBuildWorkspace.Create()) // { // workspace.LoadMetadataForReferencedProjects = true; // var project = await workspace.OpenProjectAsync(item, cancellationToken); // await engine.FormatProjectAsync(project, cancellationToken); // } } }
private static async Task RunFormatItemAsync(IFormattingEngine engine, string item, string language, CancellationToken cancellationToken) { Console.WriteLine(Path.GetFileName(item)); string extension = Path.GetExtension(item); if (StringComparer.OrdinalIgnoreCase.Equals(extension, ".rsp")) { using (var workspace = ResponseFileWorkspace.Create()) { Project project = workspace.OpenCommandLineProject(item, language); await engine.FormatProjectAsync(project, cancellationToken); } } else if (StringComparer.OrdinalIgnoreCase.Equals(extension, ".sln")) { AnalyzerManager manager = new AnalyzerManager(item); using (var workspace = manager.GetWorkspace()) { var solution = workspace.CurrentSolution; await engine.FormatSolutionAsync(solution, cancellationToken); } } else { AnalyzerManager manager = new AnalyzerManager(); ProjectAnalyzer analyzer = manager.GetProject(item); using (var workspace = analyzer.GetWorkspace()) { var project = workspace.CurrentSolution.Projects.FirstOrDefault(); await engine.FormatProjectAsync(project, cancellationToken); } } }
private static async Task RunItemAsync( IFormattingEngine engine, string item, string language, bool useAnalyzers, bool useTabs, CancellationToken cancellationToken) { Console.WriteLine(Path.GetFileName(item)); string extension = Path.GetExtension(item); try { if (StringComparer.OrdinalIgnoreCase.Equals(extension, ".rsp")) { using (var workspace = ResponseFileWorkspace.Create()) { ModifyWorkspaceOptions(workspace, language, useTabs); Project project = workspace.OpenCommandLineProject(item, language); await engine.FormatProjectAsync(project, useAnalyzers, cancellationToken); } } else if (StringComparer.OrdinalIgnoreCase.Equals(extension, ".sln")) { using (var workspace = MSBuildWorkspace.Create()) { ModifyWorkspaceOptions(workspace, language, useTabs); workspace.LoadMetadataForReferencedProjects = true; var solution = await workspace.OpenSolutionAsync(item, cancellationToken); await engine.FormatSolutionAsync(solution, useAnalyzers, cancellationToken); } } else { using (var workspace = MSBuildWorkspace.Create()) { ModifyWorkspaceOptions(workspace, language, useTabs); workspace.LoadMetadataForReferencedProjects = true; var project = await workspace.OpenProjectAsync(item, cancellationToken); await engine.FormatProjectAsync(project, useAnalyzers, cancellationToken); } } } catch (Microsoft.Build.Exceptions.InvalidProjectFileException) { // Can occur if for example a Mono based project with unknown targets files is supplied Console.WriteLine("Invalid project file in target {0}", item); } }
private static bool SetRuleMap(IFormattingEngine engine, ImmutableDictionary <string, bool> ruleMap) { var comparer = StringComparer.OrdinalIgnoreCase; foreach (var entry in ruleMap) { var rule = engine.AllRules.Where(x => comparer.Equals(x.Name, entry.Key)).FirstOrDefault(); if (rule == null) { Console.WriteLine("Could not find rule with name {0}", entry.Key); return(false); } engine.ToggleRuleEnabled(rule, entry.Value); } return(true); }
private static bool SetRuleMap(IFormattingEngine engine, ImmutableDictionary<string, bool> ruleMap) { var comparer = StringComparer.OrdinalIgnoreCase; foreach (var entry in ruleMap) { var rule = engine.AllRules.Where(x => comparer.Equals(x.Name, entry.Key)).FirstOrDefault(); if (rule == null) { Console.WriteLine("Could not find rule with name {0}", entry.Key); return false; } engine.ToggleRuleEnabled(rule, entry.Value); } return true; }
private static async Task RunFormatItemAsync(IFormattingEngine engine, string item, string language, CancellationToken cancellationToken) { Console.WriteLine(Path.GetFileName(item)); string extension = Path.GetExtension(item); if (StringComparer.OrdinalIgnoreCase.Equals(extension, ".rsp")) { using (var workspace = ResponseFileWorkspace.Create()) { Project project = workspace.OpenCommandLineProject(item, language); await engine.FormatProjectAsync(project, cancellationToken); } } else if (StringComparer.OrdinalIgnoreCase.Equals(extension, ".sln")) { using (var workspace = MSBuildWorkspace.Create()) { workspace.LoadMetadataForReferencedProjects = true; var solution = await workspace.OpenSolutionAsync(item, cancellationToken); await engine.FormatSolutionAsync(solution, cancellationToken); } } else { using (var workspace = MSBuildWorkspace.Create()) { workspace.LoadMetadataForReferencedProjects = true; var project = await workspace.OpenProjectAsync(item, cancellationToken); await engine.FormatProjectAsync(project, cancellationToken); } } }
/// <summary> /// Initializes a new instance of the ParamBuilderFormatter class. /// </summary> /// <param name="engine"></param> public ParamBuilderFormatter(IFormattingEngine engine) { _engine = engine; }
private static bool SetDiagnosticsEnabledMap(IFormattingEngine engine, ImmutableDictionary<string, bool> ruleMap) { var comparer = StringComparer.OrdinalIgnoreCase; foreach (var entry in ruleMap) { var diagnosticDescriptor = engine.AllSupportedDiagnostics.Where(x => comparer.Equals(x.Id, entry.Key)).FirstOrDefault(); if (diagnosticDescriptor == null) { Console.WriteLine("Could not find diagnostic with ID {0}", entry.Key); return false; } engine.ToggleDiagnosticEnabled(diagnosticDescriptor.Id, entry.Value); } return true; }
/// <summary> /// Initializes a new instance of the ParamBuilderFormatter class. /// </summary> /// <param name="engine"></param> public ParamBuilderFormatter(IFormattingEngine engine) { _engine = engine; }