Core: Use Utils::CommandLine in ExecuteFilter

... using a new CommandLine::fromUserInput convenience function.

Change-Id: Ic786530af89ec80f4211e66f36caa22cb705effe
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
hjk
2021-08-20 10:05:13 +02:00
parent 20a63d6c7e
commit d119c717c1
4 changed files with 28 additions and 19 deletions

View File

@@ -26,6 +26,7 @@
#include "commandline.h" #include "commandline.h"
#include "environment.h" #include "environment.h"
#include "macroexpander.h"
#include "qtcassert.h" #include "qtcassert.h"
#include "stringutils.h" #include "stringutils.h"
@@ -1434,6 +1435,21 @@ CommandLine::CommandLine(const FilePath &exe, const QString &args, RawType)
addArgs(args, Raw); addArgs(args, Raw);
} }
CommandLine CommandLine::fromUserInput(const QString &cmdline, MacroExpander *expander)
{
CommandLine cmd;
const int pos = cmdline.indexOf(' ');
if (pos == -1) {
cmd.m_executable = FilePath::fromString(cmdline);
} else {
cmd.m_executable = FilePath::fromString(cmdline.left(pos));
cmd.m_arguments = cmdline.right(cmdline.length() - pos - 1);
if (expander)
cmd.m_arguments = expander->expand(cmd.m_arguments);
}
return cmd;
}
void CommandLine::addArg(const QString &arg) void CommandLine::addArg(const QString &arg)
{ {
ProcessArgs::addArg(&m_arguments, arg, m_executable.osType()); ProcessArgs::addArg(&m_arguments, arg, m_executable.osType());

View File

@@ -37,6 +37,7 @@ namespace Utils {
class AbstractMacroExpander; class AbstractMacroExpander;
class CommandLine; class CommandLine;
class Environment; class Environment;
class MacroExpander;
class QTCREATOR_UTILS_EXPORT ProcessArgs class QTCREATOR_UTILS_EXPORT ProcessArgs
{ {
@@ -138,6 +139,8 @@ public:
CommandLine(const FilePath &exe, const QStringList &args); CommandLine(const FilePath &exe, const QStringList &args);
CommandLine(const FilePath &exe, const QString &unparsedArgs, RawType); CommandLine(const FilePath &exe, const QString &unparsedArgs, RawType);
static CommandLine fromUserInput(const QString &cmdline, MacroExpander *expander = nullptr);
void addArg(const QString &arg); void addArg(const QString &arg);
void addArgs(const QStringList &inArgs); void addArgs(const QStringList &inArgs);

View File

@@ -102,15 +102,8 @@ void ExecuteFilter::accept(LocatorFilterEntry selection,
workingDirectory = Utils::globalMacroExpander()->value("CurrentDocument:Project:Path", &found); workingDirectory = Utils::globalMacroExpander()->value("CurrentDocument:Project:Path", &found);
ExecuteData d; ExecuteData d;
d.workingDirectory = workingDirectory; d.command = CommandLine::fromUserInput(value, Utils::globalMacroExpander());
const int pos = value.indexOf(' '); d.workingDirectory = FilePath::fromString(workingDirectory);
if (pos == -1) {
d.executable = value;
} else {
d.executable = value.left(pos);
d.arguments = Utils::globalMacroExpander()->expand(
value.right(value.length() - pos - 1));
}
if (m_process) { if (m_process) {
const QString info(tr("Previous command is still running (\"%1\").\nDo you want to kill it?") const QString info(tr("Previous command is still running (\"%1\").\nDo you want to kill it?")
@@ -166,10 +159,9 @@ void ExecuteFilter::runHeadCommand()
{ {
if (!m_taskQueue.isEmpty()) { if (!m_taskQueue.isEmpty()) {
const ExecuteData &d = m_taskQueue.head(); const ExecuteData &d = m_taskQueue.head();
const Utils::FilePath fullPath = Utils::Environment::systemEnvironment().searchInPath(d.executable); if (d.command.executable().isEmpty()) {
if (fullPath.isEmpty()) {
MessageManager::writeDisrupting( MessageManager::writeDisrupting(
tr("Could not find executable for \"%1\".").arg(d.executable)); tr("Could not find executable for \"%1\".").arg(d.command.executable().toUserOutput()));
m_taskQueue.dequeue(); m_taskQueue.dequeue();
runHeadCommand(); runHeadCommand();
return; return;
@@ -178,7 +170,7 @@ void ExecuteFilter::runHeadCommand()
QTC_CHECK(!m_process); QTC_CHECK(!m_process);
createProcess(); createProcess();
m_process->setWorkingDirectory(d.workingDirectory); m_process->setWorkingDirectory(d.workingDirectory);
m_process->setCommand({fullPath, d.arguments, Utils::CommandLine::Raw}); m_process->setCommand(d.command);
m_process->start(); m_process->start();
if (!m_process->waitForStarted(1000)) { if (!m_process->waitForStarted(1000)) {
MessageManager::writeFlashing( MessageManager::writeFlashing(
@@ -221,7 +213,5 @@ QString ExecuteFilter::headCommand() const
if (m_taskQueue.isEmpty()) if (m_taskQueue.isEmpty())
return QString(); return QString();
const ExecuteData &data = m_taskQueue.head(); const ExecuteData &data = m_taskQueue.head();
if (data.arguments.isEmpty()) return data.command.toUserOutput();
return data.executable;
return data.executable + ' ' + data.arguments;
} }

View File

@@ -39,11 +39,11 @@ namespace Internal {
class ExecuteFilter : public Core::ILocatorFilter class ExecuteFilter : public Core::ILocatorFilter
{ {
Q_OBJECT Q_OBJECT
struct ExecuteData struct ExecuteData
{ {
QString executable; Utils::CommandLine command;
QString arguments; Utils::FilePath workingDirectory;
QString workingDirectory;
}; };
public: public: