Skip to content

Commit

Permalink
Allow script extensions to declare unsafe blocks.
Browse files Browse the repository at this point in the history
This change also improves the robustness of the `UseWindowsForms` check, only checking in appropriate locations within the project file and disregarding the case of `true` vs `True`.
(The latter matters if someone uses the Visual Studio project editor, as it now uses `True` instead of `true`.)

Fixes #1727
  • Loading branch information
PathogenDavid committed Apr 22, 2024
1 parent 53e9eca commit da3a40b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
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

0 comments on commit da3a40b

Please sign in to comment.