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

Development dax #2941

Closed
wants to merge 21 commits into from
Closed
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
192 changes: 149 additions & 43 deletions launcher/src/CmderLauncher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ bool FileExists(const wchar_t * filePath)
return false;
}

void StartCmder(std::wstring path = L"", bool is_single_mode = false, std::wstring taskName = L"", std::wstring title = L"", std::wstring iconPath = L"", std::wstring cfgRoot = L"", bool use_user_cfg = true, std::wstring conemu_args = L"")
void StartCmder(std::wstring path = L"", bool is_single_mode = false, std::wstring taskName = L"", std::wstring title = L"", std::wstring iconPath = L"", std::wstring cfgRoot = L"", bool use_user_cfg = true, std::wstring conemu_args = L"", bool admin = false)
{
#if USE_TASKBAR_API
wchar_t appId[MAX_PATH] = { 0 };
Expand All @@ -131,8 +131,17 @@ void StartCmder(std::wstring path = L"", bool is_single_mode = false, std::wstr
wchar_t windowsTerminalDir[MAX_PATH] = { 0 };
wchar_t conEmuDir[MAX_PATH] = { 0 };
wchar_t winDir[MAX_PATH] = { 0 };
wchar_t emulatorPath[MAX_PATH] = { 0 };

wchar_t vendorDir[MAX_PATH] = { 0 };
wchar_t initCmd[MAX_PATH] = { 0 };
wchar_t initPowerShell[MAX_PATH] = { 0 };
wchar_t initBash[MAX_PATH] = { 0 };
wchar_t initMintty[MAX_PATH] = { 0 };
wchar_t vendoredGit[MAX_PATH] = { 0 };
wchar_t amdx64Git[MAX_PATH] = { 0 };
wchar_t x86Git[MAX_PATH] = { 0 };
wchar_t programFiles[MAX_PATH] = { 0 };
wchar_t programFilesX86[MAX_PATH] = { 0 };
wchar_t minTTYPath[MAX_PATH] = { 0 };

std::wstring cmderStart = path;
std::wstring cmderTask = taskName;
Expand Down Expand Up @@ -255,8 +264,9 @@ void StartCmder(std::wstring path = L"", bool is_single_mode = false, std::wstr
}
}

PathCombine(windowsTerminalDir, exeDir, L"vendor\\windows-terminal");
PathCombine(conEmuDir, exeDir, L"vendor\\conemu-maximus5");
PathCombine(vendorDir, exeDir, L"vendor");
PathCombine(windowsTerminalDir, vendorDir, L"windows-terminal");
PathCombine(conEmuDir, vendorDir, L"conemu-maximus5");
GetEnvironmentVariable(L"WINDIR", winDir, MAX_PATH);

if (PathFileExists(windowsTerminalDir))
Expand Down Expand Up @@ -317,7 +327,8 @@ void StartCmder(std::wstring path = L"", bool is_single_mode = false, std::wstr
{
if (!CopyFile(cpuCfgPath, cfgPath, FALSE))
{
if (PathFileExists(windowsTerminalDir)) {
if (PathFileExists(windowsTerminalDir))
{
MessageBox(NULL,
(GetLastError() == ERROR_ACCESS_DENIED)
? L"Failed to copy config/windows_terminal_%COMPUTERNAME%_settings.json file to vendor/windows-terminal/settings/settings.json! Access Denied."
Expand All @@ -343,7 +354,8 @@ void StartCmder(std::wstring path = L"", bool is_single_mode = false, std::wstr
{
if (!CopyFile(cfgPath, userCfgPath, FALSE))
{
if (PathFileExists(windowsTerminalDir)) {
if (PathFileExists(windowsTerminalDir))
{
MessageBox(NULL,
(GetLastError() == ERROR_ACCESS_DENIED)
? L"Failed to copy vendor/windows-terminal/settings/settings.json file to config/windows_terminal_settings.json! Access Denied."
Expand All @@ -364,7 +376,8 @@ void StartCmder(std::wstring path = L"", bool is_single_mode = false, std::wstr
{
if (!CopyFile(userCfgPath, cfgPath, FALSE))
{
if (PathFileExists(windowsTerminalDir)) {
if (PathFileExists(windowsTerminalDir))
{
MessageBox(NULL,
(GetLastError() == ERROR_ACCESS_DENIED)
? L"Failed to copy config/user_windows_terminal_settings.json file to vendor/windows-terminal/settings/settings.json! Access Denied."
Expand Down Expand Up @@ -392,7 +405,8 @@ void StartCmder(std::wstring path = L"", bool is_single_mode = false, std::wstr
{
if (!CopyFile(cfgPath, userCfgPath, FALSE))
{
if (PathFileExists(windowsTerminalDir)) {
if (PathFileExists(windowsTerminalDir))
{
MessageBox(NULL,
(GetLastError() == ERROR_ACCESS_DENIED)
? L"Failed to copy vendor/windows-terminal/settings/settings.json file to config/user_windows_terminal_settings.json! Access Denied."
Expand All @@ -412,7 +426,8 @@ void StartCmder(std::wstring path = L"", bool is_single_mode = false, std::wstr
{
if (!CopyFile(defaultCfgPath, cfgPath, FALSE))
{
if (PathFileExists(windowsTerminalDir)) {
if (PathFileExists(windowsTerminalDir))
{
MessageBox(NULL,
(GetLastError() == ERROR_ACCESS_DENIED)
? L"Failed to copy vendor/windows-terminal_default_settings_settings.json file to vendor/windows-terminal/settings/settings.json! Access Denied."
Expand All @@ -436,14 +451,15 @@ void StartCmder(std::wstring path = L"", bool is_single_mode = false, std::wstr
(GetLastError() == ERROR_ACCESS_DENIED)
? L"Failed to copy vendor/ConEmu.xml.default file to vendor/conemu-maximus5/ConEmu.xml! Access Denied."
: L"Failed to copy vendor/ConEmu.xml.default file to vendor/conemu-maximus5/ConEmu.xml!", MB_TITLE, MB_ICONSTOP);
exit(1);
exit(1);
}
}
else if (wcscmp(cfgPath, L"") == 0 && PathFileExists(cfgPath)) // vendor/conemu-maximus5/ConEmu.xml exists, copy vendor/conemu-maximus5/ConEmu.xml to config/user_conemu.xml
{
if (!CopyFile(cfgPath, userCfgPath, FALSE))
{
if (PathFileExists(windowsTerminalDir)) {
if (PathFileExists(windowsTerminalDir))
{
MessageBox(NULL,
(GetLastError() == ERROR_ACCESS_DENIED)
? L"Failed to copy vendor/windows-terminal/settings/settings.json file to config/user_windows_terminal_settings_settings.json! Access Denied."
Expand All @@ -464,9 +480,10 @@ void StartCmder(std::wstring path = L"", bool is_single_mode = false, std::wstr
}
else if (wcscmp(defaultCfgPath, L"") == 0) // '/c [path]' was specified and 'vendor/ConEmu.xml.default' config exists, copy Cmder 'vendor/ConEmu.xml.default' file to '[user specified path]/config/user_ConEmu.xml'.
{
if ( ! CopyFile(defaultCfgPath, userCfgPath, FALSE))
if (!CopyFile(defaultCfgPath, userCfgPath, FALSE))
{
if (PathFileExists(windowsTerminalDir)) {
if (PathFileExists(windowsTerminalDir))
{
MessageBox(NULL,
(GetLastError() == ERROR_ACCESS_DENIED)
? L"Failed to copy vendor/windows-terminal_default_settings_settings.json file to [user specified path]/config/user_windows_terminal_settings.json! Access Denied."
Expand All @@ -485,22 +502,50 @@ void StartCmder(std::wstring path = L"", bool is_single_mode = false, std::wstr
PathCombine(userConEmuCfgPath, userConfigDirPath, L"user-ConEmu.xml");
}

GetEnvironmentVariable(L"ProgramFiles", programFiles, MAX_PATH);
GetEnvironmentVariable(L"ProgramFiles(x86)", programFilesX86, MAX_PATH);

PathCombine(vendoredGit, vendorDir, L"git-for-windows");
PathCombine(amdx64Git, programFiles, L"Git");
PathCombine(x86Git, programFilesX86, L"Git");

SYSTEM_INFO sysInfo;
GetNativeSystemInfo(&sysInfo);
if (PathFileExists(windowsTerminalDir)) {
if (PathFileExists(windowsTerminalDir))
{
PathCombine(terminalPath, exeDir, L"vendor\\windows-terminal\\WindowsTerminal.exe");
}
else if (PathFileExists(conEmuDir))
{
swprintf_s(args, L"%s /Icon \"%s\"", args, icoPath);
swprintf_s(args, L"%s /title \"%s\"", args, cmderTitle.c_str());
PathCombine(terminalPath, exeDir, L"vendor\\conemu-maximus5\\ConEmu64.exe");
}
else
{
PathCombine(terminalPath, winDir, L"system32\\cmd.exe");
}

if (!PathFileExists(windowsTerminalDir)) {
swprintf_s(args, L"%s /Icon \"%s\"", args, icoPath);
if (streqi(cmderTask.c_str(), L"powershell"))
{
PathCombine(terminalPath, winDir, L"System32\\WindowsPowerShell\\v1.0\\powershell.exe");
}
/*
else if (streqi(cmderTask.c_str(), L"mintty"))
{
if (PathFileExists(vendoredGit))
{
PathCombine(terminalPath, vendoredGit, L"git-bash.exe");
}
else if (PathFileExists(amdx64Git))
{
PathCombine(terminalPath, amdx64Git, L"git-bash.exe");
}
else if (PathFileExists(x86Git))
{
PathCombine(terminalPath, x86Git, L"git-bash.exe");
}
}
*/
}

if (!streqi(cmderStart.c_str(), L""))
Expand All @@ -521,13 +566,6 @@ void StartCmder(std::wstring path = L"", bool is_single_mode = false, std::wstr
}
}

if (!streqi(cmderTitle.c_str(), L""))
{
if (!PathFileExists(windowsTerminalDir)) {
swprintf_s(args, L"%s /title \"%s\"", args, cmderTitle.c_str());
}
}

if (cfgRoot.length() != 0)
{
if (!PathFileExists(windowsTerminalDir)) {
Expand All @@ -543,6 +581,11 @@ void StartCmder(std::wstring path = L"", bool is_single_mode = false, std::wstr
// The `/run` arg and its value MUST be the last arg of ConEmu
// see : https://conemu.github.io/en/ConEmuArgs.html
// > This must be the last used switch (excepting -new_console and -cur_console)
PathCombine(initCmd, vendorDir, L"init.bat");
PathCombine(initPowerShell, vendorDir, L"profile.ps1");
PathCombine(initBash, vendorDir, L"start_git_bash.cmd");
PathCombine(initMintty, vendorDir, L"start_git_mintty.cmd");

if (!streqi(cmderTask.c_str(), L""))
{
if (PathFileExists(windowsTerminalDir)) {
Expand All @@ -554,7 +597,22 @@ void StartCmder(std::wstring path = L"", bool is_single_mode = false, std::wstr
}
else
{
swprintf_s(args, L"%s %s", args, cmderTask.c_str());
if (streqi(cmderTask.c_str(), L"powershell"))
{
swprintf_s(args, L"%s -ExecutionPolicy Bypass -NoLogo -NoProfile -NoExit -Command \"Invoke-Expression 'Import-Module ''%s'''\"", args, initPowerShell);
}
else if (streqi(cmderTask.c_str(), L"bash"))
{
swprintf_s(args, L"%s /c \"%s\"", args, initBash);
}
else if (streqi(cmderTask.c_str(), L"mintty"))
{
swprintf_s(args, L"%s /c \"%s\"", args, initMintty);
}
else if (streqi(cmderTask.c_str(), L"cmder"))
{
swprintf_s(args, L"%s /k \"%s\"", args, initCmd);
}
}
}

Expand All @@ -565,6 +623,27 @@ void StartCmder(std::wstring path = L"", bool is_single_mode = false, std::wstr
SetEnvironmentVariable(L"CMDER_USER_BIN", userBinDirPath);
}

// Try to find m'intty.exe' so ConEmu can launch using %MINTTY_EXE% in external Git for Cmder Mini.
// See: https://github.com/Maximus5/ConEmu/issues/2559 for why this is commented.

/*
if (PathFileExists(vendoredGit))
{
PathCombine(minTTYPath, vendoredGit, L"usr\\bin\\mintty.exe");
SetEnvironmentVariable(L"MINTTY_EXE", minTTYPath);
}
else if (PathFileExists(amdx64Git))
{
PathCombine(minTTYPath, amdx64Git, L"usr\\bin\\mintty.exe");
SetEnvironmentVariable(L"MINTTY_EXE", minTTYPath);
}
else if (PathFileExists(x86Git))
{
PathCombine(minTTYPath, x86Git, L"usr\\bin\\mintty.exe");
SetEnvironmentVariable(L"MINTTY_EXE", minTTYPath);
}
*/

// Ensure EnvironmentVariables are propagated.

STARTUPINFO si = { 0 };
Expand All @@ -575,13 +654,42 @@ void StartCmder(std::wstring path = L"", bool is_single_mode = false, std::wstr
si.dwFlags = STARTF_TITLEISAPPID;
#endif
PROCESS_INFORMATION pi;

// MessageBox(NULL, terminalPath, _T("Error"), MB_OK);
// MessageBox(NULL, args, _T("Error"), MB_OK);
// Let's try to rerun as Administrator
SHELLEXECUTEINFO sei = { sizeof(sei) };
sei.fMask = SEE_MASK_NOASYNC | SEE_MASK_NOCLOSEPROCESS;
sei.lpVerb = L"runas";
sei.lpFile = terminalPath;
sei.lpParameters = args;
sei.nShow = SW_SHOWNORMAL;

if (admin && ShellExecuteEx(&sei))
{
if (!sei.hProcess)
{
Sleep(500);
_ASSERTE(sei.hProcess != nullptr);
}

if (!CreateProcess(terminalPath, args, NULL, NULL, false, 0, NULL, NULL, &si, &pi))
if (sei.hProcess)
{
WaitForSingleObject(sei.hProcess, INFINITE);
}

// int nZone = 0;
// if (!HasZoneIdentifier(lsFile, nZone)
// || (nZone != 0 /*LocalComputer*/))
// {
// // Assuming that elevated copy has fixed all zone problems
// break;
// }
}
else if (!CreateProcess(terminalPath, args, NULL, NULL, false, 0, NULL, NULL, &si, &pi))
{
if (PathFileExists(windowsTerminalDir)) {
if (PathFileExists(windowsTerminalDir))
{
MessageBox(NULL, _T("Unable to create the Windows Terminal process!"), _T("Error"), MB_OK);
}
else if (PathFileExists(conEmuDir))
Expand Down Expand Up @@ -709,6 +817,7 @@ struct cmderOptions
std::wstring cmderIcon = L"";
std::wstring cmderRegScope = L"USER";
std::wstring cmderTerminalArgs = L"";
bool cmderAdmin = false;
bool cmderSingle = false;
bool cmderUserCfg = true;
bool registerApp = false;
Expand All @@ -726,15 +835,13 @@ cmderOptions GetOption()
wchar_t conEmuDir[MAX_PATH] = { 0 };
wchar_t vendorDir[MAX_PATH] = { 0 };
wchar_t exeDir[MAX_PATH] = { 0 };
wchar_t cmdInit[MAX_PATH] = { 0 };

GetModuleFileName(NULL, exeDir, sizeof(exeDir));
PathRemoveFileSpec(exeDir);

PathCombine(vendorDir, exeDir, L"vendor");
PathCombine(windowsTerminalDir, vendorDir, L"windows-terminal");
PathCombine(conEmuDir, vendorDir, L"ConEmu-Maximus5");
PathCombine(cmdInit, vendorDir, L"init.bat");

szArgList = CommandLineToArgvW(GetCommandLine(), &argCount);

Expand Down Expand Up @@ -799,6 +906,10 @@ cmderOptions GetOption()
{
cmderOptions.cmderUserCfg = false;
}
else if (_wcsicmp(L"/a", szArgList[i]) == 0 && !PathFileExists(windowsTerminalDir) && !PathFileExists(conEmuDir))
{
cmderOptions.cmderAdmin = true;
}
else if (_wcsicmp(L"/register", szArgList[i]) == 0)
{
cmderOptions.registerApp = true;
Expand Down Expand Up @@ -865,14 +976,11 @@ cmderOptions GetOption()
cmderOptions.error = true;
}
}

}

if (!PathFileExists(windowsTerminalDir) && !PathFileExists(conEmuDir))
if (!PathFileExists(windowsTerminalDir) && !PathFileExists(conEmuDir) && streqi(cmderOptions.cmderTask.c_str(), L""))
{
cmderOptions.cmderTask = L"/k \"";
cmderOptions.cmderTask += cmdInit;
cmderOptions.cmderTask += L"\"";
cmderOptions.cmderTask = L"cmder";
}

if (cmderOptions.error == true)
Expand Down Expand Up @@ -906,13 +1014,12 @@ int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,

GetModuleFileName(NULL, exeDir, sizeof(exeDir));
PathRemoveFileSpec(exeDir);

PathCombine(windowsTerminalDir, exeDir, L"vendor\\windows-terminal");
PathCombine(windowsTerminalDir, exeDir, L"vendor\\windows-terminal");

if (cmderOptions.registerApp == true)
{
if (PathFileExists(windowsTerminalDir))
{
if (PathFileExists(windowsTerminalDir))
{
RegisterShellMenu(cmderOptions.cmderRegScope, SHELL_MENU_REGISTRY_PATH_BACKGROUND, cmderOptions.cmderCfgRoot, cmderOptions.cmderSingle);
RegisterShellMenu(cmderOptions.cmderRegScope, SHELL_MENU_REGISTRY_PATH_LISTITEM, cmderOptions.cmderCfgRoot, cmderOptions.cmderSingle);
RegisterShellMenu(cmderOptions.cmderRegScope, SHELL_MENU_REGISTRY_DRIVE_PATH_BACKGROUND, cmderOptions.cmderCfgRoot, cmderOptions.cmderSingle);
Expand All @@ -939,8 +1046,7 @@ int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
}
else
{
StartCmder(cmderOptions.cmderStart, cmderOptions.cmderSingle, cmderOptions.cmderTask, cmderOptions.cmderTitle, cmderOptions.cmderIcon, cmderOptions.cmderCfgRoot, cmderOptions.cmderUserCfg, cmderOptions.cmderTerminalArgs);
}
StartCmder(cmderOptions.cmderStart, cmderOptions.cmderSingle, cmderOptions.cmderTask, cmderOptions.cmderTitle, cmderOptions.cmderIcon, cmderOptions.cmderCfgRoot, cmderOptions.cmderUserCfg, cmderOptions.cmderTerminalArgs, cmderOptions.cmderAdmin); }

return 0;
}
}
2 changes: 1 addition & 1 deletion launcher/src/strings.rc2
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ STRINGTABLE
{
IDS_TITLE "Cmder Launcher"

IDS_SWITCHES L"Valid options:\n\n /c [CMDER User Root Path]\n /task [Windows Terminal Profile/ConEmu Task Name]\n /icon [CMDER Icon Path] - ConEmu ONLY!\n [/start [Start in Path] | [Start in Path]]\n /single - ConEmu ONLY!\n /m\n -- [ConEmu/Windows Terminal extra arguments]\n\nNote: '-- [...]' must be the last argument!\n\nor, either:\n /register [USER | ALL]\n /unregister [USER | ALL]"
IDS_SWITCHES L"Valid options:\n\n /a Admin - Native Terminals ONLY!\n /c [CMDER User Root Path]\n /task [Windows Terminal Profile/ConEmu Task Name]\n /icon [CMDER Icon Path] - ConEmu ONLY!\n [/start [Start in Path] | [Start in Path]]\n /single - ConEmu ONLY!\n /m\n -- [ConEmu/Windows Terminal extra arguments]\n\nNote: '-- [...]' must be the last argument!\n\nor, either:\n /register [USER | ALL]\n /unregister [USER | ALL]"
}

/////////////////////////////////////////////////////////////////////////////
Loading
Loading