From c0df1846f4f34e7a8adb5191fbda213ff68a5a24 Mon Sep 17 00:00:00 2001 From: Alex Peterson <26804013+AMZN-alexpete@users.noreply.github.com> Date: Fri, 12 Nov 2021 11:03:43 -0800 Subject: [PATCH] Create desktop shortcut functionality (#5536) Signed-off-by: Alex Peterson <26804013+AMZN-alexpete@users.noreply.github.com> --- .../Linux/ProjectManager_Traits_Linux.h | 1 + .../Platform/Linux/ProjectUtils_linux.cpp | 5 ++++ .../Platform/Mac/ProjectManager_Traits_Mac.h | 1 + .../Platform/Mac/ProjectUtils_mac.cpp | 5 ++++ .../Windows/ProjectManager_Traits_Windows.h | 1 + .../Platform/Windows/ProjectUtils_windows.cpp | 22 +++++++++++++++ .../Source/ProjectButtonWidget.cpp | 28 +++++++++++++++++++ .../ProjectManager/Source/ProjectUtils.cpp | 4 +-- .../ProjectManager/Source/ProjectUtils.h | 9 ++++++ 9 files changed, 74 insertions(+), 2 deletions(-) diff --git a/Code/Tools/ProjectManager/Platform/Linux/ProjectManager_Traits_Linux.h b/Code/Tools/ProjectManager/Platform/Linux/ProjectManager_Traits_Linux.h index 7c0543361f..d7edcfcf12 100644 --- a/Code/Tools/ProjectManager/Platform/Linux/ProjectManager_Traits_Linux.h +++ b/Code/Tools/ProjectManager/Platform/Linux/ProjectManager_Traits_Linux.h @@ -9,3 +9,4 @@ #pragma once #define AZ_TRAIT_PROJECT_MANAGER_CUSTOM_TITLEBAR false +#define AZ_TRAIT_PROJECT_MANAGER_CREATE_DESKTOP_SHORTCUT false diff --git a/Code/Tools/ProjectManager/Platform/Linux/ProjectUtils_linux.cpp b/Code/Tools/ProjectManager/Platform/Linux/ProjectUtils_linux.cpp index dc3ec55dee..a7bd2dae08 100644 --- a/Code/Tools/ProjectManager/Platform/Linux/ProjectUtils_linux.cpp +++ b/Code/Tools/ProjectManager/Platform/Linux/ProjectUtils_linux.cpp @@ -96,5 +96,10 @@ namespace O3DE::ProjectManager { return AZ::Utils::GetExecutableDirectory(); } + + AZ::Outcome CreateDesktopShortcut([[maybe_unused]] const QString& filename, [[maybe_unused]] const QString& targetPath, [[maybe_unused]] const QStringList& arguments) + { + return AZ::Failure(QObject::tr("Creating desktop shortcuts functionality not implemented for this platform yet.")); + } } // namespace ProjectUtils } // namespace O3DE::ProjectManager diff --git a/Code/Tools/ProjectManager/Platform/Mac/ProjectManager_Traits_Mac.h b/Code/Tools/ProjectManager/Platform/Mac/ProjectManager_Traits_Mac.h index 7c0543361f..d7edcfcf12 100644 --- a/Code/Tools/ProjectManager/Platform/Mac/ProjectManager_Traits_Mac.h +++ b/Code/Tools/ProjectManager/Platform/Mac/ProjectManager_Traits_Mac.h @@ -9,3 +9,4 @@ #pragma once #define AZ_TRAIT_PROJECT_MANAGER_CUSTOM_TITLEBAR false +#define AZ_TRAIT_PROJECT_MANAGER_CREATE_DESKTOP_SHORTCUT false diff --git a/Code/Tools/ProjectManager/Platform/Mac/ProjectUtils_mac.cpp b/Code/Tools/ProjectManager/Platform/Mac/ProjectUtils_mac.cpp index b768200398..62011bf04b 100644 --- a/Code/Tools/ProjectManager/Platform/Mac/ProjectUtils_mac.cpp +++ b/Code/Tools/ProjectManager/Platform/Mac/ProjectUtils_mac.cpp @@ -137,5 +137,10 @@ namespace O3DE::ProjectManager return editorPath; } + + AZ::Outcome CreateDesktopShortcut([[maybe_unused]] const QString& filename, [[maybe_unused]] const QString& targetPath, [[maybe_unused]] const QStringList& arguments) + { + return AZ::Failure(QObject::tr("Creating desktop shortcuts functionality not implemented for this platform yet.")); + } } // namespace ProjectUtils } // namespace O3DE::ProjectManager diff --git a/Code/Tools/ProjectManager/Platform/Windows/ProjectManager_Traits_Windows.h b/Code/Tools/ProjectManager/Platform/Windows/ProjectManager_Traits_Windows.h index e6422b5a77..9e4d29b58f 100644 --- a/Code/Tools/ProjectManager/Platform/Windows/ProjectManager_Traits_Windows.h +++ b/Code/Tools/ProjectManager/Platform/Windows/ProjectManager_Traits_Windows.h @@ -9,3 +9,4 @@ #pragma once #define AZ_TRAIT_PROJECT_MANAGER_CUSTOM_TITLEBAR true +#define AZ_TRAIT_PROJECT_MANAGER_CREATE_DESKTOP_SHORTCUT true diff --git a/Code/Tools/ProjectManager/Platform/Windows/ProjectUtils_windows.cpp b/Code/Tools/ProjectManager/Platform/Windows/ProjectUtils_windows.cpp index 871f8e9567..d08da1d5e1 100644 --- a/Code/Tools/ProjectManager/Platform/Windows/ProjectUtils_windows.cpp +++ b/Code/Tools/ProjectManager/Platform/Windows/ProjectUtils_windows.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include @@ -146,5 +147,26 @@ namespace O3DE::ProjectManager { return AZ::Utils::GetExecutableDirectory(); } + + AZ::Outcome CreateDesktopShortcut(const QString& filename, const QString& targetPath, const QStringList& arguments) + { + const QString cmd{"powershell.exe"}; + const QString desktopPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation); + const QString shortcutPath = QString("%1/%2.lnk").arg(desktopPath).arg(filename); + const QString arg = QString("$s=(New-Object -COM WScript.Shell).CreateShortcut('%1');$s.TargetPath='%2';$s.Arguments='%3';$s.Save();") + .arg(shortcutPath) + .arg(targetPath) + .arg(arguments.join(' ')); + auto createShortcutResult = ExecuteCommandResult(cmd, QStringList{"-Command", arg}, QProcessEnvironment::systemEnvironment()); + if (!createShortcutResult.IsSuccess()) + { + return AZ::Failure(QObject::tr("Failed to create desktop shortcut %1

" + "Please verify you have permission to create files at the specified location.

%2") + .arg(shortcutPath) + .arg(createShortcutResult.GetError())); + } + + return AZ::Success(QObject::tr("Desktop shortcut created at
%2").arg(desktopPath).arg(shortcutPath)); + } } // namespace ProjectUtils } // namespace O3DE::ProjectManager diff --git a/Code/Tools/ProjectManager/Source/ProjectButtonWidget.cpp b/Code/Tools/ProjectManager/Source/ProjectButtonWidget.cpp index c425c344e1..98916cccf6 100644 --- a/Code/Tools/ProjectManager/Source/ProjectButtonWidget.cpp +++ b/Code/Tools/ProjectManager/Source/ProjectButtonWidget.cpp @@ -8,7 +8,11 @@ #include #include +#include +#include #include +#include +#include #include #include @@ -23,6 +27,7 @@ #include #include #include +#include namespace O3DE::ProjectManager { @@ -205,6 +210,29 @@ namespace O3DE::ProjectManager { AzQtComponents::ShowFileOnDesktop(m_projectInfo.m_path); }); + +#if AZ_TRAIT_PROJECT_MANAGER_CREATE_DESKTOP_SHORTCUT + menu->addAction(tr("Create Editor desktop shortcut..."), this, [this]() + { + AZ::IO::FixedMaxPath executableDirectory = ProjectUtils::GetEditorDirectory(); + AZStd::string executableFilename = "Editor"; + AZ::IO::FixedMaxPath editorExecutablePath = executableDirectory / (executableFilename + AZ_TRAIT_OS_EXECUTABLE_EXTENSION); + + const QString shortcutName = QString("%1 Editor").arg(m_projectInfo.m_displayName); + const QString arg = QString("--regset=\"/Amazon/AzCore/Bootstrap/project_path=%1\"").arg(m_projectInfo.m_path); + + auto result = ProjectUtils::CreateDesktopShortcut(shortcutName, editorExecutablePath.c_str(), { arg }); + if(result.IsSuccess()) + { + QMessageBox::information(this, tr("Desktop Shortcut Created"), result.GetValue()); + } + else + { + QMessageBox::critical(this, tr("Failed to create shortcut"), result.GetError()); + } + }); +#endif // AZ_TRAIT_PROJECT_MANAGER_CREATE_DESKTOP_SHORTCUT + menu->addSeparator(); menu->addAction(tr("Duplicate"), this, [this]() { emit CopyProject(m_projectInfo); }); menu->addSeparator(); diff --git a/Code/Tools/ProjectManager/Source/ProjectUtils.cpp b/Code/Tools/ProjectManager/Source/ProjectUtils.cpp index bb2bcd070e..4a0e1c153c 100644 --- a/Code/Tools/ProjectManager/Source/ProjectUtils.cpp +++ b/Code/Tools/ProjectManager/Source/ProjectUtils.cpp @@ -628,11 +628,11 @@ namespace O3DE::ProjectManager return AZ::Failure(QObject::tr("Process for command '%1' timed out at %2 seconds").arg(cmd).arg(commandTimeoutSeconds)); } int resultCode = execProcess.exitCode(); + QString resultOutput = execProcess.readAllStandardOutput(); if (resultCode != 0) { - return AZ::Failure(QObject::tr("Process for command '%1' failed (result code %2").arg(cmd).arg(resultCode)); + return AZ::Failure(QObject::tr("Process for command '%1' failed (result code %2) %3").arg(cmd).arg(resultCode).arg(resultOutput)); } - QString resultOutput = execProcess.readAllStandardOutput(); return AZ::Success(resultOutput); } diff --git a/Code/Tools/ProjectManager/Source/ProjectUtils.h b/Code/Tools/ProjectManager/Source/ProjectUtils.h index 890d50d2de..ee605b5117 100644 --- a/Code/Tools/ProjectManager/Source/ProjectUtils.h +++ b/Code/Tools/ProjectManager/Source/ProjectUtils.h @@ -68,6 +68,15 @@ namespace O3DE::ProjectManager AZ::Outcome GetProjectBuildPath(const QString& projectPath); AZ::Outcome OpenCMakeGUI(const QString& projectPath); AZ::Outcome RunGetPythonScript(const QString& enginePath); + + /** + * Create a desktop shortcut. + * @param filename the name of the desktop shorcut file + * @param target the path to the target to run + * @param arguments the argument list to provide to the target + * @return AZ::Outcome with the command result on success + */ + AZ::Outcome CreateDesktopShortcut(const QString& filename, const QString& targetPath, const QStringList& arguments); AZ::IO::FixedMaxPath GetEditorDirectory();