From 0532524a50ebc062ec6cb1b666b290510e53a80e Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Tue, 28 Apr 2020 11:30:16 +0300 Subject: [PATCH] console command extension --- .../Espo/Core/Console/Commands/Extension.php | 195 ++++++++++++++++++ 1 file changed, 195 insertions(+) create mode 100644 application/Espo/Core/Console/Commands/Extension.php diff --git a/application/Espo/Core/Console/Commands/Extension.php b/application/Espo/Core/Console/Commands/Extension.php new file mode 100644 index 0000000000..b31b1ed5f7 --- /dev/null +++ b/application/Espo/Core/Console/Commands/Extension.php @@ -0,0 +1,195 @@ +out("Can't uninstall. Specify --name=\"Extension Name\".\n"); + return; + } + $params = []; + if ($id) { + $params['id'] = $id; + } else { + $params['name'] = $name; + } + $params['delete'] = !in_array('k', $flagList); + + $this->runUninstall($params); + return; + } else { + // install + + $file = $options['file'] ?? null; + if (!$file) { + $this->out("Can't install. Specify --file=\"path/to/package.zip\".\n"); + return; + } + + $this->runInstall($file); + return; + } + } + + protected function runInstall(string $file) + { + $manager = $this->createExtensionManager(); + + if (!file_exists($file)) { + $this->out("File does not exist.\n"); + return; + } + + $fileData = file_get_contents($file); + $fileData = 'data:application/zip;base64,' . base64_encode($fileData); + + try { + $id = $manager->upload($fileData); + } catch (\Throwable $e) { + $this->out($e->getMessage() . "\n"); + return; + } + + $manifest = $manager->getManifestById($id); + + $name = $manifest['name'] ?? null; + $version = $manifest['version'] ?? null; + + if (!$name) { + $this->out("Can't install. Bad manifest.\n"); + return; + } + + $this->out("Installing... Do not close the terminal. This may take a while..."); + + try { + $manager->install(['id' => $id]); + } catch (\Throwable $e) { + $this->out("\n"); + $this->out($e->getMessage() . "\n"); + return; + } + + $this->out("\n"); + $this->out("Extension '{$name}' version {$version} is installed.\nExtension ID: '{$id}'.\n"); + } + + protected function runUninstall(array $params) + { + $id = $params['id'] ?? null; + + if ($id) { + $record = $this->getEntityManager()->getRepository('Extension')->where([ + 'id' => $id, + 'isInstalled' => true, + ])->findOne(); + + if (!$record) { + $this->out("Extension with ID '{$id}' is not installed.\n"); + return; + } + + $name = $record->get('name'); + } else { + $name = $params['name'] ?? null; + if (!$name) { + $this->out("Can't uninstall. No --name or --id specified.\n"); + return; + } + + $record = $this->getEntityManager()->getRepository('Extension')->where([ + 'name' => $name, + 'isInstalled' => true, + ])->findOne(); + + if (!$record) { + $this->out("Extension '{$name}' is not installed.\n"); + return; + } + + $id = $record->id; + } + + $manager = $this->createExtensionManager(); + + $this->out("Uninstalling... Do not close the terminal. This may take a while..."); + + try { + $manager->uninstall(['id' => $id]); + } catch (\Throwable $e) { + $this->out("\n"); + $this->out($e->getMessage() . "\n"); + return; + } + + $this->out("\n"); + + if ($params['delete'] ?? false) { + try { + $manager->delete(['id' => $id]); + } catch (\Throwable $e) { + $this->out($e->getMessage() . "\n"); + $this->out("Extension '{$name}' is uninstalled but could not be deleted.\n"); + return; + } + + $this->out("Extension '{$name}' is uninstalled and deleted.\n"); + } else { + $this->out("Extension '{$name}' is uninstalled.\n"); + } + } + + protected function createExtensionManager() + { + return new \Espo\Core\ExtensionManager($this->getContainer()); + } + + protected function getEntityManager() + { + return $this->getContainer()->get('entityManager'); + } + + protected function out(string $string) + { + fwrite(\STDOUT, $string); + } +}