diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..74942bd --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,16 @@ +# Change Log + +All notable changes to this project will be documented in this file. +This project adheres to [Semantic Versioning](http://semver.org/) and [Keep a CHANGELOG](http://keepachangelog.com). + +## [Unreleased] + +### Added + +- Executable .phar file for download +- `upload` command (PHAR only) - same as calling the tool without a command when installed via composer. +- `self-update` / `selfupdate` command (PHAR only) +- `rollback` command (PHAR only) +- [Installation / Usage](./README.md) / [Distribution instructions](./DEVELOPING.md) for the PHAR tool + +[Unreleased]: https://github.com/codeclimate/php-test-reporter/compare/v0.3.2...HEAD diff --git a/DEVELOPING.md b/DEVELOPING.md index f353228..a73a3ae 100644 --- a/DEVELOPING.md +++ b/DEVELOPING.md @@ -1,18 +1,44 @@ # Developing -- Get the source +## Get the source $ git clone https://github.com/codeclimate/php-test-reporter -- Install dependencies +## Install dependencies $ curl -sS https://getcomposer.org/installer | php - $ php composer.phar install --dev + $ php composer.phar update -o -v -- Run the tests +## Run the tests $ ./vendor/bin/phpunit + +### With HTML coverage output: -- Submit PRs to https://github.com/codeclimate/php-test-reporter + $ ./vendor/bin/phpunit --coverage-html=build/logs/coverage -*Note*: all changes and fixes must have appropriate test coverage. +## Build the PHAR tool + + # Create a new git tag (optional) + $ git tag v1.x.x -m 'Version 1.x.x' + # Build the PHAR using box project + $ ./vendor/bin/box build + +## Distribute the PHAR tool + +### With verification and compatibility for phar.io / PhiVE + +* [Create a GPG key](https://phar.io/howto/generate-gpg-key.html) (Should be the repositoy's maintainer one) +* [Create a signature and upload to Github](https://phar.io/howto/sign-and-upload-to-github.html) + +### Without verification + +* Go to the [releases section on Github](https://github.com/codeclimate/php-test-reporter/releases) +* Click "Edit" on the latest tag/release +* Add the `codeclimate-test-reporter.phar` in the "Attach binaries..." section +* Click "Update release" + +## Contribute + +* Submit PRs to: https://github.com/codeclimate/php-test-reporter +* *Note*: all changes and fixes must have appropriate test coverage. diff --git a/README.md b/README.md index ba9e98e..c1d76b5 100644 --- a/README.md +++ b/README.md @@ -46,16 +46,27 @@ This package requires a user, but not necessarily a paid account, on Code Climate, so if you don't have one the first step is to signup at: https://codeclimate.com. +### Via composer + To install php-test-reporter with Composer run the following command. ```shell -composer require codeclimate/php-test-reporter --dev +$ composer require codeclimate/php-test-reporter --dev ``` This will get you the latest version of the reporter and install it. If you do want the master, untagged, version you may use the command below: ```shell -composer require codeclimate/php-test-reporter:@dev --dev +$ composer require codeclimate/php-test-reporter:@dev --dev +``` + +### As PHAR tool + +Checkout the [latest release here](https://github.com/codeclimate/php-test-reporter/releases) and replace `X.X.X` with the latest version. + +```shell +$ RELEASE=X.X.X +$ wget -c "https://github.com/codeclimate/php-test-reporter/releases/download/$RELEASE/codeclimate-test-reporter.phar" ``` ## Usage @@ -77,15 +88,19 @@ Add the following to phpunit.xml.dist: Or invoke `phpunit` as follows: -``` +```shell $ phpunit --coverage-clover build/logs/clover.xml ``` - Specifying your repo token as an environment variable, invoke the test-reporter: -``` +```shell $ CODECLIMATE_REPO_TOKEN="..." vendor/bin/test-reporter + +# ... or via PHAR ... + +$ CODECLIMATE_REPO_TOKEN="..." codeclimate-test-reporter.phar upload ``` The `CODECLIMATE_REPO_TOKEN` value is provided after you add your repo diff --git a/box.json b/box.json index 7837c62..b259834 100644 --- a/box.json +++ b/box.json @@ -1,23 +1,39 @@ { - "alias": "codeclimate-test-reporter.phar", - "chmod": "0755", - "directories": ["src"], - "compactors": [ - "Herrera\\Box\\Compactor\\Php" - ], - "extract": true, - "files": [ - "LICENSE" - ], - "finder": [ - { - "name": ["*.php", "*.pem"], - "exclude": ["tests"], - "in": ["vendor"] - } - ], - "git-version": "package_version", - "main": "composer/bin/test-reporter", - "output": "codeclimate-test-reporter.phar", - "stub": true + "chmod": "0755", + "compression": "GZ", + "alias": "codeclimate-test-reporter.phar", + "directories": [ + "src" + ], + "compactors": [ + "Herrera\\Box\\Compactor\\Php" + ], + "extract": false, + "intercept": true, + "files": [ + "LICENSE" + ], + "finder": [ + { + "name": [ + "*.php", + "*.pem" + ], + "exclude": [ + "bin", + "tests", + "phpunit", + "tm" + ], + "in": [ + "vendor" + ] + } + ], + "git-version": "package_version", + "main": "phar/bin/main.php", + "metadata": "The code climate php test reporter", + "output": "build/codeclimate-test-reporter.phar", + "shebang": "#!/usr/bin/env php", + "stub": true } diff --git a/composer.json b/composer.json index 49a70b4..492b883 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,9 @@ "php": ">=5.3", "ext-curl": "*", "satooshi/php-coveralls": "^1.0", - "symfony/console": "^2.0|^3.0" + "symfony/console": "^2.0|^3.0", + "psr/log": "^1.0", + "padraic/phar-updater": "^1.0" }, "require-dev": { "phpunit/phpunit": "3.7.*@stable", diff --git a/phar/bin/main.php b/phar/bin/main.php new file mode 100644 index 0000000..588e4f7 --- /dev/null +++ b/phar/bin/main.php @@ -0,0 +1,33 @@ + + */ + +namespace CodeClimate\PhpTestReporter; + +use CodeClimate\PhpTestReporter\ConsoleCommands\RollbackCommand; +use CodeClimate\PhpTestReporter\ConsoleCommands\SelfUpdateCommand; +use CodeClimate\PhpTestReporter\ConsoleCommands\UploadCommand; +use Symfony\Component\Console\Application; + +require(__DIR__ . '/../../vendor/autoload.php'); + +try { + $app = new Application('Code Climate PHP Test Reporter', '@package_version@'); + $app->addCommands( + [ + new UploadCommand('upload'), + new SelfUpdateCommand('self-update'), + new RollbackCommand('rollback'), + ] + ); + + $code = $app->run(); + + exit($code); +} catch (\Exception $e) { + echo 'Uncaught Exception ' . get_class($e) . ' with message: ' . $e->getMessage() . PHP_EOL; + echo $e->getTraceAsString(); + + exit(1); +} diff --git a/src/Application.php b/src/Application.php index 5ce0e60..20c938b 100644 --- a/src/Application.php +++ b/src/Application.php @@ -1,7 +1,7 @@ createTestReporterCommand(); + $defaultCommands[] = $this->createUploadCommand(); return $defaultCommands; } /** - * Create TestReporterCommand. - * @return TestReporterCommand + * Create UploadCommand. + * @return UploadCommand */ - protected function createTestReporterCommand() + protected function createUploadCommand() { - $command = new TestReporterCommand(); + $command = new UploadCommand('upload'); return $command; } diff --git a/src/ConsoleCommands/RollbackCommand.php b/src/ConsoleCommands/RollbackCommand.php new file mode 100644 index 0000000..bf288a0 --- /dev/null +++ b/src/ConsoleCommands/RollbackCommand.php @@ -0,0 +1,54 @@ +setDescription('Rolls back this PHAR to the previous version.'); + } + + /** + * @param InputInterface $input + * @param OutputInterface $output + * + * @return int + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $input->validate(); + $logger = new ConsoleLogger($output); + $updater = new Updater(null, false, Updater::STRATEGY_GITHUB); + + /** @var GithubStrategy $strategy */ + $strategy = $updater->getStrategy(); + + $strategy->setPackageName(PharTool::PACKAGE_NAME); + $strategy->setPharName(PharTool::PHAR_NAME); + $strategy->setCurrentLocalVersion('@package_version@'); + + if ($updater->rollback()) { + $logger->info('Roll back successful!'); + } else { + $logger->alert('Roll back failed.'); + } + + return 0; + } +} diff --git a/src/ConsoleCommands/SelfUpdateCommand.php b/src/ConsoleCommands/SelfUpdateCommand.php new file mode 100644 index 0000000..970e166 --- /dev/null +++ b/src/ConsoleCommands/SelfUpdateCommand.php @@ -0,0 +1,87 @@ +setAliases([ 'selfupdate' ]); + $this->setDescription('Updates this PHAR to latest version.'); + + $this->addOption( + 'stability', + 's', + InputOption::VALUE_OPTIONAL, + sprintf( + 'Specify the stability (%s, %s or %s)', + GithubStrategy::STABLE, + GithubStrategy::UNSTABLE, + GithubStrategy::ANY + ), + GithubStrategy::STABLE + ); + } + + /** + * @param InputInterface $input + * @param OutputInterface $output + * + * @return int + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $logger = new ConsoleLogger($output); + $updater = new Updater(null, false, Updater::STRATEGY_GITHUB); + + /** @var GithubStrategy $strategy */ + $strategy = $updater->getStrategy(); + + $strategy->setPackageName(PharTool::PACKAGE_NAME); + $strategy->setPharName(PharTool::PHAR_NAME); + $strategy->setCurrentLocalVersion('@package_version@'); + + $stability = $input->getOption('stability'); + $strategy->setStability($stability); + + try { + if ($updater->hasUpdate()) { + $newVersion = $updater->getNewVersion(); + + $logger->info(sprintf('The current stable version available is: %s', $newVersion)); + $logger->info('Updating...'); + + if ($updater->update()) { + $logger->info(sprintf('Successful! You now have version %s installed', $newVersion)); + } + } elseif (false === $updater->getNewVersion()) { + $logger->alert('There is no stable version available.'); + } else { + $logger->info('@package_version@ is the latest stable version.'); + } + + return 0; + } catch (HttpRequestException $e) { + $logger->alert('Error fetching current version from remote repository.'); + + return 1; + } + } +} diff --git a/src/ConsoleCommands/TestReporterCommand.php b/src/ConsoleCommands/UploadCommand.php similarity index 94% rename from src/ConsoleCommands/TestReporterCommand.php rename to src/ConsoleCommands/UploadCommand.php index 53719d4..9b402e4 100644 --- a/src/ConsoleCommands/TestReporterCommand.php +++ b/src/ConsoleCommands/UploadCommand.php @@ -12,7 +12,7 @@ /** * Test reporter command */ -class TestReporterCommand extends Command +class UploadCommand extends Command { /** * {@inheritdoc} @@ -21,8 +21,7 @@ class TestReporterCommand extends Command protected function configure() { $this - ->setName('test-reporter') - ->setDescription('Code Climate PHP Test Reporter') + ->setDescription('Uploads test report to code climate') ->addOption( 'stdout', null, diff --git a/src/Constants/PharTool.php b/src/Constants/PharTool.php new file mode 100644 index 0000000..136695c --- /dev/null +++ b/src/Constants/PharTool.php @@ -0,0 +1,17 @@ +