From 50318a8fc5f0d7454c692c12b7c27b281b9d72d5 Mon Sep 17 00:00:00 2001 From: Kevin Papst Date: Mon, 18 Mar 2019 11:57:26 +0100 Subject: [PATCH] added command to create release packages (for FTP user) (#642) --- UPGRADING.md | 2 +- src/Command/CreateReleaseCommand.php | 154 ++++++++++++++++++++++ src/Command/ResetCommand.php | 5 + src/Constants.php | 4 +- tests/API/ConfigurationControllerTest.php | 6 - 5 files changed, 162 insertions(+), 9 deletions(-) create mode 100644 src/Command/CreateReleaseCommand.php diff --git a/UPGRADING.md b/UPGRADING.md index 1d42042f..18a45d68 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -27,7 +27,7 @@ Remember to execute the necessary timezone conversion script, if you haven't upd **BC BREAKS** - in an ongoing effort to simplify future installation and upgrade processes the `.env` variable `DATABASE_PREFIX` was removed. The table prefix is now hardcoded to `kimai2_`. If you used another prefix, you have to rename your tables manually -before starting the update process. +before starting the update process. You can also delete the row `DATABASE_PREFIX` from your `.env` file. - API: DateTime objects will be returned including timezone identifier (previously 2019-03-02 14:23 - now 2019-03-02T14:23:00+00:00) diff --git a/src/Command/CreateReleaseCommand.php b/src/Command/CreateReleaseCommand.php new file mode 100644 index 00000000..091b14cc --- /dev/null +++ b/src/Command/CreateReleaseCommand.php @@ -0,0 +1,154 @@ +rootDir = realpath($projectDirectory); + parent::__construct(); + } + + /** + * {@inheritdoc} + */ + protected function configure() + { + $this + ->setName('kimai:create-release') + ->setDescription('Create a pre-installed release package') + ->setHelp('This command will create a release package with pre-installed composer, SQLite database and user.') + ->addOption('directory', null, InputOption::VALUE_OPTIONAL, 'Directory where the release package will be stored', 'var/data/') + ->addOption('release', null, InputOption::VALUE_OPTIONAL, 'The version that should be zipped', Constants::VERSION) + ; + } + + /** + * @param InputInterface $input + * @param OutputInterface $output + * @return int|null + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $io = new SymfonyStyle($input, $output); + + if (getenv('APP_ENV') === 'prod') { + $io->error('kimai:create-release is not allowed in production'); + return -2; + } + + $directory = $input->getOption('directory'); + + if ($directory[0] === '/') { + $directory = realpath($directory); + } else { + $directory = realpath($this->rootDir . '/' . $directory); + } + + $tmpDir = $directory . '/' . uniqid('kimai_release_'); + + if (!is_dir($directory)) { + $io->error('Given directory is not existing: ' . $directory); + return 1; + } + + if (is_dir($directory) && !is_writable($directory)) { + $io->error('Cannot write in directory: ' . $directory); + return 1; + } + + $version = $input->getOption('release'); + + $io->success('Prepare new packages for Kimai ' . $version . ' in ' . $tmpDir); + + $gitCmd = sprintf(self::CLONE_CMD, $version); + $tar = 'kimai-release-' . $version; + $zip = 'kimai-release-' . $version; + + if ($version === Constants::VERSION && Constants::STATUS !== 'stable') { + $tar .= '_' . Constants::STATUS; + $zip .= '_' . Constants::STATUS; + } + + $tar .= '.tar.gz'; + $zip .= '.zip'; + + // this removes the current env settings, as they might differ from the release ones + // if we don't unset them, the .env file won't be read when executing bin/console commands + putenv('DATABASE_URL'); + putenv('APP_ENV'); + + $commands = [ + 'Clone repository' => $gitCmd . ' ' . $tmpDir, + 'Install composer dependencies' => 'cd ' . $tmpDir . ' && composer install --no-dev --optimize-autoloader', + 'Create .env file' => 'cd ' . $tmpDir . ' && cp .env.dist .env', + 'Create database' => 'cd ' . $tmpDir . ' && bin/console doctrine:database:create -n', + 'Create tables' => 'cd ' . $tmpDir . ' && bin/console doctrine:schema:create -n', + 'Add all migrations' => 'cd ' . $tmpDir . ' && bin/console doctrine:migrations:version --add --all -n', + ]; + + $filesToDelete = [ + '.git*', + 'var/cache/*', + 'var/data/kimai_test.sqlite', + 'var/log/*.log', + 'var/sessions/*', + ]; + + foreach($filesToDelete as $deleteMe) { + $commands['Delete ' . $deleteMe] = 'cd ' . $tmpDir . ' && rm -rf ' . $deleteMe; + } + + $commands = array_merge($commands, [ + 'Create tar' => 'cd ' . $tmpDir . ' && tar -czf ' . $directory. '/' . $tar . ' .', + 'Create zip' => 'cd ' . $tmpDir . ' && zip -r ' . $directory. '/' . $zip . ' .', + 'Remove tmp directory' => 'rm -rf ' . $tmpDir, + ]); + + $exitCode = 0; + foreach($commands as $title => $command) { + $io->success($title); + passthru($command, $exitCode); + if ($exitCode !== 0) { + $io->error('Failed with command: ' . $command); + return -1; + } + } + + $io->success( + 'New release packages available at: ' . PHP_EOL . + $directory . '/' . $tar. PHP_EOL . + $directory . '/' . $zip + ); + + return 0; + } +} diff --git a/src/Command/ResetCommand.php b/src/Command/ResetCommand.php index 42d8f75d..311f8ba8 100644 --- a/src/Command/ResetCommand.php +++ b/src/Command/ResetCommand.php @@ -49,6 +49,11 @@ EOT { $io = new SymfonyStyle($input, $output); + if (getenv('APP_ENV') === 'prod') { + $io->error('kimai:reset-dev is not allowed in production'); + return -1; + } + if ($this->askConfirmation($input, $output, 'Do you want to create the database y/N ?')) { try { $command = $this->getApplication()->find('doctrine:database:create'); diff --git a/src/Constants.php b/src/Constants.php index c04ac525..66135c76 100644 --- a/src/Constants.php +++ b/src/Constants.php @@ -21,7 +21,7 @@ class Constants /** * The current release version */ - public const VERSION = '0.8.1'; + public const VERSION = '0.9'; /** * The release name, will only change for new major version */ @@ -29,7 +29,7 @@ class Constants /** * The current release status, either "stable" or "dev" */ - public const STATUS = 'stable'; + public const STATUS = 'dev'; /** * Used in multiple views */ diff --git a/tests/API/ConfigurationControllerTest.php b/tests/API/ConfigurationControllerTest.php index c6151f4d..d5f9337c 100644 --- a/tests/API/ConfigurationControllerTest.php +++ b/tests/API/ConfigurationControllerTest.php @@ -9,13 +9,7 @@ namespace App\Tests\API; -use App\Configuration\LanguageFormattings; -use App\Entity\Activity; -use App\Entity\Customer; -use App\Entity\Project; use App\Entity\User; -use App\Repository\Query\VisibilityQuery; -use Symfony\Bundle\FrameworkBundle\Client; /** * @coversDefaultClass \App\API\ConfigurationController