Esempio n. 1
0
        // 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();
                        }
                    }
                }
Esempio n. 2
0
        // 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);
        }
Esempio n. 3
0
        /// Parse command line arguments
        public void ParseArguments(IEnumerable <string> args)
        {
            CommandLineParameters c = new CommandLineParameters(Parameters, SwitchPrefixes, UnknownSwitches);

            c.Parse(Context, args, (Usage.Options & UsageOptions.IfHelp) != 0);
        }