Attempt to fix System.Diagnostics in SnapWrap scripts
This commit is contained in:
parent
0edc8104c9
commit
eb74649282
7 changed files with 71 additions and 19 deletions
|
@ -1,11 +1,8 @@
|
|||
using Microsoft.CodeAnalysis.CSharp.Scripting;
|
||||
using Microsoft.CodeAnalysis.Scripting;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.CodeAnalysis.CSharp.Scripting;
|
||||
using Microsoft.CodeAnalysis.Scripting;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace SnapWrap
|
||||
{
|
||||
|
@ -15,26 +12,31 @@ namespace SnapWrap
|
|||
{
|
||||
string input = args[0];
|
||||
string output = args[1];
|
||||
var customImports = args[2].Split("..");
|
||||
|
||||
try
|
||||
{
|
||||
var inputCode = File.ReadAllText(input);
|
||||
|
||||
|
||||
var options = ScriptOptions.Default
|
||||
.WithReferences(AppDomain.CurrentDomain.GetAssemblies()) // Add necessary assemblies
|
||||
.WithImports("System");
|
||||
.WithReferences(AppDomain.CurrentDomain.GetAssemblies()) // Add necessary assemblies
|
||||
.WithImports(customImports)
|
||||
.WithAllowUnsafe(true);
|
||||
|
||||
using (var sw = new StreamWriter(output))
|
||||
|
||||
// Capture console output using custom class
|
||||
using (var consoleOutput = new ConsoleOutput())
|
||||
{
|
||||
Console.SetOut(sw); // Redirect console output
|
||||
var scriptState = CSharpScript.RunAsync(inputCode, options).Result;
|
||||
|
||||
// Reset console output
|
||||
Console.SetOut(new StreamWriter(Console.OpenStandardOutput()));
|
||||
// Get captured output from the ConsoleOutput class
|
||||
string capturedOutput = consoleOutput.GetOutput();
|
||||
|
||||
// Write captured output to output file
|
||||
File.WriteAllText(output, capturedOutput);
|
||||
|
||||
Console.WriteLine("Output file generated successfully.");
|
||||
}
|
||||
Console.WriteLine("Output file generated successfully.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
@ -42,4 +44,32 @@ namespace SnapWrap
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Custom class to capture console output
|
||||
public class ConsoleOutput : IDisposable
|
||||
{
|
||||
private StringWriter _stringWriter;
|
||||
private TextWriter _originalOutput;
|
||||
|
||||
public ConsoleOutput()
|
||||
{
|
||||
_stringWriter = new StringWriter();
|
||||
_originalOutput = Console.Out;
|
||||
Console.SetOut(_stringWriter);
|
||||
}
|
||||
|
||||
public string GetOutput()
|
||||
{
|
||||
Console.SetOut(_originalOutput);
|
||||
string capturedOutput = _stringWriter.ToString();
|
||||
_stringWriter.Dispose();
|
||||
return capturedOutput;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Console.SetOut(_originalOutput);
|
||||
_stringWriter.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue