public void Generate(IGeneratorArguments input) { Encoding encoding; //auto-convert input encoding to the correct type if (_config.InputEncoding != FileEncoding.Default) { encoding = _config.InputEncoding == FileEncoding.Ascii ? Encoding.ASCII : Encoding.UTF8; using (var r = new StreamReader(input.InputPath, detectEncodingFromByteOrderMarks: true)) { if (encoding.EncodingName != r.CurrentEncoding.EncodingName) { string text = r.ReadToEnd(); r.Dispose(); File.WriteAllText(input.InputPath, text, encoding); } } } encoding = _config.OutputEncoding == FileEncoding.Ascii ? Encoding.ASCII : Encoding.UTF8; //Couple of assertions about PowerShell if (_config.Script.Type == ScriptEngine.Language.PowerShell && (_config.StandardInput.Redirect || _config.Arguments.Length > 0)) throw new ApplicationException( @"Currently PowerShell integration does not support input streams or arguments. Primarily this is due to circumventing the script-signing requirements. By using the '-Command -' argument we avoid signing or setting ExecutionPolicy."); using (DebuggingOutput debug = new DebuggingOutput(_config.Debug, input.WriteLine)) { debug.WriteLine("ConfigDir = {0}", _config.BaseDirectory); input.ConfigDir = _config.BaseDirectory; //Inject arguments into the script string script = input.ReplaceVariables(Check.NotNull(_config.Script).Text.Trim()); if (!String.IsNullOrEmpty(_config.Script.Include)) script = File.ReadAllText(CreateFullPath(_config.Script.Include)); if (_config.Script.Type == ScriptEngine.Language.Exe) script = CreateFullPath(script); StringWriter swOutput = new StringWriter(); List<string> arguments = new List<string>(); foreach (GeneratorArgument arg in _config.Arguments) arguments.Add(input.ReplaceVariables(arg.Text ?? String.Empty)); debug.WriteLine("Prepared Script:{0}{1}{0}{2}{0}{1}", Environment.NewLine, "---------------------------------------------", script ); using (ScriptRunner scriptEngine = new ScriptRunner(_config.Script.Type, script)) { IRunner runner = scriptEngine; if (input.AllowAppDomains && _config.Script.InvokeAssembly) { runner = AssemblyRunnerCache.Fetch(scriptEngine.ScriptEngine.Executable); arguments.InsertRange(0, scriptEngine.ScriptArguments); } runner.WorkingDirectory = _config.BaseDirectory; string lastErrorMessage = null; ProcessOutputEventHandler handler = delegate(object o, ProcessOutputEventArgs args) { if (args.Error) input.WriteLine(lastErrorMessage = args.Data); else if (_config.StandardOut != null) { debug.WriteLine("std::out: {0}", args.Data); swOutput.WriteLine(args.Data); } else input.WriteLine(args.Data); }; int exitCode = -1; debug.WriteLine("Executing {0} {1}", runner, ArgumentList.EscapeArguments(arguments.ToArray())); try { runner.OutputReceived += handler; if (_config.StandardInput.Redirect) exitCode = runner.Run(new StringReader(File.ReadAllText(input.InputPath)), arguments.ToArray()); else exitCode = runner.Run(arguments.ToArray()); } finally { debug.WriteLine("Exited = {0}", exitCode); runner.OutputReceived -= handler; } if (_config.StandardOut != null) { string target = Path.ChangeExtension(input.InputPath, _config.StandardOut.Extension); using (TempFile file = TempFile.FromExtension(_config.StandardOut.Extension)) { File.WriteAllText(file.TempPath, swOutput.ToString(), encoding); File.Copy(file.TempPath, target, true); input.AddOutputFile(target); } } if (exitCode != 0) { string message = "The script returned a non-zero result: " + exitCode; input.WriteLine(message); throw new ApplicationException(String.IsNullOrEmpty(lastErrorMessage) ? message : lastErrorMessage); } } EnumOutputFiles(input, input.AddOutputFile); } }
public void Generate(IGeneratorArguments input) { Encoding encoding; //auto-convert input encoding to the correct type if (_config.InputEncoding != FileEncoding.Default) { encoding = _config.InputEncoding == FileEncoding.Ascii ? Encoding.ASCII : Encoding.UTF8; using (var r = new StreamReader(input.InputPath, detectEncodingFromByteOrderMarks: true)) { if (encoding.EncodingName != r.CurrentEncoding.EncodingName) { string text = r.ReadToEnd(); r.Dispose(); File.WriteAllText(input.InputPath, text, encoding); } } } encoding = _config.OutputEncoding == FileEncoding.Ascii ? Encoding.ASCII : Encoding.UTF8; //Couple of assertions about PowerShell if (_config.Script.Type == ScriptEngine.Language.PowerShell && (_config.StandardInput.Redirect || _config.Arguments.Length > 0)) { throw new ApplicationException( @"Currently PowerShell integration does not support input streams or arguments. Primarily this is due to circumventing the script-signing requirements. By using the '-Command -' argument we avoid signing or setting ExecutionPolicy."); } using (DebuggingOutput debug = new DebuggingOutput(_config.Debug, input.WriteLine)) { debug.WriteLine("ConfigDir = {0}", _config.BaseDirectory); input.ConfigDir = _config.BaseDirectory; //Inject arguments into the script string script = input.ReplaceVariables(Check.NotNull(_config.Script).Text.Trim()); if (!String.IsNullOrEmpty(_config.Script.Include)) { script = File.ReadAllText(CreateFullPath(_config.Script.Include)); } if (_config.Script.Type == ScriptEngine.Language.Exe) { script = CreateFullPath(script); } StringWriter swOutput = new StringWriter(); List <string> arguments = new List <string>(); foreach (GeneratorArgument arg in _config.Arguments) { arguments.Add(input.ReplaceVariables(arg.Text ?? String.Empty)); } debug.WriteLine("Prepared Script:{0}{1}{0}{2}{0}{1}", Environment.NewLine, "---------------------------------------------", script ); using (ScriptRunner scriptEngine = new ScriptRunner(_config.Script.Type, script)) { IRunner runner = scriptEngine; if (input.AllowAppDomains && _config.Script.InvokeAssembly) { runner = AssemblyRunnerCache.Fetch(scriptEngine.ScriptEngine.Executable); arguments.InsertRange(0, scriptEngine.ScriptArguments); } runner.WorkingDirectory = _config.BaseDirectory; string lastErrorMessage = null; ProcessOutputEventHandler handler = delegate(object o, ProcessOutputEventArgs args) { if (args.Error) { input.WriteLine(lastErrorMessage = args.Data); } else if (_config.StandardOut != null) { debug.WriteLine("std::out: {0}", args.Data); swOutput.WriteLine(args.Data); } else { input.WriteLine(args.Data); } }; int exitCode = -1; debug.WriteLine("Executing {0} {1}", runner, ArgumentList.EscapeArguments(arguments.ToArray())); try { runner.OutputReceived += handler; if (_config.StandardInput.Redirect) { exitCode = runner.Run(new StringReader(File.ReadAllText(input.InputPath)), arguments.ToArray()); } else { exitCode = runner.Run(arguments.ToArray()); } } finally { debug.WriteLine("Exited = {0}", exitCode); runner.OutputReceived -= handler; } if (_config.StandardOut != null) { string target = Path.ChangeExtension(input.InputPath, _config.StandardOut.Extension); using (TempFile file = TempFile.FromExtension(_config.StandardOut.Extension)) { File.WriteAllText(file.TempPath, swOutput.ToString(), encoding); File.Copy(file.TempPath, target, true); input.AddOutputFile(target); } } if (exitCode != 0) { string message = "The script returned a non-zero result: " + exitCode; input.WriteLine(message); throw new ApplicationException(String.IsNullOrEmpty(lastErrorMessage) ? message : lastErrorMessage); } } EnumOutputFiles(input, input.AddOutputFile); } }