Skip to content
This repository has been archived by the owner on Apr 24, 2020. It is now read-only.

Commit

Permalink
improve module:upgrade command
Browse files Browse the repository at this point in the history
  • Loading branch information
Tabea David committed Feb 2, 2017
1 parent ada58a4 commit 72c196c
Show file tree
Hide file tree
Showing 8 changed files with 240 additions and 201 deletions.
8 changes: 4 additions & 4 deletions docs/commands/module.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ $ wireshell module:download FlagPages --github=marcus-herrmann/ProcessWire-FlagP
Disables and uninstalls one or more modules.
```sh
$ wireshell module:disable {class-name},{class-name}
$ wireshell module:disable {class-name} {class-name}
```
### Available options:
Expand All @@ -95,13 +95,13 @@ $ wireshell module:disable {class-name},{class-name}
Deinstall modules but keep files.
```sh
$ wireshell module:disable FlagPages,ImageExtra
$ wireshell module:disable FlagPages ImageExtra
```
Deinstall modules and remove files.
```sh
$ wireshell module:disable FlagPages,ImageExtra --rm
$ wireshell module:disable --rm FlagPages ImageExtra
```
---
Expand All @@ -111,7 +111,7 @@ $ wireshell module:disable FlagPages,ImageExtra --rm
Upgrades given module(s).
```sh
$ wireshell module:upgrade {class-name},{class-name}*
$ wireshell module:upgrade {class-name} {class-name}*
```
\* This argument is optional. If you want to check for module updates, just skip it.
Expand Down
145 changes: 78 additions & 67 deletions src/Commands/Module/ModuleDisableCommand.php
Original file line number Diff line number Diff line change
@@ -1,98 +1,109 @@
<?php namespace Wireshell\Commands\Module;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Wireshell\Helpers\PwConnector;
use Wireshell\Helpers\WsTools as Tools;

