1 //
2 // Copyright (c) 2010-2018 Antmicro
3 // Copyright (c) 2011-2015 Realtime Embedded
4 //
5 // This file is licensed under the MIT License.
6 // Full license text is available in 'licenses/MIT.txt'.
7 //
8 using AntShell.Commands;
9 using Antmicro.Renode.UserInterface.Tokenizer;
10 using Antmicro.Renode.Core;
11 using System;
12 using System.Linq;
13 
14 namespace Antmicro.Renode.UserInterface.Commands
15 {
16     public class CreatePlatformCommand : Command, ISuggestionProvider
17     {
PrintHelp(ICommandInteraction writer)18         public override void PrintHelp(ICommandInteraction writer)
19         {
20             base.PrintHelp(writer);
21             writer.WriteLine("\nOptions:");
22             writer.WriteLine("===========================");
23             foreach(var item in PlatformsProvider.GetAvailablePlatforms().OrderBy(x=>x.Name))
24             {
25                 writer.WriteLine(item.Name);
26             }
27         }
28 
29         #region ISuggestionProvider implementation
30 
ProvideSuggestions(string prefix)31         public string[] ProvideSuggestions(string prefix)
32         {
33             return PlatformsProvider.GetAvailablePlatforms().Where(p => p.Name.StartsWith(prefix)).Select(p => p.Name).ToArray();
34         }
35 
36         #endregion
37 
38         [Runnable]
Run(ICommandInteraction writer, LiteralToken type)39         public void Run(ICommandInteraction writer, LiteralToken type)
40         {
41             Execute(writer, type.Value, null);
42         }
43 
44         [Runnable]
Run(ICommandInteraction writer, LiteralToken type, StringToken name)45         public void Run(ICommandInteraction writer, LiteralToken type, StringToken name)
46         {
47             Execute(writer, type.Value, name.Value);
48         }
49 
Execute(ICommandInteraction writer, string type, string name)50         private void Execute(ICommandInteraction writer, string type, string name)
51         {
52             var platform = PlatformsProvider.GetPlatformByName(type);
53             if (platform == null)
54             {
55                 writer.WriteError("Invalid platform type: " + type);
56                 return;
57             }
58 
59             var mach = new Machine() { Platform = platform };
60             EmulationManager.Instance.CurrentEmulation.AddMachine(mach, name);
61             changeCurrentMachine(mach);
62             monitor.TryExecuteScript(platform.ScriptPath, writer);
63         }
64 
CreatePlatformCommand(Monitor monitor, Action<Machine> changeCurrentMachine)65         public CreatePlatformCommand(Monitor monitor, Action<Machine> changeCurrentMachine) : base(monitor, "createPlatform", "creates a platform.", "c")
66         {
67             this.changeCurrentMachine = changeCurrentMachine;
68         }
69 
70         private readonly Action<Machine> changeCurrentMachine;
71     }
72 }
73 
74