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
Binary file not shown.
|
@ -1,4 +1,4 @@
|
|||
@echo off
|
||||
|
||||
runprebuild.bat
|
||||
call runprebuild.bat
|
||||
dotnet build -c Release
|
|
@ -104,8 +104,11 @@
|
|||
</Options>
|
||||
</Configuration>
|
||||
|
||||
|
||||
<PackageReference name="Microsoft.CodeAnalysis" version="4.6.0"/>
|
||||
<PackageReference name="Microsoft.CodeAnalysis.CSharp" version="4.6.0"/>
|
||||
<PackageReference name="Microsoft.CodeAnalysis.CSharp.Scripting" version="4.6.0"/>
|
||||
<PackageReference name="System.Diagnostics.Process" version="4.3.0"/>
|
||||
|
||||
</Project>
|
||||
</Solution>
|
||||
|
|
|
@ -17,6 +17,7 @@ namespace Prebuild.Core.Nodes
|
|||
private string m_Generator;
|
||||
private bool m_AutoGen = true;
|
||||
private string m_OutputName;
|
||||
private List<string> m_Libs = new();
|
||||
|
||||
#endregion
|
||||
|
||||
|
@ -24,9 +25,17 @@ namespace Prebuild.Core.Nodes
|
|||
public override void Parse(XmlNode node)
|
||||
{
|
||||
m_Name = Helper.AttributeValue(node, "name", "");
|
||||
m_Generator = Helper.AttributeValue(node, "generator", "TextTemplatingFileGenerator");
|
||||
m_AutoGen = Helper.ParseBoolean(node, "autogen", true);
|
||||
m_OutputName = Helper.AttributeValue(node, "output", "");
|
||||
|
||||
foreach (XmlNode childNode in node.ChildNodes)
|
||||
{
|
||||
var data = Kernel.Instance.ParseNode(childNode, this);
|
||||
if(data is ReferenceNode)
|
||||
{
|
||||
m_Libs.Add(((ReferenceNode)data).Name);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
@ -71,6 +80,14 @@ namespace Prebuild.Core.Nodes
|
|||
}
|
||||
}
|
||||
|
||||
public string Libraries
|
||||
{
|
||||
get
|
||||
{
|
||||
return String.Join("..", m_Libs);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
|
|
@ -696,7 +696,7 @@ public abstract class VSGenericTarget : ITarget
|
|||
string outputFile = Path.Combine(project.Path, node.OutputName);
|
||||
|
||||
ps.WriteLine(" <Target Name=\"Prebuild\" BeforeTargets=\"PreBuildEvent\">");
|
||||
ps.WriteLine($" <Exec Command=\"'dotnet' '$(ProjectDir){pathText}' '$(ProjectDir){filePath}' '$(ProjectDir){outputFile}'\" />");
|
||||
ps.WriteLine($" <Exec Command=\"'dotnet' '$(ProjectDir){pathText}' '$(ProjectDir){node.Name}' '$(ProjectDir){node.OutputName}' '{node.Libraries}' \" />");
|
||||
ps.WriteLine($" </Target>");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -61,8 +61,10 @@
|
|||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.CodeAnalysis" Version="4.6.0" />
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.6.0" />
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="4.6.0" />
|
||||
<PackageReference Include="System.Diagnostics.Process" Version="4.3.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue