Ejemplo n.º 1
0
        public NpmScriptRunner(string workingDirectory, string scriptName, string arguments)
        {
            if (string.IsNullOrEmpty(workingDirectory))
            {
                throw new ArgumentException("Cannot be null or empty.", nameof(workingDirectory));
            }

            if (string.IsNullOrEmpty(scriptName))
            {
                throw new ArgumentException("Cannot be null or empty.", nameof(scriptName));
            }

            var npmExe            = "npm";
            var completeArguments = $"run {scriptName} -- {arguments ?? string.Empty}";

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                npmExe            = "cmd";
                completeArguments = $"/c npm {completeArguments}";
            }

            var process = LaunchNodeProcess(new ProcessStartInfo(npmExe)
            {
                Arguments              = completeArguments,
                UseShellExecute        = false,
                RedirectStandardInput  = true,
                RedirectStandardOutput = true,
                RedirectStandardError  = true,
                WorkingDirectory       = workingDirectory
            });

            StdOut = new EventedStreamReader(process.StandardOutput);
            StdErr = new EventedStreamReader(process.StandardError);
        }
Ejemplo n.º 2
0
        public NodeScriptRunner(string workingDirectory, string scriptName, string arguments, IDictionary <string, string> envVars, string pkgManagerCommand)
        {
            if (string.IsNullOrEmpty(workingDirectory))
            {
                throw new ArgumentException("Cannot be null or empty.", nameof(workingDirectory));
            }

            if (string.IsNullOrEmpty(scriptName))
            {
                throw new ArgumentException("Cannot be null or empty.", nameof(scriptName));
            }

            if (string.IsNullOrEmpty(pkgManagerCommand))
            {
                throw new ArgumentException("Cannot be null or empty.", nameof(pkgManagerCommand));
            }

            var exeToRun          = pkgManagerCommand;
            var completeArguments = $"run {scriptName} -- {arguments ?? string.Empty}";

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                // On Windows, the node executable is a .cmd file, so it can't be executed
                // directly (except with UseShellExecute=true, but that's no good, because
                // it prevents capturing stdio). So we need to invoke it via "cmd /c".
                exeToRun          = "cmd";
                completeArguments = $"/c {pkgManagerCommand} {completeArguments}";
            }

            var processStartInfo = new ProcessStartInfo(exeToRun)
            {
                Arguments              = completeArguments,
                UseShellExecute        = false,
                RedirectStandardInput  = true,
                RedirectStandardOutput = true,
                RedirectStandardError  = true,
                WorkingDirectory       = workingDirectory
            };

            if (envVars != null)
            {
                foreach (var keyValuePair in envVars)
                {
                    processStartInfo.Environment[keyValuePair.Key] = keyValuePair.Value;
                }
            }

            var process = LaunchNodeProcess(processStartInfo, pkgManagerCommand);

            StdOut = new EventedStreamReader(process.StandardOutput);
            StdErr = new EventedStreamReader(process.StandardError);
        }
Ejemplo n.º 3
0
        public ScriptRunner(string workingDirectory, string scriptName, string arguments, IDictionary <string, string> envVars, PackageManager manager)
        {
            var packageManager = Enum.GetName(typeof(PackageManager), manager).ToLower();

            if (string.IsNullOrEmpty(workingDirectory))
            {
                throw new ArgumentException("Cannot be null or empty.", nameof(workingDirectory));
            }

            if (string.IsNullOrEmpty(scriptName))
            {
                throw new ArgumentException("Cannot be null or empty.", nameof(scriptName));
            }
            var isAureliaCliScript = scriptName.ToLower().StartsWith("au");
            var packageManagerExe  = isAureliaCliScript ? string.Empty : packageManager;
            var completeArguments  = isAureliaCliScript ? $"{scriptName} {arguments ?? string.Empty}" :  $"run {scriptName} -- {arguments ?? string.Empty}";

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                // On Windows, the NPM executable is a .cmd file, so it can't be executed
                // directly (except with UseShellExecute=true, but that's no good, because
                // it prevents capturing stdio). So we need to invoke it via "cmd /c".
                completeArguments = $"/c {packageManagerExe} {completeArguments}";
                packageManagerExe = "cmd";
            }

            var processStartInfo = new ProcessStartInfo(packageManagerExe)
            {
                Arguments              = completeArguments,
                UseShellExecute        = false,
                RedirectStandardInput  = true,
                RedirectStandardOutput = true,
                RedirectStandardError  = true,
                WorkingDirectory       = workingDirectory
            };

            if (envVars != null)
            {
                foreach (var keyValuePair in envVars)
                {
                    processStartInfo.Environment[keyValuePair.Key] = keyValuePair.Value;
                }
            }

            var process = LaunchNodeProcess(processStartInfo, packageManager);

            StdOut = new EventedStreamReader(process.StandardOutput);
            StdErr = new EventedStreamReader(process.StandardError);
        }
