public NodeDebugger(
            string exe,
            string script,
            string dir,
            string env,
            string interpreterOptions,
            NodeDebugOptions debugOptions,
            ushort? debuggerPort = null,
            bool createNodeWindow = true)
            : this() {
            // Select debugger port for a local connection
            ushort debuggerPortOrDefault = NodejsConstants.DefaultDebuggerPort;
            if (debuggerPort != null) {
                debuggerPortOrDefault = debuggerPort.Value;
            } else {
                List<int> activeConnections =
                    (from listener in IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners()
                     select listener.Port).ToList();
                if (activeConnections.Contains(debuggerPortOrDefault)) {
                    debuggerPortOrDefault = (ushort)Enumerable.Range(new Random().Next(5859, 6000), 60000).Except(activeConnections).First();
                }
            }

            _debuggerEndpointUri = new UriBuilder { Scheme = "tcp", Host = "localhost", Port = debuggerPortOrDefault }.Uri;

            // Node usage: node [options] [ -e script | script.js ] [arguments]
            string allArgs = String.Format(
                "--debug-brk={0} --nolazy {1} {2}",
                debuggerPortOrDefault,
                interpreterOptions,
                script
            );

            var psi = new ProcessStartInfo(exe, allArgs) {
                CreateNoWindow = !createNodeWindow,
                WorkingDirectory = dir,
                UseShellExecute = false
            };

            if (env != null) {
                string[] envValues = env.Split('\0');
                foreach (string curValue in envValues) {
                    string[] nameValue = curValue.Split(new[] { '=' }, 2);
                    if (nameValue.Length == 2 && !String.IsNullOrWhiteSpace(nameValue[0])) {
                        psi.EnvironmentVariables[nameValue[0]] = nameValue[1];
                    }
                }
            }

            _process = new NodeProcess(
                psi,
                debugOptions.HasFlag(NodeDebugOptions.WaitOnAbnormalExit),
                debugOptions.HasFlag(NodeDebugOptions.WaitOnNormalExit),
                true);
        }
        internal NodeDebugger DebugProcess(
            string filename,
            Action<NodeDebugger> onProcessCreated = null,
            Action<NodeDebugger, NodeThread> onLoadComplete = null,
            string interpreterOptions = null,
            NodeDebugOptions debugOptions = NodeDebugOptions.None,
            string cwd = null,
            string arguments = "",
            bool resumeOnProcessLoad = true
        ) {
            if (!Path.IsPathRooted(filename)) {
                filename = DebuggerTestPath + filename;
            }
            string fullPath = Path.GetFullPath(filename);
            string dir = cwd ?? Path.GetFullPath(Path.GetDirectoryName(filename));
            if (!String.IsNullOrEmpty(arguments)) {
                arguments = "\"" + fullPath + "\" " + arguments;
            } else {
                arguments = "\"" + fullPath + "\"";
            }

            // Load process
            AutoResetEvent processLoaded = new AutoResetEvent(false);
            Assert.IsNotNull(Nodejs.NodeExePath, "Node isn't installed");
            NodeDebugger process =
                new NodeDebugger(
                    Nodejs.NodeExePath,
                    arguments,
                    dir,
                    null,
                    interpreterOptions,
                    debugOptions,
                    null,
                    createNodeWindow: false);
            if (onProcessCreated != null) {
                onProcessCreated(process);
            }
            process.ProcessLoaded += (sender, args) => {
                // Invoke onLoadComplete delegate, if requested
                if (onLoadComplete != null) {
                    onLoadComplete(process, args.Thread);
                }
                processLoaded.Set();
            };
            process.Start();
            AssertWaited(processLoaded);

            // Resume, if requested
            if (resumeOnProcessLoad) {
                process.Resume();
            }

            return process;
        }
Esempio n. 3
0
        internal NodeDebugger DebugProcess(
            string filename,
            Action <NodeDebugger> onProcessCreated           = null,
            Action <NodeDebugger, NodeThread> onLoadComplete = null,
            string interpreterOptions     = null,
            NodeDebugOptions debugOptions = NodeDebugOptions.None,
            string cwd               = null,
            string arguments         = "",
            bool resumeOnProcessLoad = true
            )
        {
            if (!Path.IsPathRooted(filename))
            {
                filename = DebuggerTestPath + filename;
            }
            string fullPath = Path.GetFullPath(filename);
            string dir      = cwd ?? Path.GetFullPath(Path.GetDirectoryName(filename));

            if (!String.IsNullOrEmpty(arguments))
            {
                arguments = "\"" + fullPath + "\" " + arguments;
            }
            else
            {
                arguments = "\"" + fullPath + "\"";
            }

            // Load process
            AutoResetEvent processLoaded = new AutoResetEvent(false);

            Assert.IsNotNull(Nodejs.NodeExePath, "Node isn't installed");
            NodeDebugger process =
                new NodeDebugger(
                    Nodejs.NodeExePath,
                    arguments,
                    dir,
                    null,
                    interpreterOptions,
                    debugOptions,
                    null,
                    createNodeWindow: false);

            if (onProcessCreated != null)
            {
                onProcessCreated(process);
            }
            process.ProcessLoaded += (sender, args) => {
                // Invoke onLoadComplete delegate, if requested
                if (onLoadComplete != null)
                {
                    onLoadComplete(process, args.Thread);
                }
                processLoaded.Set();
            };
            process.Start();
            AssertWaited(processLoaded);

            // Resume, if requested
            if (resumeOnProcessLoad)
            {
                process.Resume();
            }

            return(process);
        }