Nim: Rework NimCompilerBuildStep

Use new ProjectExplorer infrastructure.

Change-Id: Id8fed337895f9f18fec44f4756493f40ea07f3da
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
hjk
2020-08-31 09:56:10 +02:00
parent fc1fc6a07a
commit 5b8846a4f8
5 changed files with 121 additions and 303 deletions

View File

@@ -65,10 +65,6 @@ const QString C_NIMCOMPILERBUILDSTEP_USERCOMPILEROPTIONS = QStringLiteral("Nim.N
const QString C_NIMCOMPILERBUILDSTEP_DEFAULTBUILDOPTIONS = QStringLiteral("Nim.NimCompilerBuildStep.DefaultBuildOptions");
const QString C_NIMCOMPILERBUILDSTEP_TARGETNIMFILE = QStringLiteral("Nim.NimCompilerBuildStep.TargetNimFile");
// NimCompilerBuildStepWidget
const char C_NIMCOMPILERBUILDSTEPWIDGET_DISPLAY[] = QT_TRANSLATE_NOOP("NimCompilerBuildStepConfigWidget", "Nim build step");
const char C_NIMCOMPILERBUILDSTEPWIDGET_SUMMARY[] = QT_TRANSLATE_NOOP("NimCompilerBuildStepConfigWidget", "Nim build step");
// NimCompilerCleanStep
const char C_NIMCOMPILERCLEANSTEP_ID[] = "Nim.NimCompilerCleanStep";

View File

@@ -84,26 +84,7 @@ NimBuildConfiguration::NimBuildConfiguration(Target *target, Utils::Id id)
auto nimCompilerBuildStep = buildSteps()->firstOfType<NimCompilerBuildStep>();
QTC_ASSERT(nimCompilerBuildStep, return);
NimCompilerBuildStep::DefaultBuildOptions defaultOption;
switch (info.buildType) {
case BuildConfiguration::Release:
defaultOption = NimCompilerBuildStep::DefaultBuildOptions::Release;
break;
case BuildConfiguration::Debug:
defaultOption = NimCompilerBuildStep::DefaultBuildOptions::Debug;
break;
default:
defaultOption = NimCompilerBuildStep::DefaultBuildOptions::Empty;
break;
}
nimCompilerBuildStep->setDefaultCompilerOptions(defaultOption);
const Utils::FilePaths nimFiles = project()->files([](const Node *n) {
return Project::AllFiles(n) && n->path().endsWith(".nim");
});
if (!nimFiles.isEmpty())
nimCompilerBuildStep->setTargetNimFile(nimFiles.first());
nimCompilerBuildStep->setBuildType(info.buildType);
});
}
@@ -115,19 +96,12 @@ FilePath NimBuildConfiguration::cacheDirectory() const
FilePath NimBuildConfiguration::outFilePath() const
{
const NimCompilerBuildStep *step = nimCompilerBuildStep();
QTC_ASSERT(step, return FilePath());
return step->outFilePath();
}
const NimCompilerBuildStep *NimBuildConfiguration::nimCompilerBuildStep() const
{
foreach (BuildStep *step, buildSteps()->steps())
if (step->id() == Constants::C_NIMCOMPILERBUILDSTEP_ID)
return qobject_cast<NimCompilerBuildStep *>(step);
return nullptr;
auto nimCompilerBuildStep = buildSteps()->firstOfType<NimCompilerBuildStep>();
QTC_ASSERT(nimCompilerBuildStep, return {});
return nimCompilerBuildStep->outFilePath();
}
// NimBuildConfigurationFactory
NimBuildConfigurationFactory::NimBuildConfigurationFactory()
{

View File

@@ -30,8 +30,6 @@
namespace Nim {
class NimCompilerBuildStep;
class NimBuildConfiguration : public ProjectExplorer::BuildConfiguration
{
Q_OBJECT
@@ -42,13 +40,6 @@ class NimBuildConfiguration : public ProjectExplorer::BuildConfiguration
public:
Utils::FilePath cacheDirectory() const;
Utils::FilePath outFilePath() const;
signals:
void outFilePathChanged(const Utils::FilePath &outFilePath);
private:
void setupBuild(const ProjectExplorer::BuildInfo *info);
const NimCompilerBuildStep *nimCompilerBuildStep() const;
};

View File

@@ -39,9 +39,7 @@
#include <utils/qtcassert.h>
#include <utils/qtcprocess.h>
#include <QApplication>
#include <QComboBox>
#include <QDir>
#include <QFormLayout>
#include <QLabel>
#include <QLineEdit>
@@ -53,31 +51,6 @@ using namespace Utils;
namespace Nim {
class NimCompilerBuildStepConfigWidget : public ProjectExplorer::BuildStepConfigWidget
{
Q_DECLARE_TR_FUNCTIONS(Nim::NimCompilerBuildStep)
public:
explicit NimCompilerBuildStepConfigWidget(NimCompilerBuildStep *buildStep);
private:
void updateUi();
void updateCommandLineText();
void updateTargetComboBox();
void updateAdditionalArgumentsLineEdit();
void updateDefaultArgumentsComboBox();
void onAdditionalArgumentsTextEdited(const QString &text);
void onTargetChanged(int index);
void onDefaultArgumentsComboBoxIndexChanged(int index);
NimCompilerBuildStep *m_buildStep;
QTextEdit *m_commandTextEdit;
QComboBox *m_defaultArgumentsComboBox;
QComboBox *m_targetComboBox;
QLineEdit *m_additionalArgumentsLineEdit;
};
// NimParser
class NimParser : public ProjectExplorer::OutputTaskParser
@@ -122,77 +95,93 @@ NimCompilerBuildStep::NimCompilerBuildStep(BuildStepList *parentList, Utils::Id
{
setDefaultDisplayName(tr(Constants::C_NIMCOMPILERBUILDSTEP_DISPLAY));
setDisplayName(tr(Constants::C_NIMCOMPILERBUILDSTEP_DISPLAY));
setCommandLineProvider([this] { return commandLine(); });
auto bc = qobject_cast<NimBuildConfiguration *>(buildConfiguration());
connect(bc, &NimBuildConfiguration::buildDirectoryChanged,
this, &NimCompilerBuildStep::updateProcessParameters);
connect(bc, &BuildConfiguration::environmentChanged,
this, &NimCompilerBuildStep::updateProcessParameters);
connect(this, &NimCompilerBuildStep::outFilePathChanged,
bc, &NimBuildConfiguration::outFilePathChanged);
connect(project(), &ProjectExplorer::Project::fileListChanged,
this, &NimCompilerBuildStep::updateTargetNimFile);
updateProcessParameters();
}
void NimCompilerBuildStep::setupOutputFormatter(OutputFormatter *formatter)
{
formatter->addLineParser(new NimParser);
formatter->addLineParsers(target()->kit()->createOutputParsers());
formatter->addSearchDir(processParameters()->effectiveWorkingDirectory());
formatter->addSearchDir(buildDirectory());
AbstractProcessStep::setupOutputFormatter(formatter);
}
NimCompilerBuildStepConfigWidget::NimCompilerBuildStepConfigWidget(NimCompilerBuildStep *buildStep)
: BuildStepConfigWidget(buildStep)
, m_buildStep(buildStep)
{
setDisplayName(tr(Constants::C_NIMCOMPILERBUILDSTEPWIDGET_DISPLAY));
setSummaryText(tr(Constants::C_NIMCOMPILERBUILDSTEPWIDGET_SUMMARY));
m_targetComboBox = new QComboBox(this);
m_additionalArgumentsLineEdit = new QLineEdit(this);
m_commandTextEdit = new QTextEdit(this);
m_commandTextEdit->setEnabled(false);
m_commandTextEdit->setMinimumSize(QSize(0, 0));
m_defaultArgumentsComboBox = new QComboBox(this);
m_defaultArgumentsComboBox->addItem(tr("None"));
m_defaultArgumentsComboBox->addItem(tr("Debug"));
m_defaultArgumentsComboBox->addItem(tr("Release"));
auto formLayout = new QFormLayout(this);
formLayout->addRow(tr("Target:"), m_targetComboBox);
formLayout->addRow(tr("Default arguments:"), m_defaultArgumentsComboBox);
formLayout->addRow(tr("Extra arguments:"), m_additionalArgumentsLineEdit);
formLayout->addRow(tr("Command:"), m_commandTextEdit);
// Connect the project signals
connect(m_buildStep->project(),
&Project::fileListChanged,
this,
&NimCompilerBuildStepConfigWidget::updateUi);
// Connect build step signals
connect(m_buildStep, &NimCompilerBuildStep::processParametersChanged,
this, &NimCompilerBuildStepConfigWidget::updateUi);
// Connect UI signals
connect(m_targetComboBox, QOverload<int>::of(&QComboBox::activated),
this, &NimCompilerBuildStepConfigWidget::onTargetChanged);
connect(m_additionalArgumentsLineEdit, &QLineEdit::textEdited,
this, &NimCompilerBuildStepConfigWidget::onAdditionalArgumentsTextEdited);
connect(m_defaultArgumentsComboBox, QOverload<int>::of(&QComboBox::activated),
this, &NimCompilerBuildStepConfigWidget::onDefaultArgumentsComboBoxIndexChanged);
updateUi();
}
BuildStepConfigWidget *NimCompilerBuildStep::createConfigWidget()
{
return new NimCompilerBuildStepConfigWidget(this);
auto widget = new BuildStepConfigWidget(this);
widget->setDisplayName(tr("Nim build step"));
widget->setSummaryText(tr("Nim build step"));
auto targetComboBox = new QComboBox(widget);
auto additionalArgumentsLineEdit = new QLineEdit(widget);
auto commandTextEdit = new QTextEdit(widget);
commandTextEdit->setEnabled(false);
commandTextEdit->setMinimumSize(QSize(0, 0));
auto defaultArgumentsComboBox = new QComboBox(widget);
defaultArgumentsComboBox->addItem(tr("None"));
defaultArgumentsComboBox->addItem(tr("Debug"));
defaultArgumentsComboBox->addItem(tr("Release"));
auto formLayout = new QFormLayout(widget);
formLayout->addRow(tr("Target:"), targetComboBox);
formLayout->addRow(tr("Default arguments:"), defaultArgumentsComboBox);
formLayout->addRow(tr("Extra arguments:"), additionalArgumentsLineEdit);
formLayout->addRow(tr("Command:"), commandTextEdit);
auto updateUi = [=] {
const CommandLine cmd = commandLine();
const QStringList parts = QtcProcess::splitArgs(cmd.toUserOutput());
commandTextEdit->setText(parts.join(QChar::LineFeed));
// Re enter the files
targetComboBox->clear();
const FilePaths files = project()->files(Project::AllFiles);
for (const FilePath &file : files) {
if (file.endsWith(".nim"))
targetComboBox->addItem(file.fileName(), file.toString());
}
const int index = targetComboBox->findData(m_targetNimFile.toString());
targetComboBox->setCurrentIndex(index);
const QString text = m_userCompilerOptions.join(QChar::Space);
additionalArgumentsLineEdit->setText(text);
defaultArgumentsComboBox->setCurrentIndex(m_defaultOptions);
};
connect(project(), &Project::fileListChanged, this, updateUi);
connect(targetComboBox, QOverload<int>::of(&QComboBox::activated),
this, [this, targetComboBox, updateUi] {
const QVariant data = targetComboBox->currentData();
m_targetNimFile = FilePath::fromString(data.toString());
updateUi();
});
connect(additionalArgumentsLineEdit, &QLineEdit::textEdited,
this, [this, updateUi](const QString &text) {
m_userCompilerOptions = text.split(QChar::Space);
updateUi();
});
connect(defaultArgumentsComboBox, QOverload<int>::of(&QComboBox::activated),
this, [this, updateUi](int index) {
m_defaultOptions = static_cast<DefaultBuildOptions>(index);
updateUi();
});
updateUi();
return widget;
}
bool NimCompilerBuildStep::fromMap(const QVariantMap &map)
@@ -201,7 +190,6 @@ bool NimCompilerBuildStep::fromMap(const QVariantMap &map)
m_userCompilerOptions = map[Constants::C_NIMCOMPILERBUILDSTEP_USERCOMPILEROPTIONS].toString().split('|');
m_defaultOptions = static_cast<DefaultBuildOptions>(map[Constants::C_NIMCOMPILERBUILDSTEP_DEFAULTBUILDOPTIONS].toInt());
m_targetNimFile = FilePath::fromString(map[Constants::C_NIMCOMPILERBUILDSTEP_TARGETNIMFILE].toString());
updateProcessParameters();
return true;
}
@@ -214,89 +202,41 @@ QVariantMap NimCompilerBuildStep::toMap() const
return result;
}
QStringList NimCompilerBuildStep::userCompilerOptions() const
bool NimCompilerBuildStep::init()
{
return m_userCompilerOptions;
if (!AbstractProcessStep::init())
return false;
setupProcessParameters(processParameters());
return true;
}
void NimCompilerBuildStep::setUserCompilerOptions(const QStringList &options)
void NimCompilerBuildStep::setBuildType(BuildConfiguration::BuildType buildType)
{
m_userCompilerOptions = options;
emit userCompilerOptionsChanged(options);
updateProcessParameters();
switch (buildType) {
case BuildConfiguration::Release:
m_defaultOptions = DefaultBuildOptions::Release;
break;
case BuildConfiguration::Debug:
m_defaultOptions = DefaultBuildOptions::Debug;
break;
default:
m_defaultOptions = DefaultBuildOptions::Empty;
break;
}
updateTargetNimFile();
}
NimCompilerBuildStep::DefaultBuildOptions NimCompilerBuildStep::defaultCompilerOptions() const
{
return m_defaultOptions;
}
void NimCompilerBuildStep::setDefaultCompilerOptions(NimCompilerBuildStep::DefaultBuildOptions options)
{
if (m_defaultOptions == options)
return;
m_defaultOptions = options;
emit defaultCompilerOptionsChanged(options);
updateProcessParameters();
}
FilePath NimCompilerBuildStep::targetNimFile() const
{
return m_targetNimFile;
}
void NimCompilerBuildStep::setTargetNimFile(const FilePath &targetNimFile)
{
if (targetNimFile == m_targetNimFile)
return;
m_targetNimFile = targetNimFile;
emit targetNimFileChanged(targetNimFile);
updateProcessParameters();
}
FilePath NimCompilerBuildStep::outFilePath() const
{
return m_outFilePath;
}
void NimCompilerBuildStep::setOutFilePath(const FilePath &outFilePath)
{
if (outFilePath == m_outFilePath)
return;
m_outFilePath = outFilePath;
emit outFilePathChanged(outFilePath);
}
void NimCompilerBuildStep::updateProcessParameters()
{
updateOutFilePath();
updateCommand();
updateWorkingDirectory();
updateEnvironment();
emit processParametersChanged();
}
void NimCompilerBuildStep::updateOutFilePath()
{
const QString targetName = Utils::HostOsInfo::withExecutableSuffix(m_targetNimFile.toFileInfo().baseName());
setOutFilePath(buildDirectory().pathAppended(targetName));
}
void NimCompilerBuildStep::updateWorkingDirectory()
{
processParameters()->setWorkingDirectory(buildDirectory());
}
void NimCompilerBuildStep::updateCommand()
CommandLine NimCompilerBuildStep::commandLine()
{
auto bc = qobject_cast<NimBuildConfiguration *>(buildConfiguration());
QTC_ASSERT(bc, return);
QTC_ASSERT(bc, return {});
QTC_ASSERT(target(), return);
QTC_ASSERT(target()->kit(), return);
Kit *kit = target()->kit();
auto tc = dynamic_cast<NimToolChain*>(ToolChainKitAspect::toolChain(kit, Constants::C_NIMLANGUAGE_ID));
QTC_ASSERT(tc, return);
auto tc = ToolChainKitAspect::toolChain(kit, Constants::C_NIMLANGUAGE_ID);
QTC_ASSERT(tc, return {});
CommandLine cmd{tc->compilerCommand()};
@@ -307,7 +247,7 @@ void NimCompilerBuildStep::updateCommand()
else if (m_defaultOptions == Debug)
cmd.addArgs({"--debugInfo", "--lineDir:on"});
cmd.addArg("--out:" + m_outFilePath.toString());
cmd.addArg("--out:" + outFilePath().toString());
cmd.addArg("--nimCache:" + bc->cacheDirectory().toString());
for (const QString &arg : m_userCompilerOptions) {
@@ -318,90 +258,27 @@ void NimCompilerBuildStep::updateCommand()
if (!m_targetNimFile.isEmpty())
cmd.addArg(m_targetNimFile.toString());
processParameters()->setCommandLine(cmd);
return cmd;
}
void NimCompilerBuildStep::updateEnvironment()
FilePath NimCompilerBuildStep::outFilePath() const
{
processParameters()->setEnvironment(buildEnvironment());
const QString targetName = m_targetNimFile.toFileInfo().baseName();
return buildDirectory().pathAppended(HostOsInfo::withExecutableSuffix(targetName));
}
void NimCompilerBuildStep::updateTargetNimFile()
{
if (!m_targetNimFile.isEmpty())
return;
const Utils::FilePaths nimFiles = project()->files([](const Node *n) {
return Project::AllFiles(n) && n->path().endsWith(".nim");
});
if (!nimFiles.isEmpty())
setTargetNimFile(nimFiles.at(0));
}
void NimCompilerBuildStepConfigWidget::onTargetChanged(int index)
{
Q_UNUSED(index)
auto data = m_targetComboBox->currentData();
FilePath path = FilePath::fromString(data.toString());
m_buildStep->setTargetNimFile(path);
}
void NimCompilerBuildStepConfigWidget::onDefaultArgumentsComboBoxIndexChanged(int index)
{
auto options = static_cast<NimCompilerBuildStep::DefaultBuildOptions>(index);
m_buildStep->setDefaultCompilerOptions(options);
}
void NimCompilerBuildStepConfigWidget::updateUi()
{
updateCommandLineText();
updateTargetComboBox();
updateAdditionalArgumentsLineEdit();
updateDefaultArgumentsComboBox();
}
void NimCompilerBuildStepConfigWidget::onAdditionalArgumentsTextEdited(const QString &text)
{
m_buildStep->setUserCompilerOptions(text.split(QChar::Space));
}
void NimCompilerBuildStepConfigWidget::updateCommandLineText()
{
ProcessParameters *parameters = m_buildStep->processParameters();
const CommandLine cmd = parameters->command();
const QStringList parts = QtcProcess::splitArgs(cmd.toUserOutput());
m_commandTextEdit->setText(parts.join(QChar::LineFeed));
}
void NimCompilerBuildStepConfigWidget::updateTargetComboBox()
{
QTC_ASSERT(m_buildStep, return );
// Re enter the files
m_targetComboBox->clear();
const FilePaths nimFiles = m_buildStep->project()->files([](const Node *n) {
return Project::AllFiles(n) && n->path().endsWith(".nim");
});
for (const FilePath &file : nimFiles)
m_targetComboBox->addItem(file.fileName(), file.toString());
const int index = m_targetComboBox->findData(m_buildStep->targetNimFile().toString());
m_targetComboBox->setCurrentIndex(index);
}
void NimCompilerBuildStepConfigWidget::updateAdditionalArgumentsLineEdit()
{
const QString text = m_buildStep->userCompilerOptions().join(QChar::Space);
m_additionalArgumentsLineEdit->setText(text);
}
void NimCompilerBuildStepConfigWidget::updateDefaultArgumentsComboBox()
{
const int index = m_buildStep->defaultCompilerOptions();
m_defaultArgumentsComboBox->setCurrentIndex(index);
const FilePaths files = project()->files(Project::AllFiles);
for (const FilePath &file : files) {
if (file.endsWith(".nim")) {
m_targetNimFile = file;
break;
}
}
}
// NimCompilerBuildStepFactory

View File

@@ -41,40 +41,20 @@ public:
NimCompilerBuildStep(ProjectExplorer::BuildStepList *parentList, Utils::Id id);
void setBuildType(ProjectExplorer::BuildConfiguration::BuildType buildType);
Utils::FilePath outFilePath() const;
private:
void setupOutputFormatter(Utils::OutputFormatter *formatter) override;
ProjectExplorer::BuildStepConfigWidget *createConfigWidget() override;
bool fromMap(const QVariantMap &map) override;
QVariantMap toMap() const override;
QStringList userCompilerOptions() const;
void setUserCompilerOptions(const QStringList &options);
DefaultBuildOptions defaultCompilerOptions() const;
void setDefaultCompilerOptions(DefaultBuildOptions options);
Utils::FilePath targetNimFile() const;
void setTargetNimFile(const Utils::FilePath &targetNimFile);
Utils::FilePath outFilePath() const;
signals:
void userCompilerOptionsChanged(const QStringList &options);
void defaultCompilerOptionsChanged(DefaultBuildOptions options);
void targetNimFileChanged(const Utils::FilePath &targetNimFile);
void processParametersChanged();
void outFilePathChanged(const Utils::FilePath &outFilePath);
private:
void setOutFilePath(const Utils::FilePath &outFilePath);
void updateOutFilePath();
void updateProcessParameters();
void updateCommand();
void updateWorkingDirectory();
void updateEnvironment();
bool init() override;
void updateTargetNimFile();
Utils::CommandLine commandLine();
DefaultBuildOptions m_defaultOptions;
QStringList m_userCompilerOptions;