1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
|
/* This file is part of KDevelop
Copyright 2009 Andreas Pakulat <apaku@gmx.de>
Copyright 2009 Niko Sams <niko.sams@gmail.com>
This library 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 library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "scriptappjob.h"
#include "executescriptplugin.h"
#include <QFileInfo>
#include <KLocalizedString>
#include <KProcess>
#include <KSharedConfig>
#include <KShell>
#include <interfaces/ilaunchconfiguration.h>
#include <interfaces/iruntimecontroller.h>
#include <interfaces/iruntime.h>
#include <outputview/outputmodel.h>
#include <outputview/outputdelegate.h>
#include <util/processlinemaker.h>
#include <util/environmentprofilelist.h>
#include <interfaces/icore.h>
#include <interfaces/iplugincontroller.h>
#include <interfaces/idocumentcontroller.h>
#include <project/projectmodel.h>
#include <util/path.h>
#include "iexecutescriptplugin.h"
#include "debug.h"
using namespace KDevelop;
ScriptAppJob::ScriptAppJob(ExecuteScriptPlugin* parent, KDevelop::ILaunchConfiguration* cfg)
: KDevelop::OutputJob( parent ), proc(new KProcess( this )), lineMaker(new KDevelop::ProcessLineMaker( proc, this ))
{
qCDebug(PLUGIN_EXECUTESCRIPT) << "creating script app job";
setCapabilities(Killable);
IExecuteScriptPlugin* iface = KDevelop::ICore::self()->pluginController()->pluginForExtension(QStringLiteral("org.kdevelop.IExecuteScriptPlugin"))->extension<IExecuteScriptPlugin>();
Q_ASSERT(iface);
const KDevelop::EnvironmentProfileList environmentProfiles(KSharedConfig::openConfig());
QString envProfileName = iface->environmentProfileName(cfg);
QString err;
QString interpreterString = iface->interpreter( cfg, err );
// check for errors happens in the executescript plugin already
KShell::Errors err_;
QStringList interpreter = KShell::splitArgs( interpreterString, KShell::TildeExpand | KShell::AbortOnMeta, &err_ );
if ( interpreter.isEmpty() ) {
// This should not happen, because of the checks done in the executescript plugin
qCWarning(PLUGIN_EXECUTESCRIPT) << "no interpreter specified";
return;
}
if( !err.isEmpty() )
{
setError( -1 );
setErrorText( err );
return;
}
QUrl script;
if( !iface->runCurrentFile( cfg ) )
{
script = iface->script( cfg, err );
} else {
KDevelop::IDocument* document = KDevelop::ICore::self()->documentController()->activeDocument();
if( !document )
{
setError( -1 );
setErrorText( i18n( "There is no active document to launch." ) );
return;
}
script = ICore::self()->runtimeController()->currentRuntime()->pathInRuntime(KDevelop::Path(document->url())).toUrl();
}
if( !err.isEmpty() )
{
setError( -3 );
setErrorText( err );
return;
}
QString remoteHost = iface->remoteHost( cfg, err );
if( !err.isEmpty() )
{
setError( -4 );
setErrorText( err );
return;
}
if (envProfileName.isEmpty()) {
qCWarning(PLUGIN_EXECUTESCRIPT) << "Launch Configuration:" << cfg->name() << i18n("No environment profile specified, looks like a broken "
"configuration, please check run configuration '%1'. "
"Using default environment profile.", cfg->name() );
envProfileName = environmentProfiles.defaultProfileName();
}
QStringList arguments = iface->arguments( cfg, err );
if( !err.isEmpty() )
{
setError( -2 );
setErrorText( err );
}
if( error() != 0 )
{
qCWarning(PLUGIN_EXECUTESCRIPT) << "Launch Configuration:" << cfg->name() << "oops, problem" << errorText();
return;
}
KDevelop::OutputModel::OutputFilterStrategy currentFilterMode = static_cast<KDevelop::OutputModel::OutputFilterStrategy>( iface->outputFilterModeId( cfg ) );
setStandardToolView(KDevelop::IOutputView::RunView);
setBehaviours(KDevelop::IOutputView::AllowUserClose | KDevelop::IOutputView::AutoScroll);
KDevelop::OutputModel* m = new KDevelop::OutputModel;
m->setFilteringStrategy(currentFilterMode);
setModel( m );
setDelegate( new KDevelop::OutputDelegate );
connect( lineMaker, &ProcessLineMaker::receivedStdoutLines, model(), &OutputModel::appendLines );
connect( proc, static_cast<void(KProcess::*)(QProcess::ProcessError)>(&KProcess::error), this, &ScriptAppJob::processError );
connect( proc, static_cast<void(KProcess::*)(int,QProcess::ExitStatus)>(&KProcess::finished), this, &ScriptAppJob::processFinished );
// Now setup the process parameters
proc->setEnvironment(environmentProfiles.createEnvironment(envProfileName, proc->systemEnvironment()));
QUrl wc = iface->workingDirectory( cfg );
if( !wc.isValid() || wc.isEmpty() )
{
wc = QUrl::fromLocalFile( QFileInfo( script.toLocalFile() ).absolutePath() );
}
proc->setWorkingDirectory( ICore::self()->runtimeController()->currentRuntime()->pathInRuntime(KDevelop::Path(wc)).toLocalFile() );
proc->setProperty( "executable", interpreter.first() );
QStringList program;
if (!remoteHost.isEmpty()) {
program << QStringLiteral("ssh");
QStringList parts = remoteHost.split(QLatin1Char(':'));
program << parts.first();
if (parts.length() > 1) {
program << QLatin1String("-p ") + parts.at(1);
}
}
program << interpreter;
program << script.toLocalFile();
program << arguments;
qCDebug(PLUGIN_EXECUTESCRIPT) << "setting app:" << program;
proc->setOutputChannelMode(KProcess::MergedChannels);
proc->setProgram( program );
setTitle(cfg->name());
}
void ScriptAppJob::start()
{
qCDebug(PLUGIN_EXECUTESCRIPT) << "launching?" << proc;
if( proc )
{
startOutput();
appendLine( i18n("Starting: %1", proc->program().join(QLatin1Char( ' ' ) ) ) );
ICore::self()->runtimeController()->currentRuntime()->startProcess(proc);
} else
{
qCWarning(PLUGIN_EXECUTESCRIPT) << "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 ScriptAppJob::doKill()
{
if( proc ) {
proc->kill();
appendLine( i18n( "*** Killed Application ***" ) );
}
return true;
}
void ScriptAppJob::processFinished( int exitCode , QProcess::ExitStatus status )
{
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);
}
qCDebug(PLUGIN_EXECUTESCRIPT) << "Process done";
emitResult();
}
void ScriptAppJob::processError( QProcess::ProcessError error )
{
qCDebug(PLUGIN_EXECUTESCRIPT) << proc->readAllStandardError();
qCDebug(PLUGIN_EXECUTESCRIPT) << proc->readAllStandardOutput();
qCDebug(PLUGIN_EXECUTESCRIPT) << proc->errorString();
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(QLatin1Char( ' ' ) ) );
appendLine( errmsg );
setErrorText( errmsg );
emitResult();
}
qCDebug(PLUGIN_EXECUTESCRIPT) << "Process error";
}
void ScriptAppJob::appendLine(const QString& l)
{
if (KDevelop::OutputModel* m = model()) {
m->appendLine(l);
}
}
KDevelop::OutputModel* ScriptAppJob::model()
{
return dynamic_cast<KDevelop::OutputModel*>( OutputJob::model() );
}
|