Ejemplo n.º 1
0
        private static int Run(string[] args)
        {
            Console.WriteLine($"OmniSharp: {string.Join(" ", args)}");

            // omnisharp.json arguments should not be parsed by the CLI args parser
            // they will contain "=" so we should filter them out
            var omnisharpJsonArgs = args.Where(x => x.Contains("="));

            var omnisharpApp = new CommandLineApplication(throwOnUnexpectedArg: false);

            omnisharpApp.HelpOption("-? | -h | --help");

            var applicationRootOption  = omnisharpApp.Option("-s | --source", "Solution or directory for OmniSharp to point at (defaults to current directory).", CommandOptionType.SingleValue);
            var portOption             = omnisharpApp.Option("-p | --port", "OmniSharp port (defaults to 2000).", CommandOptionType.SingleValue);
            var logLevelOption         = omnisharpApp.Option("-l | --loglevel", "Level of logging (defaults to 'Information').", CommandOptionType.SingleValue);
            var verboseOption          = omnisharpApp.Option("-v | --verbose", "Explicitly set 'Debug' log level.", CommandOptionType.NoValue);
            var hostPidOption          = omnisharpApp.Option("-hpid | --hostPID", "Host process ID.", CommandOptionType.SingleValue);
            var stdioOption            = omnisharpApp.Option("-stdio | --stdio", "Use STDIO over HTTP as OmniSharp commincation protocol.", CommandOptionType.NoValue);
            var zeroBasedIndicesOption = omnisharpApp.Option("-z | --zero-based-indices", "Use zero based indices in request/responses (defaults to 'false').", CommandOptionType.NoValue);
            var serverInterfaceOption  = omnisharpApp.Option("-i | --interface", "Server interface address (defaults to 'localhost').", CommandOptionType.SingleValue);
            var encodingOption         = omnisharpApp.Option("-e | --encoding", "Input / output encoding for STDIO protocol.", CommandOptionType.SingleValue);
            var pluginOption           = omnisharpApp.Option("-pl | --plugin", "Plugin name(s).", CommandOptionType.MultipleValue);

            omnisharpApp.OnExecute(() =>
            {
                var applicationRoot            = applicationRootOption.GetValueOrDefault(Directory.GetCurrentDirectory());
                var serverPort                 = portOption.GetValueOrDefault(2000);
                var logLevel                   = verboseOption.HasValue() ? LogLevel.Debug : logLevelOption.GetValueOrDefault(LogLevel.Information);
                var hostPid                    = hostPidOption.GetValueOrDefault(-1);
                var transportType              = stdioOption.HasValue() ? TransportType.Stdio : TransportType.Http;
                var serverInterface            = serverInterfaceOption.GetValueOrDefault("localhost");
                var encodingString             = encodingOption.GetValueOrDefault <string>(null);
                var plugins                    = pluginOption.Values;
                var otherArgs                  = omnisharpApp.RemainingArguments.Union(omnisharpJsonArgs).Distinct();
                Configuration.ZeroBasedIndices = zeroBasedIndicesOption.HasValue();

                var env = new OmniSharpEnvironment(applicationRoot, serverPort, hostPid, logLevel, transportType, otherArgs.ToArray());

                var config = new ConfigurationBuilder()
                             .AddCommandLine(new[] { "--server.urls", $"http://{serverInterface}:{serverPort}" });

                // If the --encoding switch was specified, we need to set the InputEncoding and OutputEncoding before
                // constructing the SharedConsoleWriter. Otherwise, it might be created with the wrong encoding since
                // it wraps around Console.Out, which gets recreated when OutputEncoding is set.
                if (transportType == TransportType.Stdio && encodingString != null)
                {
                    var encoding           = Encoding.GetEncoding(encodingString);
                    Console.InputEncoding  = encoding;
                    Console.OutputEncoding = encoding;
                }

                var writer = new SharedConsoleWriter();

                var builder = new WebHostBuilder()
                              .UseConfiguration(config.Build())
                              .UseEnvironment("OmniSharp")
                              .ConfigureServices(serviceCollection =>
                {
                    serviceCollection.AddSingleton <IOmniSharpEnvironment>(env);
                    serviceCollection.AddSingleton <ISharedTextWriter>(writer);
                    serviceCollection.AddSingleton <PluginAssemblies>(new PluginAssemblies(plugins));
                    serviceCollection.AddSingleton <IAssemblyLoader, AssemblyLoader>();
                })
                              .UseStartup(typeof(Startup));

                if (transportType == TransportType.Stdio)
                {
                    builder.UseServer(new StdioServer(Console.In, writer));
                }
                else
                {
                    builder.UseKestrel();
                }

                using (var app = builder.Build())
                {
                    app.Start();

                    var appLifeTime = app.Services.GetRequiredService <IApplicationLifetime>();

                    Console.CancelKeyPress += (sender, e) =>
                    {
                        appLifeTime.StopApplication();
                        e.Cancel = true;
                    };

                    if (hostPid != -1)
                    {
                        try
                        {
                            var hostProcess = Process.GetProcessById(hostPid);
                            hostProcess.EnableRaisingEvents = true;
                            hostProcess.OnExit(() => appLifeTime.StopApplication());
                        }
                        catch
                        {
                            // If the process dies before we get here then request shutdown
                            // immediately
                            appLifeTime.StopApplication();
                        }
                    }

                    appLifeTime.ApplicationStopping.WaitHandle.WaitOne();
                }

                return(0);
            });

            return(omnisharpApp.Execute(args.Except(omnisharpJsonArgs).ToArray()));
        }
