Exemplo n.º 1
0
        public static bool TryLoad(out string msBuildPath)
        {
            try
            {
                // Since we are running as a dotnet tool we should be able to find an instance of
                // MSBuild in a .NET Core SDK.
                var msBuildInstance = MSBuildLocator.QueryVisualStudioInstances().First();

                // Since we do not inherit msbuild.deps.json when referencing the SDK copy
                // of MSBuild and because the SDK no longer ships with version matched assemblies, we
                // register an assembly loader that will load assemblies from the msbuild path with
                // equal or higher version numbers than requested.
                LooseVersionAssemblyLoader.Register(msBuildInstance.MSBuildPath);
                MSBuildLocator.RegisterInstance(msBuildInstance);

                msBuildPath = msBuildInstance.MSBuildPath;

                return(true);
            }
            catch
            {
                msBuildPath = null;
                return(false);
            }
        }
Exemplo n.º 2
0
 public void RegisterInstance()
 {
     if (Interlocked.Exchange(ref s_registered, 1) == 0)
     {
         var msBuildInstance = Build.Locator.MSBuildLocator.QueryVisualStudioInstances().First();
         LooseVersionAssemblyLoader.Register(msBuildInstance.MSBuildPath);
         Build.Locator.MSBuildLocator.RegisterInstance(msBuildInstance);
     }
 }
Exemplo n.º 3
0
        public void RegisterInstance(ITestOutputHelper output)
        {
            if (Interlocked.Exchange(ref s_registered, 1) == 0)
            {
                var msBuildInstance = Build.Locator.MSBuildLocator.QueryVisualStudioInstances().First();

                output.WriteLine(Resources.Using_msbuildexe_located_in_0, msBuildInstance.MSBuildPath);

                LooseVersionAssemblyLoader.Register(msBuildInstance.MSBuildPath);
                Build.Locator.MSBuildLocator.RegisterInstance(msBuildInstance);
            }
        }
Exemplo n.º 4
0
        public static string RegisterInstance(ILogger?logger = null)
        {
            if (Interlocked.Exchange(ref s_registered, 1) == 0)
            {
                var msBuildInstance = Build.Locator.MSBuildLocator.QueryVisualStudioInstances().First();

                s_msBuildPath = msBuildInstance.MSBuildPath;

                LooseVersionAssemblyLoader.Register(s_msBuildPath, logger);
                Build.Locator.MSBuildLocator.RegisterInstance(msBuildInstance);
            }

            return(s_msBuildPath !);
        }
