Beispiel #1
0
        public MSBuildResult Run(
            ProjectConfigurationInfo[] configurations, IEngineLogWriter logWriter, MSBuildVerbosity verbosity, string binLogFilePath,
            string[] runTargets, string[] evaluateItems, string[] evaluateProperties, Dictionary <string, string> globalProperties, int taskId)
        {
            if (runTargets == null || runTargets.Length == 0)
            {
                throw new ArgumentException("runTargets is empty");
            }

            MSBuildResult result = null;

            BuildEngine.RunSTA(taskId, delegate {
                Project project = null;
                Dictionary <string, string> originalGlobalProperties = null;

                MSBuildLoggerAdapter loggerAdapter;

                if (buildEngine.BuildOperationStarted)
                {
                    loggerAdapter = buildEngine.StartProjectSessionBuild(logWriter);
                }
                else
                {
                    loggerAdapter = new MSBuildLoggerAdapter(logWriter, verbosity);
                    if (!string.IsNullOrEmpty(binLogFilePath))
                    {
                        var binaryLogger = new BinaryLogger {
                            Parameters = binLogFilePath,
                            Verbosity  = LoggerVerbosity.Diagnostic
                        };
                        loggerAdapter.AddLogger(binaryLogger);
                    }
                }

                try {
                    project = SetupProject(configurations);

                    if (globalProperties != null)
                    {
                        originalGlobalProperties = new Dictionary <string, string> ();
                        foreach (var p in project.GlobalProperties)
                        {
                            originalGlobalProperties [p.Key] = p.Value;
                        }
                        if (globalProperties != null)
                        {
                            foreach (var p in globalProperties)
                            {
                                project.SetGlobalProperty(p.Key, p.Value);
                            }
                        }
                    }

                    // Building the project will create items and alter properties, so we use a new instance
                    var pi = project.CreateProjectInstance();

                    Build(pi, runTargets, loggerAdapter.Loggers);

                    result = new MSBuildResult(loggerAdapter.BuildResult.ToArray());

                    if (evaluateProperties != null)
                    {
                        foreach (var name in evaluateProperties)
                        {
                            var prop = pi.GetProperty(name);
                            result.Properties [name] = prop != null? prop.EvaluatedValue : null;
                        }
                    }

                    if (evaluateItems != null)
                    {
                        foreach (var name in evaluateItems)
                        {
                            var grp  = pi.GetItems(name);
                            var list = new List <MSBuildEvaluatedItem> ();
                            foreach (var item in grp)
                            {
                                var evItem = new MSBuildEvaluatedItem(name, UnescapeString(item.EvaluatedInclude));
                                foreach (var metadataName in item.MetadataNames)
                                {
                                    evItem.Metadata [metadataName] = UnescapeString(item.GetMetadataValue(metadataName));
                                }
                                list.Add(evItem);
                            }
                            result.Items[name] = list.ToArray();
                        }
                    }
                } catch (Microsoft.Build.Exceptions.InvalidProjectFileException ex) {
                    var r = new MSBuildTargetResult(
                        file, false, ex.ErrorSubcategory, ex.ErrorCode, ex.ProjectFile,
                        ex.LineNumber, ex.ColumnNumber, ex.EndLineNumber, ex.EndColumnNumber,
                        ex.BaseMessage, ex.HelpKeyword);
                    loggerAdapter.LogWriteLine(r.ToString());
                    result = new MSBuildResult(new [] { r });
                } finally {
                    if (buildEngine.BuildOperationStarted)
                    {
                        buildEngine.EndProjectSessionBuild();
                    }
                    else
                    {
                        loggerAdapter.Dispose();
                    }

                    if (project != null && globalProperties != null)
                    {
                        foreach (var p in globalProperties)
                        {
                            project.RemoveGlobalProperty(p.Key);
                        }
                        foreach (var p in originalGlobalProperties)
                        {
                            project.SetGlobalProperty(p.Key, p.Value);
                        }
                    }
                }
            });
            return(result);
        }