Skip to content

Commit

Permalink
Updater|Mac OS X: Run the installer script
Browse files Browse the repository at this point in the history
The AppleScript that replaces the existing Doomsday Engine
app with the downloaded one is now started when the engine
is shut down (atexit).

Implemented de::CommandLine::execute() to handle the task
of starting a background process.
  • Loading branch information
skyjake committed Jun 5, 2012
1 parent 2d6c8c3 commit eea9637
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 41 deletions.
25 changes: 24 additions & 1 deletion doomsday/engine/portable/src/updater.cpp
Expand Up @@ -20,11 +20,13 @@
* 02110-1301 USA</small>
*/

#include "updater.h"
#include <stdlib.h>
#include "sys_system.h"
#include "dd_version.h"
#include "dd_types.h"
#include "nativeui.h"
#include "json.h"
#include "updater.h"
#include "updater/downloaddialog.h"
#include "updater/updateavailabledialog.h"
#include "updater/updatersettings.h"
Expand Down Expand Up @@ -66,6 +68,18 @@ static Updater* updater = 0;
# endif
#endif

static de::CommandLine* installerCommand;

/**
* Callback for atexit(). Create the installerCommand before calling this.
*/
static void startInstallerCallback(void)
{
installerCommand->execute();
delete installerCommand;
installerCommand = 0;
}

struct Updater::Instance
{
Updater* self;
Expand Down Expand Up @@ -159,10 +173,14 @@ struct Updater::Instance
LOG_MSG(execPath);
if(!execPath.fileName().endsWith(".app"))
{
#ifdef _DEBUG
execPath = "/Applications/Doomsday Engine.app";
#else
Sys_MessageBox2(MBT_ERROR, "Updating", "Automatic update failed.",
("Expected an application bundle, but found <b>" +
execPath.fileName() + "</b> instead.").toUtf8(), 0);
return;
#endif
}

de::String appPath = execPath.fileNamePath();
Expand Down Expand Up @@ -199,6 +217,11 @@ struct Updater::Instance
}

// Register a shutdown action to execute the script and quit.
installerCommand = new de::CommandLine;
installerCommand->append("osascript");
installerCommand->append(scriptPath);
atexit(startInstallerCallback);
Sys_Quit();
#endif
}
};
Expand Down
6 changes: 4 additions & 2 deletions doomsday/libdeng2/include/de/core/commandline.h
Expand Up @@ -45,6 +45,8 @@ namespace de
DENG2_ERROR(ExecuteError)

public:
CommandLine();

/**
* Constructs a CommandLine out of the provided strings. It is assumed
* that @a argc and @a args are the ones passed from the system to the main()
Expand Down Expand Up @@ -189,9 +191,9 @@ namespace de
* specifies the file name of the executable. Returns immediately
* after the process has been started.
*
* @param envs Environment variables passed to the new process.
* @return @c true if successful, otherwise @c false.
*/
void execute(char** envs) const;
bool execute() const;

private:
struct Instance;
Expand Down
52 changes: 14 additions & 38 deletions doomsday/libdeng2/src/core/commandline.cpp
Expand Up @@ -109,6 +109,11 @@ struct CommandLine::Instance
}
};

CommandLine::CommandLine()
{
d = new Instance;
}

CommandLine::CommandLine(int argc, char** v)
{
d = new Instance;
Expand Down Expand Up @@ -410,48 +415,19 @@ bool CommandLine::matches(const String& full, const String& fullOrAlias) const
return false;
}

void CommandLine::execute(char** /*envs*/) const
bool CommandLine::execute() const
{
qDebug("CommandLine: should call QProcess to execute!\n");
if(count() < 1) return false;

/*
#ifdef Q_OS_UNIX
// Fork and execute new file.
pid_t result = fork();
if(!result)
{
// Here we go in the child process.
printf("Child loads %s...\n", _pointers[0]);
execve(_pointers[0], const_cast<char* const*>(argv()), const_cast<char* const*>(envs));
}
else
{
if(result < 0)
{
perror("CommandLine::execute");
}
}
#endif
QStringList args;
for(int i = 1; i < count(); ++i) args << at(i);

#ifdef WIN32
String quotedCmdLine;
FOR_EACH(i, _arguments, Arguments::const_iterator)
if(!QProcess::startDetached(at(0), args))
{
quotedCmdLine += "\"" + *i + "\" ";
qWarning() << "CommandLine: Failed to start" << at(0);
return false;
}

STARTUPINFO startupInfo;
PROCESS_INFORMATION processInfo;
ZeroMemory(&startupInfo, sizeof(startupInfo));
startupInfo.cb = sizeof(startupInfo);
ZeroMemory(&processInfo, sizeof(processInfo));
if(!CreateProcess(_pointers[0], const_cast<char*>(quotedCmdLine.c_str()),
NULL, NULL, FALSE, 0, NULL, NULL, &startupInfo, &processInfo))
{
/// @throw ExecuteError The system call to start a new process failed.
throw ExecuteError("CommandLine::execute", "Could not create process");
}
#endif
*/
qDebug() << "CommandLine: Started detached process" << at(0);
return true;
}

0 comments on commit eea9637

Please sign in to comment.