ClrPlus.Scripting.MsBuild.Building.BuildScript.Execute C# (CSharp) Метод

Execute() публичный Метод

public Execute ( string targets = null ) : bool
targets string
Результат bool
        public bool Execute(string[] targets = null)
        {
            _sheet.CopyToModel();

            targets = targets ?? new string[0];

            var path = Save();
            var result = true;

            try {
                //When a call comes, do the following:
                server.Value.OnExecute += ServerOnOnExecute;

                // Event<Verbose>.Raise("script", "\r\n\r\n{0}\r\n\r\n", File.ReadAllText(path));

                var targs = targets.IsNullOrEmpty() ? string.Empty : targets.Aggregate("/target:", (cur, each) => cur + each + ";").TrimEnd(';');

                var etcPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "etc") + "/";
                Environment.SetEnvironmentVariable("CoAppEtcDirectory", etcPath);

                // remove some variables...
                Environment.SetEnvironmentVariable("BuildingInsideVisualStudio", null);
                Environment.SetEnvironmentVariable("UsePTKFromVisualStudio", null);

                EnvironmentUtility.EnvironmentPath = EnvironmentUtility.EnvironmentPath.Append(EnvironmentUtility.DotNetFrameworkFolder);

                var proc = Process.Start(new ProcessStartInfo(@"{0}\MSBuild.exe".format(EnvironmentUtility.DotNetFrameworkFolder),
                    @" /nologo /noconsolelogger ""/logger:ClrPlus.Scripting.MsBuild.Building.Logger,{0};{1};{2}"" /m:{6} /p:MaxThreads={6} ""/p:CoAppEtcDirectory={3}"" {4} ""{5}""".format(Assembly.GetExecutingAssembly().Location, pipeName, iid, etcPath, targs,
                        path, MaxThreads > 0 ? MaxThreads : Environment.ProcessorCount)) {
                            RedirectStandardError = true,
                            RedirectStandardOutput = true,
                            UseShellExecute = false,
                        });

                while (!proc.HasExited) {
                    // check our messages -- we need to work on the calling thread.
                    // Thanks powershell, I appreciate working like it's 1989 again.
                    Thread.Sleep(20); // not so tight of loop.

                    lock (messages) {
                        while (messages.Any()) {
                            var obj = messages.Dequeue();
                            if (obj != null) {
                                if (BuildMessage != null) {
                                    BuildMessage(obj);
                                }

                                Event<MSBuildMessage>.Raise(obj);

                                if (obj.EventType == "BuildError") {
                                    result = false;
                                }
                            }

                            /* switch (obj.EventType) {
                                case "BuildWarning":
                                    Event<SourceWarning>.Raise(obj.Code,  new SourceLocation{ Column = obj.ColumnNumber, Row = obj.LineNumber , SourceFile = obj.File}.SingleItemAsEnumerable(), obj.Message);
                                    break;

                                case "BuildError":
                                    Event<SourceError>.Raise(obj.Code, new SourceLocation {
                                        Column = obj.ColumnNumber,
                                        Row = obj.LineNumber,
                                        SourceFile = obj.File
                                    }.SingleItemAsEnumerable(), obj.Message);
                                    result = false;
                                    break;

                                case "ProjectStarted":
                                case "ProjectFinished":
                                case "TaskStarted":
                                case "TaskFinished":
                                case "TargetStarted":
                                case "TargetFinished":
                                case "BuildStarted":
                                case "BuildFinished":
                                    Event<Verbose>.Raise(obj.EventType, obj.Message);
                                    break;

                                case "BuildMessage":
                                    if (filterMessages.Any(each => obj.Message.IndexOf(each) > -1)) {
                                        Event<Verbose>.Raise("", obj.Message);
                                    }
                                    else {
                                        Event<Message>.Raise("", obj.Message);
                                    }

                                    break;

                                default:
                                    Event<Message>.Raise(obj.EventType, obj.Message);
                                    break;
                            } */

                        }
                    }
                }
                while (!proc.WaitForExit(20)) {
                    if (_stop) {
                        proc.Kill();
                    }
                }

                var stderr = proc.StandardError.ReadToEnd();
                if (stderr.Is()) {
                    Event<Error>.Raise("stderr", stderr);
                }

                var stdout = proc.StandardOutput.ReadToEnd();
                if (stdout.Is()) {
                    Event<Verbose>.Raise("stdout", stdout);
                }

                path.TryHardToDelete();
            }
            catch {
                result = false;
            }
            finally {
                server.Value.OnExecute -= ServerOnOnExecute;
            }
            return result;
        }

Usage Example

Пример #1
0
        private void Start(string[] args) {
            CurrentTask.Events += new SourceError((code, location, message, objects) => {
                location = location ?? SourceLocation.Unknowns;
                Console.WriteLine("{0}:Error {1}:{2}", location.FirstOrDefault(), code, message.format(objects));
                return true;
            });

            CurrentTask.Events += new SourceWarning((code, location, message, objects) => {
                location = location ?? SourceLocation.Unknowns;
                Console.WriteLine("{0}:Warning {1}:{2}", location.FirstOrDefault(), message.format(objects));
                return false;
            });

            CurrentTask.Events += new SourceDebug((code, location, message, objects) => {
                location = location ?? SourceLocation.Unknowns;
                Console.WriteLine("{0}:DebugMessage {1}:{2}", location.FirstOrDefault(), code, message.format(objects));
                return false;
            });

            CurrentTask.Events += new Error((code, message, objects) => {
                Console.WriteLine("{0}:Error {1}", code, message.format(objects));
                return true;
            });

            CurrentTask.Events += new Warning((code, message, objects) => {
                Console.WriteLine("{0}:Warning {1}", code, message.format(objects));
                return false;
            });

            CurrentTask.Events += new Debug((code, message, objects) => {
                Console.WriteLine("{0}:DebugMessage {1}", code, message.format(objects));
                return false;
            });

            CurrentTask.Events += new Verbose((code, message, objects) => {
                Console.WriteLine("{0}:Verbose {1}", code, message.format(objects));
                return false;
            });
            CurrentTask.Events += new Message((code, message, objects) => {
                Console.WriteLine("{0}:Message {1}", code, message.format(objects));
                return false;
            });

#if true

            try {
                Environment.CurrentDirectory = @"C:\root\V2\coapp-packages\openssl\copkg";
                Console.WriteLine("Package script");
                using(var script = new PackageScript("openssl.autopkg")) {

                    IEnumerable<string> overlayFiles;
                    var pkgFile = script.Save(PackageTypes.NuGet, false, false, out overlayFiles);
                }
                Console.WriteLine();
            } catch (Exception e) {
                Console.WriteLine("{0} =>\r\n\r\nat {1}", e.Message, e.StackTrace.Replace("at ClrPlus.Scripting.Languages.PropertySheetV3.PropertySheetParser", "PropertySheetParser"));
            }
#else
            try {
                // Environment.CurrentDirectory = @"C:\project";
                Console.WriteLine("Build script");
                using (var script = new BuildScript("test.buildinfo")) {
                    script.Execute();
                }
            } catch (Exception e) {
                Console.WriteLine("{0} =>\r\n\r\nat {1}", e.Message, e.StackTrace.Replace("at ClrPlus.Scripting.Languages.PropertySheetV3.PropertySheetParser", "PropertySheetParser"));
            }

#endif
            return;
            //
        }
All Usage Examples Of ClrPlus.Scripting.MsBuild.Building.BuildScript::Execute