Exemplo n.º 5
0
        public static async Task <int> Run(string folder, string workspace, string verbosity, bool check, string[] include, string[] exclude, string report, IConsole console = null)
        {
            // Setup logging.
            var serviceCollection = new ServiceCollection();
            var logLevel          = GetLogLevel(verbosity);

            ConfigureServices(serviceCollection, console, logLevel);

            var serviceProvider = serviceCollection.BuildServiceProvider();
            var logger          = serviceProvider.GetService <ILogger <Program> >();

            // Hook so we can cancel and exit when ctrl+c is pressed.
            var cancellationTokenSource = new CancellationTokenSource();

            Console.CancelKeyPress += (sender, e) =>
            {
                e.Cancel = true;
                cancellationTokenSource.Cancel();
            };

            var currentDirectory = string.Empty;

            try
            {
                currentDirectory = Environment.CurrentDirectory;

                string        workspaceDirectory;
                string        workspacePath;
                WorkspaceType workspaceType;

                if (!string.IsNullOrEmpty(folder) && !string.IsNullOrEmpty(workspace))
                {
                    logger.LogWarning(Resources.Cannot_specify_both_folder_and_workspace_options);
                    return(1);
                }

                if (!string.IsNullOrEmpty(folder))
                {
                    folder             = Path.GetFullPath(folder, Environment.CurrentDirectory);
                    workspacePath      = folder;
                    workspaceDirectory = workspacePath;
                    workspaceType      = WorkspaceType.Folder;
                }
                else
                {
                    var(isSolution, workspaceFilePath) = MSBuildWorkspaceFinder.FindWorkspace(currentDirectory, workspace);

                    workspacePath = workspaceFilePath;
                    workspaceType = isSolution
                        ? WorkspaceType.Solution
                        : WorkspaceType.Project;

                    // 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.
                    workspaceDirectory = Path.GetDirectoryName(workspacePath);
                }

                Environment.CurrentDirectory = workspaceDirectory;

                // Since we are running as a dotnet tool we should be able to find an instance of
                // MSBuild in a .NET Core SDK.
                var msBuildInstance = Build.Locator.MSBuildLocator.QueryVisualStudioInstances().First();

                // Since we do not inherit msbuild.deps.json when referencing the SDK copy
                // of MSBuild and because the SDK no longer ships with version matched assemblies, we
                // register an assembly loader that will load assemblies from the msbuild path with
                // equal or higher version numbers than requested.
                LooseVersionAssemblyLoader.Register(msBuildInstance.MSBuildPath);

                Build.Locator.MSBuildLocator.RegisterInstance(msBuildInstance);

                var fileMatcher = SourceFileMatcher.CreateMatcher(include, exclude);

                var formatOptions = new FormatOptions(
                    workspacePath,
                    workspaceType,
                    logLevel,
                    saveFormattedFiles: !check,
                    changesAreErrors: check,
                    fileMatcher,
                    reportPath: report);

                var formatResult = await CodeFormatter.FormatWorkspaceAsync(
                    formatOptions,
                    logger,
                    cancellationTokenSource.Token).ConfigureAwait(false);

                return(GetExitCode(formatResult, check));
            }
            catch (FileNotFoundException fex)
            {
                logger.LogError(fex.Message);
                return(UnhandledExceptionExitCode);
            }
            catch (OperationCanceledException)
            {
                return(UnhandledExceptionExitCode);
            }
            finally
            {
                if (!string.IsNullOrEmpty(currentDirectory))
                {
                    Environment.CurrentDirectory = currentDirectory;
                }
            }
        }
Exemplo n.º 6
0
        public static async Task <int> Run(string workspace, string verbosity, bool dryRun, bool check, string files, 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();
            };

            string currentDirectory = string.Empty;

            try
            {
                currentDirectory = Environment.CurrentDirectory;

                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);
                Environment.CurrentDirectory = workingDirectory;

                var fileList = GetFileList(files);

                // Since we are running as a dotnet tool we should be able to find an instance of
                // MSBuild in a .NET Core SDK.
                var msBuildInstance = Build.Locator.MSBuildLocator.QueryVisualStudioInstances().First();

                // Since we do not inherit msbuild.deps.json when referencing the SDK copy
                // of MSBuild and because the SDK no longer ships with version matched assemblies, we
                // register an assembly loader that will load assemblies from the msbuild path with
                // equal or higher version numbers than requested.
                LooseVersionAssemblyLoader.Register(msBuildInstance.MSBuildPath);

                Build.Locator.MSBuildLocator.RegisterInstance(msBuildInstance);

                var formatResult = await CodeFormatter.FormatWorkspaceAsync(
                    logger,
                    workspacePath,
                    isSolution,
                    logAllWorkspaceWarnings : logLevel == LogLevel.Trace,
                    saveFormattedFiles : !dryRun,
                    filesToFormat : fileList,
                    cancellationTokenSource.Token).ConfigureAwait(false);

                return(GetExitCode(formatResult, check));
            }
            catch (FileNotFoundException fex)
            {
                logger.LogError(fex.Message);
                return(1);
            }
            catch (OperationCanceledException)
            {
                return(1);
            }
            finally
            {
                if (!string.IsNullOrEmpty(currentDirectory))
                {
                    Environment.CurrentDirectory = currentDirectory;
                }
            }
        }