| 423 | } |
| 424 | |
| 425 | static int openExternal( const std::string& defShell, const std::string& cmd, |
| 426 | const std::string& scriptsPath, const std::string& workingDir ) { |
| 427 | // This is an utility bat script based in the Geany utility script called "geany-run-helper" |
| 428 | static const std::string_view RUN_HELPER = |
| 429 | R"shellscript(REM USAGE: ecode-run-helper DIRECTORY AUTOCLOSE COMMAND... |
| 430 | |
| 431 | REM unnecessary, but we get the directory |
| 432 | cd %1 |
| 433 | shift |
| 434 | REM save autoclose option and remove it |
| 435 | set autoclose=%1 |
| 436 | shift |
| 437 | |
| 438 | REM spawn the child |
| 439 | REM it's tricky because shift doesn't affect %*, so hack it out |
| 440 | REM https://en.wikibooks.org/wiki/Windows_Batch_Scripting#Command-line_arguments |
| 441 | set SPAWN= |
| 442 | :argloop |
| 443 | if -%1-==-- goto argloop_end |
| 444 | set SPAWN=%SPAWN% %1 |
| 445 | shift |
| 446 | goto argloop |
| 447 | :argloop_end |
| 448 | %SPAWN% |
| 449 | |
| 450 | REM show the result |
| 451 | echo: |
| 452 | echo: |
| 453 | echo:------------------ |
| 454 | echo:(program exited with code: %ERRORLEVEL%) |
| 455 | echo: |
| 456 | |
| 457 | REM and if wanted, wait on the user |
| 458 | if not %autoclose%==1 pause |
| 459 | )shellscript"; |
| 460 | if ( !cmd.empty() && !scriptsPath.empty() ) { |
| 461 | std::string runHelperPath = scriptsPath + "ecode-run-helper.bat"; |
| 462 | bool canContinue = true; |
| 463 | if ( !FileSystem::fileExists( runHelperPath ) && |
| 464 | !FileSystem::fileWrite( runHelperPath, RUN_HELPER ) ) { |
| 465 | canContinue = false; |
| 466 | } |
| 467 | if ( canContinue ) { |
| 468 | std::string cmdDir = String::trim( FileSystem::fileRemoveFileName( cmd ) ); |
| 469 | if ( cmdDir.empty() ) |
| 470 | cmdDir = workingDir; |
| 471 | std::string cmdFile = String::trim( FileSystem::fileNameFromPath( cmd ) ); |
| 472 | auto fcmd = "cmd.exe /q /c " + quoteString( "\"" + runHelperPath + "\" \"" + cmdDir + |
| 473 | "\" 0 \"" + cmdFile + "\"" ); |
| 474 | Log::info( "Running: %s", fcmd ); |
| 475 | return Sys::execute( fcmd, workingDir ); |
| 476 | } else { |
| 477 | Log::info( "Couldn't write runHelperPath %s", runHelperPath ); |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | std::vector<std::string> options; |
| 482 | options.reserve( 3 ); |