Skip to content

Commit

Permalink
Fix Docker and Npm issues on OS Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
kekefreedog committed Apr 16, 2024
1 parent 395a4d8 commit fb9f7e0
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 9 deletions.
2 changes: 2 additions & 0 deletions docs/NewProject.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ composer require kzarshenas/crazyphp
- [ ] **(Optional)** Link to local CrazyPHP repo (for development & debug), update the `composer.json` with the content below :

> If you are using OS Windows, make sure to execute `npm i` into the CrazyPHP folder
```json {"id":"01HQDDF3C07BXVQ0PTJS0562AD"}
{
"require": {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"version": "0.1.2",
"description": "My crazy framework for creating ultra-fast webapps.",
"main": "src/Front/index.ts",
"types": "src/Front/Types/index.d.ts",
"types": "./src/Front/Types/index.d.ts",
"files": [
"src/Front/"
"./src/Front/"
],
"repository": {
"type": "git",
Expand Down
6 changes: 3 additions & 3 deletions resources/Docker/docker-compose.yml.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ services:
- './docker/mkcert/localhost.pem:/etc/nginx/certs/localhost.pem'
- './docker/mkcert/localhost-key.pem:/etc/nginx/certs/localhost-key.pem'{{/inArray}}
ports:{{#is (length configuration) "0"}} []{{/is}}{{#inArray configuration "http"}}
- '${CRAZY_HTTP_PORT}:80'{{/inArray}}{{#inArray configuration "https-online"}}
- '${CRAZY_HTTPS_PORT}:443'{{/inArray}}{{#inArray configuration "https-local"}}
- '${CRAZY_HTTPS_PORT}:443'{{/inArray}}
- '${CRAZY_HTTP_PORT:-80}:80'{{/inArray}}{{#inArray configuration "https-online"}}
- '${CRAZY_HTTPS_PORT:-443}:443'{{/inArray}}{{#inArray configuration "https-local"}}
- '${CRAZY_HTTPS_PORT:-443}:443'{{/inArray}}
networks:
- backend

Expand Down
63 changes: 61 additions & 2 deletions src/Library/File/Docker.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use CrazyPHP\Library\Cli\Command;
use CrazyPHP\Library\File\Yaml;
use CrazyPHP\Library\File\File;
use CrazyPHP\Library\System\Os;

/**
* Docker
Expand Down Expand Up @@ -50,11 +51,40 @@ public static function up(bool $detach = true, string $loadEnvFile = self::ENV_F
# Set result
$result = "";

# Set pre command
$preCommand = "";

# Check os
if(Os::isWindows()){

# 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 int or string
if($k && (is_string($value) || is_int($value)))

# Append in pre command
$preCommand .= "set $k=".(is_int($value) ? $value : '"'.str_replace('"', '\\"', $value).'"')." & ";

# Clean env file
$loadEnvFile = "";

}

# Prepare command shell
$command = self::DOCKER_COMPOSE_COMMAND.($loadEnvFile ? " --env-file '".$loadEnvFile."'" : "")." up".($detach ? " -d --no-color" : "");

# Exec command
$result = Command::exec($command);
$result = Command::exec($preCommand.$command);

# Return result
return $result;
Expand All @@ -73,11 +103,40 @@ public static function down(string $loadEnvFile = self::ENV_FILE) {
# Set result
$result = "";

# Set pre command
$preCommand = "";

# Check os
if(Os::isWindows()){

# 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 int or string
if($k && (is_string($value) || is_int($value)))

# Append in pre command
$preCommand .= "set $k=".(is_int($value) ? $value : '"'.str_replace('"', '\\"', $value).'"')." & ";

# Clean env file
$loadEnvFile = "";

}

# Prepare command shell
$command = self::DOCKER_COMPOSE_COMMAND.($loadEnvFile ? " --env-file '".$loadEnvFile."'" : "")." down";

# Exec command
exec($command, $empty, $result);
exec($preCommand.$command, $empty, $result);

# Return result
return $result;
Expand Down
2 changes: 2 additions & 0 deletions src/Library/File/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,8 @@ public static function downloadToTmp(string $url = "", string $tempFileName = "t
"png" => "image/png",
# WebP
"webp" => "image/webp",
# Env
"env" => "text/plain",
# TBC ...
];

Expand Down
36 changes: 34 additions & 2 deletions src/Model/Docker/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -502,8 +502,37 @@ public function runDockerComposeBuild():self {
# Set env file
$envFile = Docker::ENV_FILE;

# Set pre command
$preCommand = "";

# Check os
if(Os::isWindows()){

# 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 int or string
if($k && (is_string($value) || is_int($value)))

# Append in pre command
$preCommand .= "set $k=".(is_int($value) ? $value : '"'.str_replace('"', '\\"', $value).'"')." & ";

# Clean env file
$envFile = "";

}

# Exec command
Command::exec("docker-compose", (($envFile && File::exists($envFile)) ? " --env-file '".$envFile."' " : "")."build", true);
Command::exec($preCommand."docker-compose", (($envFile && File::exists($envFile)) ? " --env-file '".$envFile."' " : "")."build", true);

# Return self
return $this;
Expand Down Expand Up @@ -559,7 +588,10 @@ private function _getData(bool $onlyInput = false):array {
if(Os::isWindows()){

# Update _config.App.root
$value = $config['_config']['App']['root'];
$value = $result['_config']['App']['root'];

# Resolve path
$value = File::resolve($value);

# Change \ or \\ by /
$value = str_replace(["\\", "\\\\"], "/", $value);
Expand Down

0 comments on commit fb9f7e0

Please sign in to comment.