Skip to content

Commit

Permalink
Add self-update command (#91)
Browse files Browse the repository at this point in the history
* Start work on self-updater

* Parse version from home/build

* Fixup

* Fix the updater, add logging

* Add unit tests

* Handle empty current version

* phpcs

* shellcheck ignore ws.update.php

* shellcheck ignore ws.update.php

* phpstan fixes

* turn off phar.readonly

* Fix test action

* Pass release version tag to composer compile

* Add pretty output for update script

* Fixup

* Fix phpstan

* Move self-update command to plugin

* Delete ws.update.php
  • Loading branch information
jameshalsall authored Mar 28, 2022
1 parent d88b2fd commit 7621db9
Show file tree
Hide file tree
Showing 23 changed files with 571 additions and 22 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
SHELLCHECK_OPTS: --exclude=SC1008,SC1091
with:
additional_files: 'ws.*'
ignore: "vendor vendor-bin"
ignore_paths: "vendor vendor-bin"

composer-validate:
name: "Composer validate (${{ matrix.php-version }})"
Expand Down Expand Up @@ -112,11 +112,14 @@ jobs:
extensions: "${{ env.REQUIRED_PHP_EXTENSIONS }}"
php-version: "${{ matrix.php-version }}"
tools: composer:v2
ini-values: phar.readonly=off

-
name: "Composer install"
uses: "ramsey/composer-install@v1"

-
name: "Build ws.phar"
run: "composer compile"
-
name: "Run PHPUnit"
run: "composer test"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release-phar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
uses: "ramsey/composer-install@v1"

- name: Build PHAR
run: composer compile
run: composer compile ${{ github.event.release.tag_name }}

- name: Check existence of compiled .phar
run: test -e my127ws.phar && exit 0 || exit 10
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/vendor
/vendor-bin/**/vendor
/vendor-bin/**/composer.lock
/my127ws.phar
/ws.phar
/tests/Workspace
/tests/Test/Updater/fixtures/generated
/.php-cs-fixer.cache
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ docker-compose exec -u build builder80 /app/build.sh
### Performing a Release

1. Head to the [releases page] and create a new release:
* Enter the tag name to be created
* Give it a title containing tag name
* Click "Auto-generate release notes"
* Examine the generated release notes. For every entry in the `Other Changes` section,
examine the Pull Requests and assign each pull request either a `enhancement` label
for a new feature, `bug` for a bugfix or `deprecated` for
a deprecation.
* Cancel the release if any pull request labels needed changing, and repeat from 1
* Click `Publish Release`
* Enter the tag name to be created
* Give it a title containing tag name
* Click "Auto-generate release notes"
* Examine the generated release notes. For every entry in the `Other Changes` section,
examine the Pull Requests and assign each pull request either a `enhancement` label
for a new feature, `bug` for a bugfix or `deprecated` for
a deprecation.
* Cancel the release if any pull request labels needed changing, and repeat from 1
* Click `Publish Release`

[releases page]: https://github.com/my127/workspace/releases
5 changes: 4 additions & 1 deletion box.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"main": "bin/workspace",
"output": "my127ws.phar",
"alias": "ws.phar",
"output": "ws.phar",
"directories-bin": [
"config",
"home",
Expand All @@ -12,7 +13,9 @@
"home/service/mail/.env",
"home/service/proxy/.env"
],
"metadata": "my127\\Workspace\\Application::getMetadata",
"blacklist": [
"tools/scripts/compile.sh",

"vendor/symfony/cache/Adapter/TraceableAdapter.php",
"vendor/symfony/cache/Adapter/SimpleCacheAdapter.php",
Expand Down
1 change: 1 addition & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
set -e -o pipefail

composer install
composer compile
composer test
7 changes: 2 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,9 @@
"scripts": {
"post-install-cmd": ["@composer bin all install --ansi"],
"post-update-cmd": ["@composer bin all update --ansi"],
"test": [
"@compile",
"vendor/bin/phpunit --testdox"
],
"test": "vendor/bin/phpunit --testdox",
"phpunit": "vendor/bin/phpunit --testdox",
"compile": "date -u > home/build && bin/build && box compile",
"compile": "tools/scripts/compile.sh",
"phpstan": "./vendor/bin/phpstan analyse",
"cs": "./vendor/bin/php-cs-fixer fix",
"integrate": [
Expand Down
4 changes: 4 additions & 0 deletions config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ services:
my127\Workspace\Types\Harness\Repository\Repository $archiveRepository: '@my127\Workspace\Types\Harness\Repository\ArchiveRepository'
my127\Workspace\Types\Harness\Repository\Repository $packageRepository: '@my127\Workspace\Types\Harness\Repository\PackageRepository'

my127\Workspace\Updater\Updater:
arguments:
$apiUrl: 'https://api.github.com/repos/my127/workspace/releases'

# -- require

Symfony\Component\EventDispatcher\EventDispatcher:
Expand Down
19 changes: 17 additions & 2 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@

class Application extends ConsoleApplication
{
public const VERSION = 'alpha1';
private const DEFAULT_VERSION = '0.2.x-dev';

/** @var Environment */
private $environment;

public function __construct(Environment $environment, Executor $executor, EventDispatcher $dispatcher)
{
parent::__construct($executor, $dispatcher, 'ws', '', self::VERSION);
parent::__construct($executor, $dispatcher, 'ws', '', self::getVersion());
$this->environment = $environment;
}

Expand All @@ -26,4 +26,19 @@ public function run(?array $argv = null): void
$this->environment->build();
parent::run($argv);
}

public static function getVersion(): string
{
$version = trim(@file_get_contents(__DIR__ . '/../home/build'));
if (empty($version)) {
return self::DEFAULT_VERSION;
}

return $version;
}

public static function getMetadata(): array
{
return ['application_version' => self::getVersion()];
}
}
22 changes: 22 additions & 0 deletions src/Updater/Exception/NoUpdateAvailableException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace my127\Workspace\Updater\Exception;

use RuntimeException;

class NoUpdateAvailableException extends RuntimeException
{
private $currentVersion;

public function __construct(string $currentVersion)
{
parent::__construct('There is no update available for workspace.');

$this->currentVersion = $currentVersion;
}

public function getCurrentVersion(): string
{
return $this->currentVersion;
}
}
9 changes: 9 additions & 0 deletions src/Updater/Exception/NoVersionDeterminedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace my127\Workspace\Updater\Exception;

use RuntimeException;

class NoVersionDeterminedException extends RuntimeException
{
}
12 changes: 12 additions & 0 deletions src/Updater/Output.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace my127\Workspace\Updater;

interface Output
{
public function infof(string $info, ...$args): void;

public function info(string $info): void;

public function success(string $success): void;
}
54 changes: 54 additions & 0 deletions src/Updater/Plugin/Command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace my127\Workspace\Updater\Plugin;

use my127\Console\Application\Application;
use my127\Console\Application\Plugin\Plugin;
use my127\Workspace\Application as BaseApplication;
use my127\Workspace\Updater\Exception\NoUpdateAvailableException;
use my127\Workspace\Updater\Exception\NoVersionDeterminedException;
use my127\Workspace\Updater\Updater;
use Phar;
use RuntimeException;

class Command implements Plugin
{
/** @var Updater */
private $updater;

public function __construct(Updater $updater)
{
$this->updater = $updater;
}

public function setup(Application $application): void
{
$application->section('self-update')
->description('Updates the current version of workspace.')
->action($this->action());
}

private function action()
{
return function () {
$pharPath = Phar::running(false);
if (empty($pharPath)) {
echo 'This command can only be executed from within the ws utility.' . PHP_EOL;
exit(1);
}

try {
$this->updater->update(BaseApplication::getVersion(), $pharPath);
} catch (NoUpdateAvailableException $e) {
echo sprintf('You are already running the latest version of workspace: %s', $e->getCurrentVersion()) . PHP_EOL;
exit(1);
} catch (NoVersionDeterminedException $e) {
echo 'Unable to determine your current workspace version. You are likely not using a tagged released.' . PHP_EOL;
exit(1);
} catch (RuntimeException $e) {
echo sprintf('%s. Aborting self-update', $e->getMessage()) . PHP_EOL;
exit(1);
}
};
}
}
38 changes: 38 additions & 0 deletions src/Updater/Release.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace my127\Workspace\Updater;

class Release
{
/** @var string */
private $url;

/** @var string */
private $version;

public function __construct(string $url, string $version)
{
$this->url = $url;
$this->version = $version;
}

public function getUrl(): string
{
return $this->url;
}

public function getVersion(): string
{
return $this->version;
}

/**
* Will return true if this release is more recent than the version provided.
*
* @param string $version Semver version number, e.g. 1.0.1, 0.2.0-alpha1 etc.
*/
public function isMoreRecentThan(string $version): bool
{
return version_compare($this->getVersion(), $version, '>');
}
}
34 changes: 34 additions & 0 deletions src/Updater/StdOutput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace my127\Workspace\Updater;

use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Style\OutputStyle;
use Symfony\Component\Console\Style\SymfonyStyle;

class StdOutput implements Output
{
/** @var OutputStyle */
private $output;

public function __construct()
{
$this->output = new SymfonyStyle(new StringInput(''), new ConsoleOutput());
}

public function infof(string $info, ...$args): void
{
$this->output->writeln('<info>' . sprintf($info, ...$args) . '</info>');
}

public function info(string $info): void
{
$this->output->writeln('<info>' . $info . '</info>');
}

public function success(string $success): void
{
$this->output->success($success);
}
}
Loading

0 comments on commit 7621db9

Please sign in to comment.