Advertisement
Guest User

Untitled

a guest
May 4th, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. namespace ConsoleHostedScriptCs
  2. {
  3. using System;
  4. using Common.Logging.Simple;
  5. using NuGet;
  6. using ScriptCs.Contracts;
  7. using ScriptCs.Engine.Roslyn;
  8. using ScriptCs.Hosting;
  9. using PackageReference = ScriptCs.PackageReference;
  10.  
  11. internal class Program
  12. {
  13. private static void Main(string[] args)
  14. {
  15. var scriptcs = new ScriptServicesBuilder(new ScriptConsole(), new NoOpLogger())
  16. .Repl()
  17. .ScriptEngine<RoslynScriptEngine>()
  18. .Build();
  19.  
  20. scriptcs.InstallationProvider.Initialize();
  21.  
  22. // Install ScriptCs.Wpf
  23. var packageReference = new PackageReference(
  24. "ScriptCs.Wpf",
  25. VersionUtility.ParseFrameworkName("net40"),
  26. new Version(0, 1, 4));
  27. scriptcs.InstallationProvider.InstallPackage(packageReference);
  28.  
  29. scriptcs.Executor.Initialize(
  30. scriptcs.AssemblyResolver.GetAssemblyPaths(Environment.CurrentDirectory),
  31. scriptcs.ScriptPackResolver.GetPacks());
  32.  
  33. while (true)
  34. {
  35. // I can now do
  36. // var wpf = Require<Wpf>();
  37. // wpf.RunInSTA(() => new Window() { Content = "Kölle Alaaf!" }.ShowDialog());
  38. var command = Console.ReadLine();
  39. ScriptResult result;
  40. try
  41. {
  42. result = scriptcs.Executor.ExecuteScript(command);
  43. }
  44. catch (Exception e)
  45. {
  46. WriteError(e.Message);
  47. continue;
  48. }
  49.  
  50. if (result.CompileExceptionInfo != null)
  51. {
  52. WriteError(result.CompileExceptionInfo.SourceException.Message);
  53. continue;
  54. }
  55.  
  56. if (result.ExecuteExceptionInfo != null)
  57. {
  58. WriteError(result.ExecuteExceptionInfo.SourceException.Message);
  59. continue;
  60. }
  61.  
  62. var returnValue = string.Format("{0}", result.ReturnValue);
  63. if (!string.IsNullOrEmpty(returnValue))
  64. {
  65. Console.WriteLine(returnValue);
  66. }
  67. }
  68.  
  69. scriptcs.Executor.Terminate();
  70. }
  71.  
  72. private static void WriteError(string error)
  73. {
  74. Console.ForegroundColor = ConsoleColor.Red;
  75. Console.WriteLine(error);
  76. Console.ResetColor();
  77. }
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement