private static int RunAsDev(string[] args)
        {
            SetupLogger("IncrementalCompiler.log", true);

            var workDirectory = args[1];
            var reponseFile   = args[2];
            var settings      = Settings.Load() ?? Settings.Default;

            var logger = LogManager.GetLogger("Dev");

            logger.Info("Started");

            Directory.SetCurrentDirectory(workDirectory);
            var curPath = Directory.GetCurrentDirectory();

            var options = new CompileOptions();

            options.ParseArgument(new string[]
            {
                "-nostdlib+",
                "-noconfig",
                // Unity5
//                "-r:" + @"C:/Software/Unity-2018/Editor/Data/Mono/lib/mono/2.0/mscorlib.dll",
//                "-r:" + @"C:/Software/Unity-2018/Editor/Data/Mono/lib/mono/2.0/System.dll",
//                "-r:" + @"C:/Software/Unity-2018/Editor/Data/Mono/lib/mono/2.0/System.Core.dll",
//                "-r:" + @"C:/Software/Unity-2018/Editor/Data/Mono/lib/mono/2.0/System.Xml.dll",
                "@Temp/" + reponseFile,
            });

            options.WorkDirectory       = curPath;
            options.References          = options.References.Distinct().ToList();
            options.Files               = options.Files.Distinct().ToList();
            options.PrebuiltOutputReuse = settings.PrebuiltOutputReuse;

            var parentProcessId = Process.GetCurrentProcess().Id;

            Process?serverProcess = null;

            // new Thread(() => CompilerServiceServer.Run(logger, parentProcessId)).Start();

            while (true)
            {
                try
                {
                    var w = new Stopwatch();
                    w.Start();
                    Console.WriteLine("Run");

                    var result = CompilerServiceClient.Request(parentProcessId, curPath, options, false);

                    w.Stop();

                    Console.WriteLine("Done: Succeeded={0}. Duration={1}sec. ", result.Succeeded,
                                      w.Elapsed.TotalSeconds);
                    foreach (var warning in result.Warnings)
                    {
                        Console.WriteLine(warning);
                    }
                    foreach (var error in result.Errors)
                    {
                        Console.WriteLine(error);
                    }

                    Console.ReadLine();
                }
                catch (TimeoutException)
                {
                    if (serverProcess == null)
                    {
                        var a = new Thread(() => CompilerServiceServer.Run(logger, parentProcessId));
                        a.Start();
                        serverProcess = Process.GetCurrentProcess();

                        // serverProcess = Process.Start(
                        //     new ProcessStartInfo
                        //     {
                        //         FileName = Assembly.GetEntryAssembly().Location,
                        //         Arguments = "-server " + parentProcessId,
                        //         WindowStyle = ProcessWindowStyle.Hidden
                        //     });
                        //
                        Thread.Sleep(100);
                    }
                    else
                    {
                        if (serverProcess.HasExited == false)
                        {
                            Thread.Sleep(100);
                        }
                        else
                        {
                            serverProcess = null;
                        }
                    }
                }
            }
        }
示例#2
0
        private static int RunAsClient(string[] args)
        {
            SetupLogger("IncrementalCompiler.log", false);

            var logger = LogManager.GetLogger("Client");

            logger.Info("Started");

            Settings settings;

            try
            {
                settings = Settings.Load() ?? Settings.Default;
            }
            catch (Exception e)
            {
                logger.Error(e, "Failed in loading settings.");
                return(1);
            }

            var currentPath = Directory.GetCurrentDirectory();
            var options     = new CompileOptions();

            options.ParseArgument(args);
            options.WorkDirectory       = currentPath;
            options.References          = options.References.Distinct().ToList();
            options.Files               = options.Files.Distinct().ToList();
            options.PrebuiltOutputReuse = settings.PrebuiltOutputReuse;

            logger.Info("CurrentDir: {0}", Directory.GetCurrentDirectory());
            logger.Info("Output: {0}", options.Output);

            if (string.IsNullOrEmpty(options.Output))
            {
                logger.Error("No output");
                return(1);
            }

            // Get unity process ID

            var parentProcessId = 0;
            var pd = options.Defines.FirstOrDefault(d => d.StartsWith("__UNITY_PROCESSID__"));

            if (pd != null)
            {
                int.TryParse(pd.Substring(19), out parentProcessId);
            }
            else
            {
                var parentProcess = Process.GetProcessesByName("Unity").FirstOrDefault();
                if (parentProcess != null)
                {
                    parentProcessId = parentProcess.Id;
                }
            }

            if (parentProcessId == 0)
            {
                logger.Error("No parent process");
                return(1);
            }

            logger.Info("Parent process ID: {0}", parentProcessId);

            // Run

            var useCompilationServer = true;

            // it does not work on mac for some reason
            useCompilationServer &= PlatformHelper.CurrentPlatform == Platform.Windows;

            useCompilationServer = false;

            Process?serverProcess = null;

            while (true)
            {
                try
                {
                    var w = new Stopwatch();
                    w.Start();
                    logger.Info("Request to server");
                    var result = CompilerServiceClient.Request(parentProcessId, currentPath, options, useCompilationServer);
                    w.Stop();
                    logger.Info("Done: Succeeded={0}. Duration={1}sec.", result.Succeeded, w.Elapsed.TotalSeconds);
                    Console.WriteLine("Compile {0}. (Duration={1}sec)", result.Succeeded ? "succeeded" : "failed",
                                      w.Elapsed.TotalSeconds);
                    foreach (var warning in result.Warnings)
                    {
                        logger.Info(warning);
                        Console.Error.WriteLine(warning.Replace("\r\n", " ~~ ").Replace("\n", " ~~ "));
                    }
                    foreach (var error in result.Errors)
                    {
                        logger.Info(error);
                        Console.Error.WriteLine(error.Replace("\r\n", " ~~ ").Replace("\n", " ~~ "));
                    }
                    return(result.Succeeded ? 0 : 1);
                }
                catch (TimeoutException)
                {
                    if (serverProcess == null)
                    {
                        logger.Info("Spawn server");
                        serverProcess = Process.Start(
                            new ProcessStartInfo
                        {
                            FileName    = (Assembly.GetEntryAssembly() ?? typeof(Program).Assembly).Location,
                            Arguments   = "-server " + parentProcessId,
                            WindowStyle = ProcessWindowStyle.Hidden
                        });
                        Thread.Sleep(100);
                    }
                    else
                    {
                        if (serverProcess.HasExited == false)
                        {
                            Thread.Sleep(100);
                        }
                        else
                        {
                            return(1);
                        }
                    }
                }
                catch (Exception e)
                {
                    logger.Error(e, "Error in request");
                    Console.Error.WriteLine("Internal error: " + e);
                    return(1);
                }
            }
        }