aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/lua/bindings/qtcprocess.cpp
blob: 93d3af2e0aca1b9879e495a3e75027b71d4cbd0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "../luaengine.h"

#include <utils/environment.h>
#include <utils/qtcprocess.h>

using namespace Utils;

namespace Lua::Internal {

void addProcessModule()
{
    LuaEngine::registerProvider(
        "Process", [](sol::state_view lua) -> sol::object {
            sol::table async = lua.script("return require('async')", "_process_").get<sol::table>();
            sol::function wrap = async["wrap"];

            sol::table process = lua.create_table();

            process["runInTerminal_cb"] = [](const QString &cmdline, const sol::function &cb) {
                Process *p = new Process;
                p->setTerminalMode(TerminalMode::Run);
                p->setCommand(CommandLine::fromUserInput((cmdline)));
                p->setEnvironment(Environment::systemEnvironment());

                QObject::connect(p, &Process::done, &LuaEngine::instance(), [p, cb]() {
                    cb(p->exitCode());
                });

                p->start();
            };

            process["runInTerminal"] = wrap(process["runInTerminal_cb"]);

            return process;
        });
}

} // namespace Lua::Internal