Skip to content

Instantly share code, notes, and snippets.

View MikeCodesDotNET's full-sized avatar
💯
Avalonia!

Mike James MikeCodesDotNET

💯
Avalonia!
View GitHub Profile
private async void RunMacro()
{
if(userScript == null)
MessageBox.Show("You must first compile the script.");
try
{
ScriptState result = await userScript.RunAsync();
}
catch(Exception e)
private void ValidatePropertyDeclarationSyntax(PropertyDeclarationSyntax declarationSyntax)
{
IPropertySymbol symbol = semanticModel.GetDeclaredSymbol(declarationSyntax) as IPropertySymbol;
if (symbol.IsStatic)
{
errors.Add((ValidationErrorType.Illegal_Feature, "Static properties are not allowed"));
return;
}
private bool IsIllegalAssemblyName(string assemblyName)
{
return assemblyName switch
{
"System.Console" => true,
"System.Diagnostics.Debug" => true,
"System.IO" => true,
"System.IO.FileSystem" => true,
"System.IO.FileSystem.Primitives" => true,
"System.Reflection" => true,
foreach(UsingDirectiveSyntax usingDirective in syntaxRootNode.Usings)
{
ISymbol symbol = semanticModel.GetSymbolInfo(usingDirective.Name).Symbol;
string name = symbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
if(name == "System.IO")
throw new Exception("whoa!!, you can't be using that here!");
}
private Compilation compilation;
private SemanticModel semanticModel;
private CompilationUnitSyntax syntaxRootNode;
// Validation Class Constructor
public ScriptValidator(Script script)
{
compilation = script.GetCompilation();
SyntaxTree syntaxTree = compilation.SyntaxTrees.First();
private Script userScript;
private void CompileScript()
{
UpdateSource(); //Handles creating a copy of the script which includes variables the user never sees.
string text = csSource.Text; //csSource is an object from the UI provider
userScript = CSharpScript.Create(text, options: userScriptOptions);
userScript.Compile();
static Script script;
public static async Task Main(string[] args)
{
var userScriptPath = Path.Combine(Environment.CurrentDirectory, "scriptInput.txt");
var fileContents = File.ReadAllText(userScriptPath);
script = CSharpScript.Create(fileContents, ScriptOptions.Default);
for (int i = 0; i < 15; i++)
{
System.TimeSpan sinceMidnight = System.DateTime.Now - System.DateTime.Today;
var seconds = sinceMidnight.TotalSeconds;
public static async Task Main(string[] args)
{
var userScriptPath = Path.Combine(Environment.CurrentDirectory, "scriptInput.txt");
var fileContents = File.ReadAllText(userScriptPath);
Script script = CSharpScript.Create(fileContents, ScriptOptions.Default);
var result = await script.RunAsync();
Console.WriteLine($"Seconds in a week: {result.GetVariable("seconds").Value}");
}
using Microsoft.CodeAnalysis.CSharp.Scripting;