Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow script extensions to declare unsafe blocks #1753

Merged
merged 3 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions Bonsai.Configuration/ScriptExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Reflection;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;

namespace Bonsai.Configuration
{
Expand All @@ -20,6 +21,7 @@ public class ScriptExtensions : IDisposable
const string PackageVersionAttribute = "Version";
const string UseWindowsFormsElement = "UseWindowsForms";
const string ItemGroupElement = "ItemGroup";
const string AllowUnsafeBlocksElement = "AllowUnsafeBlocks";
const string ProjectFileTemplate = @"<Project Sdk=""Microsoft.NET.Sdk"">

<PropertyGroup>
Expand Down Expand Up @@ -98,6 +100,12 @@ static XmlReaderSettings GetXmlReaderSettings()
};
}

static XElement GetProperty(XDocument document, string key)
=> document.XPathSelectElement($"/Project/PropertyGroup/{key}");

static bool? GetBoolProperty(XDocument document, string key)
=> GetProperty(document, key)?.Value?.Equals("true", StringComparison.InvariantCultureIgnoreCase);

public IEnumerable<string> GetAssemblyReferences()
{
yield return "System.dll";
Expand All @@ -114,8 +122,7 @@ public IEnumerable<string> GetAssemblyReferences()
if (!File.Exists(ProjectFileName)) yield break;
using var stream = File.OpenRead(ProjectFileName);
var document = LoadProjectDocument(stream);
var useWindowsForms = document.Descendants(XName.Get(UseWindowsFormsElement)).FirstOrDefault();
if (useWindowsForms != null && useWindowsForms.Value == "true")
if (GetBoolProperty(document, UseWindowsFormsElement) ?? false)
{
yield return "System.Windows.Forms.dll";
}
Expand Down Expand Up @@ -195,6 +202,13 @@ public void AddAssemblyReferences(IEnumerable<string> assemblyReferences)
File.WriteAllText(ProjectFileName, root.ToString(SaveOptions.DisableFormatting));
}

public bool GetAllowUnsafeBlocks()
{
using var stream = File.OpenRead(ProjectFileName);
var document = LoadProjectDocument(stream);
return GetBoolProperty(document, AllowUnsafeBlocksElement) ?? false;
}

public void Dispose()
{
assemblyFolder.Dispose();
Expand Down
21 changes: 16 additions & 5 deletions Bonsai.Configuration/ScriptExtensionsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,26 @@ string GetAssemblyLocation(string fileName)
return fileName;
}

var compilerParameters = new CompilerParameters(assemblyReferences, assemblyFile);
compilerParameters.GenerateExecutable = false;
compilerParameters.GenerateInMemory = false;
compilerParameters.IncludeDebugInformation = includeDebugInformation;
var compilerParameters = new CompilerParameters(assemblyReferences, assemblyFile)
{
GenerateExecutable = false,
GenerateInMemory = false,
IncludeDebugInformation = includeDebugInformation,
CompilerOptions = "",
};

if (scriptEnvironment.GetAllowUnsafeBlocks())
{
compilerParameters.CompilerOptions += " /unsafe";
}

if (!includeDebugInformation)
{
compilerParameters.CompilerOptions = "/optimize";
compilerParameters.CompilerOptions += " /optimize";
}

compilerParameters.CompilerOptions = compilerParameters.CompilerOptions.TrimStart();

using (var codeProvider = new CSharpCodeProvider())
{
var results = codeProvider.CompileAssemblyFromFile(compilerParameters, scriptFiles);
Expand Down
Loading