Ejemplo n.º 2
0
        private static int Run(string[] args)
        {
            Console.WriteLine($"ILSpy.Host: {string.Join(" ", args)}");

            var msilDecompilerApp = new CommandLineApplication(throwOnUnexpectedArg: false);

            msilDecompilerApp.HelpOption("-? | -h | --help");

            var assemblyPathOption    = msilDecompilerApp.Option("-a | --assembly", "Path to the managed assembly to decompile", CommandOptionType.SingleValue);
            var portOption            = msilDecompilerApp.Option("-p | --port", "ILSpy.Host port (defaults to 2000).", CommandOptionType.SingleValue);
            var logLevelOption        = msilDecompilerApp.Option("-l | --loglevel", "Level of logging (defaults to 'Information').", CommandOptionType.SingleValue);
            var verboseOption         = msilDecompilerApp.Option("-v | --verbose", "Explicitly set 'Debug' log level.", CommandOptionType.NoValue);
            var hostPidOption         = msilDecompilerApp.Option("-hpid | --hostPID", "Host process ID.", CommandOptionType.SingleValue);
            var stdioOption           = msilDecompilerApp.Option("-stdio | --stdio", "Use STDIO over HTTP as ILSpy.Host communication protocol.", CommandOptionType.NoValue);
            var encodingOption        = msilDecompilerApp.Option("-e | --encoding", "Input / output encoding for STDIO protocol.", CommandOptionType.SingleValue);
            var serverInterfaceOption = msilDecompilerApp.Option("-i | --interface", "Server interface address (defaults to 'localhost').", CommandOptionType.SingleValue);

            msilDecompilerApp.OnExecute(() =>
            {
                var assemblyPath    = assemblyPathOption.GetValueOrDefault <string>(null);
                var serverPort      = portOption.GetValueOrDefault(2000);
                var logLevel        = verboseOption.HasValue() ? LogLevel.Debug : logLevelOption.GetValueOrDefault(LogLevel.Information);
                var hostPid         = hostPidOption.GetValueOrDefault(-1);
                var transportType   = stdioOption.HasValue() ? TransportType.Stdio : TransportType.Http;
                var serverInterface = serverInterfaceOption.GetValueOrDefault("localhost");
                var encodingString  = encodingOption.GetValueOrDefault <string>(null);
                var otherArgs       = msilDecompilerApp.RemainingArguments.Distinct();

                var env = new MsilDecompilerEnvironment(assemblyPath, serverPort, hostPid, logLevel, transportType, otherArgs.ToArray());

                var config = new ConfigurationBuilder()
                             .AddCommandLine(new[] { "--server.urls", $"http://{serverInterface}:{serverPort}" });

                // If the --encoding switch was specified, we need to set the InputEncoding and OutputEncoding before
                // constructing the SharedConsoleWriter. Otherwise, it might be created with the wrong encoding since
                // it wraps around Console.Out, which gets recreated when OutputEncoding is set.
                if (transportType == TransportType.Stdio && encodingString != null)
                {
                    var encoding           = Encoding.GetEncoding(encodingString);
                    Console.InputEncoding  = encoding;
                    Console.OutputEncoding = encoding;
                }

                var writer = new SharedConsoleWriter();

                var builder = new WebHostBuilder()
                              .UseConfiguration(config.Build())
                              .UseEnvironment("ILSpyHost")
                              .ConfigureServices(serviceCollection =>
                {
                    serviceCollection.AddSingleton <IMsilDecompilerEnvironment>(env);
                    serviceCollection.AddSingleton <ISharedTextWriter>(writer);
                    serviceCollection.AddSingleton <IDecompilationProvider, SimpleDecompilationProvider>();
                })
                              .UseStartup(typeof(Startup));

                if (transportType == TransportType.Stdio)
                {
                    builder.UseServer(new StdioServer(Console.In, writer));
                }
                else
                {
                    builder.UseKestrel();
                }

                using (var app = builder.Build())
                {
                    app.Start();

                    var appLifeTime = app.Services.GetRequiredService <IApplicationLifetime>();

                    Console.CancelKeyPress += (sender, e) =>
                    {
                        appLifeTime.StopApplication();
                        e.Cancel = true;
                    };

                    if (hostPid != -1)
                    {
                        try
                        {
                            var hostProcess = Process.GetProcessById(hostPid);
                            hostProcess.EnableRaisingEvents = true;
                            hostProcess.OnExit(() => appLifeTime.StopApplication());
                        }
                        catch
                        {
                            // If the process dies before we get here then request shutdown
                            // immediately
                            appLifeTime.StopApplication();
                        }
                    }

                    appLifeTime.ApplicationStopping.WaitHandle.WaitOne();
                }

                return(0);
            });

            return(msilDecompilerApp.Execute(args.ToArray()));
        }
