Commit 123e27be authored by Jan Möbius's avatar Jan Möbius

Added include statement for scripts

parent bfd9c4f1
Pipeline #907 passed with stage
......@@ -111,6 +111,16 @@
// The path of the currently executed script is stored in the ScriptPath variable.
print(ScriptPath)
\endcode
\subsection scripting_misc_includes Can i include additional script files?
Yes, just use the keyword include. You can also use paths relative to the current script.
The scripts will be joined internally so variables are available across the scripts.
\code
// Include with full path:
include <Path/script1.ofs>
// Relative to current script:
include <ScriptPath/script2.ofs>
\endcode
\section scripting_examples Scripting Examples
\subsection scripting_examples_qstringlist Iterating over a QStringList
......
......@@ -352,6 +352,55 @@ void ScriptingPlugin::slotExecuteScript( QString _script ) {
// Get the filename of the script and set it in the scripting environment
engine->globalObject().setProperty("ScriptPath",OpenFlipper::Options::currentScriptDirStr());
// Check if the script contains include statements
if (_script.contains(QRegExp("^include <")) ) {
// Split input script into lines
QStringList script = _script.split(QRegExp("[\r\n]"),QString::SkipEmptyParts);
// Find first include statement
int include_index = script.indexOf(QRegExp("^include.*"));
while ( include_index != -1) {
QString include_statement = script[include_index];
// Extract the file path of the include
include_statement.remove(QRegExp("^include") );
include_statement.remove("<" );
include_statement.remove(">" );
include_statement = include_statement.trimmed();
// Replace the ScriptPath component
include_statement.replace("ScriptPath",OpenFlipper::Options::currentScriptDirStr());
QFile includeFile(include_statement);
if (!includeFile.exists() ) {
emit log(LOGERR,"Script file include not found : " + include_statement + " from " + script[include_index] );
return;
} else {
if (!includeFile.open(QFile::ReadOnly | QFile::Text)) {
emit log(LOGERR,"Unable to open file : " + include_statement);
return;
}
QTextStream in(&includeFile);
script[include_index] = in.readAll();
includeFile.close();
}
// Recombine all script components
_script = script.join("\n");
// Check for next occurence of an include statement
include_index = script.indexOf(QRegExp("^include.*"));
}
}
// Execute the script
engine->evaluate( _script );
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment