From 7b1e68bfa8cf9d46e0c810cfa0f077427cfc707a Mon Sep 17 00:00:00 2001 From: Ryan Cao <70191398+ryanccn@users.noreply.github.com> Date: Wed, 19 Oct 2022 21:53:57 +0800 Subject: [PATCH 1/6] initial support for add to PATH action Signed-off-by: Ryan Cao <70191398+ryanccn@users.noreply.github.com> --- launcher/FileSystem.cpp | 13 +++++++++++++ launcher/FileSystem.h | 5 +++++ launcher/ui/MainWindow.cpp | 29 +++++++++++++++++++++++++++++ launcher/ui/MainWindow.h | 2 ++ 4 files changed, 49 insertions(+) diff --git a/launcher/FileSystem.cpp b/launcher/FileSystem.cpp index 4026d6c1..13bb95d9 100644 --- a/launcher/FileSystem.cpp +++ b/launcher/FileSystem.cpp @@ -163,6 +163,19 @@ bool ensureFolderPathExists(QString foldernamepath) return success; } +bool symlink(const QString& source, const QString& target) +{ + std::error_code err; + + fs::create_symlink(toStdString(source), toStdString(target)); + + if (err) { + qWarning() << "Failed to symlink files:" << QString::fromStdString(err.message()); + } + + return err.value() == 0; +} + bool copy::operator()(const QString& offset) { using copy_opts = fs::copy_options; diff --git a/launcher/FileSystem.h b/launcher/FileSystem.h index b46f3281..2d4dfb56 100644 --- a/launcher/FileSystem.h +++ b/launcher/FileSystem.h @@ -53,6 +53,11 @@ class FileSystemException : public ::Exception { */ void write(const QString& filename, const QByteArray& data); +/** + * create a symlink + */ +bool symlink(const QString& target, const QString& link); + /** * read data from a file safely\ */ diff --git a/launcher/ui/MainWindow.cpp b/launcher/ui/MainWindow.cpp index afbc505e..80d212e9 100644 --- a/launcher/ui/MainWindow.cpp +++ b/launcher/ui/MainWindow.cpp @@ -61,6 +61,7 @@ #include #include #include +#include #include #include #include @@ -253,6 +254,7 @@ public: QMenu * helpMenu = nullptr; TranslatedToolButton helpMenuButton; TranslatedAction actionClearMetadata; + TranslatedAction actionAddToPATH; TranslatedAction actionReportBug; TranslatedAction actionDISCORD; TranslatedAction actionMATRIX; @@ -348,6 +350,14 @@ public: actionClearMetadata.setTooltipId(QT_TRANSLATE_NOOP("MainWindow", "Clear cached metadata")); all_actions.append(&actionClearMetadata); + #ifdef Q_OS_MAC + actionAddToPATH = TranslatedAction(MainWindow); + actionAddToPATH->setObjectName(QStringLiteral("actionAddToPATH")); + actionAddToPATH.setTextId(QT_TRANSLATE_NOOP("MainWindow", "Add to &PATH")); + actionAddToPATH.setTooltipId(QT_TRANSLATE_NOOP("MainWindow", "Add the prism binary to PATH.")); + all_actions.append(&actionAddToPATH); + #endif + if (!BuildConfig.BUG_TRACKER_URL.isEmpty()) { actionReportBug = TranslatedAction(MainWindow); actionReportBug->setObjectName(QStringLiteral("actionReportBug")); @@ -448,6 +458,10 @@ public: helpMenu->addAction(actionClearMetadata); + #ifdef Q_OS_MAC + helpMenu->addAction(actionAddToPATH); + #endif + if (!BuildConfig.BUG_TRACKER_URL.isEmpty()) { helpMenu->addAction(actionReportBug); } @@ -533,6 +547,9 @@ public: helpMenu = menuBar->addMenu(tr("&Help")); helpMenu->setSeparatorsCollapsible(false); helpMenu->addAction(actionClearMetadata); + #ifdef Q_OS_MAC + helpMenu->addAction(actionAddToPATH); + #endif helpMenu->addSeparator(); helpMenu->addAction(actionAbout); helpMenu->addAction(actionOpenWiki); @@ -1902,6 +1919,18 @@ void MainWindow::on_actionClearMetadata_triggered() APPLICATION->metacache()->evictAll(); } +void MainWindow::on_actionAddToPATH_triggered() { + auto binaryPath = APPLICATION->arguments().first(); + + auto outcome = FS::symlink(binaryPath, "/usr/local/bin/prism"); + + if (!outcome) { + QMessageBox::critical(this, tr("Failed to add Prism to PATH"), tr("")); + } else { + QMessageBox::information(this, tr("Added Prism to PATH"), tr("Prism was successfully added to your PATH.")); + } +} + void MainWindow::on_actionOpenWiki_triggered() { DesktopServices::openUrl(QUrl(BuildConfig.HELP_URL.arg(""))); diff --git a/launcher/ui/MainWindow.h b/launcher/ui/MainWindow.h index cb8cb4aa..097b0983 100644 --- a/launcher/ui/MainWindow.h +++ b/launcher/ui/MainWindow.h @@ -128,6 +128,8 @@ private slots: void on_actionClearMetadata_triggered(); + void on_actionAddToPATH_triggered(); + void on_actionOpenWiki_triggered(); void on_actionMoreNews_triggered(); From cf3aad9c414782996b11cb0779a9c3d7252b5420 Mon Sep 17 00:00:00 2001 From: Ryan Cao <70191398+ryanccn@users.noreply.github.com> Date: Sat, 5 Nov 2022 22:15:31 +0800 Subject: [PATCH 2/6] fix: use `osascript` to get admin privileges inspired from VSCode's approach Signed-off-by: Ryan Cao <70191398+ryanccn@users.noreply.github.com> --- launcher/ui/MainWindow.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/launcher/ui/MainWindow.cpp b/launcher/ui/MainWindow.cpp index 80d212e9..beb73c81 100644 --- a/launcher/ui/MainWindow.cpp +++ b/launcher/ui/MainWindow.cpp @@ -1920,14 +1920,15 @@ void MainWindow::on_actionClearMetadata_triggered() } void MainWindow::on_actionAddToPATH_triggered() { - auto binaryPath = APPLICATION->arguments().first(); + auto binaryPath = APPLICATION->applicationFilePath(); - auto outcome = FS::symlink(binaryPath, "/usr/local/bin/prism"); + qDebug() << "Symlinking" << binaryPath << "to /usr/local/bin/prism"; + auto outcome = QProcess::execute("/usr/bin/osascript", QStringList()<< "-e" << tr("do shell script \"mkdir -p /usr/local/bin && ln -sf '%1' '/usr/local/bin/prismlauncher'\" with administrator privileges").arg(binaryPath)); if (!outcome) { - QMessageBox::critical(this, tr("Failed to add Prism to PATH"), tr("")); + QMessageBox::information(this, tr("Added Prism to PATH"), tr("Prism was successfully added to your PATH. You can now run it with `prismlauncher` in your Terminal. Enjoy!")); } else { - QMessageBox::information(this, tr("Added Prism to PATH"), tr("Prism was successfully added to your PATH.")); + QMessageBox::critical(this, tr("Failed to add Prism to PATH"), tr("Failed to add Prism to PATH :(")); } } From f1d3481a2907cbc72d99321f645c4a827ed436d2 Mon Sep 17 00:00:00 2001 From: Ryan Cao <70191398+ryanccn@users.noreply.github.com> Date: Sat, 5 Nov 2022 22:25:22 +0800 Subject: [PATCH 3/6] fix: remove dumb `FS::symlink` function Signed-off-by: Ryan Cao <70191398+ryanccn@users.noreply.github.com> --- launcher/FileSystem.cpp | 13 ------------- launcher/FileSystem.h | 5 ----- 2 files changed, 18 deletions(-) diff --git a/launcher/FileSystem.cpp b/launcher/FileSystem.cpp index 13bb95d9..4026d6c1 100644 --- a/launcher/FileSystem.cpp +++ b/launcher/FileSystem.cpp @@ -163,19 +163,6 @@ bool ensureFolderPathExists(QString foldernamepath) return success; } -bool symlink(const QString& source, const QString& target) -{ - std::error_code err; - - fs::create_symlink(toStdString(source), toStdString(target)); - - if (err) { - qWarning() << "Failed to symlink files:" << QString::fromStdString(err.message()); - } - - return err.value() == 0; -} - bool copy::operator()(const QString& offset) { using copy_opts = fs::copy_options; diff --git a/launcher/FileSystem.h b/launcher/FileSystem.h index 2d4dfb56..b46f3281 100644 --- a/launcher/FileSystem.h +++ b/launcher/FileSystem.h @@ -53,11 +53,6 @@ class FileSystemException : public ::Exception { */ void write(const QString& filename, const QByteArray& data); -/** - * create a symlink - */ -bool symlink(const QString& target, const QString& link); - /** * read data from a file safely\ */ From 3df31579a1b305c9e976f8a21e156a0dd349f40d Mon Sep 17 00:00:00 2001 From: Ryan Cao <70191398+ryanccn@users.noreply.github.com> Date: Sat, 5 Nov 2022 22:31:07 +0800 Subject: [PATCH 4/6] fix: @timoreo22 Signed-off-by: Ryan Cao <70191398+ryanccn@users.noreply.github.com> --- launcher/ui/MainWindow.cpp | 4 ++++ launcher/ui/MainWindow.h | 2 ++ 2 files changed, 6 insertions(+) diff --git a/launcher/ui/MainWindow.cpp b/launcher/ui/MainWindow.cpp index beb73c81..c376f4ec 100644 --- a/launcher/ui/MainWindow.cpp +++ b/launcher/ui/MainWindow.cpp @@ -254,7 +254,9 @@ public: QMenu * helpMenu = nullptr; TranslatedToolButton helpMenuButton; TranslatedAction actionClearMetadata; + #ifdef Q_OS_MAC TranslatedAction actionAddToPATH; + #endif TranslatedAction actionReportBug; TranslatedAction actionDISCORD; TranslatedAction actionMATRIX; @@ -1919,6 +1921,7 @@ void MainWindow::on_actionClearMetadata_triggered() APPLICATION->metacache()->evictAll(); } +#ifdef Q_OS_MAC void MainWindow::on_actionAddToPATH_triggered() { auto binaryPath = APPLICATION->applicationFilePath(); @@ -1931,6 +1934,7 @@ void MainWindow::on_actionAddToPATH_triggered() { QMessageBox::critical(this, tr("Failed to add Prism to PATH"), tr("Failed to add Prism to PATH :(")); } } +#endif void MainWindow::on_actionOpenWiki_triggered() { diff --git a/launcher/ui/MainWindow.h b/launcher/ui/MainWindow.h index 097b0983..c9ab8bfb 100644 --- a/launcher/ui/MainWindow.h +++ b/launcher/ui/MainWindow.h @@ -128,7 +128,9 @@ private slots: void on_actionClearMetadata_triggered(); + #ifdef Q_OS_MAC void on_actionAddToPATH_triggered(); + #endif void on_actionOpenWiki_triggered(); From 97a7af855f8a96a0e73181c5e32a15bbd2cb67f2 Mon Sep 17 00:00:00 2001 From: Ryan Cao <70191398+ryanccn@users.noreply.github.com> Date: Mon, 14 Nov 2022 19:21:47 +0800 Subject: [PATCH 5/6] slight reword: "install to PATH" Signed-off-by: Ryan Cao <70191398+ryanccn@users.noreply.github.com> --- launcher/ui/MainWindow.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/launcher/ui/MainWindow.cpp b/launcher/ui/MainWindow.cpp index c376f4ec..7140831b 100644 --- a/launcher/ui/MainWindow.cpp +++ b/launcher/ui/MainWindow.cpp @@ -355,8 +355,8 @@ public: #ifdef Q_OS_MAC actionAddToPATH = TranslatedAction(MainWindow); actionAddToPATH->setObjectName(QStringLiteral("actionAddToPATH")); - actionAddToPATH.setTextId(QT_TRANSLATE_NOOP("MainWindow", "Add to &PATH")); - actionAddToPATH.setTooltipId(QT_TRANSLATE_NOOP("MainWindow", "Add the prism binary to PATH.")); + actionAddToPATH.setTextId(QT_TRANSLATE_NOOP("MainWindow", "Install to &PATH")); + actionAddToPATH.setTooltipId(QT_TRANSLATE_NOOP("MainWindow", "Install a prismlauncher symlink to /usr/local/bin")); all_actions.append(&actionAddToPATH); #endif From e14b998da3850e43abf2606064d6b0ddebbf0025 Mon Sep 17 00:00:00 2001 From: Sefa Eyeoglu Date: Mon, 14 Nov 2022 18:37:24 +0100 Subject: [PATCH 6/6] refactor: improve readability Signed-off-by: Sefa Eyeoglu --- launcher/ui/MainWindow.cpp | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/launcher/ui/MainWindow.cpp b/launcher/ui/MainWindow.cpp index 9375039b..e1ac9551 100644 --- a/launcher/ui/MainWindow.cpp +++ b/launcher/ui/MainWindow.cpp @@ -1949,16 +1949,24 @@ void MainWindow::on_actionClearMetadata_triggered() } #ifdef Q_OS_MAC -void MainWindow::on_actionAddToPATH_triggered() { +void MainWindow::on_actionAddToPATH_triggered() +{ auto binaryPath = APPLICATION->applicationFilePath(); + auto targetPath = QString("/usr/local/bin/%1").arg(BuildConfig.LAUNCHER_APP_BINARY_NAME); + qDebug() << "Symlinking" << binaryPath << "to" << targetPath; - qDebug() << "Symlinking" << binaryPath << "to /usr/local/bin/prism"; - auto outcome = QProcess::execute("/usr/bin/osascript", QStringList()<< "-e" << tr("do shell script \"mkdir -p /usr/local/bin && ln -sf '%1' '/usr/local/bin/prismlauncher'\" with administrator privileges").arg(binaryPath)); - + QStringList args; + args << "-e"; + args << QString("do shell script \"mkdir -p /usr/local/bin && ln -sf '%1' '%2'\" with administrator privileges") + .arg(binaryPath, targetPath); + auto outcome = QProcess::execute("/usr/bin/osascript", args); if (!outcome) { - QMessageBox::information(this, tr("Added Prism to PATH"), tr("Prism was successfully added to your PATH. You can now run it with `prismlauncher` in your Terminal. Enjoy!")); + QMessageBox::information(this, tr("Successfully added %1 to PATH").arg(BuildConfig.LAUNCHER_DISPLAYNAME), + tr("%1 was successfully added to your PATH. You can now start it by running `%2`.") + .arg(BuildConfig.LAUNCHER_DISPLAYNAME, BuildConfig.LAUNCHER_APP_BINARY_NAME)); } else { - QMessageBox::critical(this, tr("Failed to add Prism to PATH"), tr("Failed to add Prism to PATH :(")); + QMessageBox::critical(this, tr("Failed to add %1 to PATH").arg(BuildConfig.LAUNCHER_DISPLAYNAME), + tr("An error occurred while trying to add %1 to PATH").arg(BuildConfig.LAUNCHER_DISPLAYNAME)); } } #endif