From ab99fb80dfc78c31b1e6cf8d5e4a8458978f4ddc Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Thu, 7 Dec 2017 17:42:02 +0200 Subject: Added Tango.Scripting Stubs.UI scripting works.. Implemented ScriptEditorControl. --- .../Visual_Studio/Tango.Scripting/ScriptEngine.cs | 73 ++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 Software/Visual_Studio/Tango.Scripting/ScriptEngine.cs (limited to 'Software/Visual_Studio/Tango.Scripting/ScriptEngine.cs') diff --git a/Software/Visual_Studio/Tango.Scripting/ScriptEngine.cs b/Software/Visual_Studio/Tango.Scripting/ScriptEngine.cs new file mode 100644 index 000000000..7e9bdd6e3 --- /dev/null +++ b/Software/Visual_Studio/Tango.Scripting/ScriptEngine.cs @@ -0,0 +1,73 @@ +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp.Scripting; +using Microsoft.CodeAnalysis.Scripting; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace Tango.Scripting +{ + public class ScriptEngine + { + private CancellationTokenSource _cancaller; + private OnExecuteParameters _onExecuteParameters; + public List ReferencedAssemblies { get; private set; } + + public ScriptEngine(OnExecuteParameters parameters) + { + _onExecuteParameters = parameters; + ReferencedAssemblies = new List(); + } + + public async Task Run(String code) + { + //My References. + var options = ScriptOptions.Default; + + //External References. + //foreach (var r in item.References) + //{ + // options = options.AddReferences(r.FilePath); + //} + + //My Assemblies. + options = options.AddReferences(typeof(Form).Assembly.Location); + options = options.AddReferences(typeof(Enumerable).Assembly.Location); + options = options.AddReferences(typeof(ScriptEngine).Assembly.Location); + + foreach (var asm in ReferencedAssemblies) + { + options = options.AddReferences(asm.Assembly.Location); + } + + //Imports. + options = options.AddImports( + "System", + "System.Collections.Generic", + "System.Linq", + "System.Text", + "System.Diagnostics", + "System.Windows.Forms" + ); + + String methodParameters = String.Join(", ", _onExecuteParameters.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance).Select(x => x.Name)); + + _cancaller = new CancellationTokenSource(); + await CSharpScript.RunAsync( + code + + Environment.NewLine + + Environment.NewLine + + "await Task.Factory.StartNew(() => { OnExecute(" + methodParameters + "); });", options: options, globals: _onExecuteParameters, cancellationToken: _cancaller.Token); + } + + public void Stop() + { + _cancaller.Cancel(); + } + } +} -- cgit v1.3.1 From 9a34f6f6456702440ecd50f902e59daa2ea77595 Mon Sep 17 00:00:00 2001 From: Roy Date: Sat, 9 Dec 2017 16:08:18 +0200 Subject: Added code comments to scripting lib and stubs execution GUI. --- .../Tango.Scripting/OnExecuteParameters.cs | 3 + .../Visual_Studio/Tango.Scripting/ScriptEngine.cs | 12 ++ .../Utilities/Tango.Stubs.UI/StubManager.cs | 50 +++++- .../Tango.Stubs.UI/StubOnExecuteParameters.cs | 11 ++ .../Tango.Stubs.UI/ViewModels/CodeTabVM.cs | 44 +++-- .../Tango.Stubs.UI/ViewModels/MainViewVM.cs | 184 ++++++++++++++++++--- .../Tango.Stubs.UI/ViewModels/StubSnippetVM.cs | 14 +- .../Tango.Stubs.UI/Views/MainView.xaml.cs | 1 + 8 files changed, 272 insertions(+), 47 deletions(-) (limited to 'Software/Visual_Studio/Tango.Scripting/ScriptEngine.cs') diff --git a/Software/Visual_Studio/Tango.Scripting/OnExecuteParameters.cs b/Software/Visual_Studio/Tango.Scripting/OnExecuteParameters.cs index af66d84a8..c87355e6b 100644 --- a/Software/Visual_Studio/Tango.Scripting/OnExecuteParameters.cs +++ b/Software/Visual_Studio/Tango.Scripting/OnExecuteParameters.cs @@ -6,6 +6,9 @@ using System.Threading.Tasks; namespace Tango.Scripting { + /// + /// Represents a base class for a global object which will be embedded in the scripting engine. + /// public abstract class OnExecuteParameters { diff --git a/Software/Visual_Studio/Tango.Scripting/ScriptEngine.cs b/Software/Visual_Studio/Tango.Scripting/ScriptEngine.cs index 7e9bdd6e3..52f595c3e 100644 --- a/Software/Visual_Studio/Tango.Scripting/ScriptEngine.cs +++ b/Software/Visual_Studio/Tango.Scripting/ScriptEngine.cs @@ -18,12 +18,21 @@ namespace Tango.Scripting private OnExecuteParameters _onExecuteParameters; public List ReferencedAssemblies { get; private set; } + /// + /// Initializes a new instance of the class. + /// + /// The parameters. public ScriptEngine(OnExecuteParameters parameters) { _onExecuteParameters = parameters; ReferencedAssemblies = new List(); } + /// + /// Runs the specified code. + /// + /// The code. + /// public async Task Run(String code) { //My References. @@ -65,6 +74,9 @@ namespace Tango.Scripting "await Task.Factory.StartNew(() => { OnExecute(" + methodParameters + "); });", options: options, globals: _onExecuteParameters, cancellationToken: _cancaller.Token); } + /// + /// Stops this instance. + /// public void Stop() { _cancaller.Cancel(); diff --git a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/StubManager.cs b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/StubManager.cs index 5bc2dab2e..f3161f991 100644 --- a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/StubManager.cs +++ b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/StubManager.cs @@ -13,31 +13,60 @@ using Tango.Transport.Adapters; namespace Tango.Stubs.UI { + /// + /// Represents a manager capable of executing stub scripts asynchronously. + /// public class StubManager { - private UsbTransportAdapter _adapter; + private UsbTransportAdapter _adapter; //Holds the USB transport adapter. + /// + /// Occurs when the stub has failed to execute. + /// public event EventHandler Failed; + + /// + /// Occurs when the stub has completed successfully. + /// public event EventHandler Completed; + + /// + /// Occurs when the stub has been initialized and executed. + /// public event EventHandler Executed; + /// + /// Gets a value indicating whether this is aborted. + /// internal bool Aborted { get; private set; } + /// + /// Initializes a new instance of the class. + /// + /// The adapter. public StubManager(UsbTransportAdapter adapter) { _adapter = adapter; } + /// + /// Aborts the current script. + /// internal void Abort() { Aborted = true; } + /// + /// Runs the specified stub name. + /// + /// Name of the stub. + /// The arguments. public void Run(String stubName, params Object[] args) { if (Aborted) return; - var stubType = GetAvailableRequestStubs().SingleOrDefault(x => x.Name.ToLower() == stubName.ToLower() || x.Name.Replace("Request", "").ToLower() == stubName.ToLower()); + var stubType = StubBase.GetAvailableRequestStubs().SingleOrDefault(x => x.Name.ToLower() == stubName.ToLower() || x.Name.Replace("Request", "").ToLower() == stubName.ToLower()); if (stubType == null) { OnFailed(new ArgumentException("Invalid stub '" + stubName + "'.")); @@ -130,19 +159,22 @@ namespace Tango.Stubs.UI } } - private void OnFailed(Exception ex) + /// + /// Raises the event. + /// + /// The exception. + protected virtual void OnFailed(Exception ex) { Failed?.Invoke(this, ex); } - private void OnCompleted(String response) + /// + /// Raises the event. + /// + /// The response. + protected virtual void OnCompleted(String response) { Completed?.Invoke(this, response); } - - private static List GetAvailableRequestStubs() - { - return typeof(MessageFactory).Assembly.GetTypes().Where(x => x.Namespace.Contains("Stubs") && x.Name.Contains("Request") && !x.Name.Contains("Reflection")).ToList(); - } } } diff --git a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/StubOnExecuteParameters.cs b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/StubOnExecuteParameters.cs index 1689c56de..14b69a0b7 100644 --- a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/StubOnExecuteParameters.cs +++ b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/StubOnExecuteParameters.cs @@ -7,10 +7,21 @@ using Tango.Scripting; namespace Tango.Stubs.UI { + /// + /// Represents the global object which will be sent to the scripting engine. + /// + /// public class StubOnExecuteParameters : OnExecuteParameters { + /// + /// Provides access to the script stub manager. + /// public StubManager stubManager; + /// + /// Initializes a new instance of the class. + /// + /// The manager. public StubOnExecuteParameters(StubManager manager) { stubManager = manager; diff --git a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/ViewModels/CodeTabVM.cs b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/ViewModels/CodeTabVM.cs index 41120e185..4bb29d4b7 100644 --- a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/ViewModels/CodeTabVM.cs +++ b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/ViewModels/CodeTabVM.cs @@ -9,18 +9,16 @@ using Tango.SharedUI; namespace Tango.Stubs.UI.ViewModels { + /// + /// Represents a single script editor tab view model; + /// + /// public class CodeTabVM : ViewModel { private String _title; - - private String _code; - - public String Code - { - get { return _code; } - set { _code = value; RaisePropertyChanged(nameof(Code)); } - } - + /// + /// Gets or sets the script title/file name. + /// public String Title { get @@ -35,7 +33,9 @@ namespace Tango.Stubs.UI.ViewModels } private String _file; - + /// + /// Gets or sets the full script file path. + /// public String File { get { return _file; } @@ -47,21 +47,41 @@ namespace Tango.Stubs.UI.ViewModels } } - private RelayCommand _insertCodeSnippetCommand; + private String _code; + /// + /// Gets or sets the script code. + /// + public String Code + { + get { return _code; } + set { _code = value; RaisePropertyChanged(nameof(Code)); } + } + private RelayCommand _insertCodeSnippetCommand; + /// + /// Gets or sets the insert snippet command. (Inserts stub snippet to editor) + /// public RelayCommand InsertSnippetCommand { get { return _insertCodeSnippetCommand; } set { _insertCodeSnippetCommand = value; RaisePropertyChanged(nameof(InsertSnippetCommand)); } } - + /// + /// Initializes a new instance of the class. + /// public CodeTabVM() { Title = "untitled"; Code = Properties.Resources.CodeTabTemplate; } + /// + /// Returns a that represents this instance. + /// + /// + /// A that represents this instance. + /// public override string ToString() { return Title; diff --git a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/ViewModels/MainViewVM.cs b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/ViewModels/MainViewVM.cs index ea1d353df..7868b84d7 100644 --- a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/ViewModels/MainViewVM.cs +++ b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/ViewModels/MainViewVM.cs @@ -16,27 +16,46 @@ using Tango.Transport.Adapters; namespace Tango.Stubs.UI.ViewModels { + /// + /// Represents the script execution utility main view model. + /// + /// public class MainViewVM : ViewModel { - private UsbTransportAdapter _adapter; + private UsbTransportAdapter _adapter; //Holds the USB transport adapter. private StubManager _stubManager; + #region Properties + + /// + /// Gets or sets the code tabs. + /// public ObservableCollection CodeTabs { get; set; } + + /// + /// Gets or sets the additional highlight C# types. + /// public ObservableCollection> HighlightTypes { get; set; } + /// + /// Gets or sets the collection of stub snippets. + /// public ObservableCollection StubSnippets { get; set; } private StubSnippetVM _selectedStubSnippet; - + /// + /// Gets or sets the selected stub snippet. + /// public StubSnippetVM SelectedStubSnippet { get { return _selectedStubSnippet; } set { _selectedStubSnippet = value; RaisePropertyChanged(nameof(SelectedStubSnippet)); } } - private String _log; - + /// + /// Gets or sets the current response log. + /// public String Log { get { return _log; } @@ -44,7 +63,9 @@ namespace Tango.Stubs.UI.ViewModels } private CodeTabVM _selectedCodeTab; - + /// + /// Gets or sets the selected code tab. + /// public CodeTabVM SelectedCodeTab { get { return _selectedCodeTab; } @@ -52,7 +73,9 @@ namespace Tango.Stubs.UI.ViewModels } private bool _isConnected; - + /// + /// Gets or sets a value indicating whether the USB adapter is connected. + /// public bool IsConnected { get { return _isConnected; } @@ -61,7 +84,7 @@ namespace Tango.Stubs.UI.ViewModels private List _ports; /// - /// Gets or sets the ports. + /// Gets or sets the available USB ports. /// public List Ports { @@ -71,7 +94,7 @@ namespace Tango.Stubs.UI.ViewModels private String _selectedPort; /// - /// Gets or sets the selected port. + /// Gets or sets the selected USB port. /// public String SelectedPort { @@ -80,7 +103,9 @@ namespace Tango.Stubs.UI.ViewModels } private String _status; - + /// + /// Gets or sets the current status bar text. + /// public String Status { get { return _status; } @@ -88,34 +113,88 @@ namespace Tango.Stubs.UI.ViewModels } private bool _isRunning; - + /// + /// Gets or sets a value indicating whether a stub is currently running. + /// public bool IsRunning { get { return _isRunning; } set { _isRunning = value; RaisePropertyChanged(nameof(IsRunning)); InvalidateRelayCommands(); } } + #endregion + #region Commands + /// + /// Gets or sets the new command. + /// public RelayCommand NewCommand { get; set; } + + /// + /// Gets or sets the close tab command. + /// public RelayCommand CloseTabCommand { get; set; } + + /// + /// Gets or sets the run command. + /// public RelayCommand RunCommand { get; set; } + + /// + /// Gets or sets the stop command. + /// public RelayCommand StopCommand { get; set; } + + /// + /// Gets or sets the toggle connection command. + /// public RelayCommand ToggleConnectionCommand { get; set; } + + /// + /// Gets or sets the open command. + /// public RelayCommand OpenCommand { get; set; } + + /// + /// Gets or sets the save command. + /// public RelayCommand SaveCommand { get; set; } + + /// + /// Gets or sets the save as command. + /// public RelayCommand SaveAsCommand { get; set; } + + /// + /// Gets or sets the stub snippet selected command. + /// public RelayCommand StubSnippetSelectedCommand { get; set; } + + /// + /// Gets or sets the insert snippet command. + /// public RelayCommand InsertSnippetCommand { get; set; } + + /// + /// Gets or sets the exit command. + /// public RelayCommand ExitCommand { get; set; } + #endregion + + #region Constructors + + /// + /// Initializes a new instance of the class. + /// public MainViewVM() { CodeTabs = new ObservableCollection(); - NewCommand = new RelayCommand(OnCreateNewTab); + NewCommand = new RelayCommand(CreateNewTab); CloseTabCommand = new RelayCommand(OnTabClosing); - RunCommand = new RelayCommand(OnTabRun, (x) => IsConnected && !IsRunning && SelectedCodeTab != null); - StopCommand = new RelayCommand(OnTabStop, (x) => IsConnected && IsRunning && SelectedCodeTab != null); + RunCommand = new RelayCommand(RunTab, (x) => IsConnected && !IsRunning && SelectedCodeTab != null); + StopCommand = new RelayCommand(StopTab, (x) => IsConnected && IsRunning && SelectedCodeTab != null); InsertSnippetCommand = new RelayCommand((x) => { }); HighlightTypes = new ObservableCollection>(); @@ -155,10 +234,17 @@ namespace Tango.Stubs.UI.ViewModels Status = "Ready"; - OnCreateNewTab(); + CreateNewTab(); } - private void OnStubSnippetSelected() + #endregion + + #region Virtual Methods + + /// + /// Called when a stub snippet is double clicked. + /// + protected virtual void OnStubSnippetSelected() { if (SelectedStubSnippet != null) { @@ -169,6 +255,22 @@ namespace Tango.Stubs.UI.ViewModels } } + /// + /// Called when user closes a script tab. + /// + /// The code tab. + protected virtual void OnTabClosing(CodeTabVM codeTab) + { + CodeTabs.Remove(codeTab); + } + + #endregion + + #region Private Methods + + /// + /// Saves the selected script file. + /// private void SaveFile() { if (SelectedCodeTab != null) @@ -184,6 +286,9 @@ namespace Tango.Stubs.UI.ViewModels } } + /// + /// Saves the selected script file. + /// private void SaveAsFile() { if (SelectedCodeTab != null) @@ -199,6 +304,9 @@ namespace Tango.Stubs.UI.ViewModels } } + /// + /// Opens a script from HD. + /// private void OpenFile() { OpenFileDialog dlg = new OpenFileDialog(); @@ -217,6 +325,9 @@ namespace Tango.Stubs.UI.ViewModels } } + /// + /// Toggles the USB adapter connection. + /// private void ToggleConnection() { try @@ -239,14 +350,20 @@ namespace Tango.Stubs.UI.ViewModels } } - private void OnCreateNewTab() + /// + /// Creates a new script tab. + /// + private void CreateNewTab() { var newTab = new CodeTabVM(); CodeTabs.Add(newTab); SelectedCodeTab = newTab; } - private async void OnTabRun() + /// + /// Runs the selected script tab. + /// + private async void RunTab() { IsRunning = true; Log = (DateTime.Now.ToTimeString() + ": ") + "Executing script '" + SelectedCodeTab.Title + "'..." + Environment.NewLine; @@ -275,7 +392,10 @@ namespace Tango.Stubs.UI.ViewModels }); } - private void OnTabStop() + /// + /// Stops the currently current script. + /// + private void StopTab() { if (_stubManager != null) { @@ -286,21 +406,40 @@ namespace Tango.Stubs.UI.ViewModels } } + #endregion + + #region Event Handlers + + /// + /// Handled the Executed event. + /// + /// The sender. + /// Name of the stub. private void Manager_Executed(object sender, string stubName) { Log += (DateTime.Now.ToTimeString() + ": ") + "Executing '" + stubName + "'..." + Environment.NewLine; Status = "Executing " + stubName + "..."; } - private void Manager_Failed(object sender, Exception e) + /// + /// Handled the Failed event. + /// + /// The sender. + /// The exception. + private void Manager_Failed(object sender, Exception ex) { if (IsRunning) { - Log += (DateTime.Now.ToTimeString() + ": ") + e.Message + Environment.NewLine; + Log += (DateTime.Now.ToTimeString() + ": ") + ex.Message + Environment.NewLine; Status = "Failed!"; } } + /// + /// Handled the Completed event. + /// + /// The sender. + /// The response. private void Manager_Completed(object sender, string response) { Log += (DateTime.Now.ToTimeString() + ": ") + "Response Received:" + Environment.NewLine; @@ -308,9 +447,6 @@ namespace Tango.Stubs.UI.ViewModels Status = "Completed"; } - private void OnTabClosing(CodeTabVM codeTab) - { - CodeTabs.Remove(codeTab); - } + #endregion } } diff --git a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/ViewModels/StubSnippetVM.cs b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/ViewModels/StubSnippetVM.cs index 46c2f9495..32964473a 100644 --- a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/ViewModels/StubSnippetVM.cs +++ b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/ViewModels/StubSnippetVM.cs @@ -7,11 +7,15 @@ using Tango.SharedUI; namespace Tango.Stubs.UI.ViewModels { + /// + /// Represents a single stub snippet view model. + /// + /// public class StubSnippetVM : ViewModel { private String _name; /// - /// Gets or sets the name. + /// Gets or sets the stub name. /// public String Name { @@ -21,7 +25,7 @@ namespace Tango.Stubs.UI.ViewModels private String _code; /// - /// Gets or sets the code. + /// Gets or sets the snippet code. /// public String Code { @@ -29,6 +33,12 @@ namespace Tango.Stubs.UI.ViewModels set { _code = value; RaisePropertyChanged(nameof(Code)); } } + /// + /// Returns a that represents this instance. + /// + /// + /// A that represents this instance. + /// public override string ToString() { return Name; diff --git a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Views/MainView.xaml.cs b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Views/MainView.xaml.cs index eb3c646dd..a51f78f7c 100644 --- a/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Views/MainView.xaml.cs +++ b/Software/Visual_Studio/Utilities/Tango.Stubs.UI/Views/MainView.xaml.cs @@ -28,6 +28,7 @@ namespace Tango.Stubs.UI.Views InitializeComponent(); } + //Auto scroll to bottom of response log each time it is changed. private void TextBox_TextChanged(object sender, TextChangedEventArgs e) { Task.Factory.StartNew(() => -- cgit v1.3.1