public ProjectFileInfoTests(ITestOutputHelper output) : base(output) { this._testAssets = TestAssets.Instance; this._logger = this.LoggerFactory.CreateLogger <ProjectFileInfoTests>(); if (!MSBuildEnvironment.IsInitialized) { MSBuildEnvironment.Initialize(this._logger); } }
public static async Task <int> Run(string workspace, string verbosity, bool dryRun, IConsole console = null) { var serviceCollection = new ServiceCollection(); var logLevel = GetLogLevel(verbosity); ConfigureServices(serviceCollection, console, logLevel); var serviceProvider = serviceCollection.BuildServiceProvider(); var logger = serviceProvider.GetService <ILogger <Program> >(); var cancellationTokenSource = new CancellationTokenSource(); Console.CancelKeyPress += (sender, e) => { e.Cancel = true; cancellationTokenSource.Cancel(); }; try { var workingDirectory = Directory.GetCurrentDirectory(); var(isSolution, workspacePath) = MSBuildWorkspaceFinder.FindWorkspace(workingDirectory, workspace); // To ensure we get the version of MSBuild packaged with the dotnet SDK used by the // workspace, use its directory as our working directory which will take into account // a global.json if present. var workspaceDirectory = Path.GetDirectoryName(workspacePath); MSBuildEnvironment.ApplyEnvironmentVariables(workspaceDirectory); MSBuildCoreLoader.LoadDotnetInstance(workspaceDirectory); return(await CodeFormatter.FormatWorkspaceAsync( logger, workspacePath, isSolution, logAllWorkspaceWarnings : logLevel == LogLevel.Trace, saveFormattedFiles : !dryRun, cancellationTokenSource.Token).ConfigureAwait(false)); } catch (FileNotFoundException fex) { logger.LogError(fex.Message); return(1); } catch (OperationCanceledException) { return(1); } }