From 1f3903451041647475c7a8ff4f288029b5b4fdc8 Mon Sep 17 00:00:00 2001 From: kekefreedog <70959083+kekefreedog@users.noreply.github.com> Date: Fri, 19 Apr 2024 00:36:40 +0200 Subject: [PATCH] Try to fix windows docker up --- resources/Docker/docker/variables.env.hbs | 2 +- src/Cli/Form.php | 34 ++++++ src/Library/Cli/Command.php | 15 ++- src/Library/File/Docker.php | 137 ++++++++++++++++++---- src/Library/System/Terminal.php | 65 ++++++++++ src/Model/Docker/Install.php | 82 ++++++++++--- 6 files changed, 294 insertions(+), 41 deletions(-) create mode 100644 src/Library/System/Terminal.php diff --git a/resources/Docker/docker/variables.env.hbs b/resources/Docker/docker/variables.env.hbs index 4bf8a44..c99a876 100644 --- a/resources/Docker/docker/variables.env.hbs +++ b/resources/Docker/docker/variables.env.hbs @@ -1,6 +1,6 @@ # Crazy env {{#inArray configuration "http"}} -CRAZY_HTTP_PORT=80{{/inArray}}{{#inArray configuration "https-online"}} +CRAZY_HTTP_PORT={{#if http_port}}{{http_port}}{{else}}80{{/if}}{{/inArray}}{{#inArray configuration "https-online"}} CRAZY_HTTPS_PORT=443{{/inArray}}{{#inArray configuration "https-local"}} CRAZY_HTTPS_PORT=443{{/inArray}} diff --git a/src/Cli/Form.php b/src/Cli/Form.php index 7f37e48..47cf2bf 100644 --- a/src/Cli/Form.php +++ b/src/Cli/Form.php @@ -316,6 +316,40 @@ public function __construct(array $valueCollection = []){ $climate->password($question) : $climate->input($question); + # Accept + if(!empty($value["accept"] ?? [])){ + + # Accept + $accept = null; + + # Check if aray + if(is_array($accept)) + + $input->accept($accept, true); + + else + # Check if is string and match with pattern + if(is_string($value["accept"]) && preg_match('/^range\((\d+),(\d+)\)$/', $value["accept"], $matches) && ($value['type'] ?? false) == "INT"){ + + # Get value 1 + $value1 = $matches[1] ?? 0; + + # Get value 2 + $value2 = $matches[2] ?? 0; + + # Get accept + $accept = range($value1 < $value2 ? $value1 : $value2, $value2 > $value1 ? $value2 : $value1); + + # Check accept + if(!empty($accept)) + + # Set accept + $input->accept($accept, true); + + } + + } + # Check if default if($value['default'] ?? false){ diff --git a/src/Library/Cli/Command.php b/src/Library/Cli/Command.php index e8656ce..4903687 100644 --- a/src/Library/Cli/Command.php +++ b/src/Library/Cli/Command.php @@ -12,6 +12,9 @@ */ namespace CrazyPHP\Library\Cli; +use CrazyPHP\Library\System\Os; +use CrazyPHP\Library\System\Terminal; + /** Dependances * */ @@ -86,8 +89,16 @@ public static function exec(string $command = "", string $argument = "", bool $l # Return return null; - # Prepare command - $command = $command.($argument ? " $argument" : ""); + // Prepare command depending on the shell environment + if(Os::isWindows() && Terminal::isWindowsPowerShell()){ + + # PowerShell requires different syntax, especially if arguments are involved + $command = 'powershell.exe -NonInteractive -NoProfile -Command "& {' . $command . ($argument ? " " . escapeshellarg($argument) : "") . '}"'; + + }else + + # Prepare command + $command = $command.($argument ? " $argument" : ""); # Check if live result enable if($liveResult){ diff --git a/src/Library/File/Docker.php b/src/Library/File/Docker.php index 929f27d..96e6579 100644 --- a/src/Library/File/Docker.php +++ b/src/Library/File/Docker.php @@ -22,6 +22,7 @@ use CrazyPHP\Library\File\Yaml; use CrazyPHP\Library\File\File; use CrazyPHP\Library\System\Os; +use CrazyPHP\Library\System\Terminal; /** * Docker @@ -57,23 +58,67 @@ public static function up(bool $detach = true, string $loadEnvFile = self::ENV_F # Check os if(Os::isWindows()){ - # Set pwd - $preCommand .= "set PWD=%CD% & "; + # Check power shell + if(Terminal::isWindowsPowerShell()){ - # Set env - $envFileContent = parse_ini_file(File::path($loadEnvFile)); + # Set pwd + $preCommand .= '$env:PWD = Get-Location;'; - # Check env file - if(!empty($envFileContent)) + # Set env + $envFileContent = parse_ini_file(File::path($loadEnvFile)); - # Iteration - foreach($envFileContent as $k => $value) + # Check env file + if(!empty($envFileContent)) - # Check if is int or string - if($k && (is_string($value) || is_int($value))) + # Iteration + foreach($envFileContent as $k => $value) + + # Check if is string + if($k && is_numeric($value)) + + # Append in pre command + $preCommand .= ' $env:'."$k = \"".str_replace('"', '\\"', $value).'"; '; + + else + # check if number is int + if($k && is_string($value)) + + # Append in pre command + $preCommand .= ' $env:'."$k = \"".str_replace('"', '\\"', $value).'"; '; + + }else + # check if class terminal + if(Terminal::isWindowsCommandPrompt()){ + + # Set pwd + $preCommand .= "set PWD=%CD% | "; + + # Set env + $envFileContent = parse_ini_file(File::path($loadEnvFile)); + + # Check env file + if(!empty($envFileContent)) + + # Iteration + foreach($envFileContent as $k => $value) + + # Check if is string + if($k && is_numeric($value)) + + # Append in pre command + $preCommand .= "set $k=$value | "; + + else + # check if number is int + if($k && is_string($value)) + + # Append in pre command + $preCommand .= 'set '.$k.'="'.str_replace('"', '\\"', $value).'" | '; + + }else + + throw new CrazyException("Windows terminal used unknown, use power shell instead."); - # Append in pre command - $preCommand .= "set $k=".(is_int($value) ? $value : '"'.str_replace('"', '\\"', $value).'"')." & "; # Clean env file $loadEnvFile = ""; @@ -109,23 +154,67 @@ public static function down(string $loadEnvFile = self::ENV_FILE) { # Check os if(Os::isWindows()){ - # Set pwd - $preCommand .= "set PWD=%CD% & "; + # Check power shell + if(Terminal::isWindowsPowerShell()){ + + # Set pwd + $preCommand .= '$env:PWD = Get-Location;'; + + # Set env + $envFileContent = parse_ini_file(File::path($loadEnvFile)); + + # Check env file + if(!empty($envFileContent)) + + # Iteration + foreach($envFileContent as $k => $value) + + # Check if is string + if($k && is_numeric($value)) + + # Append in pre command + $preCommand .= ' $env:'."$k = \"".str_replace('"', '\\"', $value).'"; '; + + else + # check if number is int + if($k && is_string($value)) + + # Append in pre command + $preCommand .= ' $env:'."$k = \"".str_replace('"', '\\"', $value).'"; '; + + }else + # check if class terminal + if(Terminal::isWindowsCommandPrompt()){ + + # Set pwd + $preCommand .= "set PWD=%CD% | "; + + # Set env + $envFileContent = parse_ini_file(File::path($loadEnvFile)); + + # Check env file + if(!empty($envFileContent)) + + # Iteration + foreach($envFileContent as $k => $value) + + # Check if is string + if($k && is_numeric($value)) + + # Append in pre command + $preCommand .= "set $k=$value | "; - # Set env - $envFileContent = parse_ini_file(File::path($loadEnvFile)); + else + # check if number is int + if($k && is_string($value)) - # Check env file - if(!empty($envFileContent)) + # Append in pre command + $preCommand .= 'set '.$k.'="'.str_replace('"', '\\"', $value).'" | '; - # Iteration - foreach($envFileContent as $k => $value) + }else - # Check if is int or string - if($k && (is_string($value) || is_int($value))) + throw new CrazyException("Windows terminal used unknown, use power shell instead."); - # Append in pre command - $preCommand .= "set $k=".(is_int($value) ? $value : '"'.str_replace('"', '\\"', $value).'"')." & "; # Clean env file $loadEnvFile = ""; diff --git a/src/Library/System/Terminal.php b/src/Library/System/Terminal.php new file mode 100644 index 0000000..1f63a31 --- /dev/null +++ b/src/Library/System/Terminal.php @@ -0,0 +1,65 @@ + + * @copyright 2022-2024 Kévin Zarshenas + */ +namespace CrazyPHP\Library\System; + +/** + * Dependances + */ +use CrazyPHP\Exception\CrazyException; + +/** + * Terminal + * + * Methods for check terminal + * + * @package kzarshenas/crazyphp + * @author kekefreedog + * @copyright 2022-2024 Kévin Zarshenas + */ +class Terminal { + + /** Public Static Methods | Windows + ****************************************************** + */ + + /** + * Is Windows Power Shell + * + * Checks if the script is running in Windows PowerShell. + * + * @return bool Returns true if the script is running in PowerShell, false otherwise. + */ + public static function isWindowsPowerShell():bool { + + # Check for a common PowerShell environment variable + return getenv('PSModulePath') !== false; + + } + + /** + * Is Windows Command Prompt + * + * Checks if the script is running in Windows Command Prompt. + * + * @return bool Returns true if the script is running in Command Prompt, false otherwise. + */ + public static function isWindowsCommandPrompt() { + + # Check for the ComSpec environment variable typical in CMD that includes 'cmd.exe' + $comSpec = getenv('ComSpec'); + + # Return check + return $comSpec !== false && strpos(strtolower($comSpec), 'cmd.exe') !== false; + } + +} \ No newline at end of file diff --git a/src/Model/Docker/Install.php b/src/Model/Docker/Install.php index a8fed8b..b100adf 100644 --- a/src/Model/Docker/Install.php +++ b/src/Model/Docker/Install.php @@ -25,8 +25,10 @@ use CrazyPHP\Library\Cli\Command; use CrazyPHP\Library\System\Os; use CrazyPHP\Library\File\File; +use CrazyPHP\Library\System\Terminal; use League\CLImate\CLImate; use CrazyPHP\Model\Env; +use Exception; /** * Create new Application @@ -49,7 +51,7 @@ class Install implements CrazyCommand { "name" => "configuration", "description" => "Type of configuration to set up on your crazy docker", "type" => "ARRAY", - "default" => "https", + "default" => "http", "multiple" => true, "select" => [ "http" => "Http", @@ -57,6 +59,14 @@ class Install implements CrazyCommand { "https-local" => "Https (Local using Mkcert)" ], ], + # Port + [ + "name" => "http_port", + "description" => "Port of http by default", + "type" => "INT", + "default" => 80, + # "accept" => "range(0,10000)", + ] ]; /** Private Parameters @@ -508,23 +518,67 @@ public function runDockerComposeBuild():self { # Check os if(Os::isWindows()){ - # Set pwd - $preCommand .= "set PWD=%CD% & "; + # Check power shell + if(Terminal::isWindowsPowerShell()){ + + # Set pwd + $preCommand .= '$env:PWD = Get-Location;'; + + # Set env + $envFileContent = parse_ini_file(File::path($envFile)); + + # Check env file + if(!empty($envFileContent)) + + # Iteration + foreach($envFileContent as $k => $value) + + # Check if is string + if($k && is_numeric($value)) + + # Append in pre command + $preCommand .= ' $env:'."$k = \"".str_replace('"', '\\"', $value).'"; '; + + else + # check if number is int + if($k && is_string($value)) + + # Append in pre command + $preCommand .= ' $env:'."$k = \"".str_replace('"', '\\"', $value).'"; '; + + }else + # check if class terminal + if(Terminal::isWindowsCommandPrompt()){ + + # Set pwd + $preCommand .= "set PWD=%CD% | "; + + # Set env + $envFileContent = parse_ini_file(File::path($envFile)); + + # Check env file + if(!empty($envFileContent)) + + # Iteration + foreach($envFileContent as $k => $value) + + # Check if is string + if($k && is_numeric($value)) + + # Append in pre command + $preCommand .= "set $k=$value | "; - # Set env - $envFileContent = parse_ini_file(File::path($envFile)); + else + # check if number is int + if($k && is_string($value)) - # Check env file - if(!empty($envFileContent)) + # Append in pre command + $preCommand .= 'set '.$k.'="'.str_replace('"', '\\"', $value).'" | '; - # Iteration - foreach($envFileContent as $k => $value) + }else - # Check if is int or string - if($k && (is_string($value) || is_int($value))) + throw new CrazyException("Windows terminal used unknown, use power shell instead."); - # Append in pre command - $preCommand .= "set $k=".(is_int($value) ? $value : '"'.str_replace('"', '\\"', $value).'"')." & "; # Clean env file $envFile = ""; @@ -532,7 +586,7 @@ public function runDockerComposeBuild():self { } # Exec command - Command::exec($preCommand."docker-compose", (($envFile && File::exists($envFile)) ? " --env-file '".$envFile."' " : "")."build", true); + Command::exec($preCommand."docker-compose", (($envFile && File::exists($envFile) && !Os::isWindows()) ? " --env-file '".$envFile."' " : "")."build", true); # Return self return $this;