/**
* Class ModuleDisableCommand
*
* Disables provided module(s)
*
* @package Wireshell
* @author Marcus Herrmann
* @author Tabea David <info@justonestep.de>
*/
class ModuleDisableCommand extends PwConnector {

/**
* Configures the current command.
*/
protected function configure()
{
$this
->setName('module:disable')
->setDescription('Disable provided module(s)')
->addArgument('modules', InputOption::VALUE_REQUIRED,
'Provide one or more module class name, comma separated: Foo,Bar')
->addOption('rm', null, InputOption::VALUE_NONE, 'Remove module');
}

/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int|null|void
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
parent::bootstrapProcessWire($output);
/**
* Configures the current command.
*/
protected function configure() {
$this
->setName('module:disable')
->setDescription('Disable provided module(s)')
->addArgument('modules', InputArgument::IS_ARRAY, 'Module classname (separate multiple names with a space)')
->addOption('rm', null, InputOption::VALUE_NONE, 'Remove module');
}

$modules = explode(",", $input->getArgument('modules'));
$remove = $input->getOption('rm') === true ? true : false;
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int|null|void
*/
protected function execute(InputInterface $input, OutputInterface $output) {
parent::setOutput($output)::setInput($input)::bootstrapProcessWire();

foreach ($modules as $module) {
$this->checkIfModuleExists($module, $output, $remove);
$this->tools = new Tools($output);
$this->tools
->setInput($input)
->setHelper($this->getHelper('question'))
->writeBlockCommand($this->getName());

if (\ProcessWire\wire('modules')->uninstall($module)) {
$output->writeln("Module {$module} <comment>uninstalled</comment> successfully.");
}
$modules = $this->tools->ask(
$input->getArgument('modules'),
$this->getDefinition()->getArgument('modules')->getDescription(),
null,
false,
null,
'required'
);

// remove module
if ($remove === true && is_dir(\ProcessWire\wire('config')->paths->$module)) {
if ($this->recurseRmdir(\ProcessWire\wire('config')->paths->$module)) {
$output->writeln("Module {$module} was <comment>removed</comment> successfully.");
} else {
$output->writeln("Module {$module} could not be removed <fg=red>could not be removed</fg=red>.");
}
}
}
if (!is_array($modules)) $modules = explode(" ", $modules);
$remove = $input->getOption('rm') === true ? true : false;

}
foreach ($modules as $module) {
$this->checkIfModuleExists($module, $remove);

private function checkIfModuleExists($module, $output, $remove)
{
if (!is_dir(\ProcessWire\wire('config')->paths->siteModules . $module)) {
$output->writeln("<error>Module '{$module}' does not exist!</error>");
exit(1);
}
if (\ProcessWire\wire('modules')->uninstall($module)) {
$this->tools->writeSuccess("Module `{$module}` uninstalled successfully.");
}

if (!\ProcessWire\wire('modules')->getModule($module, array('noPermissionCheck' => true, 'noInit' => true)) && $remove === false) {
$output->writeln("<info>Module '{$module}' is not installed!</info>");
exit(1);
// remove module
if ($remove === true && is_dir(\ProcessWire\wire('config')->paths->$module)) {
$this->tools->nl();
if ($this->recurseRmdir(\ProcessWire\wire('config')->paths->$module)) {
$this->tools->writeInfo("Module `{$module}` was <comment>removed</comment> successfully.");
} else {
$this->tools->writeError("Module `{$module}` could not be removed <fg=red>could not be removed</fg=red>.");
}
}
}

}

private function checkIfModuleExists($module, $remove) {
if (!is_dir(\ProcessWire\wire('config')->paths->siteModules . $module)) {
$this->tools->writeError("Module `{$module}` does not exist.");
exit(1);
}

/**
* remove uninstalled module recursive
*
* @param string $dir
* @return boolean
*/
private function recurseRmdir($dir)
{
if (is_dir($dir)) {
chmod($dir, 0775);
$files = array_diff(scandir($dir), array('.', '..'));
foreach ($files as $file) {
(is_dir("$dir/$file")) ? $this->recurseRmdir("$dir/$file") : unlink("$dir/$file");
}
$removed = rmdir($dir);
} else {
$removed = false;
}
if (!\ProcessWire\wire('modules')->getModule($module, array('noPermissionCheck' => true, 'noInit' => true)) && $remove === false) {
$this->tools->writeError("Module `{$module}` is not installed.");
exit(1);
}
}

return $removed;
/**
* remove uninstalled module recursive
*
* @param string $dir
* @return boolean
*/
private function recurseRmdir($dir) {
if (is_dir($dir)) {
chmod($dir, 0775);
$files = array_diff(scandir($dir), array('.', '..'));
foreach ($files as $file) {
(is_dir("$dir/$file")) ? $this->recurseRmdir("$dir/$file") : unlink("$dir/$file");
}
$removed = rmdir($dir);
} else {
$removed = false;
}

return $removed;
}
}
10 changes: 8 additions & 2 deletions src/Commands/Module/ModuleEnableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* Enables provided module(s)
*
* @package Wireshell
* @author Tabea David <td@kf-interactive.com>
* @author Tabea David <info@justonestep.de>
*/
class ModuleEnableCommand extends PwModuleTools {

Expand Down Expand Up @@ -51,6 +51,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
// if module doesn't exist, download the module
if (!$this->checkIfModuleExists($module)) {
$this->tools->writeComment("Cannot find '{$module}' locally, trying to download...");
$this->tools->nl();
$this->passOnToModuleDownloadCommand($module, $output, $input);
}

Expand All @@ -61,7 +62,12 @@ protected function execute(InputInterface $input, OutputInterface $output) {
}

// install module
if (\ProcessWire\wire('modules')->getModule($module, array('noPermissionCheck' => true, 'noInit' => true))) {
$options = array(
'noPermissionCheck' => true,
'noInit' => true
);

if (\ProcessWire\wire('modules')->getInstall($module, $options)) {
$this->tools->writeSuccess(" Module `{$module}` installed successfully.");
} else {
$this->tools->writeError(" Module `{$module}` does not exist.");
Expand Down

0 comments on commit 72c196c

Please sign in to comment.