protected int execute()

in impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnsh/ShellInvoker.java [69:184]


    protected int execute(LookupContext context) throws Exception {
        // set up JLine built-in commands
        ConfigurationPath configPath = new ConfigurationPath(context.cwd.get(), context.cwd.get());
        Builtins builtins = new Builtins(context.cwd, configPath, null);
        builtins.rename(Builtins.Command.TTOP, "top");
        builtins.alias("zle", "widget");
        builtins.alias("bindkey", "keymap");

        ShellCommandRegistryHolder holder = new ShellCommandRegistryHolder();
        holder.addCommandRegistry(builtins);

        // gather commands
        Map<String, ShellCommandRegistryFactory> factories =
                context.lookup.lookupMap(ShellCommandRegistryFactory.class);
        for (Map.Entry<String, ShellCommandRegistryFactory> entry : factories.entrySet()) {
            holder.addCommandRegistry(entry.getValue().createShellCommandRegistry(context));
        }

        DefaultParser parser = new DefaultParser();
        parser.setRegexCommand("[:]{0,1}[a-zA-Z!]{1,}\\S*"); // change default regex to support shell commands

        String banner =
                """

                ░▒▓██████████████▓▒░ ░▒▓█▓▒░░▒▓█▓▒░░▒▓███████▓▒░  ░▒▓███████▓▒░░▒▓█▓▒░░▒▓█▓▒░\s
                ░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒░       ░▒▓█▓▒░░▒▓█▓▒░\s
                ░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒░       ░▒▓█▓▒░░▒▓█▓▒░\s
                ░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓██████▓▒░ ░▒▓████████▓▒░\s
                ░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒░  ░▒▓█▓▓█▓▒░  ░▒▓█▓▒░░▒▓█▓▒░       ░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒░\s
                ░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒░  ░▒▓█▓▓█▓▒░  ░▒▓█▓▒░░▒▓█▓▒░       ░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒░\s
                ░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒░   ░▒▓██▓▒░   ░▒▓█▓▒░░▒▓█▓▒░░▒▓███████▓▒░ ░▒▓█▓▒░░▒▓█▓▒░""";
        context.writer.accept(banner);
        if (!context.invokerRequest.options().showVersion().orElse(false)) {
            context.writer.accept(CLIReportingUtils.showVersionMinimal());
        }
        context.writer.accept("");

        try (holder) {
            SimpleSystemRegistryImpl systemRegistry =
                    new SimpleSystemRegistryImpl(parser, context.terminal, context.cwd, configPath) {
                        @Override
                        public boolean isCommandOrScript(String command) {
                            return command.startsWith("!") || super.isCommandOrScript(command);
                        }
                    };
            systemRegistry.setCommandRegistries(holder.getCommandRegistries());

            Path history = context.userDirectory.resolve(".mvnsh_history");
            LineReader reader = LineReaderBuilder.builder()
                    .terminal(context.terminal)
                    .history(new DefaultHistory())
                    .highlighter(new ReplHighlighter())
                    .completer(systemRegistry.completer())
                    .parser(parser)
                    .variable(LineReader.LIST_MAX, 50) // max tab completion candidates
                    .variable(LineReader.HISTORY_FILE, history)
                    .variable(LineReader.OTHERS_GROUP_NAME, "Others")
                    .variable(LineReader.COMPLETION_STYLE_GROUP, "fg:blue,bold")
                    .variable("HELP_COLORS", "ti=1;34:co=38:ar=3:op=33:de=90")
                    .option(LineReader.Option.GROUP_PERSIST, true)
                    .build();
            builtins.setLineReader(reader);
            systemRegistry.setLineReader(reader);
            new TailTipWidgets(reader, systemRegistry::commandDescription, 5, TailTipWidgets.TipType.COMPLETER);
            KeyMap<Binding> keyMap = reader.getKeyMaps().get("main");
            keyMap.bind(new Reference("tailtip-toggle"), KeyMap.alt("s"));

            // start the shell and process input until the user quits with Ctrl-D
            AtomicReference<Exception> failure = new AtomicReference<>();
            while (true) {
                try {
                    failure.set(null);
                    systemRegistry.cleanUp();
                    Thread commandThread = new Thread(() -> {
                        try {
                            systemRegistry.execute(reader.readLine(
                                    context.cwd.get().getFileName().toString() + " mvnsh> ",
                                    null,
                                    (MaskingCallback) null,
                                    null));
                        } catch (Exception e) {
                            failure.set(e);
                        }
                    });
                    context.terminal.handle(Terminal.Signal.INT, signal -> commandThread.interrupt());
                    commandThread.start();
                    commandThread.join();
                    if (failure.get() != null) {
                        throw failure.get();
                    }
                } catch (UserInterruptException e) {
                    // Ignore
                    // return CANCELED;
                } catch (EndOfFileException e) {
                    return OK;
                } catch (SystemRegistryImpl.UnknownCommandException e) {
                    context.writer.accept(context.invokerRequest
                            .messageBuilderFactory()
                            .builder()
                            .error(e.getMessage())
                            .build());
                } catch (Exception e) {
                    systemRegistry.trace(e);
                    context.writer.accept(context.invokerRequest
                            .messageBuilderFactory()
                            .builder()
                            .error("Error: " + e.getMessage())
                            .build());
                    if (context.invokerRequest.options().showErrors().orElse(false)) {
                        e.printStackTrace(context.terminal.writer());
                    }
                    return ERROR;
                }
            }
        }
    }