Skip to content

Commit

Permalink
Try to fix windows docker up
Browse files Browse the repository at this point in the history
  • Loading branch information
kekefreedog committed Apr 18, 2024
1 parent fb9f7e0 commit 1f39034
Show file tree
Hide file tree
Showing 6 changed files with 294 additions and 41 deletions.
2 changes: 1 addition & 1 deletion resources/Docker/docker/variables.env.hbs
Original file line number Diff line number Diff line change
@@ -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}}

Expand Down
34 changes: 34 additions & 0 deletions src/Cli/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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){

Expand Down
15 changes: 13 additions & 2 deletions src/Library/Cli/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
*/
namespace CrazyPHP\Library\Cli;

use CrazyPHP\Library\System\Os;
use CrazyPHP\Library\System\Terminal;

/** Dependances
*
*/
Expand Down Expand Up @@ -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){
Expand Down
137 changes: 113 additions & 24 deletions src/Library/File/Docker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = "";
Expand Down Expand Up @@ -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 = "";
Expand Down
65 changes: 65 additions & 0 deletions src/Library/System/Terminal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php declare(strict_types=1);
/**
* System
*
* Usefull class for manipulate system
*
* PHP version 8.1.2
*
* @package kzarshenas/crazyphp
* @author kekefreedog <[email protected]>
* @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 <[email protected]>
* @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;
}

}
Loading

0 comments on commit 1f39034

Please sign in to comment.