Example #1
0
        private static bool BuildProjectWithOldOM(string projectFile, string[] targets, string toolsVersion, Microsoft.Build.BuildEngine.BuildPropertyGroup propertyBag, ILogger[] loggers, LoggerVerbosity verbosity, DistributedLoggerRecord[] distributedLoggerRecords, bool needToValidateProject, string schemaFile, int cpuCount)
        {
            string msbuildLocation = Path.GetDirectoryName(Assembly.GetAssembly(typeof(MSBuildApp)).Location);
            string localNodeProviderParameters = "msbuildlocation=" + msbuildLocation; /*This assembly is the exe*/ ;

            localNodeProviderParameters += ";nodereuse=false";

            Microsoft.Build.BuildEngine.Engine engine = new Microsoft.Build.BuildEngine.Engine(propertyBag, Microsoft.Build.BuildEngine.ToolsetDefinitionLocations.ConfigurationFile | Microsoft.Build.BuildEngine.ToolsetDefinitionLocations.Registry, cpuCount, localNodeProviderParameters);
            bool success = false;

            try
            {
                foreach (ILogger logger in loggers)
                {
                    engine.RegisterLogger(logger);
                }

                // Targeted perf optimization for the case where we only have our own parallel console logger, and verbosity is quiet. In such a case
                // we know we won't emit any messages except for errors and warnings, so the engine should not bother even logging them.
                // If we're using the original serial console logger we can't do this, as it shows project started/finished context
                // around errors and warnings.
                // Telling the engine to not bother logging non-critical messages means that typically it can avoid loading any resources in the successful
                // build case.
                if (loggers.Length == 1 &&
                    verbosity == LoggerVerbosity.Quiet &&
                    loggers[0].Parameters.IndexOf("ENABLEMPLOGGING", StringComparison.OrdinalIgnoreCase) != -1 &&
                    loggers[0].Parameters.IndexOf("DISABLEMPLOGGING", StringComparison.OrdinalIgnoreCase) == -1 &&
                    loggers[0].Parameters.IndexOf("V=", StringComparison.OrdinalIgnoreCase) == -1 &&                // Console logger could have had a verbosity
                    loggers[0].Parameters.IndexOf("VERBOSITY=", StringComparison.OrdinalIgnoreCase) == -1)          // override with the /clp switch
                {
                    // Must be exactly the console logger, not a derived type like the file logger.
                    Type t1 = loggers[0].GetType();
                    Type t2 = typeof(ConsoleLogger);
                    if (t1 == t2)
                    {
                        engine.OnlyLogCriticalEvents = true;
                    }
                }

                Microsoft.Build.BuildEngine.Project project = null;

                try
                {
                    project = new Microsoft.Build.BuildEngine.Project(engine, toolsVersion);
                }
                catch (InvalidOperationException e)
                {
                    InitializationException.Throw("InvalidToolsVersionError", toolsVersion, e, false /*no stack*/);
                }

                project.IsValidated = needToValidateProject;
                project.SchemaFile = schemaFile;

                project.Load(projectFile);

                success = engine.BuildProject(project, targets);
            }
            // handle project file errors
            catch (InvalidProjectFileException)
            {
                // just eat the exception because it has already been logged
            }
            finally
            {
                // Unregister loggers and finish with engine
                engine.Shutdown();
            }
            return success;
        }