Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cli #37

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft

Cli #37

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,5 @@ FakesAssemblies/
*.Debug
*.Release
*.exe

build-*
76 changes: 76 additions & 0 deletions src/DriverCli.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include "DriverCli.h"

#include <QApplication>
#include <QCommandLineParser>
#include <QProcess>

#include "AssertMacros.h"
#include "ColorManager.h"
#include "Logging.h"
#include "Settings.h"
#include "Window.h"
#include "SettingsMouseAlgos.h"

namespace mms {

int DriverCli::drive(int argc, char *argv[]) {
// Make sure that this function is called just once
ASSERT_RUNS_JUST_ONCE();

// Initialize Qt
QApplication app(argc, argv);

QCommandLineParser parser;
parser.setApplicationDescription("Test helper");
parser.addHelpOption();

QCommandLineOption mazeFileOption(QStringList() << "m" << "mazeFile");
mazeFileOption.setValueName("mazePath");
parser.addOption(mazeFileOption);

QCommandLineOption runCommandOption(QStringList() << "r" << "runCommand");
runCommandOption.setValueName("runCommand");
parser.addOption(runCommandOption);

QCommandLineOption directoryCommandOption(QStringList() << "d" << "runDirectory");
directoryCommandOption.setValueName("runDirectory");
parser.addOption(directoryCommandOption);

QCommandLineOption buildCommandOption(QStringList() << "b" << "buildCommand");
buildCommandOption.setValueName("buildCommand");
parser.addOption(buildCommandOption);

parser.process(app);

// Initialize singletons
Logging::init();
Settings::init();
ColorManager::init();

if(!parser.isSet(mazeFileOption)){
return -1;
}

if(!parser.isSet(runCommandOption)){
return -2;
}

if(!parser.isSet(directoryCommandOption)){
return -3;
}

QString mazePath = parser.values(mazeFileOption).first();
QString runCommand = parser.values(runCommandOption).first();
QString directory = parser.values(directoryCommandOption).first();

SettingsMouseAlgos::removeall();
SettingsMouseAlgos::add("temp", directory, "test", runCommand);

Window window;
window.onMazeFileComboBoxChanged(mazePath);
window.startRun();

return app.exec();
}

} // namespace mms
11 changes: 11 additions & 0 deletions src/DriverCli.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

namespace mms {

class DriverCli {
public:
DriverCli() = delete;
static int drive(int argc, char *argv[]);
};

}
11 changes: 9 additions & 2 deletions src/Main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
#include "Driver.h"

