aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Scripting/Tango.Scripting.Basic/Project.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Software/Visual_Studio/Scripting/Tango.Scripting.Basic/Project.cs')
-rw-r--r--Software/Visual_Studio/Scripting/Tango.Scripting.Basic/Project.cs178
1 files changed, 112 insertions, 66 deletions
diff --git a/Software/Visual_Studio/Scripting/Tango.Scripting.Basic/Project.cs b/Software/Visual_Studio/Scripting/Tango.Scripting.Basic/Project.cs
index 8ca64ca18..7500e404f 100644
--- a/Software/Visual_Studio/Scripting/Tango.Scripting.Basic/Project.cs
+++ b/Software/Visual_Studio/Scripting/Tango.Scripting.Basic/Project.cs
@@ -21,6 +21,8 @@ namespace Tango.Scripting.Basic
{
public class Project<T> : ExtendedObject where T : IContext
{
+ private object _compileLock = new object();
+
public String ID { get; set; }
private String _name;
@@ -37,6 +39,22 @@ namespace Tango.Scripting.Basic
set { _description = value; RaisePropertyChangedAuto(); }
}
+ private bool _isRunning;
+ [JsonIgnore]
+ public bool IsRunning
+ {
+ get { return _isRunning; }
+ set { _isRunning = value; RaisePropertyChangedAuto(); }
+ }
+
+ private bool _isCompiling;
+ [JsonIgnore]
+ public bool IsCompiling
+ {
+ get { return _isCompiling; }
+ set { _isCompiling = value; RaisePropertyChangedAuto(); }
+ }
+
public ApartmentState ApartmentState { get; set; }
public ObservableCollection<ReferenceAssembly> ReferenceAssemblies { get; set; }
@@ -52,6 +70,9 @@ namespace Tango.Scripting.Basic
}
}
+ [JsonIgnore]
+ public List<ScriptBreakPoint> BreakPoints { get; set; }
+
public Project()
{
ID = Guid.NewGuid().ToString();
@@ -62,99 +83,121 @@ namespace Tango.Scripting.Basic
Scripts = new ObservableCollection<Script>();
Scripts.CollectionChanged += (x, e) => { RaisePropertyChanged(nameof(AdditionalScripts)); };
+
+ BreakPoints = new List<ScriptBreakPoint>();
}
public Task<CompilationResult> Compile()
{
return Task.Factory.StartNew<CompilationResult>(() =>
{
- var result = new CompilationResult();
- var tempFolder = TemporaryManager.CreateFolder(Name + "_" + ID);
- result.TemporaryProjectPath = tempFolder;
+ lock (_compileLock)
+ {
+ try
+ {
+ IsCompiling = true;
+ var result = new CompilationResult();
+ var tempFolder = TemporaryManager.CreateFolder(Name + "_" + ID);
+ result.TemporaryProjectPath = tempFolder;
- String mainScriptCode = String.Empty;
+ String mainScriptCode = String.Empty;
- foreach (var script in Scripts)
- {
- script.LoadCount = 0;
- script.LoadCharCount = 0;
- String code = script.Code;
- String codeFile = Path.Combine(tempFolder, script.Name);
+ foreach (var script in Scripts)
+ {
+ script.LoadCount = 0;
+ script.LoadCharCount = 0;
+ String code = script.Code;
+ String codeFile = Path.Combine(tempFolder, script.Name);
- String loadingString = String.Empty;
+ String loadingString = String.Empty;
- foreach (var file in Scripts.Where(x => !x.IsEntryPoint && script != x).Select(x => Path.Combine(tempFolder, x.Name)))
- {
- loadingString += $"#load \"{file}\"\n";
- script.LoadCount++;
- }
+ foreach (var file in Scripts.Where(x => !x.IsEntryPoint && script != x).Select(x => Path.Combine(tempFolder, x.Name)))
+ {
+ loadingString += $"#load \"{file}\"\n";
+ script.LoadCount++;
+ }
- script.LoadCharCount += loadingString.Length;
+ script.LoadCharCount += loadingString.Length;
- code = loadingString + code;
+ code = loadingString + code;
- if (!script.IsEntryPoint)
- {
- //In case we use #load
- //foreach (var match in Regex.Matches(code, "#load \".+\"").OfType<Match>())
- //{
- // String line = match.ToString();
- // var pathMatch = Regex.Match(line, "(?<=\")(.*?)(?=\")");
- // if (pathMatch.Success)
- // {
- // String path = pathMatch.ToString();
+ int debugLinesLength = 0;
- // if (!System.IO.Path.IsPathRooted(path))
- // {
- // StringBuilder builder = new StringBuilder(code);
- // builder.Insert(match.Index + pathMatch.Index, System.IO.Path.GetFullPath(tempFolder + "\\"));
- // code = builder.ToString();
- // }
- // }
- //}
- File.WriteAllText(codeFile, code);
- }
- else
- {
- code += Environment.NewLine + Environment.NewLine + "new Program().OnExecute(GlobalContext);";
- mainScriptCode = code;
- }
- }
+ foreach (var breakPoint in BreakPoints.Where(x => x.Script == script).OrderBy(x => x.LineNumber))
+ {
+ var debugLine = $"context.BreakPoint(\"{script.Name}\",{breakPoint.LineNumber}";
- var scriptOptions = ScriptOptions.Default.WithReferences(LoadReferenceAssemblies());
+ foreach (var symbol in breakPoint.ContextSymbols)
+ {
+ debugLine += $",\"{symbol.Name}\",{symbol.Offset},{symbol.Length},{symbol.Name}";
+ }
- var s = CSharpScript.Create<object>(mainScriptCode, scriptOptions, typeof(GlobalObject<T>));
- result.Script = s;
+ debugLine += ");";
- var compileResults = s.Compile();
+ StringBuilder builder = new StringBuilder(code);
+ builder.Insert(breakPoint.LineStartOffset + loadingString.Length + debugLinesLength, debugLine);
+ code = builder.ToString();
- GC.Collect();
+ debugLinesLength += debugLine.Length;
+ }
- foreach (var error in compileResults.Where(x => x.Severity == Microsoft.CodeAnalysis.DiagnosticSeverity.Error))
- {
- CompilationError cError = new CompilationError();
- cError.File = System.IO.Path.GetFileName(error.Location.SourceTree.FilePath);
- if (cError.File == String.Empty)
+ if (!script.IsEntryPoint)
+ {
+ File.WriteAllText(codeFile, code);
+ }
+ else
+ {
+ code += Environment.NewLine + Environment.NewLine + "new Program().OnExecute(GlobalContext);";
+ mainScriptCode = code;
+ }
+ }
+
+ var scriptOptions = ScriptOptions.Default.WithReferences(LoadReferenceAssemblies()).WithEmitDebugInformation(true);
+
+ var s = CSharpScript.Create<object>(mainScriptCode, scriptOptions, typeof(GlobalObject<T>));
+
+ result.Script = s;
+
+ var compileResults = s.Compile();
+
+ GC.Collect();
+
+ foreach (var error in compileResults.Where(x => x.Severity == Microsoft.CodeAnalysis.DiagnosticSeverity.Error))
+ {
+ CompilationError cError = new CompilationError();
+ cError.File = System.IO.Path.GetFileName(error.Location.SourceTree.FilePath);
+ if (cError.File == String.Empty)
+ {
+ cError.File = Scripts.Single(x => x.IsEntryPoint).Name;
+ }
+ Script errorScript = Scripts.Single(x => x.Name == cError.File);
+ cError.Message = error.GetMessage();
+ cError.Severity = error.Severity;
+ cError.Position = error.Location.SourceSpan.Start - (errorScript != null ? errorScript.LoadCharCount : 0);
+ var line = error.Location.GetMappedLineSpan();
+ cError.Line = line.StartLinePosition.Line + 1 - (errorScript != null ? errorScript.LoadCount : 0);
+ cError.Column = line.StartLinePosition.Character + 1;
+ cError.Length = line.EndLinePosition.Character - line.StartLinePosition.Character;
+ result.Errors.Add(cError);
+ }
+
+ return result;
+ }
+ catch (Exception)
+ {
+ throw;
+ }
+ finally
{
- cError.File = Scripts.Single(x => x.IsEntryPoint).Name;
+ IsCompiling = false;
}
- Script errorScript = Scripts.Single(x => x.Name == cError.File);
- cError.Message = error.GetMessage();
- cError.Severity = error.Severity;
- cError.Position = error.Location.SourceSpan.Start - (errorScript != null ? errorScript.LoadCharCount : 0);
- var line = error.Location.GetMappedLineSpan();
- cError.Line = line.StartLinePosition.Line + 1 - (errorScript != null ? errorScript.LoadCount : 0);
- cError.Column = line.StartLinePosition.Character + 1;
- cError.Length = line.EndLinePosition.Character - line.StartLinePosition.Character;
- result.Errors.Add(cError);
}
-
- return result;
});
}
public async Task<ProjectSession<T>> Run(T context)
{
+ IsRunning = true;
var result = await Compile();
if (result.Errors.Count > 0)
@@ -175,6 +218,7 @@ namespace Tango.Scripting.Basic
try
{
var runResult = result.Script.RunAsync(globals: new GlobalObject<T>() { GlobalContext = context }).Result;
+ IsRunning = false;
session.Completed(runResult.ReturnValue);
}
catch (ThreadAbortException)
@@ -187,7 +231,9 @@ namespace Tango.Scripting.Basic
}
finally
{
+ BreakPoints.Clear();
GC.Collect();
+ IsRunning = false;
}
});