-
-
Notifications
You must be signed in to change notification settings - Fork 522
/
CreateReleaseCommand.php
160 lines (129 loc) · 5.07 KB
/
CreateReleaseCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Command;
use App\Constants;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
/**
* Command used to create a release package with pre-installed composer, SQLite database and user.
*
* @codeCoverageIgnore
*/
class CreateReleaseCommand extends Command
{
public const CLONE_CMD = 'git clone -b %s --depth 1 https://github.com/kevinpapst/kimai2.git';
/**
* @var string
*/
protected $rootDir = '';
/**
* @param string $projectDirectory
*/
public function __construct(string $projectDirectory)
{
$this->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;
}
}