Ejemplo n.º 4
0
        public NpmScriptRunner(
            string workingDirectory,
            string scriptName,
            string arguments,
            IDictionary <string, string> envVars)
        {
            if (string.IsNullOrEmpty(workingDirectory))
            {
                throw new ArgumentException("不能为空.", nameof(workingDirectory));
            }
            if (string.IsNullOrEmpty(scriptName))
            {
                throw new ArgumentException("不能为空.", nameof(scriptName));
            }
            var fileName = "npm";
            var str      = "run " + scriptName + " -- " + (arguments ?? string.Empty);

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                fileName = "cmd";
                str      = "/c npm " + str;
            }

            var startInfo = new ProcessStartInfo(fileName)
            {
                Arguments              = str,
                UseShellExecute        = false,
                RedirectStandardInput  = true,
                RedirectStandardOutput = true,
                RedirectStandardError  = true,
                WorkingDirectory       = workingDirectory
            };

            if (envVars != null)
            {
                foreach (var envVar in envVars)
                {
                    startInfo.Environment[envVar.Key] = envVar.Value;
                }
            }
            var process = LaunchNodeProcess(startInfo);

            StdOut = new EventedStreamReader(process.StandardOutput);
            StdErr = new EventedStreamReader(process.StandardError);
        }
        public async Task ReadChunks_MultipleNewLines_OnCompleteLineFiresEachNewline()
        {
            string testMessage    = "this \nis \na \ntest \nof \nmultiple \nnewlines\n  trailing data";
            int    numNewLines    = testMessage.Split('\n').Length - 1;
            int    chunksReceived = 0;
            int    linesReceived  = 0;

            using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(testMessage)))
            {
                var streamReader = new StreamReader(ms);

                var esr = new EventedStreamReader(streamReader);
                esr.OnReceivedChunk += (e) => Interlocked.Increment(ref chunksReceived);
                esr.OnReceivedLine  += (e) => Interlocked.Increment(ref linesReceived);
                await Task.Delay(200);
            }

            Assert.AreEqual(numNewLines, linesReceived);
        }
Ejemplo n.º 6
0
        public DotnetScriptRunner(string workingDirectory, string scriptName, string arguments,
                                  IDictionary <string, string> envVars)
        {
            if (string.IsNullOrEmpty(workingDirectory))
            {
                throw new ArgumentException("Cannot be null or empty.", nameof(workingDirectory));
            }

            if (string.IsNullOrEmpty(scriptName))
            {
                throw new ArgumentException("Cannot be null or empty.", nameof(scriptName));
            }

            var dotnetExe         = "dotnet";
            var completeArguments = $"{scriptName} {arguments ?? string.Empty}";

            var processStartInfo = new ProcessStartInfo(dotnetExe)
            {
                Arguments              = completeArguments,
                UseShellExecute        = false,
                RedirectStandardInput  = true,
                RedirectStandardOutput = true,
                RedirectStandardError  = true,
                WorkingDirectory       = workingDirectory
            };

            if (envVars != null)
            {
                foreach (var keyValuePair in envVars)
                {
                    processStartInfo.Environment[keyValuePair.Key] = keyValuePair.Value;
                }
            }

            var process = LaunchNodeProcess(processStartInfo);

            StdOut = new EventedStreamReader(process.StandardOutput);
            StdErr = new EventedStreamReader(process.StandardError);
        }
        public NodeScriptRunner(string workingDirectory, string scriptName, string arguments,
                                IDictionary <string, string> envVars, string pkgManagerCommand, DiagnosticSource diagnosticSource,
                                CancellationToken applicationStoppingToken)
        {
            if (string.IsNullOrEmpty(workingDirectory))
            {
                throw new ArgumentException("Cannot be null or empty.", nameof(workingDirectory));
            }

            if (string.IsNullOrEmpty(scriptName))
            {
                throw new ArgumentException("Cannot be null or empty.", nameof(scriptName));
            }

            if (string.IsNullOrEmpty(pkgManagerCommand))
            {
                throw new ArgumentException("Cannot be null or empty.", nameof(pkgManagerCommand));
            }

            var exeToRun          = pkgManagerCommand;
            var completeArguments = $"run {scriptName} {(scriptName == "npm" ? "-- " : string.Empty)}{arguments ?? string.Empty}";

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                // On Windows, the node executable is a .cmd file, so it can't be executed
                // directly (except with UseShellExecute=true, but that's no good, because
                // it prevents capturing stdio). So we need to invoke it via "cmd /c".
                exeToRun          = "cmd";
                completeArguments = $"/c {pkgManagerCommand} {completeArguments}";
            }

            var processStartInfo = new ProcessStartInfo(exeToRun)
            {
                Arguments              = completeArguments,
                UseShellExecute        = false,
                RedirectStandardInput  = true,
                RedirectStandardOutput = true,
                RedirectStandardError  = true,
                WorkingDirectory       = workingDirectory
            };

            if (envVars != null)
            {
                foreach (var keyValuePair in envVars)
                {
                    processStartInfo.Environment[keyValuePair.Key] = keyValuePair.Value;
                }
            }

            _npmProcess = LaunchNodeProcess(processStartInfo, pkgManagerCommand);
            StdOut      = new EventedStreamReader(_npmProcess.StandardOutput);
            StdErr      = new EventedStreamReader(_npmProcess.StandardError);

            applicationStoppingToken.Register(((IDisposable)this).Dispose);

            if (diagnosticSource.IsEnabled("Microsoft.AspNetCore.NodeServices.Npm.NpmStarted"))
            {
                diagnosticSource.Write(
                    "Microsoft.AspNetCore.NodeServices.Npm.NpmStarted",
                    new
                {
                    processStartInfo = processStartInfo,
                    process          = _npmProcess
                });
            }
        }
Ejemplo n.º 8
0
 public EventedStreamStringReader(EventedStreamReader eventedStreamReader)
 {
     _eventedStreamReader = eventedStreamReader
                            ?? throw new ArgumentNullException(nameof(eventedStreamReader));
     _eventedStreamReader.OnReceivedLine += OnReceivedLine;
 }