From dc3cd66c84457e55c78a4d50684f69f4dc15dca3 Mon Sep 17 00:00:00 2001 From: Matthieu Marcillaud Date: Wed, 8 Mar 2023 12:17:15 +0100 Subject: [PATCH] feat: option --prod pour copier le phar dans public_html --- .gitignore | 2 ++ compile | 25 +++++++++++++++++++++++++ compiler/Build.php | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) diff --git a/.gitignore b/.gitignore index 2625d89..8040bf9 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,8 @@ /loader/version.php /loader/vendor/ /build/ +/public_html/spip_loader.php +/public_html/version /vendor/ /composer.lock /.php_cs.cache diff --git a/compile b/compile index 4d94525..cc8f312 100755 --- a/compile +++ b/compile @@ -1,12 +1,33 @@ #!/usr/bin/env php false, + 'prod' => false, +]; + +unset($argv[0]); +foreach ($argv as $option) { + try { + match($option) { + '--prod' => $config['prod'] = true, + '--debug' => $config['debug'] = true, + default => throw new \RuntimeException(sprintf('Compile: unknown \'%s\'', $option)) + }; + } catch (\Exception $e) { + (new CLImate())->error($e->getMessage()); + exit(1); + } +} + $build = new Build( sourceDirectory: __DIR__ . '/loader', buildDirectory: __DIR__ . '/build', @@ -17,4 +38,8 @@ $build->prepare(); $build->run(); $build->showResults(); +if ($config['prod']) { + $build->showCopyTo(__DIR__ . '/public_html'); +} + exit(0); diff --git a/compiler/Build.php b/compiler/Build.php index 8943a18..651fcb9 100644 --- a/compiler/Build.php +++ b/compiler/Build.php @@ -64,6 +64,40 @@ class Build { $climate->out(''); } + public function showCopyTo(string $prodDirectory) { + $climate = new CLImate; + if ($this->copyTo($prodDirectory)) { + $climate->info('> Fichiers copiés en prod'); + } else { + $climate->error('! Échec de la recopie en prod'); + } + $climate->out(); + } + + public function copyTo(string $prodDirectory) { + if (!is_dir($prodDirectory)) { + throw new \Exception(sprintf('Directory %s needs to exists', $prodDirectory)); + } + $files = [$this->pharFilename, 'version']; + foreach ($files as $file) { + if (!file_exists($this->buildDirectory . '/' . $file)) { + throw new \Exception(sprintf('File %s is not compiled ?', $this->buildDirectory . '/' . $file)); + } + } + foreach ($files as $file) { + if (file_exists($prodDirectory . '/' . $file)) { + unlink($prodDirectory . '/' . $file); + } + copy($this->buildDirectory . '/' . $file, $prodDirectory . '/' . $file); + } + foreach ($files as $file) { + if (!file_exists($prodDirectory . '/' . $file)) { + return false; + } + } + return true; + } + public function getStats(): array { return $this->stats; }