public Task <MSBuildResult> Run(
            ProjectConfigurationInfo[] configurations,
            ILogWriter logWriter,
            MSBuildVerbosity verbosity,
            string[] runTargets,
            string[] evaluateItems,
            string[] evaluateProperties,
            Dictionary <string, string> globalProperties,
            CancellationToken cancellationToken
            )
        {
            // Get an id for the task, and get ready to cancel it if the cancellation token is signalled
            var taskId = Interlocked.Increment(ref lastTaskId);
            var cr     = RegisterCancellation(cancellationToken, taskId);

            var t = Task.Run(() => {
                try {
                    BeginOperation();
                    var res = builder.Run(configurations, logWriter, verbosity, runTargets, evaluateItems, evaluateProperties, globalProperties, taskId);
                    if (res == null && cancellationToken.IsCancellationRequested)
                    {
                        MSBuildTargetResult err = new MSBuildTargetResult(file, false, "", "", file, 1, 1, 1, 1, "Build cancelled", "");
                        return(new MSBuildResult(new [] { err }));
                    }
                    if (res == null)
                    {
                        throw new Exception("Unknown failure");
                    }
                    return(res);
                } catch (Exception ex) {
                    CheckDisconnected();
                    LoggingService.LogError("RunTarget failed", ex);
                    MSBuildTargetResult err = new MSBuildTargetResult(file, false, "", "", file, 1, 1, 1, 1, "Unknown MSBuild failure. Please try building the project again", "");
                    MSBuildResult res       = new MSBuildResult(new [] { err });
                    return(res);
                } finally {
                    EndOperation();
                }
            });

            // Dispose the cancel registration
            t.ContinueWith(r => cr.Dispose());

            return(t);
        }
