static int Main(string[] args) { ScriptContext context = new ScriptContext(); using (ConsoleWithColors cout = new ConsoleWithColors(Environment.GetEnvironmentVariable("XSH_COLORS"))) using (ConsoleRedirector redir = new ConsoleRedirector(context)) using (CtrlCInterceptor ctrl = new CtrlCInterceptor()) { context.Output += cout.OnOutput; ctrl.Output = context.Error; ctrl.Abort += delegate { context.Abort(); }; cout.DebugMode = true; // context.Progress=console.OnOutputProgress; int exitCode = 0; try { var script = new Generated.$safeprojectname$().Script; ctrl.IgnoreCtrlC = script.IgnoreCtrlC; ctrl.AbortDelay = Utils.ToTimeSpan(script.AbortDelay) ?? ctrl.AbortDelay; ctrl.ExitDelay = Utils.ToTimeSpan(script.ExitDelay) ?? ctrl.ExitDelay; object o = context.ExecuteScript(script, args); if (o != null) exitCode = Utils.To<int>(o); } catch (ScriptTerminateException te) { exitCode = te.ExitCode; if (te.InnerException != null) context.WriteException(te.InnerException); } catch (Exception e) { context.WriteException(e); exitCode = -1; } return exitCode; } }
// Real main static int MainWithContext(XS.ScriptContext context, string[] args) { AppDomainLoader.progress("MainWithContext: Entering --------------------"); XS.CommandLineParameter[] param = new XS.CommandLineParameter[] { new XS.CommandLineParameter(xs.quiet, XS.CommandLineValueCount.None, null, "true"), new XS.CommandLineParameter(xs.debug, XS.CommandLineValueCount.None, "false", "true"), new XS.CommandLineParameter(xs.debugc, XS.CommandLineValueCount.None, "false", "true"), new XS.CommandLineParameter(xs.verbose, XS.CommandLineValueCount.None, "false", "true"), new XS.CommandLineParameter(xs.nocolors, XS.CommandLineValueCount.None, "false", "true"), new XS.CommandLineParameter(xs.wait, XS.CommandLineValueCount.None, null, "true"), new XS.CommandLineParameter(xs.save, XS.CommandLineValueCount.Single, null, "xsharper_save.xsh"), new XS.CommandLineParameter(xs.log, XS.CommandLineValueCount.Single, null, "xsharper.log"), new XS.CommandLineParameter(xs.requireAdmin, XS.CommandLineValueCount.None, null, "true"), new XS.CommandLineParameter(xs.last, XS.CommandLineValueCount.None, null, "true"), new XS.CommandLineParameter(xs.utf8, XS.CommandLineValueCount.None, "false", "true"), new XS.CommandLineParameter(xs.scriptargs, null, XS.CommandLineValueCount.Multiple, null, null) }; param[param.Length - 1].Last = true; param[param.Length - 2].Last = true; XS.CommandLineParameters xsParams = new XS.CommandLineParameters(param, "//", false); foreach (XS.CommandLineParameter a in xsParams) { if (!string.IsNullOrEmpty(a.Name) && a.Name != xs.scriptargs) { a.Switch = a.Name.Replace("xs.", ""); } } int exitCode = 0; bool utf8 = false; foreach (string arg in args) { if (arg.Equals(xs.utf8.Replace("xs.", "//"), StringComparison.OrdinalIgnoreCase)) { utf8 = true; } } using (XS.ConsoleWithColors cout = new XS.ConsoleWithColors(Environment.GetEnvironmentVariable("XSH_COLORS"), utf8)) using (XS.CtrlCInterceptor ctrl = new XS.CtrlCInterceptor()) { context.Output += cout.OnOutput; ctrl.Output = context.Error; ctrl.Abort += delegate { context.Abort(); }; AppDomainLoader.progress("MainWithContext: Console ready --------------------"); System.Diagnostics.Stopwatch w = System.Diagnostics.Stopwatch.StartNew(); try { AppDomainLoader.progress("MainWithContext: Before parse--------------------"); xsParams.Parse(context, args, false); AppDomainLoader.progress("MainWithContext: Before set options--------------------"); setOutputOptions(context, cout); AppDomainLoader.progress("MainWithContext: Before load--------------------"); $ { GENERATED_CLASS } cl = new $ { GENERATED_CLASS } (); XS.Script s = cl.Script; ctrl.IgnoreCtrlC = s.IgnoreCtrlC; ctrl.AbortDelay = XS.Utils.ToTimeSpan(s.AbortDelay) ?? ctrl.AbortDelay; ctrl.ExitDelay = XS.Utils.ToTimeSpan(s.ExitDelay) ?? ctrl.ExitDelay; AppDomainLoader.progress("MainWithContext: After load--------------------"); if (context.IsSet(xs.save)) { using (XS.ScriptContextScope r = new XS.ScriptContextScope(context)) s.Save(context.GetString(xs.save)); } else { context.Compiler.AddRequireAdmin(XS.Utils.To <XS.RequireAdminMode>(context.GetStr(xs.requireAdmin, XS.RequireAdminMode.User.ToString()))); context.Compiler.AddRequireAdmin(s.RequireAdmin); if (context.Compiler.RequireAdmin != XS.RequireAdminMode.User && !context.IsAdministrator) { return(restartAsAdmin(context, args, context.Compiler.RequireAdmin == XS.RequireAdminMode.Hidden)); } AppDomainLoader.progress("MainWithContext: Before initialization --------------------"); context.Initialize(s); AppDomainLoader.progress("MainWithContext: After initialization --------------------"); string[] parms = null; if (context.IsSet(xs.scriptargs)) { parms = context.GetStringArray(xs.scriptargs); } XS.ConsoleRedirector redir = new XS.ConsoleRedirector(context); try { AppDomainLoader.progress("MainWithContext: Before executing --------------------"); object r = context.ExecuteScript(s, parms, XS.CallIsolation.High); ctrl.KillAbortTimer(); AppDomainLoader.progress("MainWithContext: After executing --------------------"); if (r != null) { int.TryParse(r.ToString(), out exitCode); } } finally { ctrl.KillAbortTimer(); redir.Dispose(); } } } catch (ThreadAbortException ae) { resetAbort(context); context.WriteException(ae.InnerException); exitCode = -1000; } catch (XS.ScriptTerminateException te) { resetAbort(context); exitCode = te.ExitCode; if (te.InnerException != null) { context.WriteException(te.InnerException); } } catch (Exception e) { resetAbort(context); context.WriteException(e); exitCode = -1; } finally { resetAbort(context); } if (context.GetBool(xs.wait, false)) { cout.WriteLine(XS.OutputType.Info, string.Format("Completed in {0} with exit code={1}. Press Enter to close...", w.Elapsed, exitCode)); Console.ReadLine(); } } return(exitCode); }
// Real main static int MainWithContext(XS.ScriptContext context, string[] args) { AppDomainLoader.progress("MainWithContext: Entering --------------------"); XS.CommandLineParameter[] param = new XS.CommandLineParameter[] { new XS.CommandLineParameter(xs.quiet, XS.CommandLineValueCount.None, null,"true" ), new XS.CommandLineParameter(xs.debug, XS.CommandLineValueCount.None, "false", "true" ) , new XS.CommandLineParameter(xs.debugc, XS.CommandLineValueCount.None, "false", "true" ) , new XS.CommandLineParameter(xs.verbose, XS.CommandLineValueCount.None, "false", "true" ) , new XS.CommandLineParameter(xs.nocolors, XS.CommandLineValueCount.None, "false", "true" ) , new XS.CommandLineParameter(xs.wait, XS.CommandLineValueCount.None, null,"true" ) , new XS.CommandLineParameter(xs.save, XS.CommandLineValueCount.Single, null,"xsharper_save.xsh" ) , new XS.CommandLineParameter(xs.log, XS.CommandLineValueCount.Single, null,"xsharper.log" ) , new XS.CommandLineParameter(xs.requireAdmin, XS.CommandLineValueCount.None, null,"true" ) , new XS.CommandLineParameter(xs.last, XS.CommandLineValueCount.None, null,"true") , new XS.CommandLineParameter(xs.utf8, XS.CommandLineValueCount.None, "false", "true"), new XS.CommandLineParameter(xs.scriptargs, null, XS.CommandLineValueCount.Multiple, null,null) }; param[param.Length - 1].Last = true; param[param.Length - 2].Last = true; XS.CommandLineParameters xsParams = new XS.CommandLineParameters(param,"//",false); foreach (XS.CommandLineParameter a in xsParams) if (!string.IsNullOrEmpty(a.Name) && a.Name!=xs.scriptargs) a.Switch = a.Name.Replace("xs.", ""); int exitCode = 0; bool utf8 = false; foreach (string arg in args) if (arg.Equals(xs.utf8.Replace("xs.", "//"), StringComparison.OrdinalIgnoreCase)) utf8 = true; using (XS.ConsoleWithColors cout = new XS.ConsoleWithColors(Environment.GetEnvironmentVariable("XSH_COLORS"),utf8)) using (XS.CtrlCInterceptor ctrl = new XS.CtrlCInterceptor()) { context.Output += cout.OnOutput; ctrl.Output = context.Error; ctrl.Abort += delegate { context.Abort(); }; AppDomainLoader.progress("MainWithContext: Console ready --------------------"); System.Diagnostics.Stopwatch w = System.Diagnostics.Stopwatch.StartNew(); try { AppDomainLoader.progress("MainWithContext: Before parse--------------------"); xsParams.Parse(context, args, false); AppDomainLoader.progress("MainWithContext: Before set options--------------------"); setOutputOptions(context, cout); AppDomainLoader.progress("MainWithContext: Before load--------------------"); ${GENERATED_CLASS} cl = new ${GENERATED_CLASS}(); XS.Script s = cl.Script; ctrl.IgnoreCtrlC = s.IgnoreCtrlC; ctrl.AbortDelay = XS.Utils.ToTimeSpan(s.AbortDelay) ?? ctrl.AbortDelay; ctrl.ExitDelay = XS.Utils.ToTimeSpan(s.ExitDelay) ?? ctrl.ExitDelay; AppDomainLoader.progress("MainWithContext: After load--------------------"); if (context.IsSet(xs.save)) { using (XS.ScriptContextScope r=new XS.ScriptContextScope(context)) s.Save(context.GetString(xs.save)); } else { context.Compiler.AddRequireAdmin(XS.Utils.To<XS.RequireAdminMode>(context.GetStr(xs.requireAdmin, XS.RequireAdminMode.User.ToString()))); context.Compiler.AddRequireAdmin(s.RequireAdmin); if (context.Compiler.RequireAdmin!=XS.RequireAdminMode.User && !context.IsAdministrator) return restartAsAdmin(context, args, context.Compiler.RequireAdmin==XS.RequireAdminMode.Hidden); AppDomainLoader.progress("MainWithContext: Before initialization --------------------"); context.Initialize(s); AppDomainLoader.progress("MainWithContext: After initialization --------------------"); string[] parms = null; if (context.IsSet(xs.scriptargs)) parms = context.GetStringArray(xs.scriptargs); XS.ConsoleRedirector redir = new XS.ConsoleRedirector(context); try { AppDomainLoader.progress("MainWithContext: Before executing --------------------"); object r = context.ExecuteScript(s, parms, XS.CallIsolation.High); ctrl.KillAbortTimer(); AppDomainLoader.progress("MainWithContext: After executing --------------------"); if (r != null) int.TryParse(r.ToString(), out exitCode); } finally { ctrl.KillAbortTimer(); redir.Dispose(); } } }
static int MainWithContext(ScriptContext context, string[] args) { CommandLineParameters xsParams = getXsParams(); int exitCode = 0; ConsoleRedirector redir = null; AppDomainLoader.progress("MainWithContext: Entering --------------------"); bool utf8 = false; foreach (string arg in args) if (arg.Equals(xs.utf8.Replace("xs.", "//"), StringComparison.OrdinalIgnoreCase)) utf8 = true; using (ConsoleWithColors cout = new ConsoleWithColors(Environment.GetEnvironmentVariable("XSH_COLORS"),utf8)) using (CtrlCInterceptor ctrl = new CtrlCInterceptor()) { context.Output += cout.OnOutput; ctrl.Output = context.Error; ctrl.Abort += delegate { context.Abort(); }; Stopwatch w = Stopwatch.StartNew(); try { // Parse arguments var usage = new UsageGenerator() { Options = UsageOptions.None }; xsParams.Parse(context, args, false); setOutputOptions(context, cout); if (context.IsSet(xs.path)) context.ScriptPath = context.GetString(xs.path); // Load references List<IScriptAction> preScript = getCommandlineReferences(context); // Print help if (args.Length == 0 || context.IsSet(xs.help)) { loadReferences(context,preScript); exitCode = HelpHelper.Help(context, usage , xsParams); goto end; } // Handle upgrade if (context.GetBool(xs.upgrade, false)) { return upgrade(context);} if (context.IsSet(xs.updateStage)) { return updateStage(context, context.GetStringArray(xs.updateStage));} AppDomainLoader.progress("MainWithContext: Processing options"); // Process the remaining options context.Compiler.AddRequireAdmin(Utils.To<RequireAdminMode>(context.GetStr(xs.requireAdmin, RequireAdminMode.User.ToString()))); if (context.IsSet(xs.codeout)) context.CodeOutputDirectory = Path.GetFullPath(context.GetString(xs.codeout)); if (context.IsSet(xs.genconfig) || context.IsSet(xs.gensample)) genDemoConfig(cout, context); if (context.IsSet(xs.forcenet20)) context.Compiler.DefaultNETVersion = new Version(2,0); AppDomainLoader.progress("MainWithContext: Processing options, continuing"); List<string> filteredArgs = new List<string>(); if (context.IsSet(xs.scriptargs)) filteredArgs.AddRange(context.GetStringArray(xs.scriptargs)); // Run utilities, like //download etc Script script= getInlineScript(context, filteredArgs); string scriptName = context.GetString(xs.script, null); if (script == null) { // Load the script if (scriptName == "/?" || scriptName == "-?") { for (int i = 0; i < args.Length;++i ) if (args[i] == "/?" || args[i]=="-?") { if (i != args.Length - 1) context[xs.help] = args[i + 1]; break; } loadReferences(context, preScript); return HelpHelper.Help(context, usage, xsParams); } if (scriptName != null) { AppDomainLoader.progress("MainWithContext: Loading script " + scriptName); script = loadScript(scriptName, context); AppDomainLoader.progress("MainWithContext: Loading completed"); } } AppDomainLoader.progress("MainWithContext: About to initialize"); // Attach script if (script != null) { // Insert pre-script before the script body int n = 0; foreach (var list in preScript) script.Items.Insert(n++, list); AppDomainLoader.BaseDirectory = script.DirectoryName; RequireAdminMode mode = context.Compiler.RequireAdmin; if ((!context.IsAdministrator || context.GetBool(xs.testElevation, false)) && !isCodeGeneration(context) && mode!=RequireAdminMode.User) { return restartAsAdmin(context, args, mode==RequireAdminMode.Hidden && !(context.GetBool(xs.testElevation, false))); } AppDomainLoader.progress("MainWithContext: Before script initialization"); if (isCodeGeneration(context)) { if (!context.EnableCodePrecompilation && (context.IsSet(xs.genexe) || context.IsSet(xs.genwinexe) || context.IsSet(xs.genlibrary) || context.IsSet(xs.gencs))) throw new ParsingException("One of the loaded scripts has precompilation disabled. Executable cannot be generated from this script."); context.EnableCodePrecompilation = false; } ctrl.IgnoreCtrlC = script.IgnoreCtrlC; ctrl.AbortDelay = Utils.ToTimeSpan(script.AbortDelay) ?? ctrl.AbortDelay; ctrl.ExitDelay = Utils.ToTimeSpan(script.ExitDelay) ?? ctrl.ExitDelay; context.Initialize(script); AppDomainLoader.progress("MainWithContext: Script initialization completed"); } // After precompilation we're ready to write .exe, if requested if (isCodeGeneration(context)) { doCodeGeneration(context, script); } else if (script != null) { // Run the script AppDomainLoader.progress("MainWithContext: Before script execution"); redir = new ConsoleRedirector(context); try { object r = context.ExecuteScript(script, filteredArgs.ToArray(), CallIsolation.High); if (r != null) int.TryParse(r.ToString(), out exitCode); } finally { ctrl.KillAbortTimer(); redir.Dispose(); redir = null; } AppDomainLoader.progress("MainWithContext: Script execution completed"); } } catch (ThreadAbortException ae) { AppDomainLoader.progress("MainWithContext: ThreadAbortException is being aborted"); resetAbort(context); context.WriteException(ae); exitCode = -1; } catch(ScriptTerminateException te) { exitCode = te.ExitCode; resetAbort(context); if (te.InnerException != null) { context.WriteException(te.InnerException); AppDomainLoader.progress("MainWithContext: " + te.InnerException); } AppDomainLoader.progress("MainWithContext: Terminating with exit code " + exitCode); } catch (Exception e) { exitCode = -1; resetAbort(context); context.WriteException(e); AppDomainLoader.progress("MainWithContext: " + e); } finally { resetAbort(context); AppDomainLoader.BaseDirectory = null; } end: // Display how long did it take w.Stop(); if (context.GetBool(xs.wait,false)) { cout.WriteLine(OutputType.Info,string.Format("Completed in {0} with exit code={1}. Press Enter to close...", w.Elapsed, exitCode)); Console.ReadLine(); } } AppDomainLoader.progress("MainWithContext: Exiting with code "+exitCode); return exitCode; }