1// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5#include "jsgenerator.h"
6#include "services/project/projectservice.h"
7#include "common/widget/outputpane.h"
8#include <framework/framework.h>
9
10#include <QFile>
11#include <QDBusMessage>
12#include <QDBusConnection>
13
14using namespace dpfservice;
15
16class JSGeneratorPrivate
17{
18 friend class JSGenerator;
19};
20
21JSGenerator::JSGenerator()
22 : d(new JSGeneratorPrivate())
23{
24}
25
26JSGenerator::~JSGenerator()
27{
28 if (d)
29 delete d;
30}
31
32QString JSGenerator::debugger()
33{
34 return "local";
35}
36
37bool JSGenerator::prepareDebug(const QMap<QString, QVariant> &param, QString &retMsg)
38{
39 Q_UNUSED(param)
40 Q_UNUSED(retMsg)
41 return true;
42}
43
44bool JSGenerator::requestDAPPort(const QString &uuid, const QMap<QString, QVariant> &param, QString &retMsg)
45{
46 QString targetPath = param.value("targetPath").toString();
47 QStringList arguments = param.value("arguments").toStringList();
48 QString kit = "jsdirectory";
49
50 QDBusMessage msg = QDBusMessage::createSignal("/path",
51 "com.deepin.unioncode.interface",
52 "getDebugPort");
53
54 msg << uuid
55 << kit
56 << targetPath
57 << arguments;
58 bool ret = QDBusConnection::sessionBus().send(msg);
59 if (!ret) {
60 retMsg = tr("Request debug dap port failed, please retry.");
61 return false;
62 }
63
64 return true;
65}
66
67bool JSGenerator::isNeedBuild()
68{
69 return false;
70}
71
72bool JSGenerator::isTargetReady()
73{
74 return true;
75}
76
77bool JSGenerator::isLaunchNotAttach()
78{
79 return true;
80}
81
82dap::LaunchRequest JSGenerator::launchDAP(const QMap<QString, QVariant> &param)
83{
84 QString targetPath = param.value("targetPath").toString();
85 QStringList arguments = param.value("arguments").toStringList();
86
87 dap::LaunchRequest request;
88 request.name = "(jsdbg) Launch";
89 request.type = "jsdbg";
90 request.request = "launch";
91 request.program = targetPath.toStdString();
92 request.stopAtEntry = false;
93 dap::array<dap::string> arrayArg;
94 foreach (QString arg, arguments) {
95 arrayArg.push_back(arg.toStdString());
96 }
97 request.args = arrayArg;
98 request.externalConsole = false;
99 request.MIMode = "jsdbg";
100 request.__sessionId = QUuid::createUuid().toString().toStdString();
101
102 return request;
103}
104
105QString JSGenerator::build(const QString& projectPath)
106{
107 Q_UNUSED(projectPath)
108 return "";
109}
110
111QString JSGenerator::getProjectFile(const QString& projectPath)
112{
113 Q_UNUSED(projectPath)
114 return "";
115}
116
117QMap<QString, QVariant> JSGenerator::getDebugArguments(const dpfservice::ProjectInfo &projectInfo,
118 const QString &currentFile)
119{
120 Q_UNUSED(currentFile)
121
122 QMap<QString, QVariant> param;
123 param.insert("workspace", projectInfo.workspaceFolder());
124
125 return param;
126}
127
128RunCommandInfo JSGenerator::getRunArguments(const ProjectInfo &projectInfo, const QString &currentFile)
129{
130 Q_UNUSED(projectInfo)
131 if (!QFile::exists(currentFile)) {
132 OutputPane::instance()->appendText(tr("Please open a JS file in editor!"), OutputPane::ErrorMessage);
133 }
134 return {"node", {currentFile}, ""};
135}
136