From 8db5b35f252d563d65daa3c53484065944032b56 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Fri, 30 Dec 2022 17:00:48 +0800 Subject: [PATCH] Added additional file utilities, PowerShell based execution of commands and tools for working with visual studio --- Dependencies.bat | 11 +++++- Dependencies.lua | 3 +- SHADE_Engine/premake5.lua | 6 ++- SHADE_Engine/src/Scripting/SHScriptEngine.cpp | 16 +++++--- SHADE_Engine/src/Scripting/SHVSUtilities.cpp | 39 ++++++++++++++++++- SHADE_Engine/src/Scripting/SHVSUtilities.h | 9 ++++- .../src/Tools/Utilities/SHExecUtilities.cpp | 23 ++++++++++- .../src/Tools/Utilities/SHExecUtilities.h | 10 +++-- .../src/Tools/Utilities/SHFileUtilties.cpp | 20 ++++++++-- .../src/Tools/Utilities/SHFileUtilties.h | 35 ++++++++++------- 10 files changed, 136 insertions(+), 36 deletions(-) diff --git a/Dependencies.bat b/Dependencies.bat index ba411815..460a1d87 100644 --- a/Dependencies.bat +++ b/Dependencies.bat @@ -20,10 +20,11 @@ echo "M - SDL" echo "N - dotnet" echo "O - tinyddsloader" echo "P - fmod" +echo "Q - vswhere" echo --------------------------------------------------- echo. -choice /C ABCDEFGHIJKLMNOP /T 10 /D A +choice /C ABCDEFGHIJKLMNOPQ /T 10 /D A set _e=%ERRORLEVEL% if %_e%==1 goto VMA @@ -42,6 +43,7 @@ if %_e%==13 goto SDL if %_e%==14 goto dotnet if %_e%==15 goto tinyddsloader if %_e%==16 goto fmod +if %_e%==17 goto vswhere :VMA echo -----------------------VMA---------------------------- @@ -155,6 +157,13 @@ if %_e%==15 (goto :done) else (goto :fmod) echo --------------------fmod------------------------- rmdir "Dependencies/fmod" /S /Q git clone https://github.com/SHADE-DP/FMOD.git "Dependencies/fmod" +if %_e%==16 (goto :done) else (goto :vswhere) + +:vswhere +echo -----------------------vswhere---------------------------- +rmdir "Dependencies/vswhere" /S /Q +mkdir "Dependencies/vswhere" +powershell -Command "& {wget https://github.com/microsoft/vswhere/releases/download/3.1.1/vswhere.exe -OutFile "Dependencies/vswhere/vswhere.exe"}" :done echo DONE! diff --git a/Dependencies.lua b/Dependencies.lua index fe75c3f4..2694fe35 100644 --- a/Dependencies.lua +++ b/Dependencies.lua @@ -16,4 +16,5 @@ IncludeDir["SDL"] = "%{wks.location}\\Dependencies\\SDL" IncludeDir["VULKAN"] = "$(VULKAN_SDK)" IncludeDir["dotnet"] = "%{wks.location}\\Dependencies\\dotnet" IncludeDir["tinyddsloader"] = "%{wks.location}\\Dependencies\\tinyddsloader" -IncludeDir["fmod"] = "%{wks.location}\\Dependencies\\fmod" \ No newline at end of file +IncludeDir["fmod"] = "%{wks.location}\\Dependencies\\fmod" +IncludeDir["vswhere"] = "%{wks.location}\\Dependencies\\vswhere" \ No newline at end of file diff --git a/SHADE_Engine/premake5.lua b/SHADE_Engine/premake5.lua index 7fb7291d..17ca5be8 100644 --- a/SHADE_Engine/premake5.lua +++ b/SHADE_Engine/premake5.lua @@ -124,7 +124,8 @@ project "SHADE_Engine" "xcopy /r /y /q \"%{IncludeDir.ModelCompiler}\\bin\\Debug\\ModelCompiler.exe\" \"$(OutDir)\"", "xcopy /r /y /q \"%{IncludeDir.FontCompiler}\\bin\\Debug\\FontCompiler.exe\" \"$(OutDir)\"", "xcopy /r /y /q \"%{IncludeDir.fmod}\\lib\\fmodL.dll\" \"$(OutDir)\"", - "xcopy /r /y /q \"%{IncludeDir.fmod}\\lib\\fmodstudioL.dll\" \"$(OutDir)\"" + "xcopy /r /y /q \"%{IncludeDir.fmod}\\lib\\fmodstudioL.dll\" \"$(OutDir)\"", + "xcopy /r /y /q \"%{IncludeDir.vswhere}\\vswhere.exe\" \"$(OutDir)\"" } filter "configurations:Release" @@ -134,7 +135,8 @@ project "SHADE_Engine" "xcopy /r /y /q \"%{IncludeDir.ModelCompiler}\\bin\\Release\\ModelCompiler.exe\" \"$(OutDir)\"", "xcopy /r /y /q \"%{IncludeDir.FontCompiler}\\bin\\Release\\FontCompiler.exe\" \"$(OutDir)\"", "xcopy /r /y /q \"%{IncludeDir.fmod}\\lib\\fmod.dll\" \"$(OutDir)\"", - "xcopy /r /y /q \"%{IncludeDir.fmod}\\lib\\fmodstudio.dll\" \"$(OutDir)\"" + "xcopy /r /y /q \"%{IncludeDir.fmod}\\lib\\fmodstudio.dll\" \"$(OutDir)\"", + "xcopy /r /y /q \"%{IncludeDir.vswhere}\\vswhere.exe\" \"$(OutDir)\"" } filter "configurations:Publish" diff --git a/SHADE_Engine/src/Scripting/SHScriptEngine.cpp b/SHADE_Engine/src/Scripting/SHScriptEngine.cpp index b17697ad..a8417117 100644 --- a/SHADE_Engine/src/Scripting/SHScriptEngine.cpp +++ b/SHADE_Engine/src/Scripting/SHScriptEngine.cpp @@ -28,9 +28,9 @@ of DigiPen Institute of Technology is prohibited. #include "Physics/System/SHPhysicsSystem.h" #include "Physics/SHPhysicsEvents.h" #include "Scene/SHSceneEvents.h" - #include "Assets/SHAssetMacros.h" #include "Tools/Utilities/SHExecUtilities.h" +#include "SHVSUtilities.h" namespace SHADE { @@ -313,11 +313,15 @@ namespace SHADE } // Open it - SHExecUtilties::ExecProcess - ( - L"C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\Common7\\IDE\\devenv.exe", - L"/Edit " + SHStringUtilities::StrToWstr(CSPROJ_PATH) - ); + try + { + SHVSUtilties::OpenFile(CSPROJ_PATH); + } + catch (std::exception& e) + { + SHLOG_ERROR("{}", e.what()); + SHLOG_ERROR("[SHScriptEngine] Failed to open project csproj file."); + } } /*-----------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Scripting/SHVSUtilities.cpp b/SHADE_Engine/src/Scripting/SHVSUtilities.cpp index f2fdc0a0..3a53fc79 100644 --- a/SHADE_Engine/src/Scripting/SHVSUtilities.cpp +++ b/SHADE_Engine/src/Scripting/SHVSUtilities.cpp @@ -13,14 +13,51 @@ of DigiPen Institute of Technology is prohibited. #include // Primary Header #include "SHVSUtilities.h" +// Project Headers +#include "Tools/Utilities/SHExecUtilities.h" namespace SHADE { + /*-----------------------------------------------------------------------------------*/ + /* Static Data Members */ + /*-----------------------------------------------------------------------------------*/ + std::filesystem::path SHVSUtilties::devEnvPath; + + /*-----------------------------------------------------------------------------------*/ + /* Usage Functions */ + /*-----------------------------------------------------------------------------------*/ + void SHVSUtilties::OpenFile(const std::filesystem::path& path) + try + { + if (devEnvPath.empty()) + { + devEnvPath = SHVSUtilties::getDevEnvPath(); + } + SHExecUtilties::ExecProcess(devEnvPath.generic_wstring(), L"/Edit " + path.generic_wstring()); + } + catch (std::exception& e) + { + SHLOG_ERROR("{}", e.what()); + SHLOG_ERROR("[SHVSUtilities] Failed to launch Visual Studio."); + } + /*-----------------------------------------------------------------------------------*/ /* Helper Functions */ /*-----------------------------------------------------------------------------------*/ std::filesystem::path SHVSUtilties::getDevEnvPath() { - return {}; +#ifdef _PUBLISH + return {}; // Don't do anything if it's a published build +#else + static constexpr int EXCESS_CHARS_COUNT = 2; + + const auto RESULT = SHExecUtilties::ExecBlockingPowerShellCommand(L"./vswhere -version \"[15.0,19.0]\" -requires Microsoft.NetCore.Component.DevelopmentTools -find Common7\\\\IDE\\\\devenv.exe | Select-Object -last 1", true, true); + if (RESULT.StdOutput.size() < EXCESS_CHARS_COUNT) + { + SHLOG_ERROR("[SHVSUtilities] Unable to get path to Visual Studio installation. SHVSUtilities will not work."); + return {}; + } + return RESULT.StdOutput.substr(0, RESULT.StdOutput.size() - EXCESS_CHARS_COUNT); +#endif } } diff --git a/SHADE_Engine/src/Scripting/SHVSUtilities.h b/SHADE_Engine/src/Scripting/SHVSUtilities.h index 52a83e22..5613779b 100644 --- a/SHADE_Engine/src/Scripting/SHVSUtilities.h +++ b/SHADE_Engine/src/Scripting/SHVSUtilities.h @@ -24,7 +24,14 @@ namespace SHADE class SH_API SHVSUtilties final { public: - + /*---------------------------------------------------------------------------------*/ + /* Usage Functions */ + /*---------------------------------------------------------------------------------*/ + /// + /// Opens the file at the specified path with a new instance of Visual Studio. + /// + /// Path to the file to open. + static void OpenFile(const std::filesystem::path& path); private: /*---------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Tools/Utilities/SHExecUtilities.cpp b/SHADE_Engine/src/Tools/Utilities/SHExecUtilities.cpp index f3f6334d..4a5a963c 100644 --- a/SHADE_Engine/src/Tools/Utilities/SHExecUtilities.cpp +++ b/SHADE_Engine/src/Tools/Utilities/SHExecUtilities.cpp @@ -15,6 +15,7 @@ of DigiPen Institute of Technology is prohibited. #include "SHExecUtilities.h" // Project Includes #include "SHStringUtilities.h" +#include "SHFileUtilties.h" namespace SHADE { @@ -104,6 +105,26 @@ namespace SHADE ); } + PROCESS_INFORMATION SHExecUtilties::ExecPowerShellCommand(const std::wstring& command) + { + return ExecProcess + ( + L"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe", + L"-Command \"& {" + command + L"} \"" + ); + } + + ExecResult SHExecUtilties::ExecBlockingPowerShellCommand(const std::wstring& command, bool output, bool errorOutput) + { + return ExecBlockingProcess + ( + L"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe", + L"-Command \"& { cd \"" + SHFileUtilities::GetExecDir().generic_wstring() + L"\";" + command + L"} \"", + output, + errorOutput + ); + } + PROCESS_INFORMATION SHExecUtilties::execProcess(const std::wstring& path, const std::wstring& args, HANDLE outputWritePipe, HANDLE errorOutputWritePipe) { STARTUPINFOW startInfo; @@ -183,7 +204,7 @@ namespace SHADE { std::array buffer{}; DWORD bytesRead = 0; - const auto RESULT = ReadFile(readPipe, buffer.data(), buffer.size(), &bytesRead, nullptr); // Blocking call here? + const auto RESULT = ReadFile(readPipe, buffer.data(), buffer.size(), &bytesRead, nullptr); if (!RESULT || bytesRead <= 0) break; output.insert(output.end(), buffer.data(), buffer.data() + bytesRead); diff --git a/SHADE_Engine/src/Tools/Utilities/SHExecUtilities.h b/SHADE_Engine/src/Tools/Utilities/SHExecUtilities.h index 9c5f38eb..94c853cd 100644 --- a/SHADE_Engine/src/Tools/Utilities/SHExecUtilities.h +++ b/SHADE_Engine/src/Tools/Utilities/SHExecUtilities.h @@ -55,7 +55,7 @@ namespace SHADE /// Arguments to pass to the process. /// If true, stdout will be routed to return. /// If true, outstderr will be routed to return. - /// Return value of the process. + /// Information about the process's execution. /// /// Thrown if failed to start the process. /// @@ -83,7 +83,7 @@ namespace SHADE /// Command to execute. /// If true, stdout will be routed to return. /// If true, outstderr will be routed to return. - /// Return value of the process. + /// Information about the process's execution. /// /// Thrown if failed to start the process. /// @@ -109,11 +109,13 @@ namespace SHADE /// executing. /// /// Command to execute. - /// Return value of the process. + /// If true, stdout will be routed to return. + /// If true, outstderr will be routed to return. + /// Information about the process's execution. /// /// Thrown if failed to start the process. /// - static DWORD ExecBlockingPowerShellCommand(const std::wstring& command); + static ExecResult ExecBlockingPowerShellCommand(const std::wstring& command, bool output, bool errorOutput); private: /*---------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Tools/Utilities/SHFileUtilties.cpp b/SHADE_Engine/src/Tools/Utilities/SHFileUtilties.cpp index 0e75b16a..63e7d26e 100644 --- a/SHADE_Engine/src/Tools/Utilities/SHFileUtilties.cpp +++ b/SHADE_Engine/src/Tools/Utilities/SHFileUtilties.cpp @@ -22,9 +22,21 @@ namespace SHADE { void SHFileUtilities::SetWorkDirToExecDir() { - TCHAR currentExecFilePath[MAX_PATH] = { '\0' }; - GetModuleFileName(nullptr, currentExecFilePath, MAX_PATH); - PathRemoveFileSpec(currentExecFilePath); - std::filesystem::current_path(currentExecFilePath); + std::filesystem::current_path(GetExecDir()); + } + + std::filesystem::path SHFileUtilities::GetExecDir() + { + TCHAR currentExecFilePath[MAX_PATH] = { '\0' }; + GetModuleFileName(nullptr, currentExecFilePath, MAX_PATH); + PathRemoveFileSpec(currentExecFilePath); + return std::filesystem::path(currentExecFilePath); + } + + std::filesystem::path SHFileUtilities::GetExecPath() + { + TCHAR currentExecFilePath[MAX_PATH] = { '\0' }; + GetModuleFileName(nullptr, currentExecFilePath, MAX_PATH); + return std::filesystem::path(currentExecFilePath); } } diff --git a/SHADE_Engine/src/Tools/Utilities/SHFileUtilties.h b/SHADE_Engine/src/Tools/Utilities/SHFileUtilties.h index b9ba164b..fdbee34e 100644 --- a/SHADE_Engine/src/Tools/Utilities/SHFileUtilties.h +++ b/SHADE_Engine/src/Tools/Utilities/SHFileUtilties.h @@ -15,24 +15,29 @@ of DigiPen Institute of Technology is prohibited. namespace SHADE { - /*!************************************************************************************ - - \class SHFileUtilities - - \brief - Static class that contains functions for working with files and directories. - - **************************************************************************************/ + /// + /// Static class that contains functions for working with files and directories. + /// class SH_API SHFileUtilities { public: - /*!********************************************************************************** - - \brief - Sets the application's current working directory to the application executable's - directory. - - ************************************************************************************/ + /*---------------------------------------------------------------------------------*/ + /* Executable Directory Functions */ + /*---------------------------------------------------------------------------------*/ + /// + /// Sets the application's current working directory to the application executable's + /// directory. + /// static void SetWorkDirToExecDir(); + /// + /// Retrieves the file path to the executable's directory. + /// + /// File path to the executable's directory. + static std::filesystem::path GetExecDir(); + /// + /// Retrieves the file path to the executable. + /// + /// File path to the executable. + static std::filesystem::path GetExecPath(); }; }