Beispiel #2
0
        public async Task <MSBuildResult> Run(
            ProjectConfigurationInfo[] configurations,
            TextWriter logWriter,
            MSBuildLogger logger,
            MSBuildVerbosity verbosity,
            string binLogFilePath,
            string[] runTargets,
            string[] evaluateItems,
            string[] evaluateProperties,
            Dictionary <string, string> globalProperties,
            CancellationToken cancellationToken
            )
        {
            // Get an id for the task, and get ready to cancel it if the cancellation token is signalled
            var taskId   = Interlocked.Increment(ref lastTaskId);
            var cr       = RegisterCancellation(cancellationToken, taskId);
            var loggerId = engine.RegisterLogger(logWriter, logger);

            try {
                BeginOperation();
                var res = await SendRun(configurations, loggerId, logger.EnabledEvents, verbosity, binLogFilePath, runTargets, evaluateItems, evaluateProperties, globalProperties, taskId).ConfigureAwait(false);

                if (res == null && cancellationToken.IsCancellationRequested)
                {
                    MSBuildTargetResult err = new MSBuildTargetResult(file, false, "", "", file, 1, 1, 1, 1, "Build cancelled", "");
                    return(new MSBuildResult(new [] { err }));
                }
                if (res == null)
                {
                    throw new Exception("Unknown failure");
                }
                return(res);
            } catch (Exception ex) {
                await CheckDisconnected().ConfigureAwait(false);

                LoggingService.LogError("RunTarget failed", ex);
                MSBuildTargetResult err = new MSBuildTargetResult(file, false, "", "", file, 1, 1, 1, 1, "Unknown MSBuild failure. Please try building the project again", "");
                MSBuildResult       res = new MSBuildResult(new [] { err });
                return(res);
            } finally {
                engine.UnregisterLogger(loggerId);
                EndOperation();
                cr.Dispose();
            }
        }
		public Task<MSBuildResult> Run (
			ProjectConfigurationInfo[] configurations,
			ILogWriter logWriter,
			MSBuildVerbosity verbosity,
			string[] runTargets,
			string[] evaluateItems,
			string[] evaluateProperties,
			Dictionary<string, string> globalProperties,
			CancellationToken cancellationToken
		)
		{
			// Get an id for the task, and get ready to cancel it if the cancellation token is signalled
			var taskId = Interlocked.Increment (ref lastTaskId);
			var cr = RegisterCancellation (cancellationToken, taskId);

			var t = Task.Run (() => {
				try {
					BeginOperation ();
					var res = builder.Run (configurations, logWriter, verbosity, runTargets, evaluateItems, evaluateProperties, globalProperties, taskId);
					if (res == null && cancellationToken.IsCancellationRequested) {
						MSBuildTargetResult err = new MSBuildTargetResult (file, false, "", "", file, 1, 1, 1, 1, "Build cancelled", "");
						return new MSBuildResult (new [] { err });
					}
					if (res == null)
						throw new Exception ("Unknown failure");
					return res;
				} catch (Exception ex) {
					CheckDisconnected ();
					LoggingService.LogError ("RunTarget failed", ex);
					MSBuildTargetResult err = new MSBuildTargetResult (file, false, "", "", file, 1, 1, 1, 1, "Unknown MSBuild failure. Please try building the project again", "");
					MSBuildResult res = new MSBuildResult (new [] { err });
					return res;
				} finally {
					EndOperation ();
				}
			});

			// Dispose the cancel registration
			t.ContinueWith (r => cr.Dispose ());

			return t;
		}
		public MSBuildResult Run (
			ProjectConfigurationInfo[] configurations, ILogWriter logWriter, MSBuildVerbosity verbosity,
			string[] runTargets, string[] evaluateItems, string[] evaluateProperties, Dictionary<string,string> globalProperties, int taskId)
		{
			MSBuildResult result = null;
			BuildEngine.RunSTA (taskId, delegate {
				try {
					var project = SetupProject (configurations);
					InitLogger (logWriter);

					buildEngine.Engine.UnregisterAllLoggers ();

					var logger = new LocalLogger (file);
					buildEngine.Engine.RegisterLogger (logger);
					if (logWriter != null) {
						buildEngine.Engine.RegisterLogger (consoleLogger);
						consoleLogger.Verbosity = GetVerbosity (verbosity);
					}

					if (runTargets != null && runTargets.Length > 0) {
						if (globalProperties != null) {
							foreach (var p in globalProperties)
								project.GlobalProperties.SetProperty (p.Key, p.Value);
                        }

						// We are using this BuildProject overload and the BuildSettings.None argument as a workaround to
						// an xbuild bug which causes references to not be resolved after the project has been built once.
						buildEngine.Engine.BuildProject (project, runTargets, new Hashtable (), BuildSettings.None);

						if (globalProperties != null) {
							foreach (var p in globalProperties.Keys) {
								project.GlobalProperties.RemoveProperty (p);
								buildEngine.Engine.GlobalProperties.RemoveProperty (p);
							}
						}
					}

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

					if (evaluateProperties != null) {
						foreach (var name in evaluateProperties)
							result.Properties [name] = project.GetEvaluatedProperty (name);
					}

					if (evaluateItems != null) {
						foreach (var name in evaluateItems) {
							BuildItemGroup grp = project.GetEvaluatedItemsByName (name);
							var list = new List<MSBuildEvaluatedItem> ();
							foreach (BuildItem item in grp) {
								var evItem = new MSBuildEvaluatedItem (name, UnescapeString (item.FinalItemSpec));
								foreach (DictionaryEntry de in (IDictionary) evaluatedMetadataField.GetValue (item)) {
									evItem.Metadata [(string)de.Key] = UnescapeString ((string)de.Value);
								}
								list.Add (evItem);
							}
							result.Items[name] = list;
						}
					}
				} catch (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);
					LogWriteLine (r.ToString ());
					result = new MSBuildResult (new [] { r });
				} finally {
					DisposeLogger ();
				}
			});
			return result;
		}
        public MSBuildResult Run(
            ProjectConfigurationInfo[] configurations, IEngineLogWriter logWriter, MSBuildVerbosity verbosity,
            string[] runTargets, string[] evaluateItems, string[] evaluateProperties, Dictionary <string, string> globalProperties, int taskId)
        {
            MSBuildResult result = null;

            BuildEngine.RunSTA(taskId, delegate {
                try {
                    var project = SetupProject(configurations);
                    InitLogger(logWriter);

                    buildEngine.Engine.UnregisterAllLoggers();

                    var logger = new LocalLogger(file);
                    buildEngine.Engine.RegisterLogger(logger);
                    if (logWriter != null)
                    {
                        buildEngine.Engine.RegisterLogger(consoleLogger);
                        consoleLogger.Verbosity = GetVerbosity(verbosity);
                        buildEngine.Engine.RegisterLogger(new TargetLogger(logWriter.RequiredEvents, LogEvent));
                    }

                    if (runTargets != null && runTargets.Length > 0)
                    {
                        if (globalProperties != null)
                        {
                            foreach (var p in globalProperties)
                            {
                                project.GlobalProperties.SetProperty(p.Key, p.Value);
                            }
                        }

                        // We are using this BuildProject overload and the BuildSettings.None argument as a workaround to
                        // an xbuild bug which causes references to not be resolved after the project has been built once.
                        buildEngine.Engine.BuildProject(project, runTargets, new Hashtable(), BuildSettings.None);

                        if (globalProperties != null)
                        {
                            foreach (var p in globalProperties.Keys)
                            {
                                project.GlobalProperties.RemoveProperty(p);
                                buildEngine.Engine.GlobalProperties.RemoveProperty(p);
                            }
                        }
                    }

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

                    if (evaluateProperties != null)
                    {
                        foreach (var name in evaluateProperties)
                        {
                            result.Properties [name] = project.GetEvaluatedProperty(name);
                        }
                    }

                    if (evaluateItems != null)
                    {
                        foreach (var name in evaluateItems)
                        {
                            BuildItemGroup grp = project.GetEvaluatedItemsByName(name);
                            var list           = new List <MSBuildEvaluatedItem> ();
                            foreach (BuildItem item in grp)
                            {
                                var evItem = new MSBuildEvaluatedItem(name, UnescapeString(item.FinalItemSpec));
                                foreach (DictionaryEntry de in (IDictionary)evaluatedMetadataField.GetValue(item))
                                {
                                    evItem.Metadata [(string)de.Key] = UnescapeString((string)de.Value);
                                }
                                list.Add(evItem);
                            }
                            result.Items[name] = list.ToArray();
                        }
                    }
                } catch (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);
                    LogWriteLine(r.ToString());
                    result = new MSBuildResult(new [] { r });
                } finally {
                    DisposeLogger();
                }
            });
            return(result);
        }
        public MSBuildResult Run(
            ProjectConfigurationInfo[] configurations, IEngineLogWriter logWriter, MSBuildVerbosity verbosity,
            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);
                }

                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);
                            }
                        }
                        project.ReevaluateIfNecessary();
                    }

                    // 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);
                        }
                        project.ReevaluateIfNecessary();
                    }
                }
            });
            return(result);
        }
        public MSBuildResult Run(
            ProjectConfigurationInfo[] configurations, ILogWriter logWriter, MSBuildVerbosity verbosity,
            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 {
                try {
                    var project = SetupProject(configurations);
                    InitLogger(logWriter);

                    ILogger[] loggers;
                    var logger = new LocalLogger(file);
                    if (logWriter != null)
                    {
                        var consoleLogger = new ConsoleLogger(GetVerbosity(verbosity), LogWriteLine, null, null);
                        loggers           = new ILogger[] { logger, consoleLogger };
                    }
                    else
                    {
                        loggers = new ILogger[] { logger };
                    }

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

                    if (globalProperties != null)
                    {
                        foreach (var p in globalProperties)
                        {
                            pi.SetProperty(p.Key, p.Value);
                        }
                    }

                    pi.Build(runTargets, loggers);

                    result = new MSBuildResult(logger.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 m in item.Metadata)
                                {
                                    evItem.Metadata [m.Name] = UnescapeString(m.EvaluatedValue);
                                }
                                list.Add(evItem);
                            }
                            result.Items[name] = list;
                        }
                    }
                } 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);
                    LogWriteLine(r.ToString());
                    result = new MSBuildResult(new [] { r });
                } finally {
                    DisposeLogger();
                }
            });
            return(result);
        }
		public MSBuildResult (MSBuildTargetResult[] errors)
		{
			this.errors = errors;
			this.properties = new Dictionary<string,string> ();
			this.items = new Dictionary<string,List<MSBuildEvaluatedItem>> ();
		}
		public MSBuildResult Run (
			ProjectConfigurationInfo[] configurations, ILogWriter logWriter, MSBuildVerbosity verbosity,
			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 {
				try {
					var project = SetupProject (configurations);
					InitLogger (logWriter);

					ILogger[] loggers;
					var logger = new LocalLogger (file);
					if (logWriter != null) {
						var consoleLogger = new ConsoleLogger (GetVerbosity (verbosity), LogWriteLine, null, null);
						loggers = new ILogger[] { logger, consoleLogger };
					} else {
						loggers = new ILogger[] { logger };
					}

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

					if (globalProperties != null)
						foreach (var p in globalProperties)
							pi.SetProperty (p.Key, p.Value);
					
					pi.Build (runTargets, loggers);

					result = new MSBuildResult (logger.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 m in item.Metadata) {
									evItem.Metadata [m.Name] = UnescapeString (m.EvaluatedValue);
								}
								list.Add (evItem);
							}
							result.Items[name] = list;
						}
					}
				} 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);
					LogWriteLine (r.ToString ());
					result = new MSBuildResult (new [] { r });
				} finally {
					DisposeLogger ();
				}
			});
			return result;
		}