From 9a3b5e78547e44407fe009cfd64d8a990510568e Mon Sep 17 00:00:00 2001 From: flydev Date: Sun, 8 Sep 2024 00:50:12 +0200 Subject: [PATCH 1/3] feat: Allow `new` command in non-empty dirs #14 --- src/Commands/Common/NewCommand.php | 6 ++++-- src/Helpers/Downloader.php | 20 +++++++++++++------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/Commands/Common/NewCommand.php b/src/Commands/Common/NewCommand.php index 97dfc3a..1f1b046 100644 --- a/src/Commands/Common/NewCommand.php +++ b/src/Commands/Common/NewCommand.php @@ -66,7 +66,7 @@ class NewCommand extends PWConnector { 'userpass' => '', 'userpass_confirm' => '', 'useremail' => '', - 'color' => 'classic', + 'color' => 'classic' ); /** @@ -82,6 +82,7 @@ protected function configure() { ->setName('new') ->setDescription('Creates a new ProcessWire project') ->addArgument('directory', InputArgument::OPTIONAL, 'Directory where the new project will be created') + ->addOption('force', null, InputOption::VALUE_NONE, 'Force installation in an non empty directory') ->addOption('dbUser', null, InputOption::VALUE_REQUIRED, 'Database user') ->addOption('dbPass', null, InputOption::VALUE_OPTIONAL, 'Database password') ->addOption('dbName', null, InputOption::VALUE_REQUIRED, 'Database name') @@ -410,9 +411,10 @@ private function download($branch) { * @throws \RuntimeException if the downloaded archive could not be extracted */ private function extract() { + $forceInstall = $this->input->getOption('force'); $this->tools->writeBlockBasic('Preparing project...'); $cfp = $this->src ? $this->src : $this->compressedFilePath; - $this->downloader->extract($cfp, $this->projectDir, $this->getName()); + $this->downloader->extract($cfp, $this->projectDir, $this->getName(), $forceInstall); return $this; } diff --git a/src/Helpers/Downloader.php b/src/Helpers/Downloader.php index b6fb957..a1c485f 100644 --- a/src/Helpers/Downloader.php +++ b/src/Helpers/Downloader.php @@ -154,7 +154,7 @@ public function download($uri, $prefix = 'pw') { * @param string $to * @param string $name */ - public function extract($from, $to, $name = '') { + public function extract($from, $to, $name = '', $force = false) { //var_dump($from, $to, $name);exit; $source = $name ? 'ProcessWire' : 'the module'; @@ -164,12 +164,18 @@ public function extract($from, $to, $name = '') { } // check for empty directory - $filesInsideDir = new \FilesystemIterator($to, \FilesystemIterator::SKIP_DOTS); - if (iterator_count($filesInsideDir) > 1) { - throw new \RuntimeException(sprintf( - "ProcessWire can't be installed because the target folder `%s` is not empty.\n" . - "Use an empty directory or provide an argument where the new project will be created like `wire-cli new `", - $to)); + if (!$force) { + $filesInsideDir = new \FilesystemIterator($to, \FilesystemIterator::SKIP_DOTS); + if (iterator_count($filesInsideDir) > 1) { + throw new \RuntimeException(sprintf( + "ProcessWire can't be installed because the target folder `%s` is not empty.\n" . + "Use an empty directory or provide an argument where the new project will be created like `wire-cli new `.\n" . + "Use option --force to proceed the installation in a non empty directory.", + $to)); + } + } + else { + $this->tools->writeWarning("ProcessWire will be installed in a non-empty dir (--force found on command-line).\n"); } try { From 258c69cf16e232ec6c9557aea7f14c8bcb4f0b62 Mon Sep 17 00:00:00 2001 From: flydev Date: Sun, 8 Sep 2024 00:51:09 +0200 Subject: [PATCH 2/3] feat(helpers): Added `writeWarning` helper --- src/Helpers/WsTools.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Helpers/WsTools.php b/src/Helpers/WsTools.php index 989d828..b32b380 100644 --- a/src/Helpers/WsTools.php +++ b/src/Helpers/WsTools.php @@ -23,7 +23,7 @@ protected $helper; protected $input; - protected static $types = array('error', 'success', 'info', 'comment', 'link', 'header', 'mark'); + protected static $types = array('error', 'warning', 'success', 'info', 'comment', 'link', 'header', 'mark'); /** * Construct WsTools @@ -51,6 +51,9 @@ public function __construct(OutputInterface $output) { $style = new OutputFormatterStyle('blue', 'white', array('reverse')); $output->getFormatter()->setStyle('mark', $style); + + $style = new OutputFormatterStyle('yellow', null, array('bold')); + $output->getFormatter()->setStyle('warning', $style); } /** @@ -167,6 +170,17 @@ public function writeErrorAndExit($string) { public function writeComment($string, $write = true) { return $this->write($string, 'comment', $write); } + + /** + * Simple method for coloring warning output + * + * @param string $string + * @param boolean $write + * @return tinted string + */ + public function writeWarning($string, $write = true) { + return $this->write($string, 'warning', $write); + } /** * Simple method for coloring info output From 013397dbef673560eb3039c6117987b7d7128aac Mon Sep 17 00:00:00 2001 From: flydev Date: Sun, 8 Sep 2024 00:52:28 +0200 Subject: [PATCH 3/3] refactor: Fixed some deprecated notice #8 --- src/Commands/Common/NewCommand.php | 4 ++++ src/Commands/Common/ServeCommand.php | 3 +++ src/Helpers/Downloader.php | 1 + src/Helpers/Installer.php | 1 + 4 files changed, 9 insertions(+) diff --git a/src/Commands/Common/NewCommand.php b/src/Commands/Common/NewCommand.php index 1f1b046..34eec92 100644 --- a/src/Commands/Common/NewCommand.php +++ b/src/Commands/Common/NewCommand.php @@ -41,9 +41,13 @@ class NewCommand extends PWConnector { private $projectName; private $projectDir; private $version; + private $helper; private $compressedFilePath; private $requirementsErrors = array(); + private $src; private $installer; + private $verbose; + protected $downloader; protected $tools; /** diff --git a/src/Commands/Common/ServeCommand.php b/src/Commands/Common/ServeCommand.php index f22040d..5844b57 100644 --- a/src/Commands/Common/ServeCommand.php +++ b/src/Commands/Common/ServeCommand.php @@ -19,6 +19,9 @@ */ class ServeCommand extends PwConnector { + private $helper = null; + + /** * Configures the current command. */ diff --git a/src/Helpers/Downloader.php b/src/Helpers/Downloader.php index a1c485f..6d09f96 100644 --- a/src/Helpers/Downloader.php +++ b/src/Helpers/Downloader.php @@ -25,6 +25,7 @@ class Downloader { private $version; private $output; private $tools; + private $compressedFilePath; /** * Construct Downloader diff --git a/src/Helpers/Installer.php b/src/Helpers/Installer.php index 67dd793..7472e9b 100644 --- a/src/Helpers/Installer.php +++ b/src/Helpers/Installer.php @@ -31,6 +31,7 @@ class Installer { protected $fs = null; + protected $v = true; /** * Whether or not we force installed files to be copied.