Exemple #1
0
        public void RunAnonymousScript(string script, CommandOptions options, string workingDirectory = null)
        {
            string scriptExecutable = PathUtility.GetFileNameFromCommand(script);

            if (!PathUtility.TryGetPathFile(scriptExecutable, RuntimeInfo, out string fileName))
            {
                fileName = RuntimeInfo.IsWindows ? "powershell.exe" : "bash";
            }
            else
            {
                script = script.Remove(0, scriptExecutable.Length);
            }

            if (options.Verbose)
            {
                _console.WriteDefaultLine($"Executing '{fileName} {script}'");
            }

            using (var process = ProcessUtility.CreateProcess(fileName, script, _fileSystem, workingDirectory))
            {
                process.ErrorDataReceived += ProcessUtility.WriteOutput(_console, ConsoleLogLevel.Error);
                if (options.Verbose)
                {
                    process.OutputDataReceived += ProcessUtility.WriteOutput(_console);
                }

                process.Start();

                process.BeginOutputReadLine();
                process.BeginErrorReadLine();

                process.WaitForExit(5000);
            }
        }