Skip to content
Browse files

First implementation of --launcher-generate-exec-wrapper-script

See issues #21, #36
  • Loading branch information...
1 parent c2ded81 commit 2872536d785476bf052d8a01b3e43e9c1de95afc @jcfr jcfr committed Dec 3, 2012
View
2 Base/Testing/Cpp/ctkAppLauncherTest1.cpp
@@ -78,6 +78,8 @@ void ctkAppLauncherTest1er::testDisplayHelp_data()
"variables to be set, then exit\n"
" --launcher-additional-settings Additional settings file to consider\n"
" --launcher-ignore-user-additional-settings Ignore additional user settings\n"
+ " --launcher-generate-exec-wrapper-script Generate executable wrapper script "
+ "allowing to set the environment\n"
" --launcher-generate-template Generate an example of setting file\n");
}
View
87 Base/ctkAppLauncher.cpp
@@ -7,6 +7,7 @@
#include <QFileInfo>
#include <QProcessEnvironment>
#include <QRegExp>
+#include <QTemporaryFile>
#include <QTimer>
// CTK includes
@@ -63,9 +64,9 @@ void ctkAppLauncherInternal::reportError(const QString& msg) const
}
// --------------------------------------------------------------------------
-void ctkAppLauncherInternal::reportInfo(const QString& msg) const
+void ctkAppLauncherInternal::reportInfo(const QString& msg, bool force) const
{
- if (this->verbose())
+ if (this->verbose() || force)
{
std::cout << "info: " << qPrintable(msg) << std::endl;
}
@@ -745,6 +746,77 @@ void ctkAppLauncher::displayEnvironment(std::ostream &output)
{
output << qPrintable(envArg) << std::endl;
}
+ if(envAsList.isEmpty())
+ {
+ output << "(No environment to display)" << std::endl;
+ }
+}
+
+// --------------------------------------------------------------------------
+bool ctkAppLauncher::generateExecWrapperScript()
+{
+ if (this->Internal->LauncherName.isEmpty())
+ {
+ return false;
+ }
+ QProcessEnvironment env;
+ env.insert("PATH", "$PATH");
+ env.insert(this->Internal->LibraryPathVariableName, "$" + this->Internal->LibraryPathVariableName);
+
+ this->Internal->buildEnvironment(env);
+ QStringList envAsList = env.toStringList();
+ envAsList.sort();
+
+ QStringList output;
+
+#ifdef Q_OS_LINUX
+ output << "#! /usr/bin/env bash\n";
+ QString scriptComment("#");
+ QString exportCmd("declare -x");
+ QString scriptExtension("sh");
+#elif Q_OS_WIN32
+ QString scriptComment("::");
+ QString exportCmd("@set");
+ QString scriptExtension("bat");
+#endif
+
+ output << QString("%1 This script has been generated using %2 launcher %3\n").
+ arg(scriptComment).
+ arg(this->Internal->LauncherName).
+ arg(CTKAppLauncher_VERSION);
+
+ output << QString("%1 WARNING Usage of --launcher-generate-exec-wrapper-script is experimental and not tested.").
+ arg(scriptComment);
+ output << QString("%1 WARNING Community contribution are welcome. See https://github.com/commontk/AppLauncher/issues/36\n").
+ arg(scriptComment);
+
+ foreach(const QString& env, envAsList)
+ {
+ output << QString("%1 \"%2\"").arg(exportCmd).arg(env);
+ }
+
+#ifdef Q_OS_LINUX
+ output << "if [ \"$#\" -gt \"0\" ]";
+ output << "then";
+ output << " exec \"$@\"";
+ output << "fi";
+#endif
+
+ QTemporaryFile launcherScript(QDir::temp().filePath("Slicer-XXXXXX.%1").arg(scriptExtension));
+ launcherScript.setAutoRemove(false);
+ if (!launcherScript.open())
+ {
+ this->Internal->reportError("Failed to generate executable wrapper script.");
+ return false;
+ }
+ QTextStream out(&launcherScript);
+ out << output.join("\n");
+
+ this->Internal->reportInfo(
+ QString("Launcher script generated [%1]").arg(launcherScript.fileName()),
+ /* force= */ true);
+
+ return true;
}
// --------------------------------------------------------------------------
@@ -820,6 +892,8 @@ bool ctkAppLauncher::initialize(QString launcherFilePath)
"Additional settings file to consider");
parser.addArgument("launcher-ignore-user-additional-settings", "", QVariant::Bool,
"Ignore additional user settings");
+ parser.addArgument("launcher-generate-exec-wrapper-script", "", QVariant::Bool,
+ "Generate executable wrapper script allowing to set the environment");
parser.addArgument("launcher-generate-template", "", QVariant::Bool,
"Generate an example of setting file");
@@ -873,7 +947,8 @@ int ctkAppLauncher::processArguments()
|| this->Internal->ParsedArgs.value("launcher-help").toBool()
|| this->Internal->ParsedArgs.value("launcher-version").toBool()
|| this->Internal->ParsedArgs.value("launcher-dump-environment").toBool()
- || this->Internal->ParsedArgs.value("launcher-generate-template").toBool();
+ || this->Internal->ParsedArgs.value("launcher-generate-template").toBool()
+ || this->Internal->ParsedArgs.value("launcher-generate-exec-wrapper-script").toBool();
if (reportInfo)
{
@@ -978,6 +1053,12 @@ int ctkAppLauncher::processArguments()
return Self::ExitWithError;
}
+ if (this->Internal->ParsedArgs.value("launcher-generate-exec-wrapper-script").toBool())
+ {
+ bool success = this->generateExecWrapperScript();
+ return success ? Self::ExitWithSuccess : Self::ExitWithError;
+ }
+
if (this->Internal->ParsedArgs.value("launcher-dump-environment").toBool())
{
this->displayEnvironment();
View
2 Base/ctkAppLauncher.h
@@ -33,6 +33,8 @@ class ctkAppLauncher : public QObject
/// Display environment variables on standard output
void displayEnvironment(std::ostream &output = std::cout);
+ bool generateExecWrapperScript();
+
/// Display help string on standard output
void displayHelp(std::ostream &output = std::cout);
View
2 Base/ctkAppLauncher_p.h
@@ -28,7 +28,7 @@ class ctkAppLauncherInternal : public QObject
void reportError(const QString& msg)const;
/// Display status on Standard Output
- void reportInfo(const QString& msg)const;
+ void reportInfo(const QString& msg, bool force = false)const;
/// Exit the application if the application has been set.
/// \sa ctkAppLauncher::setApplication
View
1 Testing/CMake/AppLauncher-Param-TestLauncherHelp.cmake
@@ -46,6 +46,7 @@ Options
--launcher-dump-environment Launcher will print environment variables to be set, then exit
--launcher-additional-settings Additional settings file to consider
--launcher-ignore-user-additional-settings Ignore additional user settings
+ --launcher-generate-exec-wrapper-script Generate executable wrapper script allowing to set the environment
--launcher-generate-template Generate an example of setting file
")
View
1 Testing/CMake/AppLauncherTestPrerequisites.cmake.in
@@ -129,4 +129,5 @@ Options
--launcher-dump-environment Launcher will print environment variables to be set, then exit
--launcher-additional-settings Additional settings file to consider
--launcher-ignore-user-additional-settings Ignore additional user settings
+ --launcher-generate-exec-wrapper-script Generate executable wrapper script allowing to set the environment
--launcher-generate-template Generate an example of setting file")

0 comments on commit 2872536

Please sign in to comment.
Something went wrong with that request. Please try again.