Exemple #1
0
        private static bool ProcessArgs(string commandLine, string[] args)
        {
            if (args.Length > 0)
            {
                switch (args[0].ToLower())
                {
                case "exit":
                case "close":
                case "quit":
                case "q":
                    return(true);

                case "clear":
                case "cls":
                    Console.Clear();
                    break;

                case "build":
                    CompileCs(args);
                    CompileCpp(args);
                    Console.WriteLine("done");
                    break;

                case "buildcs":
                    CompileCs(args);
                    Console.WriteLine("done");
                    break;

                case "buildcustomsln":
                    if (args.Length >= 3 &&
                        !string.IsNullOrEmpty(args[1]) && File.Exists(args[1]) &&
                        !string.IsNullOrEmpty(args[2]) && File.Exists(args[2]))
                    {
                        BuildCustomSolution(args[1], args[2]);
                    }
                    break;

                case "buildcpp":
                {
                    Stopwatch cppCompileTime = new Stopwatch();
                    cppCompileTime.Start();
                    CompileCpp(args);
                    Console.WriteLine("done (" + cppCompileTime.Elapsed + ")");
                }
                break;

                case "copyruntime":
                    if (args.Length >= 2)
                    {
                        bool   minimal    = args.Length >= 3 && args[2].ToLower() == "min";
                        string configFile = NetRuntimeHelper.GetConfigFile(minimal);

                        if (File.Exists(configFile))
                        {
                            switch (args[1].ToLower())
                            {
                            case "all":
                                NetRuntimeHelper.CopyAll(minimal);
                                Console.WriteLine("done");
                                break;

                            case "mono":
                                NetRuntimeHelper.CopyMono(minimal);
                                Console.WriteLine("done");
                                break;

                            case "coreclr":
                                NetRuntimeHelper.CopyCoreCLR(minimal);
                                Console.WriteLine("done");
                                break;
                            }
                        }
                        else
                        {
                            Console.WriteLine("Couldn't find '" + configFile + "'");
                        }
                    }
                    else
                    {
                        Console.WriteLine("Invalid input. Expected one of the following: all, mono, coreclr");
                    }
                    break;

                case "help":
                case "?":
                    PrintHelp();
                    break;
                }
            }

            return(false);
        }
Exemple #2
0
        private static bool BuildCs(string solutionPath, string projectPath, bool debug, bool x86, string customDefines)
        {
            string buildLogFile = Path.Combine(GetCurrentDirectory(), "build.log");

            if (!string.IsNullOrEmpty(solutionPath) && File.Exists(solutionPath))
            {
                solutionPath = Path.GetFullPath(solutionPath);
            }
            if (!string.IsNullOrEmpty(projectPath) && File.Exists(projectPath))
            {
                projectPath = Path.GetFullPath(projectPath);
            }

            if (string.IsNullOrEmpty(msbuildPath))
            {
                msbuildPath = NetRuntimeHelper.FindMsBuildPath();
            }

            if (string.IsNullOrEmpty(msbuildPath))
            {
                File.AppendAllText(buildLogFile, "Couldn't find MSBuild path" + Environment.NewLine);
                return(false);
            }

            string config   = debug ? "Debug" : "Release";
            string platform = x86 ? "x86" : "\"Any CPU\"";
            string fileArgs = "\"" + solutionPath + "\"" + " /p:Configuration=" + config + " /p:Platform=" + platform;

            if (!string.IsNullOrEmpty(projectPath))
            {
                // '.' must be replaced with '_' for /t
                string projectName = Path.GetFileNameWithoutExtension(projectPath).Replace(".", "_");

                // Skip project references just in case (this means projects should be built in the correct order though)
                fileArgs += " /t:" + projectName + " /p:BuildProjectReferences=false";
            }
            if (!string.IsNullOrEmpty(customDefines))
            {
                Debug.Assert(!customDefines.Contains(' '));
                fileArgs += " /p:DefineConstants=" + customDefines;
            }

            File.AppendAllText(buildLogFile, "Build: " + msbuildPath + " - " + fileArgs + Environment.NewLine);

            using (Process process = new Process())
            {
                process.StartInfo = new ProcessStartInfo
                {
                    FileName  = msbuildPath,
                    Arguments = fileArgs,
                    RedirectStandardOutput = true,
                    RedirectStandardError  = true,
                    UseShellExecute        = false,
                    CreateNoWindow         = true
                };

                StringBuilder output = new StringBuilder();
                StringBuilder error  = new StringBuilder();

                using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false))
                    using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false))
                    {
                        process.OutputDataReceived += (sender, e) =>
                        {
                            if (e.Data == null)
                            {
                                outputWaitHandle.Set();
                            }
                            else
                            {
                                output.AppendLine(e.Data);
                            }
                        };
                        process.ErrorDataReceived += (sender, e) =>
                        {
                            if (e.Data == null)
                            {
                                errorWaitHandle.Set();
                            }
                            else
                            {
                                error.AppendLine(e.Data);
                            }
                        };

                        process.Start();

                        process.BeginOutputReadLine();
                        process.BeginErrorReadLine();

                        int  timeout = 60000;
                        bool built   = process.WaitForExit(timeout) && outputWaitHandle.WaitOne(timeout) && errorWaitHandle.WaitOne(timeout);

                        File.AppendAllText(buildLogFile, "Build sln '" + solutionPath + "' proj '" + projectPath + "'" + Environment.NewLine);
                        File.AppendAllText(buildLogFile, string.Empty.PadLeft(100, '-') + Environment.NewLine);
                        File.AppendAllText(buildLogFile, output.ToString() + Environment.NewLine);
                        File.AppendAllText(buildLogFile, error.ToString() + Environment.NewLine + Environment.NewLine);

                        if (!built)
                        {
                            Console.WriteLine("Failed to wait for compile.");
                        }

                        return(built && process.ExitCode == 0);
                    }
            }
        }