Exemple #1
0
        public async Task <string> Evaluate(string script)
        {
            _log.Detail("Executing Nodejs script...");
            string program     = ProcessInterop.AppendExecutableExtension("node");
            string?programPath = ProcessInterop.ResolveProgramFullPath(program, _appSettings.NodeJsPath);

            if (programPath == null)
            {
                _log.Warning("Script run failed. Program not found: {0}", program);
                return($"{program} not found to execute");
            }

            string output = "";

            try
            {
                using (var process = new Process())
                {
                    process.StartInfo = new ProcessStartInfo
                    {
                        CreateNoWindow         = true,
                        RedirectStandardInput  = true,
                        RedirectStandardOutput = true,
                        UseShellExecute        = false,
                        RedirectStandardError  = true,
                        FileName  = programPath,
                        Arguments = $"-e \"{EncodeForCmdLine(script)}\""
                    };

                    process.Start();
                    output = await process.StandardOutput.ReadToEndAsync();

                    process.WaitForExit(_appSettings.NodeJsTimeout * 1000);
                }
                return(output);
            }
            catch (Exception ex)
            {
                _log.Warning("Script run failed with Exception: {0}", ex.Message);
                _log.Detail("Stack Trace: {0}", ex.StackTrace ?? "");
                return($"Script run failed with Exception: {ex.Message}");
            }
        }
Exemple #2
0
        public string Generate(IArguments arguments)
        {
            var file = new FsPath(arguments.GetArgumentOrThrow <string>("file"));

            _log.Info("Trying to execute {0}", file);

            var nodeProgram = ProcessInterop.AppendExecutableExtension(processName);

            string?programPath = ProcessInterop.ResolveProgramFullPath(nodeProgram);

            if (programPath == null)
            {
                _log.Warning("{0} was not found on path.", processName);
                return($"{processName} was not found on path");
            }

            StringBuilder script = new StringBuilder(16 * 1024);

            script.AppendLine(SerializeHostInfo());
            script.AppendLine(file.ReadFile(_log));

            var temp = new FsPath(Path.GetTempFileName());

            temp.WriteFile(_log, script.ToString());

            var(exitcode, output) = ProcessInterop.RunProcess("node", temp.ToString(), ScriptTimeOut);

            if (temp.IsExisting)
            {
                File.Delete(temp.ToString());
            }

            if (exitcode != 0)
            {
                _log.Warning("Script run failed. Exit code: {0}", exitcode);
                _log.Detail("Script output: {0}", output);
                return($"Script run failed: {temp}");
            }
            else
            {
                return(output);
            }
        }
        protected string ExecuteScriptProcess(string programWithoutExtension, string searchPath, string fileToExecute, int timeout)
        {
            string program     = ProcessInterop.AppendExecutableExtension(programWithoutExtension);
            string?programPath = ProcessInterop.ResolveProgramFullPath(program, searchPath);

            if (programPath == null)
            {
                _log.Warning("{0} was not found on path.", program);
                return($"{program} was not found on path");
            }

            string?scriptPath = Path.GetDirectoryName(fileToExecute);

            if (scriptPath != null)
            {
                SerializeHostInfo(scriptPath);
            }

            try
            {
                var(exitcode, output) = ProcessInterop.RunProcess(programPath, fileToExecute, timeout);

                if (exitcode != 0)
                {
                    _log.Warning("Script run failed. Exit code: {0}", exitcode);
                    _log.Detail("Script output: {0}", output);
                    return($"Script run failed: {fileToExecute}");
                }
                else
                {
                    return(output);
                }
            }
            catch (Exception ex)
            {
                _log.Warning("Script run failed with Exception: {0}", ex.Message);
                _log.Detail("Stack Trace: {0}", ex.StackTrace ?? "");
                return($"Script run failed with Exception: {ex.Message}");
            }
        }