diff --git a/src/ArtefactTrait.php b/src/ArtefactTrait.php index 02d1a43..d96d3d8 100644 --- a/src/ArtefactTrait.php +++ b/src/ArtefactTrait.php @@ -58,6 +58,20 @@ trait ArtefactTrait */ protected $now; + /** + * Path to report file. + * + * @var string + */ + protected $report; + + /** + * Artefact build result. + * + * @var bool + */ + protected $result; + /** * Artefact constructor. */ @@ -82,6 +96,7 @@ public function __construct() * @option $message Commit message with optional tokens. * @option $gitignore Path to gitignore file to replace current .gitignore. * @option $push Push artefact to the remote repository. Defaults to FALSE. + * @option $report Path to the report file. */ public function artefact($remote, array $opts = [ 'root' => InputOption::VALUE_REQUIRED, @@ -91,6 +106,7 @@ public function artefact($remote, array $opts = [ 'gitignore' => InputOption::VALUE_REQUIRED, 'push' => false, 'now' => InputOption::VALUE_REQUIRED, + 'report' => InputOption::VALUE_REQUIRED, ]) { $this->checkRequirements(); @@ -135,11 +151,16 @@ protected function doPush() $this->say(sprintf('Added changes: %s', $result->getMessage())); $result = $this->gitPush($this->gitGetSrcRepo(), 'dst', $this->branch); - if ($result->wasSuccessful()) { + $this->result = $result->wasSuccessful(); + if ($this->result) { $this->sayOkay(sprintf('Pushed branch "%s" with commit message "%s"', $this->branch, $this->message)); } else { $this->say(sprintf('Error occurred while pushing branch "%s" with commit message "%s"', $this->branch, $this->message)); } + + if ($this->report) { + $this->dumpReport(); + } } /** @@ -171,6 +192,8 @@ protected function resolveOptions(array $options) } $this->needsPush = !empty($options['push']); + + $this->report = !empty($options['report']) ? $options['report'] : null; } /** @@ -181,6 +204,7 @@ protected function showInfo() $this->writeln('----------------------------------------------------------------------'); $this->writeln(' Artefact information'); $this->writeln('----------------------------------------------------------------------'); + $this->writeln(' Build timestamp: '.date('Y/m/d H:i:s', $this->now)); $this->writeln(' Source repository: '.$this->gitGetSrcRepo()); $this->writeln(' Remote repository: '.$this->gitGetRemoteRepo()); $this->writeln(' Remote branch: '.$this->branch); @@ -189,6 +213,26 @@ protected function showInfo() $this->writeln('----------------------------------------------------------------------'); } + /** + * Dump artefact report to a file. + */ + protected function dumpReport() + { + $lines[] = '----------------------------------------------------------------------'; + $lines[] = ' Artefact report'; + $lines[] = '----------------------------------------------------------------------'; + $lines[] = ' Build timestamp: '.date('Y/m/d H:i:s', $this->now); + $lines[] = ' Source repository: '.$this->gitGetSrcRepo(); + $lines[] = ' Remote repository: '.$this->gitGetRemoteRepo(); + $lines[] = ' Remote branch: '.$this->branch; + $lines[] = ' Gitignore file: '.($this->gitignoreFile ? $this->gitignoreFile : 'No'); + $lines[] = ' Commit message: '.$this->message; + $lines[] = ' Push result: '.($this->result ? 'Success' : 'Failure'); + $lines[] = '----------------------------------------------------------------------'; + + $this->fsFileSystem->dumpFile($this->report, implode(PHP_EOL, $lines)); + } + /** * Set the branch of the remote repository. * diff --git a/tests/ArtefactTest.php b/tests/ArtefactTest.php index 5901ea9..32700f5 100644 --- a/tests/ArtefactTest.php +++ b/tests/ArtefactTest.php @@ -233,4 +233,22 @@ public function testPushMultipleTags() '1.txt', ], $remoteBranch); } + + public function testReport() + { + $report = $this->getFixtureSrcDir().DIRECTORY_SEPARATOR.'report.txt'; + + $this->gitCreateFixtureCommits(1, $this->getFixtureSrcDir()); + $this->runRoboCommand(sprintf('artefact --src=%s --push %s --report=%s', $this->getFixtureSrcDir(), $this->getFixtureRemoteDir(), $report)); + + $this->assertFileExists($report); + $output = file_get_contents($report); + + $this->assertContains('Artefact report', $output); + $this->assertContains(sprintf('Source repository: %s', $this->getFixtureSrcDir()), $output); + $this->assertContains(sprintf('Remote repository: %s', $this->getFixtureRemoteDir()), $output); + $this->assertContains(sprintf('Remote branch: %s', $this->defaultCurrentBranch), $output); + $this->assertContains('Gitignore file: No', $output); + $this->assertContains('Push result: Success', $output); + } }