Skip to content

Commit 55c3766

Browse files
committed
added self update/rollback/check commands to manage application easier
1 parent e1d8172 commit 55c3766

8 files changed

+349
-9
lines changed

README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ A PHP application to access your Kimai 2 installation via its API (http).
1010
- iconv extension
1111
- zlib extension
1212

13-
## Installation and updates
13+
## Installation
1414

1515
To install and update the Kimai console tools, execute the following commands:
1616

@@ -64,6 +64,12 @@ You get a list of all available commands with `kimai`.
6464
- `kimai version` - show the full version string of the remote installation
6565
- `kimai configuration` - creates the initial configuration file or displays it
6666

67+
The following commands will help you with updating the command:
68+
69+
- `kimai self:check` - check if there is a new version available
70+
- `kimai self:update` - update your local version to the latest release
71+
- `kimai self:rollback` - rollback to a previous release
72+
6773
To get help for a dedicated command use the `--help` switch, eg: `kimai project:list --help`
6874

6975
### Start a timesheet

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"ext-iconv": "*",
1515
"ext-json": "*",
1616
"guzzlehttp/guzzle": "^6.5",
17+
"padraic/phar-updater": "^1.0",
1718
"symfony/console": "5.0.*"
1819
},
1920
"config": {

composer.lock

+178-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Application.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,11 @@ protected function getDefaultCommands()
8989
new Command\VersionCommand(),
9090
]);
9191

92-
// TODO commented, until it is working
93-
/*
9492
if ('phar:' === substr(__FILE__, 0, 5)) {
93+
$commands[] = new Command\SelfCheckCommand();
9594
$commands[] = new Command\SelfUpdateCommand();
95+
$commands[] = new Command\SelfRollbackCommand();
9696
}
97-
*/
9897

9998
return $commands;
10099
}

src/Command/AbstractSelfCommand.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Kimai 2 - Remote Console.
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace KimaiConsole\Command;
11+
12+
use Humbug\SelfUpdate\Updater;
13+
use KimaiConsole\Constants;
14+
15+
abstract class AbstractSelfCommand extends BaseCommand
16+
{
17+
/**
18+
* {@inheritdoc}
19+
*/
20+
protected function getUpdater(): Updater
21+
{
22+
$updater = new Updater(null, false, Updater::STRATEGY_GITHUB);
23+
$updater->getStrategy()->setPackageName('kevinpapst/kimai2-console');
24+
$updater->getStrategy()->setPharName('kimai.phar');
25+
$updater->getStrategy()->setCurrentLocalVersion(Constants::VERSION);
26+
27+
return $updater;
28+
}
29+
}

src/Command/SelfCheckCommand.php

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Kimai 2 - Remote Console.
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace KimaiConsole\Command;
11+
12+
use KimaiConsole\Constants;
13+
use Symfony\Component\Console\Input\InputInterface;
14+
use Symfony\Component\Console\Output\OutputInterface;
15+
use Symfony\Component\Console\Style\SymfonyStyle;
16+
17+
class SelfCheckCommand extends AbstractSelfCommand
18+
{
19+
/**
20+
* {@inheritdoc}
21+
*/
22+
protected function configure()
23+
{
24+
$this
25+
->setName('self:check')
26+
->setDescription('Checks for available updates')
27+
->setHelp('This command checks if there is a new version available')
28+
;
29+
}
30+
31+
/**
32+
* {@inheritdoc}
33+
*/
34+
protected function execute(InputInterface $input, OutputInterface $output)
35+
{
36+
$io = new SymfonyStyle($input, $output);
37+
38+
$updater = $this->getUpdater();
39+
40+
try {
41+
$result = $updater->hasUpdate();
42+
43+
if ($result) {
44+
// this is also triggered, if the current local version is higher then the remote version
45+
// it does not trigger a version_compare, but only compare if the two versions are equal
46+
$io->success(sprintf('There is a new release available in version: %s', $updater->getNewVersion()));
47+
} elseif (false === $updater->getNewVersion()) {
48+
$io->success('There are no stable builds available.');
49+
} else {
50+
$io->success(sprintf('You are running the latest version: %s', Constants::VERSION));
51+
}
52+
} catch (\Exception $ex) {
53+
$io->error('Failed to check for updates: ' . $ex->getMessage());
54+
55+
return 1;
56+
}
57+
58+
return 0;
59+
}
60+
}

src/Command/SelfRollbackCommand.php

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Kimai 2 - Remote Console.
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace KimaiConsole\Command;
11+
12+
use Symfony\Component\Console\Input\InputInterface;
13+
use Symfony\Component\Console\Output\OutputInterface;
14+
use Symfony\Component\Console\Style\SymfonyStyle;
15+
16+
class SelfRollbackCommand extends AbstractSelfCommand
17+
{
18+
/**
19+
* {@inheritdoc}
20+
*/
21+
protected function configure()
22+
{
23+
$this
24+
->setName('self:rollback')
25+
->setDescription('Rollback to the latest available version')
26+
->setHelp('This command lets you rollback an updated application to the previous version')
27+
;
28+
}
29+
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
protected function execute(InputInterface $input, OutputInterface $output)
34+
{
35+
$io = new SymfonyStyle($input, $output);
36+
37+
$updater = $this->getUpdater();
38+
39+
try {
40+
$result = $updater->rollback();
41+
if ($result) {
42+
$io->success(sprintf('Rollback from version %s to %s', $updater->getOldVersion(), $updater->getNewVersion()));
43+
} else {
44+
$io->success('Cannot rollback');
45+
}
46+
} catch (\Exception $ex) {
47+
$io->error('Failed to rollback! ' . $ex->getMessage());
48+
49+
return 1;
50+
}
51+
52+
return 0;
53+
}
54+
}

0 commit comments

Comments
 (0)