public bool Execute(Session ses, Arguments arg, Level child) { if (!Executable) return false; try { if (Action != null) Process.Start(Action); if (Executed != null) { ScriptEventArgs e = new ScriptEventArgs(ses, arg, child); Executed?.Invoke(this, e); return e.Succeeded; } return true; } catch (Exception e) { ses.Error(e); return false; } }
public MainWindow() { InitializeComponent(); engine = Engine.Load("config.xml"); session = new Session(engine); session.WidgetChanged += (sender, widget) => UpdateWidget(widget); session.CommandExecuted += (sender, lvl) => ClearInput(); session.ExceptionThrown += (sender, e) => DisplayException(e); session.ScopeChanged += (sender, cmd) => ClearInput(); noMatch.Content = "No completion"; }
public Level Analyze(Session ses, Tokens tokens) { Level lvl = new Level(this); ses.Leaf = lvl; tokens.SetLevel(lvl); // Match parameters for (int i = 0; i < Parameters.Count; i++) { Parameter p = Parameters[i]; p.Update(ses); MatchResult res = p.Analyze(ses, tokens, lvl); // if no tokens left, leave unmatched alone (to match previous params) // else, update so that later ones would not go back before the param // this also ensures auto completion even if the word is already completed // TODO: unmatched might jump back (e.g. "task name -s") if (tokens.Count == 0) break; if (res == MatchResult.Matched) lvl.FirstUnmatchedParam = i + 1; else if (res == MatchResult.Extensible) lvl.FirstUnmatchedParam = i; else if (p.Required) // implied "Failed" { // TODO: handle "required" ses.Error(new ArgumentException($"{p} requires a valid value")); break; } } // Raise event InputChanged?.Invoke(this, new ScriptEventArgs(ses, lvl.Arguments, lvl.Child)); return lvl; }
public void Analyze(Session ses, string input) { Tokens tokens = new Tokens(input); ses.Input = tokens.Last(); ses.Root = ses.Scope.Analyze(ses, tokens); if (tokens.Count > 0) ses.Input = tokens.First(); // If script is not loaded, it's time to load now ses.Leaf.Command.InitializeScript(ses); // Possibly empty - to clear widget when out of scope ses.Widget = ses.Leaf.Command.Widget; // Auto complete ses.Completion = new Completion(); Level lvl = ses.Input.Level; List<Parameter> param = lvl.Command.Parameters; for (int i = lvl.FirstUnmatchedParam; i < param.Count; i++) { param[i].Complete(ses.Completion, ses.Input.Text, ses.Leaf.Arguments); // no auto completion after required parameter if (param[i].Required) break; } // Auto launch if (AutoLaunch && ses.Completion.Completions.Count == 1) { Command cmd = ses.Completion.Completions[0].Unit as Command; if (cmd?.Standalone == true) ses.ExecuteCommand(this, new Level(cmd)); } }
public void InitializeScript(Session ses) { if (ScriptSource == null || Script != null) return; ses.Engine.LoadScriptEngine(); try { Script = ses.Engine.ScriptEngine.CreateScope(); Script.SetVariable("engine", ses.Engine); Script.SetVariable("command", this); ses.Engine.ScriptEngine.ExecuteFile(ScriptSource, Script); } catch (Exception e) { ses.Error(e); } }
public ScriptEventArgs(Session ses, Arguments arg, Level child) { Engine = ses.Engine; Session = ses; Arguments = arg; Child = child; }
/// <summary> /// Every application in Starcounter works like a console application. They have an .EXE ending. They have a Main() function and /// they can do console output. However, they are run inside the scope of a database rather than connecting to it. /// </summary> public static void Init() { Application application = Application.Current; Layout.Register(); JsonResponseMerger.RegisterMergeCallback(OnJsonMerge); application.Use((Request req) => { string uri = req.Uri; // Checking if we should process this request. if (("/" == uri) || (uri.StartsWith("/launcher/", StringComparison.InvariantCultureIgnoreCase)) || (uri.Equals("/launcher", StringComparison.InvariantCultureIgnoreCase))) { return null; } return WrapInLauncher(req); }); Handle.GET("/launcher", (Request req) => { var session = Session.Current; LauncherPage launcher; if (session != null && session.Data != null) { launcher = (LauncherPage)Session.Current.Data; launcher.uri = req.Uri; MarkWorkspacesInactive(launcher.workspaces); return launcher; } if (session == null) { session = new Session(SessionOptions.PatchVersioning); } launcher = new LauncherPage() { Html = "/Launcher/viewmodels/LauncherTemplate.html" }; launcher.Session = session; launcher.launchpad.names = Self.GET<Json>(UriMapping.MappingUriPrefix + "/app-name", () => { var p = new Page(); return p; }); var setup = Layout.GetSetup("/launcher/launchpad"); if (setup == null) { // launcher.launchpad.layout = null // workaround for https://github.com/Starcounter/Starcounter/issues/3072 // set default value // consider moving to HTML, or pre-populatind default layouts //launcher.launchpad.layout = new Json("{\"width\": \"1000\", \"items\":[]}"); launcher.launchpad.layout = "{\"width\": \"1000\", \"items\":[]}"; } else { // dynamic setupJson = new Json(setup.Value); launcher.launchpad.layout = setup.Value; //setupJson; } launcher.user = Self.GET(UriMapping.MappingUriPrefix + "/user", () => { var p = new Page(); return p; }); launcher.menu = Self.GET<Json>(UriMapping.MappingUriPrefix + "/menu", () => { var p = new Page() { Html = "/Launcher/viewmodels/LauncherMenu.html" }; return p; }); launcher.uri = req.Uri; MarkWorkspacesInactive(launcher.workspaces); return launcher; }); Handle.GET("/launcher/dashboard", (Request req) => { LauncherPage launcher = Self.GET<LauncherPage>("/launcher"); launcher.currentPage = Self.GET(UriMapping.MappingUriPrefix + "/dashboard", () => { var p = new Page(); return p; }); launcher.uri = req.Uri; return launcher; }); Handle.GET("/launcher/settings", (Request req) => { LauncherPage launcher = Self.GET<LauncherPage>("/launcher"); launcher.currentPage = Self.GET<SettingsPage>(UriMapping.MappingUriPrefix + "/settings", () => { var p = new SettingsPage() { Html = "/Launcher/viewmodels/SettingsPage.html" }; return p; }); launcher.uri = req.Uri; return launcher; }); Handle.GET("/launcher/search?query={?}", (Request req, string query) => { // LauncherPage launcher = Self.GET<LauncherPage>("/launcher"); // string uri = UriMapping.MappingUriPrefix + "/search?query=" + HttpUtility.UrlEncode(query); /* launcher.currentPage = Self.GET<ResultPage>(uri, () => { var p = new ResultPage() { Html = "/Launcher/viewmodels/ResultPage.html" }; return p; }); launcher.uri = req.Uri; launcher.searchBar.query = query;*/ return null; }); // + dummy responses from launcher itself // Merges HTML partials according to provided URLs. Handle.GET(StarcounterConstants.HtmlMergerPrefix + "{?}", (String s) => { StringBuilder sb = new StringBuilder(); String[] allPartialInfos = s.Split(new char[] { '&' }); foreach (String appNamePlusPartialUrl in allPartialInfos) { String[] a = appNamePlusPartialUrl.Split(new char[] { '=' }); if (String.IsNullOrEmpty(a[1])) continue; Response resp = Self.GET(a[1]); sb.Append("<imported-template-scope scope=\"" + a[0] + "\">"); sb.Append("<template><juicy-tile-group name=\"" + a[0] + "\"></juicy-tile-group></template>"); if (resp != null) sb.Append(resp.Body); sb.Append("</imported-template-scope>"); } return sb.ToString(); }, new HandlerOptions() { SkipHandlersPolicy = true, ReplaceExistingHandler = true }); }
public bool Execute(Session ses) { return Command.Execute(ses, Arguments, Child); }