1// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5#include "gradlebuildergenerator.h"
6#include "gradle/builder/parser/gradleparser.h"
7#include "services/window/windowservice.h"
8#include "services/builder/builderservice.h"
9#include "services/option/optionmanager.h"
10
11using namespace dpfservice;
12
13class GradleBuilderGeneratorPrivate
14{
15 friend class GradleBuilderGenerator;
16};
17
18GradleBuilderGenerator::GradleBuilderGenerator()
19 : d(new GradleBuilderGeneratorPrivate())
20{
21
22}
23
24GradleBuilderGenerator::~GradleBuilderGenerator()
25{
26 if (d)
27 delete d;
28}
29
30BuildCommandInfo GradleBuilderGenerator::getMenuCommand(const BuildMenuType buildMenuType, const dpfservice::ProjectInfo &projectInfo)
31{
32 BuildCommandInfo info;
33 info.kitName = projectInfo.kitName();
34 info.workingDir = projectInfo.workspaceFolder();
35 info.program = projectInfo.buildProgram();
36 if (info.program.isEmpty())
37 info.program = OptionManager::getInstance()->getGradleToolPath();
38 switch (buildMenuType) {
39 case Build:
40 info.arguments.append("build");
41 break;
42 case Clean:
43 info.arguments.append("clean");
44 break;
45 }
46
47 QString gradlewPath = info.workingDir + QDir::separator() + "gradlew";
48 if (QFileInfo(gradlewPath).isFile()) {
49 QFileDevice::Permissions p = QFile(gradlewPath).permissions();
50 QFile(gradlewPath).setPermissions(p | QFileDevice::ReadOwner | QFileDevice::WriteOwner | QFileDevice::ExeOwner);
51 }
52
53 return info;
54}
55
56void GradleBuilderGenerator::appendOutputParser(std::unique_ptr<IOutputParser> &outputParser)
57{
58 if (outputParser) {
59 outputParser->takeOutputParserChain();
60 outputParser->appendOutputParser(new GradleParser());
61 }
62}
63
64
65bool GradleBuilderGenerator::checkCommandValidity(const BuildCommandInfo &info, QString &retMsg)
66{
67 if (info.program.trimmed().isEmpty()) {
68 retMsg = tr("The build command of %1 project is null! "\
69 "please install it in console with \"sudo apt install gradle\", and then restart the tool.")
70 .arg(info.kitName.toUpper());
71 return false;
72 }
73
74 if (!QFileInfo(info.workingDir.trimmed()).exists()) {
75 retMsg = tr("The path of \"%1\" is not exist! "\
76 "please check and reopen the project.")
77 .arg(info.workingDir);
78 return false;
79 }
80
81 return true;
82}
83
84