2019-08-27 20:10:36 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2019 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
|
|
|
|
**
|
|
|
|
|
** This file is part of the Qt Design Tooling
|
|
|
|
|
**
|
|
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
|
|
|
|
**
|
|
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include <generateresource.h>
|
|
|
|
|
|
|
|
|
|
#include <coreplugin/actionmanager/actionmanager.h>
|
|
|
|
|
#include <coreplugin/actionmanager/actioncontainer.h>
|
|
|
|
|
#include <coreplugin/actionmanager/command.h>
|
|
|
|
|
#include <coreplugin/documentmanager.h>
|
|
|
|
|
#include <coreplugin/messagemanager.h>
|
|
|
|
|
|
|
|
|
|
#include <projectexplorer/project.h>
|
|
|
|
|
#include <projectexplorer/session.h>
|
|
|
|
|
#include <projectexplorer/projectexplorerconstants.h>
|
|
|
|
|
#include <projectexplorer/target.h>
|
|
|
|
|
|
|
|
|
|
#include <qmlprojectmanager/qmlprojectmanagerconstants.h>
|
|
|
|
|
|
|
|
|
|
#include <qtsupport/baseqtversion.h>
|
|
|
|
|
#include <qtsupport/qtkitinformation.h>
|
|
|
|
|
|
|
|
|
|
#include <utils/fileutils.h>
|
|
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
#include <utils/utilsicons.h>
|
|
|
|
|
#include <utils/synchronousprocess.h>
|
|
|
|
|
|
|
|
|
|
#include <QAction>
|
|
|
|
|
#include <QTemporaryFile>
|
|
|
|
|
#include <QMap>
|
|
|
|
|
#include <QProcess>
|
|
|
|
|
#include <QByteArray>
|
|
|
|
|
#include <QObject>
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
|
|
namespace QmlDesigner {
|
|
|
|
|
void GenerateResource::generateMenuEntry()
|
|
|
|
|
{
|
|
|
|
|
Core::ActionContainer *buildMenu =
|
|
|
|
|
Core::ActionManager::actionContainer(ProjectExplorer::Constants::M_BUILDPROJECT);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const Core::Context projectContext(QmlProjectManager::Constants::QML_PROJECT_ID);
|
|
|
|
|
// ToDo: move this to QtCreator and add tr to the string then
|
2020-01-23 16:58:10 +01:00
|
|
|
auto action = new QAction(QCoreApplication::translate("QmlDesigner::GenerateResource", "Generate Resource File"));
|
2019-08-27 20:10:36 +02:00
|
|
|
action->setEnabled(ProjectExplorer::SessionManager::startupProject() != nullptr);
|
|
|
|
|
// todo make it more intelligent when it gets enabled
|
|
|
|
|
QObject::connect(ProjectExplorer::SessionManager::instance(), &ProjectExplorer::SessionManager::startupProjectChanged, [action]() {
|
|
|
|
|
action->setEnabled(ProjectExplorer::SessionManager::startupProject());
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Core::Command *cmd = Core::ActionManager::registerAction(action, "QmlProject.CreateResource");
|
|
|
|
|
QObject::connect(action, &QAction::triggered, [] () {
|
|
|
|
|
auto currentProject = ProjectExplorer::SessionManager::startupProject();
|
|
|
|
|
auto projectPath = currentProject->projectFilePath().parentDir().toString();
|
|
|
|
|
|
|
|
|
|
static QMap<QString, QString> lastUsedPathes;
|
|
|
|
|
auto saveLastUsedPath = [currentProject] (const QString &lastUsedPath) {
|
|
|
|
|
lastUsedPathes.insert(currentProject->displayName(), lastUsedPath);
|
|
|
|
|
};
|
|
|
|
|
saveLastUsedPath(lastUsedPathes.value(currentProject->displayName(),
|
2019-08-30 14:15:30 +02:00
|
|
|
currentProject->projectFilePath().parentDir().parentDir().toString()));
|
2019-08-27 20:10:36 +02:00
|
|
|
|
|
|
|
|
auto resourceFileName = Core:: DocumentManager::getSaveFileName(
|
2020-01-23 16:58:10 +01:00
|
|
|
QCoreApplication::translate("QmlDesigner::GenerateResource", "Save Project As Resource"),
|
2019-08-30 14:15:30 +02:00
|
|
|
lastUsedPathes.value(currentProject->displayName()) + "/" + currentProject->displayName() + ".qmlrc",
|
2020-01-23 16:58:10 +01:00
|
|
|
QCoreApplication::translate("QmlDesigner::GenerateResource", "QML Resource File (*.qmlrc)"));
|
2019-08-27 20:10:36 +02:00
|
|
|
if (resourceFileName.isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
|
2020-01-23 16:58:10 +01:00
|
|
|
Core::MessageManager::write(QCoreApplication::translate("QmlDesigner::GenerateResource",
|
|
|
|
|
"Generate a resource file out of project %1 to %2").arg(
|
|
|
|
|
currentProject->displayName(), QDir::toNativeSeparators(resourceFileName)));
|
2019-08-27 20:10:36 +02:00
|
|
|
|
|
|
|
|
QTemporaryFile temp(projectPath + "/XXXXXXX.create.resource.qrc");
|
|
|
|
|
if (!temp.open())
|
|
|
|
|
return;
|
|
|
|
|
temp.close();
|
|
|
|
|
|
2019-09-30 14:34:31 +02:00
|
|
|
auto rccBinary = QtSupport::QtKitAspect::qtVersion(currentProject->activeTarget()->kit())->hostBinPath();
|
2019-11-14 16:07:55 +01:00
|
|
|
rccBinary = rccBinary.pathAppended(Utils::HostOsInfo::withExecutableSuffix("rcc"));
|
|
|
|
|
|
2019-08-27 20:10:36 +02:00
|
|
|
QProcess rccProcess;
|
|
|
|
|
rccProcess.setProgram(rccBinary.toString());
|
|
|
|
|
rccProcess.setWorkingDirectory(projectPath);
|
|
|
|
|
|
|
|
|
|
const QStringList arguments1 = {"--project", "--output", temp.fileName()};
|
|
|
|
|
const QStringList arguments2 = {"--binary", "--output", resourceFileName, temp.fileName()};
|
|
|
|
|
|
|
|
|
|
for (auto arguments : {arguments1, arguments2}) {
|
|
|
|
|
rccProcess.start(rccBinary.toString(), arguments);
|
|
|
|
|
if (!rccProcess.waitForStarted()) {
|
2020-01-23 16:58:10 +01:00
|
|
|
Core::MessageManager::write(QCoreApplication::translate("QmlDesigner::GenerateResource",
|
|
|
|
|
"Unable to generate resource file: %1").arg(resourceFileName));
|
2019-08-27 20:10:36 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
QByteArray stdOut;
|
|
|
|
|
QByteArray stdErr;
|
|
|
|
|
if (!Utils::SynchronousProcess::readDataFromProcess(rccProcess, 30, &stdOut, &stdErr, true)) {
|
|
|
|
|
Utils::SynchronousProcess::stopProcess(rccProcess);
|
2020-01-23 16:58:10 +01:00
|
|
|
Core::MessageManager::write(QCoreApplication::translate("QmlDesigner::GenerateResource",
|
|
|
|
|
"A timeout occurred running \"%1\"").arg(rccBinary.toString() + arguments.join(" ")));
|
2019-08-27 20:10:36 +02:00
|
|
|
return ;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
if (!stdOut.trimmed().isEmpty()) {
|
|
|
|
|
Core::MessageManager::write(QString::fromLocal8Bit(stdOut));
|
|
|
|
|
}
|
|
|
|
|
if (!stdErr.trimmed().isEmpty())
|
|
|
|
|
Core::MessageManager::write(QString::fromLocal8Bit(stdErr));
|
|
|
|
|
|
|
|
|
|
if (rccProcess.exitStatus() != QProcess::NormalExit) {
|
2020-01-23 16:58:10 +01:00
|
|
|
Core::MessageManager::write(QCoreApplication::translate("QmlDesigner::GenerateResource",
|
|
|
|
|
"\"%1\" crashed.").arg(rccBinary.toString() + arguments.join(" ")));
|
2019-08-27 20:10:36 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (rccProcess.exitCode() != 0) {
|
2020-01-23 16:58:10 +01:00
|
|
|
Core::MessageManager::write(QCoreApplication::translate("QmlDesigner::GenerateResource",
|
2019-08-27 20:10:36 +02:00
|
|
|
"\"%1\" failed (exit code %2).").arg(rccBinary.toString() +
|
2020-01-23 16:58:10 +01:00
|
|
|
" " + arguments.join(" ")).arg(rccProcess.exitCode()));
|
2019-08-27 20:10:36 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
saveLastUsedPath(Utils::FilePath::fromString(resourceFileName).parentDir().toString());
|
|
|
|
|
});
|
|
|
|
|
buildMenu->addAction(cmd, ProjectExplorer::Constants::G_BUILD_RUN);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace QmlDesigner
|
|
|
|
|
|