int main(int argc, char *argv[]) { return mms::Driver::drive(argc, argv); }
#include "DriverCli.h"
int main(int argc, char *argv[])
{
#ifdef CLI
return mms::DriverCli::drive(argc, argv);
#else
return mms::Driver::drive(argc, argv);
#endif
}
6 changes: 5 additions & 1 deletion src/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,11 @@ void Settings::update(QString group, QString key, QString value,
Settings::Settings() {
QCoreApplication::setOrganizationName("mackorone");
QCoreApplication::setOrganizationDomain("www.github.com/mackorone");
QCoreApplication::setApplicationName("mms");
QString applicationName = "mms";
#ifdef CLI
applicationName = "mmm-cli";
#endif
QCoreApplication::setApplicationName(applicationName);
}

QVector<QMap<QString, QString>> Settings::getGroup(QString group) {
Expand Down
8 changes: 8 additions & 0 deletions src/SettingsMouseAlgos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ void SettingsMouseAlgos::remove(const QString &name) {
Settings::get()->remove(GROUP, KEY_NAME, name);
}


void SettingsMouseAlgos::removeall() {
auto values = Settings::get()->values(GROUP, KEY_NAME);
foreach(auto value, values){
Settings::get()->remove(GROUP, KEY_NAME, value);
}
}

QString SettingsMouseAlgos::getValue(const QString &name, const QString &key) {
const auto &vector = Settings::get()->find(GROUP, KEY_NAME, name);
return (vector.size() == 0 ? "" : vector.at(0).value(key));
Expand Down
1 change: 1 addition & 0 deletions src/SettingsMouseAlgos.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class SettingsMouseAlgos {
const QString &newBuildCommand,
const QString &newRunCommand);
static void remove(const QString &name);
static void removeall();

private:
static const QString GROUP;
Expand Down
21 changes: 21 additions & 0 deletions src/Window.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <QAction>
#include <QApplication>
#include <QDebug>
#include <QDoubleSpinBox>
#include <QFileDialog>
Expand All @@ -18,6 +19,10 @@
#include <QVBoxLayout>
#include <QtMath>

#ifdef CLI
#include<QDebug>
#endif

#include "AssertMacros.h"
#include "Color.h"
#include "ColorDialog.h"
Expand Down Expand Up @@ -50,7 +55,11 @@ const QString Window::CRASH = "crash";
const QString Window::INVALID = "invalid";

const int Window::SPEED_SLIDER_MAX = 99;
#ifdef CLI
const int Window::SPEED_SLIDER_DEFAULT = SPEED_SLIDER_MAX;
#else
const int Window::SPEED_SLIDER_DEFAULT = 33;
#endif
const double Window::MIN_PROGRESS_PER_SECOND = 10.0;
const double Window::MAX_PROGRESS_PER_SECOND = 5000.0;
const double Window::MAX_SLEEP_SECONDS = 0.008;
Expand Down Expand Up @@ -390,6 +399,9 @@ void Window::showInvalidMazeFileWarning(QString path) {
"The following is not a valid maze file:\n\n" + path +
"\n\n "
"The maze must be nonempty, rectangular, enclosed, and consistent.");
#ifdef CLI
QCoreApplication::exit(-12);
#endif
}

void Window::refreshMazeFileComboBox(QString selected) {
Expand Down Expand Up @@ -710,10 +722,16 @@ void Window::startRun() {
} else {
// Clean up the failed process
m_runOutput->appendPlainText(process->errorString());
#ifdef CLI
qDebug() << process->errorString();
#endif
m_runStatus->setText("ERROR");
m_runStatus->setStyleSheet(ERROR_STYLE_SHEET);
removeMouseFromMaze();
delete process;
#ifdef CLI
QCoreApplication::exit(-10);
#endif
}
}

Expand Down Expand Up @@ -755,6 +773,9 @@ void Window::onRunExit(int exitCode, QProcess::ExitStatus exitStatus) {
// Stop consuming queued commands
m_commandQueueTimer->stop();
m_commandQueue.clear();
#ifdef CLI
QCoreApplication::exit(exitCode);
#endif
}

void Window::removeMouseFromMaze() {
Expand Down
7 changes: 5 additions & 2 deletions src/Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ class Window : public QMainWindow {
void closeEvent(QCloseEvent *event);
void resizeEvent(QResizeEvent *event);

void onMazeFileComboBoxChanged(QString path);

void startRun();

private:
// ----- Graphics -----

Expand All @@ -62,7 +66,7 @@ class Window : public QMainWindow {
QComboBox *m_mazeFileComboBox;

void onMazeFileButtonPressed();
void onMazeFileComboBoxChanged(QString path);

void showInvalidMazeFileWarning(QString path);
void refreshMazeFileComboBox(QString selected);
void updateMazeAndPath(Maze *maze, QString path);
Expand Down Expand Up @@ -113,7 +117,6 @@ class Window : public QMainWindow {
QProcess *m_runProcess;
QLabel *m_runStatus;

void startRun();
void cancelRun();
void onRunExit(int exitCode, QProcess::ExitStatus exitStatus);

Expand Down
5 changes: 5 additions & 0 deletions src/mms-cli.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
include(mms.pro)

DEFINES += CLI
CONFIG += console