diff --git a/outputview/outputexecutejob.cpp b/outputview/outputexecutejob.cpp --- a/outputview/outputexecutejob.cpp +++ b/outputview/outputexecutejob.cpp @@ -466,18 +466,23 @@ QProcessEnvironment OutputExecuteJobPrivate::effectiveEnvironment() const { - QProcessEnvironment environment = QProcessEnvironment::systemEnvironment(); const EnvironmentGroupList environmentGroup( KSharedConfig::openConfig() ); QString environmentProfile = m_owner->environmentProfile(); if( environmentProfile.isEmpty() ) { environmentProfile = environmentGroup.defaultGroup(); } - OutputExecuteJobPrivate::mergeEnvironment( environment, environmentGroup.variables( environmentProfile ) ); + QProcessEnvironment environment = QProcessEnvironment::systemEnvironment(); + auto userEnv = environmentGroup.variables(environmentProfile); + expandVariables(userEnv, environment); + + qWarning() << userEnv; + OutputExecuteJobPrivate::mergeEnvironment( environment, userEnv ); OutputExecuteJobPrivate::mergeEnvironment( environment, m_environmentOverrides ); if( m_properties.testFlag( OutputExecuteJob::PortableMessages ) ) { environment.remove( "LC_ALL" ); environment.insert( "LC_MESSAGES", "C" ); } + qWarning() << "2" << environment.toStringList(); return environment; } diff --git a/plugins/execute/nativeappjob.h b/plugins/execute/nativeappjob.h --- a/plugins/execute/nativeappjob.h +++ b/plugins/execute/nativeappjob.h @@ -20,36 +20,27 @@ #ifndef KDEVPLATFORM_PLUGIN_NATIVEAPPJOB_H #define KDEVPLATFORM_PLUGIN_NATIVEAPPJOB_H +#include + #include -#include namespace KDevelop { class ILaunchConfiguration; -class ProcessLineMaker; -class OutputModel; } class KProcess; -class NativeAppJob : public KDevelop::OutputJob +class NativeAppJob : public KDevelop::OutputExecuteJob { -Q_OBJECT + Q_OBJECT + public: NativeAppJob( QObject* parent, KDevelop::ILaunchConfiguration* cfg ); + void start() override; - bool doKill() override; - KDevelop::OutputModel* model(); -private slots: - void processError(QProcess::ProcessError); - void processFinished(int,QProcess::ExitStatus); - void outputDone(); private: - void appendLine(const QString &l); - - KProcess* proc; - KDevelop::ProcessLineMaker* lineMaker; QString m_cfgname; }; diff --git a/plugins/execute/nativeappjob.cpp b/plugins/execute/nativeappjob.cpp --- a/plugins/execute/nativeappjob.cpp +++ b/plugins/execute/nativeappjob.cpp @@ -24,20 +24,15 @@ #include #include -#include #include #include -#include #include #include #include -#include -#include #include #include -#include #include #include @@ -47,7 +42,7 @@ using namespace KDevelop; NativeAppJob::NativeAppJob(QObject* parent, KDevelop::ILaunchConfiguration* cfg) - : KDevelop::OutputJob( parent ), proc(0) + : KDevelop::OutputExecuteJob( parent ) , m_cfgname(cfg->name()) { setCapabilities(Killable); @@ -75,6 +70,7 @@ "Using default environment group.", cfg->name() ); envgrp = l.defaultGroup(); } + setEnvironmentProfile(envgrp); QStringList arguments = iface->arguments( cfg, err ); if( !err.isEmpty() ) @@ -89,36 +85,21 @@ return; } - proc = new KProcess( this ); - - lineMaker = new KDevelop::ProcessLineMaker( proc, this ); - setStandardToolView(KDevelop::IOutputView::RunView); setBehaviours(KDevelop::IOutputView::AllowUserClose | KDevelop::IOutputView::AutoScroll); - OutputModel* m = new OutputModel; - m->setFilteringStrategy(OutputModel::NativeAppErrorFilter); - setModel(m); - setDelegate( new KDevelop::OutputDelegate ); - - connect( lineMaker, &ProcessLineMaker::receivedStdoutLines, model(), &OutputModel::appendLines ); - connect( proc, static_cast(&KProcess::error), this, &NativeAppJob::processError ); - connect( proc, static_cast(&KProcess::finished), this, &NativeAppJob::processFinished ); + setFilteringStrategy(OutputModel::NativeAppErrorFilter); + setProperties(DisplayStdout | DisplayStderr); // Now setup the process parameters - proc->setEnvironment( l.createEnvironment( envgrp, proc->systemEnvironment()) ); QUrl wc = iface->workingDirectory( cfg ); - if( !wc.isValid() || wc.isEmpty() ) - { + if( !wc.isValid() || wc.isEmpty() ) { wc = QUrl::fromLocalFile( QFileInfo( executable.toLocalFile() ).absolutePath() ); } - proc->setWorkingDirectory( wc.toLocalFile() ); - proc->setProperty( "executable", executable ); + setWorkingDirectory( wc ); qCDebug(PLUGIN_EXECUTE) << "setting app:" << executable << arguments; - proc->setOutputChannelMode(KProcess::MergedChannels); - if (iface->useTerminal(cfg)) { QStringList args = KShell::splitArgs(iface->terminal(cfg)); for (QStringList::iterator it = args.begin(); it != args.end(); ++it) { @@ -129,12 +110,13 @@ } } args.append( arguments ); - proc->setProgram( args ); + *this << args; } else { - proc->setProgram( executable.toLocalFile(), arguments ); + *this << executable.toLocalFile(); + *this << arguments; } - setObjectName(cfg->name()); + setJobName(cfg->name()); } NativeAppJob* findNativeJob(KJob* j) @@ -160,84 +142,5 @@ } } - qCDebug(PLUGIN_EXECUTE) << "launching?" << proc; - if( proc ) - { - startOutput(); - appendLine( i18n("Starting: %1", proc->program().join(" ") ) ); - proc->start(); - } else - { - qWarning() << "No process, something went wrong when creating the job"; - // No process means we've returned early on from the constructor, some bad error happened - emitResult(); - } -} - -bool NativeAppJob::doKill() -{ - if( proc ) { - proc->kill(); - appendLine( i18n( "*** Killed Application ***" ) ); - } - return true; -} - -void NativeAppJob::processFinished( int exitCode , QProcess::ExitStatus status ) -{ - if (!model()) { - outputDone(); - return; - } - - connect(model(), &OutputModel::allDone, this, &NativeAppJob::outputDone); - lineMaker->flushBuffers(); - - if (exitCode == 0 && status == QProcess::NormalExit) { - appendLine( i18n("*** Exited normally ***") ); - } else if (status == QProcess::NormalExit) { - appendLine( i18n("*** Exited with return code: %1 ***", QString::number(exitCode)) ); - setError(OutputJob::FailedShownError); - } else if (error() == KJob::KilledJobError) { - appendLine( i18n("*** Process aborted ***") ); - setError(KJob::KilledJobError); - } else { - appendLine( i18n("*** Crashed with return code: %1 ***", QString::number(exitCode)) ); - setError(OutputJob::FailedShownError); - } - - model()->ensureAllDone(); -} - -void NativeAppJob::outputDone() -{ - emitResult(); -} - -void NativeAppJob::processError( QProcess::ProcessError error ) -{ - if( error == QProcess::FailedToStart ) - { - setError( FailedShownError ); - QString errmsg = i18n("*** Could not start program '%1'. Make sure that the " - "path is specified correctly ***", proc->program().join(" ") ); - appendLine( errmsg ); - setErrorText( errmsg ); - emitResult(); - } - qCDebug(PLUGIN_EXECUTE) << "Process error"; + OutputExecuteJob::start(); } - -void NativeAppJob::appendLine(const QString& l) -{ - if (KDevelop::OutputModel* m = model()) { - m->appendLine(l); - } -} - -KDevelop::OutputModel* NativeAppJob::model() -{ - return dynamic_cast( OutputJob::model() ); -} - - diff --git a/util/environmentgrouplist.h b/util/environmentgrouplist.h --- a/util/environmentgrouplist.h +++ b/util/environmentgrouplist.h @@ -24,6 +24,7 @@ #include +class QProcessEnvironment; class KConfig; template class QMap; class QString; @@ -140,6 +141,8 @@ }; +KDEVPLATFORMUTIL_EXPORT void expandVariables(QMap& variables, const QProcessEnvironment& environment); + } #endif diff --git a/util/environmentgrouplist.cpp b/util/environmentgrouplist.cpp --- a/util/environmentgrouplist.cpp +++ b/util/environmentgrouplist.cpp @@ -25,6 +25,8 @@ #include +#include +#include namespace KDevelop { class EnvironmentGroupListPrivate @@ -201,3 +203,21 @@ return env; } + +void KDevelop::expandVariables(QMap& variables, const QProcessEnvironment& environment) +{ + QRegularExpression rVar("(? + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +#ifndef TESTENVIRONMENT_H +#define TESTENVIRONMENT_H + +#include + +class TestEnvironment : public QObject +{ + Q_OBJECT + +private Q_SLOTS: + void testExpandVariables_data(); + void testExpandVariables(); +}; + +#endif // TESTENVIRONMENT_H diff --git a/util/tests/test_environment.cpp b/util/tests/test_environment.cpp new file mode 100644 --- /dev/null +++ b/util/tests/test_environment.cpp @@ -0,0 +1,82 @@ +/* + * This file is part of KDevelop + * + * Copyright 2015 Artur Puzio + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "test_environment.h" + +#include "util/environmentgrouplist.h" + +#include + +QTEST_MAIN(TestEnvironment); + +using ProcEnv = QMap; + +void TestEnvironment::testExpandVariables_data() +{ + QTest::addColumn("env"); + QTest::addColumn("expectedEnv"); + + QTest::newRow("no variables") << ProcEnv({}) << ProcEnv({}); + QTest::newRow("simple variables") << ProcEnv{ + {"VAR1","data"}, + {"Var2","some other data"} + } << ProcEnv({ + {"VAR1","data"}, + {"Var2","some other data"} + }); + QTest::newRow("PATH append and prepend") << ProcEnv({ + {"PATH","/home/usr/bin:$PATH:/home/user/folder"} + }) << ProcEnv({ + {"PATH", "/home/usr/bin:/bin:/usr/bin:/home/user/folder"} + }); + QTest::newRow("\\$VAR") << ProcEnv({ + {"MY_VAR","\\$PATH something \\$HOME"} + }) << ProcEnv({ + {"MY_VAR","$PATH something $HOME"} + }); + QTest::newRow("spaces, \\$VAR after $VAR") << ProcEnv({ + {"MY_VAR","$PATH:$HOME something \\$HOME"} + }) << ProcEnv({ + {"MY_VAR","/bin:/usr/bin:/home/tom something $HOME"} + }); + QTest::newRow("VAR2=$VAR1") << ProcEnv({ + {"VAR1","/some/path"},{"VAR2","$VAR1"} + }) << ProcEnv({ + {"VAR1","/some/path"},{"VAR2",""} + }); +} + +void TestEnvironment::testExpandVariables() +{ + QFETCH(ProcEnv, env); + QFETCH(ProcEnv, expectedEnv); + + QProcessEnvironment environment; + environment.insert("PATH","/bin:/usr/bin"); + environment.insert("HOME","/home/tom"); + + KDevelop::expandVariables(env, environment); + + qDebug() << env; + for (auto it = expectedEnv.cbegin(); it!= expectedEnv.cend(); ++it) { + QCOMPARE(env.value(it.key()), it.value()); + } +}