protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // List all *.xsh files in App_Data var files = getScripts(); if (files.Length > 0) { cbScript.Items.Clear(); tbScript.Visible = false; cbScript.Visible = true; foreach (var file in files) { var name = Path.GetFileName(file); var sb = new StringBuilder("[" + name + "] "); if (Path.GetExtension(file).ToUpper() == ".XSH") { var ctx = new XS.ScriptContext() {EnableCodePrecompilation = false}; var s = ctx.LoadScript(file, false); sb.Append(s.VersionInfo.Title); cbScript.Items.Add(new ListItem(sb.ToString(), name)); } else { var f=File.ReadAllLines(file); if (f.Length>0 && f[0].StartsWith("@rem",StringComparison.OrdinalIgnoreCase)) sb.Append(f[0].Substring(5)); else sb.Append(f[0].Substring(5)); cbScript.Items.Add(new ListItem(sb.ToString(), name)); } } var script = Request.QueryString["id"]; if (script != null) { int? selection=null; for (int i = 0; i < cbScript.Items.Count; ++i) { if (script.Equals(cbScript.Items[i].Value, StringComparison.OrdinalIgnoreCase)) { selection = i; break; } if (selection == null && cbScript.Items[i].Value.StartsWith(script, StringComparison.OrdinalIgnoreCase)) selection = i; } if (selection.HasValue) cbScript.SelectedIndex = selection.Value; } var arg = Request.QueryString["args"]; if (arg != null) tbArguments.Text = arg; } else { var script = new XS.Script { Items = new List<XS.IScriptAction> { new XS.Print("Current directory: ${=.CurrentDirectory}") {OutTo = "^bold"}, new XS.Print("Current user: ${=System.Security.Principal.WindowsIdentity.GetCurrent().Name}") {OutTo = "^bold"}, new XS.Print("Temp directory: ${%TEMP%}") {OutTo = "^bold"}, new XS.Print(), new XS.Print("-- This will print 3 steps, ${=2+1} seconds one after another ---"), new XS.While { MaxCount = "3", Name = "i", Items = new List<XS.IScriptAction> { new XS.Print("Hello ") {NewLine = false}, new XS.Print("World #${i}!") {OutTo = "^bold"}, new XS.Sleep(3000) } }, new XS.Shell(@"@echo off echo -- This batch file will print 10 steps, 2 seconds one after another for /l %%f in (1 1 10) do (@echo Step #%%f) & (echo | @CHOICE /D y /T 2 2>nul 1>nul ) echo -- Completed -- ") { OutTo = "^info", ErrorTo = "^error", CreateNoWindow = true, IgnoreExitCode = true, Mode = XS.ShellMode.Batch } } }; tbScript.Text = script.SaveToString(); tbScript.Visible = true; cbScript.Visible = false; } } }
// 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); }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // List all *.xsh files in App_Data var files = getScripts(); if (files.Length > 0) { cbScript.Items.Clear(); tbScript.Visible = false; cbScript.Visible = true; foreach (var file in files) { var name = Path.GetFileName(file); var sb = new StringBuilder("[" + name + "] "); if (Path.GetExtension(file).ToUpper() == ".XSH") { var ctx = new XS.ScriptContext() { EnableCodePrecompilation = false }; var s = ctx.LoadScript(file, false); sb.Append(s.VersionInfo.Title); cbScript.Items.Add(new ListItem(sb.ToString(), name)); } else { var f = File.ReadAllLines(file); if (f.Length > 0 && f[0].StartsWith("@rem", StringComparison.OrdinalIgnoreCase)) { sb.Append(f[0].Substring(5)); } else { sb.Append(f[0].Substring(5)); } cbScript.Items.Add(new ListItem(sb.ToString(), name)); } } var script = Request.QueryString["id"]; if (script != null) { int?selection = null; for (int i = 0; i < cbScript.Items.Count; ++i) { if (script.Equals(cbScript.Items[i].Value, StringComparison.OrdinalIgnoreCase)) { selection = i; break; } if (selection == null && cbScript.Items[i].Value.StartsWith(script, StringComparison.OrdinalIgnoreCase)) { selection = i; } } if (selection.HasValue) { cbScript.SelectedIndex = selection.Value; } } var arg = Request.QueryString["args"]; if (arg != null) { tbArguments.Text = arg; } } else { var script = new XS.Script { Items = new List <XS.IScriptAction> { new XS.Print("Current directory: ${=.CurrentDirectory}") { OutTo = "^bold" }, new XS.Print("Current user: ${=System.Security.Principal.WindowsIdentity.GetCurrent().Name}") { OutTo = "^bold" }, new XS.Print("Temp directory: ${%TEMP%}") { OutTo = "^bold" }, new XS.Print(), new XS.Print("-- This will print 3 steps, ${=2+1} seconds one after another ---"), new XS.While { MaxCount = "3", Name = "i", Items = new List <XS.IScriptAction> { new XS.Print("Hello ") { NewLine = false }, new XS.Print("World #${i}!") { OutTo = "^bold" }, new XS.Sleep(3000) } }, new XS.Shell(@"@echo off echo -- This batch file will print 10 steps, 2 seconds one after another for /l %%f in (1 1 10) do (@echo Step #%%f) & (echo | @CHOICE /D y /T 2 2>nul 1>nul ) echo -- Completed -- ") { OutTo = "^info", ErrorTo = "^error", CreateNoWindow = true, IgnoreExitCode = true, Mode = XS.ShellMode.Batch } } }; tbScript.Text = script.SaveToString(); tbScript.Visible = true; cbScript.Visible = false; } } }