1// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5#include "dapconfig.h"
6#include "util/custompaths.h"
7#include "util/processutil.h"
8
9#include <QJsonDocument>
10#include <QJsonObject>
11#include <QFile>
12
13namespace support_file {
14
15DapSupportConfig::DapSupportConfig()
16{
17
18}
19
20QString DapSupportConfig::globalPath()
21{
22 auto result = CustomPaths::endSeparator(CustomPaths::global(CustomPaths::Configures));
23 return result + QString("dapconfig.support");
24}
25
26QString DapSupportConfig::userPath()
27{
28 auto result = CustomPaths::endSeparator(CustomPaths::user(CustomPaths::Configures));
29 return result + QString("dapconfig.support");
30}
31
32bool DapSupportConfig::readFromSupportFile(const QString &filePath, const QString &arch, JavaDapPluginConfig &javaconfig, const QString &configHome)
33{
34 QFile file(filePath);
35 if (!file.open(QIODevice::ReadOnly))
36 return false;
37
38 QByteArray data = file.readAll();
39 file.close();
40
41 QJsonParseError parseError;
42 QJsonDocument doc = QJsonDocument::fromJson(data, &parseError);
43 if (QJsonParseError::NoError != parseError.error)
44 return false;
45
46 if (!doc.isObject())
47 return false;
48
49 QJsonObject rootObject = doc.object();
50 if (!rootObject.contains(arch))
51 return false;
52
53 QJsonObject archObject = rootObject.value(arch).toObject();
54 if (archObject.isEmpty())
55 return false;
56
57 QJsonObject valueObject = archObject.value("java_config_path").toObject();
58 if (valueObject.isEmpty())
59 return false;
60
61 javaconfig.launchPackageFile = configHome + valueObject.value("launch_package_file").toString();
62 javaconfig.launchConfigPath = configHome + valueObject.value("launch_config_path").toString();
63 javaconfig.dapPackageFile = configHome + valueObject.value("dap_package_file").toString();
64 QString jrePath = valueObject.value("jre_path").toString();
65 if (jrePath.isEmpty()) {
66 QString ret = ProcessUtil::execute({"which java"}, true);
67 auto getLink = [](QString &file)->QString{
68 QString ret = ProcessUtil::execute({QString("ls -lrt %1").arg(file)}, true);
69 if (!ret.isEmpty()) {
70 ret = ret.split(" -> ").back();
71 }
72 return ret;
73 };
74 QString etc = getLink(ret);
75 QString link = getLink(etc);
76 javaconfig.jreExecute = link;
77 javaconfig.jrePath = link.split("bin/java").front();
78 } else {
79 javaconfig.jrePath = configHome + jrePath;
80 javaconfig.jreExecute = configHome + valueObject.value("jre_execute").toString();
81 }
82 return true;
83}
84
85} // namespace support_file
86