3 namespace Cli\Commands;
5 use Cli\Services\ComposerLocator;
6 use Cli\Services\EnvironmentLoader;
7 use Cli\Services\ProgramRunner;
8 use Cli\Services\RequirementsValidator;
9 use Symfony\Component\Console\Command\Command;
10 use Symfony\Component\Console\Input\InputArgument;
11 use Symfony\Component\Console\Input\InputInterface;
12 use Symfony\Component\Console\Output\OutputInterface;
14 class InitCommand extends Command
16 protected function configure(): void
18 $this->setName('init');
19 $this->setDescription('Initialise a new BookStack install. Does not configure the webserver or database.');
20 $this->addArgument('target-directory', InputArgument::OPTIONAL, 'The directory to create the BookStack install within. Must be empty.', '');
24 * @throws CommandError
26 protected function execute(InputInterface $input, OutputInterface $output): int
28 $output->writeln("<info>Checking system requirements...</info>");
29 RequirementsValidator::validate();
31 $suggestedOutPath = $input->getArgument('target-directory');
33 $output->writeln("<info>Locating and checking install directory...</info>");
34 $installDir = $this->getInstallDir($suggestedOutPath);
35 $this->ensureInstallDirEmptyAndWritable($installDir);
37 $output->writeln("<info>Cloning down BookStack project to install directory...</info>");
38 $this->cloneBookStackViaGit($installDir);
40 $output->writeln("<info>Checking composer exists...</info>");
41 $composerLocator = new ComposerLocator($installDir);
42 $composer = $composerLocator->getProgram();
43 if (!$composer->isFound()) {
44 $output->writeln("<info>Composer does not exist, downloading a local copy...</info>");
45 $composerLocator->download();
48 $output->writeln("<info>Installing application dependencies using composer...</info>");
49 $this->installComposerDependencies($composer, $installDir);
51 $output->writeln("<info>Creating .env file from .env.example...</info>");
52 copy($installDir . DIRECTORY_SEPARATOR . '.env.example', $installDir . DIRECTORY_SEPARATOR . '.env');
55 $output->writeln("<info>Generating app key...</info>");
56 $this->generateAppKey($installDir);
59 $output->writeln("<info>A BookStack install has been initialized at: {$installDir}\n</info>");
60 $output->writeln("<info>You will still need to:</info>");
61 $output->writeln("<info>- Update the .env file in the install with correct URL, database and email details.</info>");
62 $output->writeln("<info>- Run 'php artisan migrate' to set-up the database.</info>");
63 $output->writeln("<info>- Configure your webserver for use with BookStack.</info>");
64 $output->writeln("<info>- Ensure the required directories (storage/ bootstrap/cache public/uploads) are web-server writable.</info>");
66 return Command::SUCCESS;
69 protected function generateAppKey(string $installDir): void
71 $errors = (new ProgramRunner('php', '/usr/bin/php'))
74 ->withEnvironment(EnvironmentLoader::load($installDir))
75 ->runCapturingAllOutput([
76 $installDir . DIRECTORY_SEPARATOR . 'artisan',
77 'key:generate', '--force', '-n', '-q'
81 throw new CommandError("Failed 'php artisan key:generate' with errors:\n" . $errors);
86 * Run composer install to download PHP dependencies.
87 * @throws CommandError
89 protected function installComposerDependencies(ProgramRunner $composer, string $installDir): void
91 $errors = $composer->runCapturingStdErr([
93 '--no-dev', '-n', '-q', '--no-progress',
98 throw new CommandError("Failed composer install with errors:\n" . $errors);
103 * Clone a new instance of BookStack to the given install folder.
104 * @throws CommandError
106 protected function cloneBookStackViaGit(string $installDir): void
108 $errors = (new ProgramRunner('git', '/usr/bin/git'))
110 ->withIdleTimeout(15)
111 ->runCapturingStdErr([
113 '--branch', 'release',
115 'https://github.com/BookStackApp/BookStack.git',
120 throw new CommandError("Failed git clone with errors:\n" . $errors);
125 * Ensure that the installation directory is completely empty to avoid potential conflicts or issues.
126 * @throws CommandError
128 protected function ensureInstallDirEmptyAndWritable(string $installDir): void
130 $contents = array_diff(scandir($installDir), ['..', '.']);
131 if (count($contents) > 0) {
132 throw new CommandError("Expected install directory to be empty but existing files found in [{$installDir}] target location.");
135 if (!is_writable($installDir)) {
136 throw new CommandError("Target install directory [{$installDir}] is not writable.");
141 * Build a full path to the intended location for the BookStack install.
142 * @throws CommandError
144 protected function getInstallDir(string $suggestedDir): string
149 if (is_file($suggestedDir)) {
150 throw new CommandError("Was provided [{$suggestedDir}] as an install path but existing file provided.");
151 } else if (is_dir($suggestedDir)) {
152 $dir = realpath($suggestedDir);
153 } else if (is_dir(dirname($suggestedDir))) {
154 $created = mkdir($suggestedDir);
156 throw new CommandError("Could not create directory [{$suggestedDir}] for install.");
158 $dir = realpath($suggestedDir);
160 throw new CommandError("Could not resolve provided [{$suggestedDir}] path to an existing folder.");