Ejemplo n.º 3
0
        public static void Main(string[] args)
        {
            Console.WriteLine($"OmniSharp: {string.Join(" ", args)}");

            var applicationRoot = Directory.GetCurrentDirectory();
            var serverPort      = 2000;
            var logLevel        = LogLevel.Information;
            var hostPID         = -1;
            var transportType   = TransportType.Http;
            var otherArgs       = new List <string>();
            var plugins         = new List <string>();

            var enumerator = args.GetEnumerator();

            while (enumerator.MoveNext())
            {
                var arg = (string)enumerator.Current;
                if (arg == "-s")
                {
                    enumerator.MoveNext();
                    applicationRoot = Path.GetFullPath((string)enumerator.Current);
                }
                else if (arg == "-p")
                {
                    enumerator.MoveNext();
                    serverPort = int.Parse((string)enumerator.Current);
                }
                else if (arg == "-v")
                {
                    logLevel = LogLevel.Debug;
                }
                else if (arg == "--hostPID")
                {
                    enumerator.MoveNext();
                    hostPID = int.Parse((string)enumerator.Current);
                }
                else if (arg == "--stdio")
                {
                    transportType = TransportType.Stdio;
                }
                else if (arg == "--plugin")
                {
                    enumerator.MoveNext();
                    plugins.Add((string)enumerator.Current);
                }
                else
                {
                    otherArgs.Add((string)enumerator.Current);
                }
            }

            Environment = new OmnisharpEnvironment(applicationRoot, serverPort, hostPID, logLevel, transportType, otherArgs.ToArray());

            var config = new ConfigurationBuilder()
                         .AddCommandLine(new[] { "--server.urls", "http://localhost:" + serverPort });

            var writer = new SharedConsoleWriter();

            var builder = new WebHostBuilder()
                          .UseConfiguration(config.Build())
                          .UseEnvironment("OmniSharp")
                          .UseStartup(typeof(Startup))
                          .ConfigureServices(serviceCollection =>
            {
                serviceCollection.AddSingleton <IOmnisharpEnvironment>(Environment);
                serviceCollection.AddSingleton <ISharedTextWriter>(writer);
                serviceCollection.AddSingleton <PluginAssemblies>(new PluginAssemblies(plugins));
                serviceCollection.AddSingleton <IOmnisharpAssemblyLoader>(new OmnisharpAssemblyLoader());
            });

            if (transportType == TransportType.Stdio)
            {
                builder.UseServer(new StdioServer(Console.In, writer));
            }
            else
            {
                builder.UseServer("Microsoft.AspNetCore.Server.Kestrel");
            }

            using (var app = builder.Build())
            {
                app.Start();

                var appLifeTime = app.Services.GetRequiredService <IApplicationLifetime>();

                Console.CancelKeyPress += (sender, e) =>
                {
                    appLifeTime.StopApplication();
                    e.Cancel = true;
                };

                if (hostPID != -1)
                {
                    try
                    {
                        var hostProcess = Process.GetProcessById(hostPID);
                        hostProcess.EnableRaisingEvents = true;
                        hostProcess.OnExit(() => appLifeTime.StopApplication());
                    }
                    catch
                    {
                        // If the process dies before we get here then request shutdown
                        // immediately
                        appLifeTime.StopApplication();
                    }
                }

                appLifeTime.ApplicationStopping.WaitHandle.WaitOne();
            }
        }
