]> BookStack Code Mirror - system-cli/blobdiff - scripts/Commands/UpdateCommand.php
Added central way to resolve app path, improved ouput formatting
[system-cli] / scripts / Commands / UpdateCommand.php
index 573b04cfe1f61151ee286e410e31306f4b760b0a..e9dfa60301120c961a4c404ea71338454f4a9399 100644 (file)
@@ -2,27 +2,23 @@
 
 namespace Cli\Commands;
 
+use Cli\Services\AppLocator;
 use Cli\Services\ComposerLocator;
 use Cli\Services\EnvironmentLoader;
 use Cli\Services\ProgramRunner;
 use Cli\Services\RequirementsValidator;
 use Symfony\Component\Console\Command\Command;
 use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\InputOption;
 use Symfony\Component\Console\Output\OutputInterface;
 
 class UpdateCommand extends Command
 {
-
-    public function __construct(
-        protected string $appDir
-    ) {
-        parent::__construct();
-    }
-
     protected function configure(): void
     {
         $this->setName('update');
         $this->setDescription('Update an existing BookStack instance.');
+        $this->addOption('app-directory', null, InputOption::VALUE_OPTIONAL, 'BookStack install directory to update', '');
     }
 
     /**
@@ -30,11 +26,12 @@ class UpdateCommand extends Command
      */
     protected function execute(InputInterface $input, OutputInterface $output): int
     {
+        $appDir = AppLocator::require($input->getOption('app-directory'));
         $output->writeln("<info>Checking system requirements...</info>");
         RequirementsValidator::validate();
 
         $output->writeln("<info>Checking composer exists...</info>");
-        $composerLocator = new ComposerLocator($this->appDir);
+        $composerLocator = new ComposerLocator($appDir);
         $composer = $composerLocator->getProgram();
         if (!$composer->isFound()) {
             $output->writeln("<info>Composer does not exist, downloading a local copy...</info>");
@@ -42,18 +39,18 @@ class UpdateCommand extends Command
         }
 
         $output->writeln("<info>Fetching latest code via Git...</info>");
-        $this->updateCodeUsingGit();
+        $this->updateCodeUsingGit($appDir);
 
         $output->writeln("<info>Installing PHP dependencies via composer...</info>");
-        $this->installComposerDependencies($composer);
+        $this->installComposerDependencies($composer, $appDir);
 
         $output->writeln("<info>Running database migrations...</info>");
-        $this->runArtisanCommand(['migrate', '--force']);
+        $this->runArtisanCommand(['migrate', '--force'], $appDir);
 
         $output->writeln("<info>Clearing app caches...</info>");
-        $this->runArtisanCommand(['cache:clear']);
-        $this->runArtisanCommand(['config:clear']);
-        $this->runArtisanCommand(['view:clear']);
+        $this->runArtisanCommand(['cache:clear'], $appDir);
+        $this->runArtisanCommand(['config:clear'], $appDir);
+        $this->runArtisanCommand(['view:clear'], $appDir);
 
         return Command::SUCCESS;
     }
@@ -61,13 +58,13 @@ class UpdateCommand extends Command
     /**
      * @throws CommandError
      */
-    protected function updateCodeUsingGit(): void
+    protected function updateCodeUsingGit(string $appDir): void
     {
         $errors = (new ProgramRunner('git', '/usr/bin/git'))
             ->withTimeout(240)
             ->withIdleTimeout(15)
             ->runCapturingStdErr([
-                '-C', $this->appDir,
+                '-C', $appDir,
                 'pull', '-q', 'origin', 'release',
             ]);
 
@@ -79,12 +76,12 @@ class UpdateCommand extends Command
     /**
      * @throws CommandError
      */
-    protected function installComposerDependencies(ProgramRunner $composer): void
+    protected function installComposerDependencies(ProgramRunner $composer, string $appDir): void
     {
         $errors = $composer->runCapturingStdErr([
             'install',
             '--no-dev', '-n', '-q', '--no-progress',
-            '-d', $this->appDir,
+            '-d', $appDir,
         ]);
 
         if ($errors) {
@@ -92,14 +89,14 @@ class UpdateCommand extends Command
         }
     }
 
-    protected function runArtisanCommand(array $commandArgs): void
+    protected function runArtisanCommand(array $commandArgs, string $appDir): void
     {
         $errors = (new ProgramRunner('php', '/usr/bin/php'))
             ->withTimeout(60)
             ->withIdleTimeout(5)
-            ->withEnvironment(EnvironmentLoader::load($this->appDir))
+            ->withEnvironment(EnvironmentLoader::load($appDir))
             ->runCapturingAllOutput([
-                $this->appDir . DIRECTORY_SEPARATOR . 'artisan',
+                $appDir . DIRECTORY_SEPARATOR . 'artisan',
                 '-n', '-q',
                 ...$commandArgs
             ]);