File indexing completed on 2024-04-28 04:38:40

0001 /*
0002     SPDX-FileCopyrightText: 2007 Hamish Rodda <rodda@kde.org>
0003     SPDX-FileCopyrightText: 2009 Niko Sams <niko.sams@gmail.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "executescriptplugin.h"
0009 
0010 #include <KConfigGroup>
0011 #include <KLocalizedString>
0012 #include <KPluginFactory>
0013 #include <KShell>
0014 
0015 #include <interfaces/icore.h>
0016 #include <interfaces/iruncontroller.h>
0017 #include <interfaces/ilaunchconfiguration.h>
0018 #include "scriptappconfig.h"
0019 #include "debug.h"
0020 #include <project/projectmodel.h>
0021 #include <util/kdevstringhandler.h>
0022 
0023 using namespace KDevelop;
0024 
0025 K_PLUGIN_FACTORY_WITH_JSON(KDevExecuteFactory, "kdevexecutescript.json", registerPlugin<ExecuteScriptPlugin>();)
0026 
0027 ExecuteScriptPlugin::ExecuteScriptPlugin(QObject *parent, const QVariantList&)
0028     : KDevelop::IPlugin(QStringLiteral("kdevexecutescript"), parent)
0029 {
0030     m_configType = new ScriptAppConfigType();
0031     m_configType->addLauncher( new ScriptAppLauncher( this ) );
0032     qCDebug(PLUGIN_EXECUTESCRIPT) << "adding script launch config";
0033     core()->runController()->addConfigurationType( m_configType );
0034 }
0035 
0036 ExecuteScriptPlugin::~ExecuteScriptPlugin()
0037 {
0038 }
0039 
0040 
0041 void ExecuteScriptPlugin::unload()
0042 {
0043     core()->runController()->removeConfigurationType( m_configType );
0044     delete m_configType;
0045     m_configType = nullptr;
0046 }
0047 
0048 QUrl ExecuteScriptPlugin::script( KDevelop::ILaunchConfiguration* cfg, QString& err_ ) const
0049 {
0050     QUrl script;
0051 
0052     if( !cfg )
0053     {
0054         return script;
0055     }
0056     KConfigGroup grp = cfg->config();
0057 
0058     script = grp.readEntry( ExecuteScriptPlugin::executableEntry, QUrl() );
0059     if( !script.isLocalFile() || script.isEmpty() )
0060     {
0061         err_ = i18n("No valid executable specified");
0062         qCWarning(PLUGIN_EXECUTESCRIPT) << "Launch Configuration:" << cfg->name() << "no valid script set";
0063     } else
0064     {
0065         KShell::Errors err;
0066         if( KShell::splitArgs( script.toLocalFile(), KShell::TildeExpand | KShell::AbortOnMeta, &err ).isEmpty() || err != KShell::NoError )
0067         {
0068             script = QUrl();
0069             if( err == KShell::BadQuoting )
0070             {
0071                 err_ = i18n("There is a quoting error in the script "
0072                 "for the launch configuration '%1'. "
0073                 "Aborting start.", cfg->name() );
0074             } else
0075             {
0076                 err_ = i18n("A shell meta character was included in the "
0077                 "script for the launch configuration '%1', "
0078                 "this is not supported currently. Aborting start.", cfg->name() );
0079             }
0080             qCWarning(PLUGIN_EXECUTESCRIPT) << "Launch Configuration:" << cfg->name() << "script has meta characters";
0081         }
0082     }
0083     return script;
0084 }
0085 
0086 QString ExecuteScriptPlugin::remoteHost(ILaunchConfiguration* cfg, QString& err) const
0087 {
0088     if (!cfg) return QString();
0089     KConfigGroup grp = cfg->config();
0090     if(grp.readEntry(ExecuteScriptPlugin::executeOnRemoteHostEntry, false)) {
0091         QString host = grp.readEntry(ExecuteScriptPlugin::remoteHostEntry, "");
0092         if (host.isEmpty()) {
0093             err = i18n("No remote host set for launch configuration '%1'. "
0094             "Aborting start.", cfg->name() );
0095             qCWarning(PLUGIN_EXECUTESCRIPT) << "Launch Configuration:" << cfg->name() << "no remote host set";
0096         }
0097         return host;
0098     }
0099     return QString();
0100 }
0101 
0102 QStringList ExecuteScriptPlugin::arguments( KDevelop::ILaunchConfiguration* cfg, QString& err_ ) const
0103 {
0104     if( !cfg )
0105     {
0106         return QStringList();
0107     }
0108 
0109     KShell::Errors err;
0110     QStringList args = KShell::splitArgs( cfg->config().readEntry( ExecuteScriptPlugin::argumentsEntry, "" ), KShell::TildeExpand | KShell::AbortOnMeta, &err );
0111     if( err != KShell::NoError )
0112     {
0113 
0114         if( err == KShell::BadQuoting )
0115         {
0116             err_ = i18n("There is a quoting error in the arguments for "
0117             "the launch configuration '%1'. Aborting start.", cfg->name() );
0118         } else
0119         {
0120             err_ = i18n("A shell meta character was included in the "
0121             "arguments for the launch configuration '%1', "
0122             "this is not supported currently. Aborting start.", cfg->name() );
0123         }
0124         args = QStringList();
0125         qCWarning(PLUGIN_EXECUTESCRIPT) << "Launch Configuration:" << cfg->name() << "arguments have meta characters";
0126     }
0127     return args;
0128 }
0129 
0130 QString ExecuteScriptPlugin::environmentProfileName(KDevelop::ILaunchConfiguration* cfg) const
0131 {
0132     if( !cfg )
0133     {
0134         return QString();
0135     }
0136 
0137     return cfg->config().readEntry(ExecuteScriptPlugin::environmentProfileEntry, QString());
0138 }
0139 
0140 int ExecuteScriptPlugin::outputFilterModeId( KDevelop::ILaunchConfiguration* cfg ) const
0141 {
0142     if( !cfg )
0143     {
0144         return 0;
0145     }
0146 
0147     return cfg->config().readEntry( ExecuteScriptPlugin::outputFilteringEntry, 0 );
0148 }
0149 
0150 bool ExecuteScriptPlugin::runCurrentFile(ILaunchConfiguration* cfg) const
0151 {
0152     if( !cfg )
0153     {
0154         return false;
0155     }
0156 
0157     return cfg->config().readEntry( ExecuteScriptPlugin::runCurrentFileEntry, true );
0158 }
0159 
0160 QString ExecuteScriptPlugin::interpreter( KDevelop::ILaunchConfiguration* cfg, QString& err ) const
0161 {
0162     QString interpreter;
0163     if( !cfg )
0164     {
0165         return interpreter;
0166     }
0167     KConfigGroup grp = cfg->config();
0168 
0169     interpreter = grp.readEntry( ExecuteScriptPlugin::interpreterEntry, QString() );
0170 
0171     if( interpreter.isEmpty() )
0172     {
0173         err = i18n("No valid interpreter specified");
0174         qCWarning(PLUGIN_EXECUTESCRIPT) << "Launch Configuration:" << cfg->name() << "no valid interpreter set";
0175     } else
0176     {
0177         KShell::Errors err_;
0178         if( KShell::splitArgs( interpreter, KShell::TildeExpand | KShell::AbortOnMeta, &err_ ).isEmpty() || err_ != KShell::NoError )
0179         {
0180             interpreter.clear();
0181             if( err_ == KShell::BadQuoting )
0182             {
0183                 err = i18n("There is a quoting error in the interpreter "
0184                 "for the launch configuration '%1'. "
0185                 "Aborting start.", cfg->name() );
0186             } else
0187             {
0188                 err = i18n("A shell meta character was included in the "
0189                 "interpreter for the launch configuration '%1', "
0190                 "this is not supported currently. Aborting start.", cfg->name() );
0191             }
0192             qCWarning(PLUGIN_EXECUTESCRIPT) << "Launch Configuration:" << cfg->name() << "interpreter has meta characters";
0193         }
0194     }
0195     return interpreter;
0196 }
0197 
0198 QUrl ExecuteScriptPlugin::workingDirectory( KDevelop::ILaunchConfiguration* cfg ) const
0199 {
0200     if( !cfg )
0201     {
0202         return QUrl();
0203     }
0204 
0205     return cfg->config().readEntry( ExecuteScriptPlugin::workingDirEntry, QUrl() );
0206 }
0207 
0208 
0209 QString ExecuteScriptPlugin::scriptAppConfigTypeId() const
0210 {
0211     return ScriptAppConfigType::sharedId();
0212 }
0213 
0214 
0215 #include "executescriptplugin.moc"
0216 #include "moc_executescriptplugin.cpp"