Ejemplo n.º 4
0
        public void Main(string[] args)
        {
            var applicationRoot = Directory.GetCurrentDirectory();
            var serverPort      = 2000;
            var logLevel        = LogLevel.Information;
            var hostPID         = -1;
            var transportType   = TransportType.Http;
            var otherArgs       = new List <string>();

            var enumerator = args.GetEnumerator();

            while (enumerator.MoveNext())
            {
                var arg = (string)enumerator.Current;
                if (arg == "-s")
                {
                    enumerator.MoveNext();
                    applicationRoot = Path.GetFullPath((string)enumerator.Current);
                }
                else if (arg == "-p")
                {
                    enumerator.MoveNext();
                    serverPort = int.Parse((string)enumerator.Current);
                }
                else if (arg == "-v")
                {
                    logLevel = LogLevel.Verbose;
                }
                else if (arg == "--hostPID")
                {
                    enumerator.MoveNext();
                    hostPID = int.Parse((string)enumerator.Current);
                }
                else if (arg == "--stdio")
                {
                    transportType = TransportType.Stdio;
                }
                else
                {
                    otherArgs.Add((string)enumerator.Current);
                }
            }

            Environment = new OmnisharpEnvironment(applicationRoot, serverPort, hostPID, logLevel, transportType, otherArgs.ToArray());

            var config = new Configuration()
                         .AddCommandLine(new[] { "--server.urls", "http://localhost:" + serverPort });

            var engine = new HostingEngine(_serviceProvider);

            var context = new HostingContext()
            {
                ServerFactoryLocation = "Kestrel",
                Configuration         = config,
            };

            var writer = new SharedConsoleWriter();

            context.Services.AddInstance <IOmnisharpEnvironment>(Environment);
            context.Services.AddInstance <ISharedTextWriter>(writer);

            if (transportType == TransportType.Stdio)
            {
                context.Server        = null;
                context.ServerFactory = new Stdio.StdioServerFactory(Console.In, writer);
            }

            var serverShutdown = engine.Start(context);

            var appShutdownService = _serviceProvider.GetRequiredService <IApplicationShutdown>();
            var shutdownHandle     = new ManualResetEvent(false);

            appShutdownService.ShutdownRequested.Register(() =>
            {
                serverShutdown.Dispose();
                shutdownHandle.Set();
            });

#if DNXCORE50
            var ignored = Task.Run(() =>
            {
                Console.WriteLine("Started");
                Console.ReadLine();
                appShutdownService.RequestShutdown();
            });
#else
            Console.CancelKeyPress += (sender, e) =>
            {
                appShutdownService.RequestShutdown();
            };
#endif

            if (hostPID != -1)
            {
                try
                {
                    var hostProcess = Process.GetProcessById(hostPID);
                    hostProcess.EnableRaisingEvents = true;
                    hostProcess.OnExit(() => appShutdownService.RequestShutdown());
                }
                catch
                {
                    // If the process dies before we get here then request shutdown
                    // immediately
                    appShutdownService.RequestShutdown();
                }
            }

            shutdownHandle.WaitOne();
        }