From fdee180beefb3f668ab9ac73d2c50545ebba9b1b Mon Sep 17 00:00:00 2001 From: "tan.nguyen" Date: Tue, 19 Mar 2024 21:36:27 +0700 Subject: [PATCH 01/37] Init. --- composer.json | 3 +- src/Commands/ArtifactCommand.php | 23 ++++-------- src/LogTrait.php | 61 ++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 17 deletions(-) create mode 100644 src/LogTrait.php diff --git a/composer.json b/composer.json index a0d8c68..e4ed791 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,8 @@ "symfony/filesystem": "^6", "symfony/finder": "^6", "czproject/git-php": "^4.2", - "symfony/process": "^6" + "symfony/process": "^6", + "symfony/monolog-bridge": "^6" }, "require-dev": { "bamarni/composer-bin-plugin": "^1.8", diff --git a/src/Commands/ArtifactCommand.php b/src/Commands/ArtifactCommand.php index b59607a..6fd1251 100644 --- a/src/Commands/ArtifactCommand.php +++ b/src/Commands/ArtifactCommand.php @@ -7,6 +7,7 @@ use DrevOps\GitArtifact\FilesystemTrait; use DrevOps\GitArtifact\GitArtifactGit; use DrevOps\GitArtifact\GitArtifactGitRepository; +use DrevOps\GitArtifact\LogTrait; use DrevOps\GitArtifact\TokenTrait; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; @@ -28,6 +29,7 @@ class ArtifactCommand extends Command { use TokenTrait; use FilesystemTrait; + use LogTrait; const GIT_REMOTE_NAME = 'dst'; @@ -221,7 +223,11 @@ protected function configure(): void { * @throws \Exception */ protected function execute(InputInterface $input, OutputInterface $output): int { + if ($input->getOption('debug')) { + $output->setVerbosity(OutputInterface::VERBOSITY_DEBUG); + } $this->output = $output; + $this->logger = self::createLogger((string) $this->getName(), $output); try { $this->checkRequirements(); $remote = $input->getArgument('remote'); @@ -230,7 +236,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int } catch (\Exception $exception) { $output->writeln('' . $exception->getMessage() . ''); - return Command::FAILURE; } @@ -955,16 +960,6 @@ protected function getTokenTimestamp(string $format = 'Y-m-d_H-i-s'): string { return date($format, $this->now); } - /** - * Check if running in debug mode. - * - * @return bool - * Check is debugging mode or not. - */ - protected function isDebug(): bool { - return $this->debug || $this->output->isDebug(); - } - /** * Write line as yell style. * @@ -1012,12 +1007,8 @@ protected function sayOkay(string $text): void { * The args. */ protected function printDebug(mixed ...$args): void { - if (!$this->isDebug()) { - return; - } $message = array_shift($args); - /* @phpstan-ignore-next-line */ - $this->writeln(vsprintf($message, $args)); + $this->logDebug(vsprintf($message, $args)); } /** diff --git a/src/LogTrait.php b/src/LogTrait.php new file mode 100644 index 0000000..474c1e3 --- /dev/null +++ b/src/LogTrait.php @@ -0,0 +1,61 @@ + Level::Error, + OutputInterface::VERBOSITY_NORMAL => Level::Warning, + OutputInterface::VERBOSITY_VERBOSE => Level::Notice, + OutputInterface::VERBOSITY_VERY_VERBOSE => Level::Info, + OutputInterface::VERBOSITY_DEBUG => Level::Debug, + ]; + $verbosity = $output->getVerbosity(); + $level = $verbosityMapping[$verbosity] ?? Level::Debug; + $handler = new StreamHandler(__DIR__ . DIRECTORY_SEPARATOR . 'hello-log.log', $level); + $logger->pushHandler($handler); + $handler = new ConsoleHandler($output); + $logger->pushHandler($handler); + + return $logger; + } + + /** + * Log debug. + */ + public function logDebug(string|\Stringable $message, array $context = []): void { + $this->logger->debug($message, $context); + } + +} From c799edb8cec48b1f4fe4efea0431b1977809733d Mon Sep 17 00:00:00 2001 From: "tan.nguyen" Date: Tue, 19 Mar 2024 21:45:42 +0700 Subject: [PATCH 02/37] Init. --- composer.json | 2 +- src/Commands/ArtifactCommand.php | 1 + src/LogTrait.php | 5 +++++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index e4ed791..d35a71e 100644 --- a/composer.json +++ b/composer.json @@ -53,7 +53,7 @@ "lint": [ "phpcs", "phpmd --exclude vendor,vendor-bin,node_modules . text phpmd.xml", - "phpstan", + "phpstan --memory-limit=-1", "rector --clear-cache --dry-run" ], "lint-fix": [ diff --git a/src/Commands/ArtifactCommand.php b/src/Commands/ArtifactCommand.php index 6fd1251..4bd1b8f 100644 --- a/src/Commands/ArtifactCommand.php +++ b/src/Commands/ArtifactCommand.php @@ -1008,6 +1008,7 @@ protected function sayOkay(string $text): void { */ protected function printDebug(mixed ...$args): void { $message = array_shift($args); + /* @phpstan-ignore-next-line */ $this->logDebug(vsprintf($message, $args)); } diff --git a/src/LogTrait.php b/src/LogTrait.php index 474c1e3..67d679f 100644 --- a/src/LogTrait.php +++ b/src/LogTrait.php @@ -53,6 +53,11 @@ public static function createLogger(string $name, OutputInterface $output): Logg /** * Log debug. + * + * @param string|\Stringable $message + * Message. + * @param array $context + * Context. */ public function logDebug(string|\Stringable $message, array $context = []): void { $this->logger->debug($message, $context); From c04a9752f6b8d3b7cc080b539367fc796bbba336 Mon Sep 17 00:00:00 2001 From: "tan.nguyen" Date: Tue, 19 Mar 2024 21:55:34 +0700 Subject: [PATCH 03/37] Init. --- src/LogTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LogTrait.php b/src/LogTrait.php index 67d679f..25e5763 100644 --- a/src/LogTrait.php +++ b/src/LogTrait.php @@ -43,7 +43,7 @@ public static function createLogger(string $name, OutputInterface $output): Logg ]; $verbosity = $output->getVerbosity(); $level = $verbosityMapping[$verbosity] ?? Level::Debug; - $handler = new StreamHandler(__DIR__ . DIRECTORY_SEPARATOR . 'hello-log.log', $level); + $handler = new StreamHandler(sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'hello-log.log', $level); $logger->pushHandler($handler); $handler = new ConsoleHandler($output); $logger->pushHandler($handler); From 42fb668e599b3edbec7d0754f8f385fec3619946 Mon Sep 17 00:00:00 2001 From: "tan.nguyen" Date: Thu, 28 Mar 2024 14:21:44 +0700 Subject: [PATCH 04/37] Remove debug option. --- src/Commands/ArtifactCommand.php | 11 ----------- tests/phpunit/AbstractTestCase.php | 2 +- tests/phpunit/Functional/ForcePushTest.php | 4 ++-- tests/phpunit/Functional/GeneralTest.php | 2 +- 4 files changed, 4 insertions(+), 15 deletions(-) diff --git a/src/Commands/ArtifactCommand.php b/src/Commands/ArtifactCommand.php index 4bd1b8f..aa72be1 100644 --- a/src/Commands/ArtifactCommand.php +++ b/src/Commands/ArtifactCommand.php @@ -112,11 +112,6 @@ class ArtifactCommand extends Command { */ protected bool $result = FALSE; - /** - * Flag to print debug information. - */ - protected bool $debug = FALSE; - /** * Internal option to set current timestamp. */ @@ -164,7 +159,6 @@ protected function configure(): void { $this ->addOption('branch', NULL, InputOption::VALUE_REQUIRED, 'Destination branch with optional tokens.', '[branch]') - ->addOption('debug', NULL, InputOption::VALUE_NONE, 'Print debug information.') ->addOption( 'gitignore', NULL, @@ -223,9 +217,6 @@ protected function configure(): void { * @throws \Exception */ protected function execute(InputInterface $input, OutputInterface $output): int { - if ($input->getOption('debug')) { - $output->setVerbosity(OutputInterface::VERBOSITY_DEBUG); - } $this->output = $output; $this->logger = self::createLogger((string) $this->getName(), $output); try { @@ -273,7 +264,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int */ protected function processArtifact(string $remote, array $opts = [ 'branch' => '[branch]', - 'debug' => FALSE, 'gitignore' => '', 'message' => 'Deployment commit', 'mode' => 'force-push', @@ -512,7 +502,6 @@ protected function resolveOptions(string $remote, array $options): void { $this->needsPush = !empty($options['push']); $this->reportFile = empty($options['report']) ? '' : $options['report']; $this->now = empty($options['now']) ? time() : (int) $options['now']; - $this->debug = !empty($options['debug']); $this->remoteName = self::GIT_REMOTE_NAME; $this->remoteUrl = $remote; $this->setMode($options['mode'], $options); diff --git a/tests/phpunit/AbstractTestCase.php b/tests/phpunit/AbstractTestCase.php index e48e6d6..8726a4d 100644 --- a/tests/phpunit/AbstractTestCase.php +++ b/tests/phpunit/AbstractTestCase.php @@ -78,7 +78,7 @@ protected function tearDown(): void { * TRUE if is in debug mode, FALSE otherwise. */ protected function isDebug(): bool { - return in_array('--debug', $_SERVER['argv'], TRUE); + return in_array('-vvv', $_SERVER['argv'], TRUE); } } diff --git a/tests/phpunit/Functional/ForcePushTest.php b/tests/phpunit/Functional/ForcePushTest.php index 79d5e7c..b793780 100644 --- a/tests/phpunit/Functional/ForcePushTest.php +++ b/tests/phpunit/Functional/ForcePushTest.php @@ -75,7 +75,7 @@ public function testSubRepos(): void { $this->gitAssertFilesNotExist($this->src, ['r2/r21.git/index']); $this->gitAssertFilesNotExist($this->src, ['r3/r31/r311/.git/index']); - $output = $this->assertBuildSuccess('--debug'); + $output = $this->assertBuildSuccess('-vvv'); $this->assertStringContainsString(sprintf('Removing sub-repository "%s"', $this->src . DIRECTORY_SEPARATOR . 'r1/.git'), $output); $this->assertStringContainsString(sprintf('Removing sub-repository "%s"', $this->src . DIRECTORY_SEPARATOR . 'r2/r21/.git'), $output); $this->assertStringContainsString(sprintf('Removing sub-repository "%s"', $this->src . DIRECTORY_SEPARATOR . 'r3/r31/r311/.git'), $output); @@ -234,7 +234,7 @@ public function testGitignoreCustomWhitelisting(): void { ]); // Run the build. - $this->assertBuildSuccess('--debug --gitignore=' . $this->src . DIRECTORY_SEPARATOR . 'mygitignore'); + $this->assertBuildSuccess('-vvv --gitignore=' . $this->src . DIRECTORY_SEPARATOR . 'mygitignore'); $this->assertFixtureCommits(2, $this->dst, 'testbranch', ['Custom third commit', 'Deployment commit'], FALSE); diff --git a/tests/phpunit/Functional/GeneralTest.php b/tests/phpunit/Functional/GeneralTest.php index 7fdac32..2d44760 100644 --- a/tests/phpunit/Functional/GeneralTest.php +++ b/tests/phpunit/Functional/GeneralTest.php @@ -81,7 +81,7 @@ public function testReport(): void { public function testDebug(): void { $this->gitCreateFixtureCommits(1); - $output = $this->runBuild('--debug'); + $output = $this->runBuild('-vvv'); $this->assertStringContainsString('Debug messages enabled', $output); From e4c94d5534fdabca4b896eae45755b0622225cc6 Mon Sep 17 00:00:00 2001 From: "tan.nguyen" Date: Thu, 28 Mar 2024 14:48:38 +0700 Subject: [PATCH 05/37] Remove file report option. --- src/Commands/ArtifactCommand.php | 29 ++++++++++++------------ src/LogTrait.php | 28 +++++++++++++---------- tests/phpunit/Functional/GeneralTest.php | 2 +- 3 files changed, 32 insertions(+), 27 deletions(-) diff --git a/src/Commands/ArtifactCommand.php b/src/Commands/ArtifactCommand.php index aa72be1..bd78f8b 100644 --- a/src/Commands/ArtifactCommand.php +++ b/src/Commands/ArtifactCommand.php @@ -98,9 +98,9 @@ class ArtifactCommand extends Command { protected bool $needCleanup = TRUE; /** - * Path to report file. + * Path to log file. */ - protected string $reportFile = ''; + protected string $logFile = ''; /** * Flag to show changes made to the repo by the build in the output. @@ -182,7 +182,7 @@ protected function configure(): void { ->addOption('no-cleanup', NULL, InputOption::VALUE_NONE, 'Do not cleanup after run.') ->addOption('now', NULL, InputOption::VALUE_REQUIRED, 'Internal value used to set internal time.') ->addOption('push', NULL, InputOption::VALUE_NONE, 'Push artifact to the remote repository') - ->addOption('report', NULL, InputOption::VALUE_REQUIRED, 'Path to the report file.') + ->addOption('log', NULL, InputOption::VALUE_REQUIRED, 'Path to the log file.') ->addOption( 'root', NULL, @@ -217,11 +217,16 @@ protected function configure(): void { * @throws \Exception */ protected function execute(InputInterface $input, OutputInterface $output): int { - $this->output = $output; - $this->logger = self::createLogger((string) $this->getName(), $output); try { - $this->checkRequirements(); + $this->output = $output; $remote = $input->getArgument('remote'); + // Resolve options first. + // @phpstan-ignore-next-line + $this->resolveOptions($remote, $input->getOptions()); + $this->logger = self::createLogger((string) $this->getName(), $output, $this->logFile); + // Now we have all what we need. + // Let process artifact function. + $this->checkRequirements(); // @phpstan-ignore-next-line $this->processArtifact($remote, $input->getOptions()); } @@ -270,17 +275,13 @@ protected function processArtifact(string $remote, array $opts = [ 'no-cleanup' => FALSE, 'now' => '', 'push' => FALSE, - 'report' => '', + 'log' => '', 'root' => '', 'show-changes' => FALSE, 'src' => '', ]): void { try { $error = NULL; - $this->resolveOptions($remote, $opts); - - // Now we have all what we need. - // Let process artifact function. $this->printDebug('Debug messages enabled'); $this->setupRemoteForRepository(); @@ -300,7 +301,7 @@ protected function processArtifact(string $remote, array $opts = [ $error = $exception->getMessage(); } - if (!empty($this->reportFile)) { + if (!empty($this->logFile)) { $this->dumpReport(); } @@ -500,7 +501,7 @@ protected function resolveOptions(string $remote, array $options): void { $this->showChanges = !empty($options['show-changes']); $this->needCleanup = empty($options['no-cleanup']); $this->needsPush = !empty($options['push']); - $this->reportFile = empty($options['report']) ? '' : $options['report']; + $this->logFile = empty($options['log']) ? '' : $options['log']; $this->now = empty($options['now']) ? time() : (int) $options['now']; $this->remoteName = self::GIT_REMOTE_NAME; $this->remoteUrl = $remote; @@ -577,7 +578,7 @@ protected function dumpReport(): void { $lines[] = ' Push result: ' . ($this->result ? 'Success' : 'Failure'); $lines[] = '----------------------------------------------------------------------'; - $this->fsFileSystem->dumpFile($this->reportFile, implode(PHP_EOL, $lines)); + $this->fsFileSystem->dumpFile($this->logFile, implode(PHP_EOL, $lines)); } /** diff --git a/src/LogTrait.php b/src/LogTrait.php index 25e5763..607a106 100644 --- a/src/LogTrait.php +++ b/src/LogTrait.php @@ -32,21 +32,25 @@ trait LogTrait { * @return \Psr\Log\LoggerInterface * Logger. */ - public static function createLogger(string $name, OutputInterface $output): LoggerInterface { + public static function createLogger(string $name, OutputInterface $output, string $logFile): LoggerInterface { $logger = new Logger($name); - $verbosityMapping = [ - OutputInterface::VERBOSITY_QUIET => Level::Error, - OutputInterface::VERBOSITY_NORMAL => Level::Warning, - OutputInterface::VERBOSITY_VERBOSE => Level::Notice, - OutputInterface::VERBOSITY_VERY_VERBOSE => Level::Info, - OutputInterface::VERBOSITY_DEBUG => Level::Debug, - ]; - $verbosity = $output->getVerbosity(); - $level = $verbosityMapping[$verbosity] ?? Level::Debug; - $handler = new StreamHandler(sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'hello-log.log', $level); - $logger->pushHandler($handler); + // Console handler. $handler = new ConsoleHandler($output); $logger->pushHandler($handler); + // Stream handler if needed. + if (!empty($logFile)) { + $verbosityMapping = [ + OutputInterface::VERBOSITY_QUIET => Level::Error, + OutputInterface::VERBOSITY_NORMAL => Level::Warning, + OutputInterface::VERBOSITY_VERBOSE => Level::Notice, + OutputInterface::VERBOSITY_VERY_VERBOSE => Level::Info, + OutputInterface::VERBOSITY_DEBUG => Level::Debug, + ]; + $verbosity = $output->getVerbosity(); + $level = $verbosityMapping[$verbosity] ?? Level::Debug; + $handler = new StreamHandler($logFile, $level); + $logger->pushHandler($handler); + } return $logger; } diff --git a/tests/phpunit/Functional/GeneralTest.php b/tests/phpunit/Functional/GeneralTest.php index 2d44760..cf7c463 100644 --- a/tests/phpunit/Functional/GeneralTest.php +++ b/tests/phpunit/Functional/GeneralTest.php @@ -66,7 +66,7 @@ public function testReport(): void { $report = $this->src . DIRECTORY_SEPARATOR . 'report.txt'; $this->gitCreateFixtureCommits(1); - $this->runBuild(sprintf('--report=%s', $report)); + $this->runBuild(sprintf('--log=%s', $report)); $this->assertFileExists($report); $output = file_get_contents($report); From 8ec6ae0d09ffba86e0f73808ebd3b228e3c6ac0c Mon Sep 17 00:00:00 2001 From: "tan.nguyen" Date: Thu, 28 Mar 2024 15:04:31 +0700 Subject: [PATCH 06/37] Update logic show message. --- src/Commands/ArtifactCommand.php | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/src/Commands/ArtifactCommand.php b/src/Commands/ArtifactCommand.php index bd78f8b..ec9cc38 100644 --- a/src/Commands/ArtifactCommand.php +++ b/src/Commands/ArtifactCommand.php @@ -223,18 +223,22 @@ protected function execute(InputInterface $input, OutputInterface $output): int // Resolve options first. // @phpstan-ignore-next-line $this->resolveOptions($remote, $input->getOptions()); + // Set logger. $this->logger = self::createLogger((string) $this->getName(), $output, $this->logFile); + // Now we have all what we need. // Let process artifact function. $this->checkRequirements(); - // @phpstan-ignore-next-line - $this->processArtifact($remote, $input->getOptions()); + $this->processArtifact(); } catch (\Exception $exception) { + $this->say('Deployment failed.'); $output->writeln('' . $exception->getMessage() . ''); return Command::FAILURE; } + $this->say('Deployment finished successfully.'); + return Command::SUCCESS; } @@ -267,19 +271,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int * * @phpstan-ignore-next-line */ - protected function processArtifact(string $remote, array $opts = [ - 'branch' => '[branch]', - 'gitignore' => '', - 'message' => 'Deployment commit', - 'mode' => 'force-push', - 'no-cleanup' => FALSE, - 'now' => '', - 'push' => FALSE, - 'log' => '', - 'root' => '', - 'show-changes' => FALSE, - 'src' => '', - ]): void { + protected function processArtifact(): void { try { $error = NULL; $this->printDebug('Debug messages enabled'); @@ -309,11 +301,7 @@ protected function processArtifact(string $remote, array $opts = [ $this->cleanup(); } - if ($this->result) { - $this->say('Deployment finished successfully.'); - } - else { - $this->say('Deployment failed.'); + if (!$this->result) { throw new \Exception((string) $error); } } From 3dc28757c6fde5c1ed8b57874ac6b4e97047e382 Mon Sep 17 00:00:00 2001 From: "tan.nguyen" Date: Thu, 28 Mar 2024 15:25:14 +0700 Subject: [PATCH 07/37] Deployment commit --- .idea/workspace.xml | 209 +++++++++++++++++++++++++++++++ src/Commands/ArtifactCommand.php | 1 + src/LogTrait.php | 12 ++ 3 files changed, 222 insertions(+) create mode 100644 .idea/workspace.xml diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..2a19aea --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,209 @@ + + + + + + + + + + + + + + + $PROJECT_DIR$/composer.json + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + { + "associatedIndex": 3 +} + + + + + + + { + "keyToString": { + "RunOnceActivity.OpenProjectViewOnStart": "true", + "RunOnceActivity.ShowReadmeOnStart": "true", + "node.js.detected.package.eslint": "true", + "node.js.selected.package.eslint": "(autodetect)", + "node.js.selected.package.tslint": "(autodetect)", + "nodejs_package_manager_path": "npm", + "vue.rearranger.settings.migration": "true" + } +} + + + + + + + + + + + + + + + + + 1711330852789 + + + + + + \ No newline at end of file diff --git a/src/Commands/ArtifactCommand.php b/src/Commands/ArtifactCommand.php index ec9cc38..237a5ba 100644 --- a/src/Commands/ArtifactCommand.php +++ b/src/Commands/ArtifactCommand.php @@ -690,6 +690,7 @@ protected function setGitignoreFile(string $path): void { protected function checkRequirements(): void { // @todo Refactor this into more generic implementation. $this->say('Checking requirements'); + $this->logNotice('Checking requirements'); if (!$this->fsIsCommandAvailable('git')) { throw new \RuntimeException('At least one of the script running requirements was not met'); } diff --git a/src/LogTrait.php b/src/LogTrait.php index 607a106..78b562d 100644 --- a/src/LogTrait.php +++ b/src/LogTrait.php @@ -67,4 +67,16 @@ public function logDebug(string|\Stringable $message, array $context = []): void $this->logger->debug($message, $context); } + /** + * Log debug. + * + * @param string|\Stringable $message + * Message. + * @param array $context + * Context. + */ + public function logNotice(string|\Stringable $message, array $context = []): void { + $this->logger->notice($message, $context); + } + } From 699f40e846e24b3eab6c498473bef6a524a149e7 Mon Sep 17 00:00:00 2001 From: "tan.nguyen" Date: Thu, 28 Mar 2024 15:35:53 +0700 Subject: [PATCH 08/37] replace printdebug by logdebug. --- src/Commands/ArtifactCommand.php | 37 +++++++++++--------------------- src/LogTrait.php | 14 +++++++++++- 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/src/Commands/ArtifactCommand.php b/src/Commands/ArtifactCommand.php index 237a5ba..27930e6 100644 --- a/src/Commands/ArtifactCommand.php +++ b/src/Commands/ArtifactCommand.php @@ -274,7 +274,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int protected function processArtifact(): void { try { $error = NULL; - $this->printDebug('Debug messages enabled'); + $this->logDebug('Debug messages enabled'); $this->setupRemoteForRepository(); $this->showInfo(); @@ -689,12 +689,11 @@ protected function setGitignoreFile(string $path): void { */ protected function checkRequirements(): void { // @todo Refactor this into more generic implementation. - $this->say('Checking requirements'); $this->logNotice('Checking requirements'); if (!$this->fsIsCommandAvailable('git')) { throw new \RuntimeException('At least one of the script running requirements was not met'); } - $this->sayOkay('All requirements were met'); + $this->logNotice('All requirements were met'); } /** @@ -705,7 +704,7 @@ protected function checkRequirements(): void { */ protected function replaceGitignoreInGitRepository(string $filename): void { $path = $this->getSourcePathGitRepository(); - $this->printDebug('Replacing .gitignore: %s with %s', $path . DIRECTORY_SEPARATOR . '.gitignore', $filename); + $this->logDebug(sprintf('Replacing .gitignore: %s with %s', $path . DIRECTORY_SEPARATOR . '.gitignore', $filename)); $this->fsFileSystem->copy($filename, $path . DIRECTORY_SEPARATOR . '.gitignore', TRUE); $this->fsFileSystem->remove($filename); } @@ -787,7 +786,7 @@ protected function disableLocalExclude(string $path): void { $filename = $this->getLocalExcludeFileName($path); $filenameDisabled = $filename . '.bak'; if ($this->fsFileSystem->exists($filename)) { - $this->printDebug('Disabling local exclude'); + $this->logDebug('Disabling local exclude'); $this->fsFileSystem->rename($filename, $filenameDisabled); } } @@ -802,7 +801,7 @@ protected function restoreLocalExclude(string $path): void { $filename = $this->getLocalExcludeFileName($path); $filenameDisabled = $filename . '.bak'; if ($this->fsFileSystem->exists($filenameDisabled)) { - $this->printDebug('Restoring local exclude'); + $this->logDebug('Restoring local exclude'); $this->fsFileSystem->rename($filenameDisabled, $filename); } } @@ -824,12 +823,12 @@ protected function removeIgnoredFiles(string $location, string $gitignorePath = $gitignoreContent = file_get_contents($gitignorePath); if (!$gitignoreContent) { - $this->printDebug('Unable to load ' . $gitignoreContent); + $this->logDebug('Unable to load ' . $gitignoreContent); } else { - $this->printDebug('-----.gitignore---------'); - $this->printDebug($gitignoreContent); - $this->printDebug('-----.gitignore---------'); + $this->logDebug('-----.gitignore---------'); + $this->logDebug($gitignoreContent); + $this->logDebug('-----.gitignore---------'); } $files = $this @@ -840,7 +839,7 @@ protected function removeIgnoredFiles(string $location, string $gitignorePath = $files = array_filter($files); foreach ($files as $file) { $fileName = $location . DIRECTORY_SEPARATOR . $file; - $this->printDebug('Removing excluded file %s', $fileName); + $this->logDebug(sprintf('Removing excluded file %s', $fileName)); if ($this->fsFileSystem->exists($fileName)) { $this->fsFileSystem->remove($fileName); } @@ -862,7 +861,7 @@ protected function removeOtherFilesInGitRepository(): void { $files = array_filter($files); foreach ($files as $file) { $fileName = $this->getSourcePathGitRepository() . DIRECTORY_SEPARATOR . $file; - $this->printDebug('Removing other file %s', $fileName); + $this->logDebug(sprintf('Removing other file %s', $fileName)); $this->fsFileSystem->remove($fileName); } } @@ -888,7 +887,7 @@ protected function removeSubReposInGitRepository(): void { $dir = $dir->getPathname(); } $this->fsFileSystem->remove($dir); - $this->printDebug('Removing sub-repository "%s"', (string) $dir); + $this->logDebug(sprintf('Removing sub-repository "%s"', (string) $dir)); } } @@ -979,18 +978,6 @@ protected function sayOkay(string $text): void { $this->writeln(sprintf($format, $char, $text)); } - /** - * Print debug information. - * - * @param mixed ...$args - * The args. - */ - protected function printDebug(mixed ...$args): void { - $message = array_shift($args); - /* @phpstan-ignore-next-line */ - $this->logDebug(vsprintf($message, $args)); - } - /** * Write output. * diff --git a/src/LogTrait.php b/src/LogTrait.php index 78b562d..d064fd1 100644 --- a/src/LogTrait.php +++ b/src/LogTrait.php @@ -68,7 +68,7 @@ public function logDebug(string|\Stringable $message, array $context = []): void } /** - * Log debug. + * Log notice. * * @param string|\Stringable $message * Message. @@ -79,4 +79,16 @@ public function logNotice(string|\Stringable $message, array $context = []): voi $this->logger->notice($message, $context); } + /** + * Log error. + * + * @param string|\Stringable $message + * Message. + * @param array $context + * Context. + */ + public function logError(string|\Stringable $message, array $context = []): void { + $this->logger->error($message, $context); + } + } From 4a4998d6e643f10445abbfd8dd3e6424adf0eda0 Mon Sep 17 00:00:00 2001 From: "tan.nguyen" Date: Thu, 28 Mar 2024 15:37:59 +0700 Subject: [PATCH 09/37] Delete unuse file. --- .idea/workspace.xml | 209 -------------------------------------------- 1 file changed, 209 deletions(-) delete mode 100644 .idea/workspace.xml diff --git a/.idea/workspace.xml b/.idea/workspace.xml deleted file mode 100644 index 2a19aea..0000000 --- a/.idea/workspace.xml +++ /dev/null @@ -1,209 +0,0 @@ - - - - - - - - - - - - - - - $PROJECT_DIR$/composer.json - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - { - "associatedIndex": 3 -} - - - - - - - { - "keyToString": { - "RunOnceActivity.OpenProjectViewOnStart": "true", - "RunOnceActivity.ShowReadmeOnStart": "true", - "node.js.detected.package.eslint": "true", - "node.js.selected.package.eslint": "(autodetect)", - "node.js.selected.package.tslint": "(autodetect)", - "nodejs_package_manager_path": "npm", - "vue.rearranger.settings.migration": "true" - } -} - - - - - - - - - - - - - - - - - 1711330852789 - - - - - - \ No newline at end of file From 582072ac5e5cce59c3777ea29a75efcda58e58b7 Mon Sep 17 00:00:00 2001 From: "tan.nguyen" Date: Thu, 28 Mar 2024 15:43:04 +0700 Subject: [PATCH 10/37] Show info using lognotice. --- src/Commands/ArtifactCommand.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Commands/ArtifactCommand.php b/src/Commands/ArtifactCommand.php index 27930e6..c303be7 100644 --- a/src/Commands/ArtifactCommand.php +++ b/src/Commands/ArtifactCommand.php @@ -546,7 +546,10 @@ protected function showInfo(): void { $lines[] = (' Gitignore file: ' . ($this->gitignoreFile ?: 'No')); $lines[] = (' Will push: ' . ($this->needsPush ? 'Yes' : 'No')); $lines[] = ('----------------------------------------------------------------------'); - $this->output->writeln($lines); + + foreach ($lines as $line) { + $this->logNotice($line); + } } /** From dbc228c61fdb9ec6743fd19c069c5646eef49b08 Mon Sep 17 00:00:00 2001 From: "tan.nguyen" Date: Thu, 28 Mar 2024 16:02:48 +0700 Subject: [PATCH 11/37] Style output. --- src/Commands/ArtifactCommand.php | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/Commands/ArtifactCommand.php b/src/Commands/ArtifactCommand.php index c303be7..e47ec22 100644 --- a/src/Commands/ArtifactCommand.php +++ b/src/Commands/ArtifactCommand.php @@ -14,6 +14,7 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Finder\Finder; @@ -127,6 +128,11 @@ class ArtifactCommand extends Command { */ protected GitArtifactGit $git; + /** + * Symfony style. + */ + protected SymfonyStyle $io; + /** * Artifact constructor. * @@ -217,9 +223,10 @@ protected function configure(): void { * @throws \Exception */ protected function execute(InputInterface $input, OutputInterface $output): int { + $this->io = new SymfonyStyle($input, $output); + $this->output = $output; + $remote = $input->getArgument('remote'); try { - $this->output = $output; - $remote = $input->getArgument('remote'); // Resolve options first. // @phpstan-ignore-next-line $this->resolveOptions($remote, $input->getOptions()); @@ -232,12 +239,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int $this->processArtifact(); } catch (\Exception $exception) { - $this->say('Deployment failed.'); - $output->writeln('' . $exception->getMessage() . ''); + $this->io->error('Deployment failed.'); + $this->io->error($exception->getMessage()); return Command::FAILURE; } - $this->say('Deployment finished successfully.'); + $this->io->success('Deployment finished successfully.'); return Command::SUCCESS; } From 3bcf172bbb55b03fb593a4e3579db2027916b0aa Mon Sep 17 00:00:00 2001 From: "tan.nguyen" Date: Thu, 28 Mar 2024 16:10:07 +0700 Subject: [PATCH 12/37] Relace some say statements. --- src/Commands/ArtifactCommand.php | 70 +++----------------------------- 1 file changed, 6 insertions(+), 64 deletions(-) diff --git a/src/Commands/ArtifactCommand.php b/src/Commands/ArtifactCommand.php index e47ec22..8d70f77 100644 --- a/src/Commands/ArtifactCommand.php +++ b/src/Commands/ArtifactCommand.php @@ -291,7 +291,7 @@ protected function processArtifact(): void { $this->doPush(); } else { - $this->yell('Cowardly refusing to push to remote. Use --push option to perform an actual push.'); + $this->io->warning('Cowardly refusing to push to remote. Use --push option to perform an actual push.'); } $this->result = TRUE; } @@ -373,7 +373,8 @@ protected function prepareArtifact(): void { $result = $this->commitAllChangesInGitRepository(); // Show all changes if needed. if ($this->showChanges) { - $this->say(sprintf('Added changes: %s', implode("\n", $result))); + $this->io->info(sprintf('Added changes: %s', implode("\n", $result))); + $this->logNotice(sprintf('Added changes: %s', implode("\n", $result))); } } @@ -459,7 +460,7 @@ protected function doPush(): void { $this->gitRepository->push([$this->remoteName, $refSpec]); } - $this->sayOkay(sprintf('Pushed branch "%s" with commit message "%s"', $this->destinationBranch, $this->message)); + $this->io->success(sprintf('Pushed branch "%s" with commit message "%s"', $this->destinationBranch, $this->message)); } catch (\Exception $exception) { // Re-throw the message with additional context. @@ -590,7 +591,7 @@ protected function dumpReport(): void { * @phpstan-ignore-next-line */ protected function setMode(string $mode, array $options): void { - $this->say(sprintf('Running in "%s" mode', $mode)); + $this->io->info(sprintf('Running in "%s" mode', $mode)); switch ($mode) { case self::modeForcePush(): @@ -599,7 +600,7 @@ protected function setMode(string $mode, array $options): void { case self::modeBranch(): if (!$this->hasToken($options['branch'])) { - $this->say('WARNING! Provided branch name does not have a token. + $this->io->warning('WARNING! Provided branch name does not have a token. Pushing of the artifact into this branch will fail on second and follow up pushes to remote. Consider adding tokens with unique values to the branch name.'); } @@ -948,46 +949,6 @@ protected function getTokenTimestamp(string $format = 'Y-m-d_H-i-s'): string { return date($format, $this->now); } - /** - * Write line as yell style. - * - * @param string $text - * Text yell. - */ - protected function yell(string $text): void { - $color = 'green'; - $char = $this->decorationCharacter('>', '➜'); - $format = sprintf('%%s %%s', $color, $color); - $this->writeln(sprintf($format, $char, $text)); - } - - /** - * Write line as say style. - * - * @param string $text - * Text. - */ - protected function say(string $text): void { - $char = $this->decorationCharacter('>', '➜'); - $this->writeln(sprintf('%s %s', $char, $text)); - } - - /** - * Print success message. - * - * Usually used to explicitly state that some action was successfully - * executed. - * - * @param string $text - * Message text. - */ - protected function sayOkay(string $text): void { - $color = 'green'; - $char = $this->decorationCharacter('V', '✔'); - $format = sprintf('%%s %%s', $color, $color); - $this->writeln(sprintf($format, $char, $text)); - } - /** * Write output. * @@ -998,25 +959,6 @@ protected function writeln(string $text): void { $this->output->writeln($text); } - /** - * Decoration character. - * - * @param string $nonDecorated - * Non decorated. - * @param string $decorated - * Decorated. - * - * @return string - * The decoration character. - */ - protected function decorationCharacter(string $nonDecorated, string $decorated): string { - if (!$this->output->isDecorated() || (strncasecmp(PHP_OS, 'WIN', 3) === 0)) { - return $nonDecorated; - } - - return $decorated; - } - /** * Setup remote for current repository. * From a6e64ea5ffda705f56cc04660f490a33aa5ad459 Mon Sep 17 00:00:00 2001 From: "tan.nguyen" Date: Thu, 28 Mar 2024 16:20:51 +0700 Subject: [PATCH 13/37] Test. --- src/Commands/ArtifactCommand.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Commands/ArtifactCommand.php b/src/Commands/ArtifactCommand.php index 8d70f77..21d71fd 100644 --- a/src/Commands/ArtifactCommand.php +++ b/src/Commands/ArtifactCommand.php @@ -239,8 +239,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int $this->processArtifact(); } catch (\Exception $exception) { - $this->io->error('Deployment failed.'); - $this->io->error($exception->getMessage()); + $this->io->error([ + 'Deployment failed.', + $exception->getMessage(), + ]); return Command::FAILURE; } From cb866a4db285914aa31a3beff05a9f8c3ff7715c Mon Sep 17 00:00:00 2001 From: "tan.nguyen" Date: Thu, 28 Mar 2024 16:26:05 +0700 Subject: [PATCH 14/37] Log notice. --- src/Commands/ArtifactCommand.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Commands/ArtifactCommand.php b/src/Commands/ArtifactCommand.php index 21d71fd..a660e33 100644 --- a/src/Commands/ArtifactCommand.php +++ b/src/Commands/ArtifactCommand.php @@ -579,7 +579,9 @@ protected function dumpReport(): void { $lines[] = ' Push result: ' . ($this->result ? 'Success' : 'Failure'); $lines[] = '----------------------------------------------------------------------'; - $this->fsFileSystem->dumpFile($this->logFile, implode(PHP_EOL, $lines)); + foreach ($lines as $line) { + $this->logNotice($line); + } } /** From 0244027424a436356bd73e8e4fa89c7486f59e6e Mon Sep 17 00:00:00 2001 From: "tan.nguyen" Date: Thu, 28 Mar 2024 16:39:17 +0700 Subject: [PATCH 15/37] Test. --- src/Commands/ArtifactCommand.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Commands/ArtifactCommand.php b/src/Commands/ArtifactCommand.php index a660e33..6736755 100644 --- a/src/Commands/ArtifactCommand.php +++ b/src/Commands/ArtifactCommand.php @@ -595,8 +595,7 @@ protected function dumpReport(): void { * @phpstan-ignore-next-line */ protected function setMode(string $mode, array $options): void { - $this->io->info(sprintf('Running in "%s" mode', $mode)); - + $this->io->writeln(sprintf('Running in "%s" mode', $mode)); switch ($mode) { case self::modeForcePush(): // Intentionally empty. From 58bf88108fb49e6e84d45bad36161104be637f49 Mon Sep 17 00:00:00 2001 From: "tan.nguyen" Date: Thu, 28 Mar 2024 21:18:54 +0700 Subject: [PATCH 16/37] Update the text. --- src/Commands/ArtifactCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Commands/ArtifactCommand.php b/src/Commands/ArtifactCommand.php index 6736755..c1fcc38 100644 --- a/src/Commands/ArtifactCommand.php +++ b/src/Commands/ArtifactCommand.php @@ -557,6 +557,7 @@ protected function showInfo(): void { $lines[] = (' Will push: ' . ($this->needsPush ? 'Yes' : 'No')); $lines[] = ('----------------------------------------------------------------------'); + $this->io->text($lines); foreach ($lines as $line) { $this->logNotice($line); } @@ -595,7 +596,6 @@ protected function dumpReport(): void { * @phpstan-ignore-next-line */ protected function setMode(string $mode, array $options): void { - $this->io->writeln(sprintf('Running in "%s" mode', $mode)); switch ($mode) { case self::modeForcePush(): // Intentionally empty. From 0b5997610db9d5aae18f42ef7ea9df14105b4194 Mon Sep 17 00:00:00 2001 From: "tan.nguyen" Date: Thu, 28 Mar 2024 21:33:30 +0700 Subject: [PATCH 17/37] test. --- src/Commands/ArtifactCommand.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Commands/ArtifactCommand.php b/src/Commands/ArtifactCommand.php index c1fcc38..3a90ccb 100644 --- a/src/Commands/ArtifactCommand.php +++ b/src/Commands/ArtifactCommand.php @@ -421,6 +421,11 @@ protected function addAllFilesInGitRepository(): void { else { $this->gitRepository->addAllChanges(); } + + // We do not want to add the log file. + if (!empty($this->logFile)) { + $this->fsFileSystem->remove($this->logFile); + } } /** From b9a6eb011cb473ab04271f81e9a77ff224ff2b8b Mon Sep 17 00:00:00 2001 From: "tan.nguyen" Date: Thu, 28 Mar 2024 22:12:45 +0700 Subject: [PATCH 18/37] test. --- src/Commands/ArtifactCommand.php | 4 ++-- src/GitArtifactGitRepository.php | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Commands/ArtifactCommand.php b/src/Commands/ArtifactCommand.php index 3a90ccb..f7e11d1 100644 --- a/src/Commands/ArtifactCommand.php +++ b/src/Commands/ArtifactCommand.php @@ -424,7 +424,7 @@ protected function addAllFilesInGitRepository(): void { // We do not want to add the log file. if (!empty($this->logFile)) { - $this->fsFileSystem->remove($this->logFile); + $this->gitRepository->unstageFile($this->logFile); } } @@ -504,7 +504,7 @@ protected function resolveOptions(string $remote, array $options): void { $this->showChanges = !empty($options['show-changes']); $this->needCleanup = empty($options['no-cleanup']); $this->needsPush = !empty($options['push']); - $this->logFile = empty($options['log']) ? '' : $options['log']; + $this->logFile = empty($options['log']) ? '' : $this->fsGetAbsolutePath($options['log']); $this->now = empty($options['now']) ? time() : (int) $options['now']; $this->remoteName = self::GIT_REMOTE_NAME; $this->remoteUrl = $remote; diff --git a/src/GitArtifactGitRepository.php b/src/GitArtifactGitRepository.php index b768a4e..96e77fd 100644 --- a/src/GitArtifactGitRepository.php +++ b/src/GitArtifactGitRepository.php @@ -334,6 +334,12 @@ public function isRemoteExists(string $remoteName): bool { return in_array($remoteName, $remotes); } + public function unstageFile(string $filePath) { + $this->run('restore', ['--staged' => $filePath]); + + return $this; + } + /** * Override run method to add --no-pager option to all command. * From a8eb5082fcd5dbad048e75e1f522942b21e01ee1 Mon Sep 17 00:00:00 2001 From: "tan.nguyen" Date: Thu, 28 Mar 2024 22:35:10 +0700 Subject: [PATCH 19/37] test. --- src/Commands/ArtifactCommand.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Commands/ArtifactCommand.php b/src/Commands/ArtifactCommand.php index f7e11d1..5333a0a 100644 --- a/src/Commands/ArtifactCommand.php +++ b/src/Commands/ArtifactCommand.php @@ -422,8 +422,7 @@ protected function addAllFilesInGitRepository(): void { $this->gitRepository->addAllChanges(); } - // We do not want to add the log file. - if (!empty($this->logFile)) { + if ($this->logFile) { $this->gitRepository->unstageFile($this->logFile); } } @@ -880,6 +879,11 @@ protected function removeOtherFilesInGitRepository(): void { $files = array_filter($files); foreach ($files as $file) { $fileName = $this->getSourcePathGitRepository() . DIRECTORY_SEPARATOR . $file; + // We do not want to remove log file if someone set log file in repo. + if (!empty($this->logFile) && $this->logFile === $fileName) { + continue; + } + $this->logDebug(sprintf('Removing other file %s', $fileName)); $this->fsFileSystem->remove($fileName); } From bf12210719e44358d558350aefbf4502fec96bf6 Mon Sep 17 00:00:00 2001 From: "tan.nguyen" Date: Thu, 28 Mar 2024 22:43:47 +0700 Subject: [PATCH 20/37] test --- src/Commands/ArtifactCommand.php | 9 +++++---- src/GitArtifactGitRepository.php | 6 ++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/Commands/ArtifactCommand.php b/src/Commands/ArtifactCommand.php index 5333a0a..5e67ed6 100644 --- a/src/Commands/ArtifactCommand.php +++ b/src/Commands/ArtifactCommand.php @@ -289,6 +289,11 @@ protected function processArtifact(): void { $this->showInfo(); $this->prepareArtifact(); + if ($this->logFile) { + $this->gitRepository->untrackFile($this->logFile); + $this->gitRepository->unstageFile($this->logFile); + } + if ($this->needsPush) { $this->doPush(); } @@ -421,10 +426,6 @@ protected function addAllFilesInGitRepository(): void { else { $this->gitRepository->addAllChanges(); } - - if ($this->logFile) { - $this->gitRepository->unstageFile($this->logFile); - } } /** diff --git a/src/GitArtifactGitRepository.php b/src/GitArtifactGitRepository.php index 96e77fd..dd0a52e 100644 --- a/src/GitArtifactGitRepository.php +++ b/src/GitArtifactGitRepository.php @@ -340,6 +340,12 @@ public function unstageFile(string $filePath) { return $this; } + public function untrackFile(string $filePath) { + $this->run('rm', ['--cached'], $filePath); + + return $this; + } + /** * Override run method to add --no-pager option to all command. * From eeefe0015ba5d856fa04b8a7054f09400a08583a Mon Sep 17 00:00:00 2001 From: "tan.nguyen" Date: Thu, 28 Mar 2024 22:44:22 +0700 Subject: [PATCH 21/37] test --- test.kakaaaaa | 1 + 1 file changed, 1 insertion(+) create mode 100644 test.kakaaaaa diff --git a/test.kakaaaaa b/test.kakaaaaa new file mode 100644 index 0000000..a143cda --- /dev/null +++ b/test.kakaaaaa @@ -0,0 +1 @@ +dasdagi From b6db3a9819aee78a9699d56cdea39b9d77c28553 Mon Sep 17 00:00:00 2001 From: "tan.nguyen" Date: Thu, 28 Mar 2024 22:51:41 +0700 Subject: [PATCH 22/37] test. --- src/Commands/ArtifactCommand.php | 19 +++++++------------ test.kakaaaaa | 1 - 2 files changed, 7 insertions(+), 13 deletions(-) delete mode 100644 test.kakaaaaa diff --git a/src/Commands/ArtifactCommand.php b/src/Commands/ArtifactCommand.php index 5e67ed6..682b6c4 100644 --- a/src/Commands/ArtifactCommand.php +++ b/src/Commands/ArtifactCommand.php @@ -231,12 +231,17 @@ protected function execute(InputInterface $input, OutputInterface $output): int // @phpstan-ignore-next-line $this->resolveOptions($remote, $input->getOptions()); // Set logger. - $this->logger = self::createLogger((string) $this->getName(), $output, $this->logFile); - + $tmpLogFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . time() . 'log.lock'; + $this->logger = self::createLogger((string) $this->getName(), $output, $tmpLogFile); // Now we have all what we need. // Let process artifact function. $this->checkRequirements(); $this->processArtifact(); + // Dump log file. + if ($this->logFile) { + $this->fsFileSystem->copy($tmpLogFile, $this->logFile); + } + $this->fsFileSystem->remove($tmpLogFile); } catch (\Exception $exception) { $this->io->error([ @@ -289,11 +294,6 @@ protected function processArtifact(): void { $this->showInfo(); $this->prepareArtifact(); - if ($this->logFile) { - $this->gitRepository->untrackFile($this->logFile); - $this->gitRepository->unstageFile($this->logFile); - } - if ($this->needsPush) { $this->doPush(); } @@ -880,11 +880,6 @@ protected function removeOtherFilesInGitRepository(): void { $files = array_filter($files); foreach ($files as $file) { $fileName = $this->getSourcePathGitRepository() . DIRECTORY_SEPARATOR . $file; - // We do not want to remove log file if someone set log file in repo. - if (!empty($this->logFile) && $this->logFile === $fileName) { - continue; - } - $this->logDebug(sprintf('Removing other file %s', $fileName)); $this->fsFileSystem->remove($fileName); } diff --git a/test.kakaaaaa b/test.kakaaaaa deleted file mode 100644 index a143cda..0000000 --- a/test.kakaaaaa +++ /dev/null @@ -1 +0,0 @@ -dasdagi From eea424e9253bf48f9e8499daa6b942bb2ec76306 Mon Sep 17 00:00:00 2001 From: "tan.nguyen" Date: Thu, 28 Mar 2024 22:57:45 +0700 Subject: [PATCH 23/37] test. --- src/Commands/ArtifactCommand.php | 4 ++++ src/GitArtifactGitRepository.php | 12 ------------ 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/src/Commands/ArtifactCommand.php b/src/Commands/ArtifactCommand.php index 682b6c4..d82817e 100644 --- a/src/Commands/ArtifactCommand.php +++ b/src/Commands/ArtifactCommand.php @@ -226,6 +226,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int $this->io = new SymfonyStyle($input, $output); $this->output = $output; $remote = $input->getArgument('remote'); + // If log option was set, we set verbosity is debugging. + if ($input->getOption('log')) { + $output->setVerbosity(OutputInterface::VERBOSITY_VERY_VERBOSE); + } try { // Resolve options first. // @phpstan-ignore-next-line diff --git a/src/GitArtifactGitRepository.php b/src/GitArtifactGitRepository.php index dd0a52e..b768a4e 100644 --- a/src/GitArtifactGitRepository.php +++ b/src/GitArtifactGitRepository.php @@ -334,18 +334,6 @@ public function isRemoteExists(string $remoteName): bool { return in_array($remoteName, $remotes); } - public function unstageFile(string $filePath) { - $this->run('restore', ['--staged' => $filePath]); - - return $this; - } - - public function untrackFile(string $filePath) { - $this->run('rm', ['--cached'], $filePath); - - return $this; - } - /** * Override run method to add --no-pager option to all command. * From 9583e0d5983b5e1d7b7cd1ac806cf6c8c1fcf242 Mon Sep 17 00:00:00 2001 From: "tan.nguyen" Date: Thu, 28 Mar 2024 22:59:12 +0700 Subject: [PATCH 24/37] test. --- src/Commands/ArtifactCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Commands/ArtifactCommand.php b/src/Commands/ArtifactCommand.php index d82817e..804d74d 100644 --- a/src/Commands/ArtifactCommand.php +++ b/src/Commands/ArtifactCommand.php @@ -235,7 +235,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int // @phpstan-ignore-next-line $this->resolveOptions($remote, $input->getOptions()); // Set logger. - $tmpLogFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . time() . 'log.lock'; + $tmpLogFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . time() . '-artifact-log.log'; $this->logger = self::createLogger((string) $this->getName(), $output, $tmpLogFile); // Now we have all what we need. // Let process artifact function. From 9fdd617ccd5ce848b7fce3a722969e2d096963f5 Mon Sep 17 00:00:00 2001 From: "tan.nguyen" Date: Thu, 28 Mar 2024 23:10:37 +0700 Subject: [PATCH 25/37] test. --- src/Commands/ArtifactCommand.php | 42 ++++++-------------------------- src/LogTrait.php | 2 ++ 2 files changed, 10 insertions(+), 34 deletions(-) diff --git a/src/Commands/ArtifactCommand.php b/src/Commands/ArtifactCommand.php index 804d74d..095c7ea 100644 --- a/src/Commands/ArtifactCommand.php +++ b/src/Commands/ArtifactCommand.php @@ -223,26 +223,25 @@ protected function configure(): void { * @throws \Exception */ protected function execute(InputInterface $input, OutputInterface $output): int { + // Setup io and logger. $this->io = new SymfonyStyle($input, $output); - $this->output = $output; - $remote = $input->getArgument('remote'); - // If log option was set, we set verbosity is debugging. + $tmpLogFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . time() . '-artifact-log.log'; + $this->logger = self::createLogger((string) $this->getName(), $output, $tmpLogFile); + // If log option was set, we set verbosity is very verbose. if ($input->getOption('log')) { $output->setVerbosity(OutputInterface::VERBOSITY_VERY_VERBOSE); } + $remote = $input->getArgument('remote'); try { - // Resolve options first. + // Let resolve options into properties first. // @phpstan-ignore-next-line $this->resolveOptions($remote, $input->getOptions()); - // Set logger. - $tmpLogFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . time() . '-artifact-log.log'; - $this->logger = self::createLogger((string) $this->getName(), $output, $tmpLogFile); // Now we have all what we need. // Let process artifact function. $this->checkRequirements(); $this->processArtifact(); - // Dump log file. - if ($this->logFile) { + // Dump log file and clean tmp log file. + if (!empty($this->logFile)) { $this->fsFileSystem->copy($tmpLogFile, $this->logFile); } $this->fsFileSystem->remove($tmpLogFile); @@ -263,37 +262,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int /** * Assemble a code artifact from your codebase. * - * @param string $remote - * Path to the remote git repository. - * @param array $opts - * Options. - * - * @option $branch Destination branch with optional tokens. - * @option $debug Print debug information. - * @option $gitignore Path to gitignore file to replace current .gitignore. - * @option $message Commit message with optional tokens. - * @option $mode Mode of artifact build: branch, force-push or diff. - * Defaults to force-push. - * @option $now Internal value used to set internal time. - * @option $no-cleanup Do not cleanup after run. - * @option $push Push artifact to the remote repository. Defaults to FALSE. - * @option $report Path to the report file. - * @option $root Path to the root for file path resolution. If not - * specified, current directory is used. - * @option $show-changes Show changes made to the repo by the build in the - * output. - * @option $src Directory where source repository is located. If not - * specified, root directory is used. - * * @throws \Exception - * - * @phpstan-ignore-next-line */ protected function processArtifact(): void { try { $error = NULL; $this->logDebug('Debug messages enabled'); - $this->setupRemoteForRepository(); $this->showInfo(); $this->prepareArtifact(); diff --git a/src/LogTrait.php b/src/LogTrait.php index d064fd1..c2ce54f 100644 --- a/src/LogTrait.php +++ b/src/LogTrait.php @@ -28,6 +28,8 @@ trait LogTrait { * Name. * @param \Symfony\Component\Console\Output\OutputInterface $output * Output. + * @param string $logFile + * Log file. * * @return \Psr\Log\LoggerInterface * Logger. From bf1eecc0210adc6117c59bcb2f526a67e53ba8e3 Mon Sep 17 00:00:00 2001 From: "tan.nguyen" Date: Thu, 28 Mar 2024 23:16:15 +0700 Subject: [PATCH 26/37] test. --- src/Commands/ArtifactCommand.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Commands/ArtifactCommand.php b/src/Commands/ArtifactCommand.php index 095c7ea..8503d6a 100644 --- a/src/Commands/ArtifactCommand.php +++ b/src/Commands/ArtifactCommand.php @@ -285,9 +285,7 @@ protected function processArtifact(): void { $error = $exception->getMessage(); } - if (!empty($this->logFile)) { - $this->dumpReport(); - } + $this->dumpReport(); if ($this->needCleanup) { $this->cleanup(); From e677b620a56a6ac5a9ebc07c7cd0950e4323b326 Mon Sep 17 00:00:00 2001 From: "tan.nguyen" Date: Thu, 28 Mar 2024 23:17:20 +0700 Subject: [PATCH 27/37] test --- src/Commands/ArtifactCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Commands/ArtifactCommand.php b/src/Commands/ArtifactCommand.php index 8503d6a..f355d53 100644 --- a/src/Commands/ArtifactCommand.php +++ b/src/Commands/ArtifactCommand.php @@ -285,7 +285,7 @@ protected function processArtifact(): void { $error = $exception->getMessage(); } - $this->dumpReport(); + $this->logReport(); if ($this->needCleanup) { $this->cleanup(); @@ -547,7 +547,7 @@ protected function showInfo(): void { /** * Dump artifact report to a file. */ - protected function dumpReport(): void { + protected function logReport(): void { $lines[] = '----------------------------------------------------------------------'; $lines[] = ' Artifact report'; $lines[] = '----------------------------------------------------------------------'; From fc769666d6124f8638bd03c0842ca1477844cf30 Mon Sep 17 00:00:00 2001 From: "tan.nguyen" Date: Thu, 28 Mar 2024 23:21:39 +0700 Subject: [PATCH 28/37] test --- src/Commands/ArtifactCommand.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Commands/ArtifactCommand.php b/src/Commands/ArtifactCommand.php index f355d53..0e1c22a 100644 --- a/src/Commands/ArtifactCommand.php +++ b/src/Commands/ArtifactCommand.php @@ -223,14 +223,14 @@ protected function configure(): void { * @throws \Exception */ protected function execute(InputInterface $input, OutputInterface $output): int { - // Setup io and logger. - $this->io = new SymfonyStyle($input, $output); - $tmpLogFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . time() . '-artifact-log.log'; - $this->logger = self::createLogger((string) $this->getName(), $output, $tmpLogFile); // If log option was set, we set verbosity is very verbose. if ($input->getOption('log')) { $output->setVerbosity(OutputInterface::VERBOSITY_VERY_VERBOSE); } + // Setup io and logger. + $this->io = new SymfonyStyle($input, $output); + $tmpLogFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . time() . '-artifact-log.log'; + $this->logger = self::createLogger((string) $this->getName(), $output, $tmpLogFile); $remote = $input->getArgument('remote'); try { // Let resolve options into properties first. From b54df34353d4035abdaeacf3f5aa8bd342fd7520 Mon Sep 17 00:00:00 2001 From: "tan.nguyen" Date: Thu, 28 Mar 2024 23:23:51 +0700 Subject: [PATCH 29/37] test --- src/Commands/ArtifactCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Commands/ArtifactCommand.php b/src/Commands/ArtifactCommand.php index 0e1c22a..1fa1d8c 100644 --- a/src/Commands/ArtifactCommand.php +++ b/src/Commands/ArtifactCommand.php @@ -223,9 +223,9 @@ protected function configure(): void { * @throws \Exception */ protected function execute(InputInterface $input, OutputInterface $output): int { - // If log option was set, we set verbosity is very verbose. + // If log option was set, we set verbosity is debug. if ($input->getOption('log')) { - $output->setVerbosity(OutputInterface::VERBOSITY_VERY_VERBOSE); + $output->setVerbosity(OutputInterface::VERBOSITY_DEBUG); } // Setup io and logger. $this->io = new SymfonyStyle($input, $output); From 93cc512f743a3e41ade9fd01cda7551b680a334c Mon Sep 17 00:00:00 2001 From: "tan.nguyen" Date: Thu, 28 Mar 2024 23:25:47 +0700 Subject: [PATCH 30/37] test --- src/Commands/ArtifactCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Commands/ArtifactCommand.php b/src/Commands/ArtifactCommand.php index 1fa1d8c..ef06a3c 100644 --- a/src/Commands/ArtifactCommand.php +++ b/src/Commands/ArtifactCommand.php @@ -356,7 +356,7 @@ protected function prepareArtifact(): void { $result = $this->commitAllChangesInGitRepository(); // Show all changes if needed. if ($this->showChanges) { - $this->io->info(sprintf('Added changes: %s', implode("\n", $result))); + $this->io->text(sprintf('Added changes: %s', implode("\n", $result))); $this->logNotice(sprintf('Added changes: %s', implode("\n", $result))); } } From 4a775536332d9bbf233353599efa87d584abb73d Mon Sep 17 00:00:00 2001 From: "tan.nguyen" Date: Fri, 29 Mar 2024 09:06:18 +0700 Subject: [PATCH 31/37] test. --- src/Commands/ArtifactCommand.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Commands/ArtifactCommand.php b/src/Commands/ArtifactCommand.php index ef06a3c..056f0dd 100644 --- a/src/Commands/ArtifactCommand.php +++ b/src/Commands/ArtifactCommand.php @@ -228,7 +228,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $output->setVerbosity(OutputInterface::VERBOSITY_DEBUG); } // Setup io and logger. - $this->io = new SymfonyStyle($input, $output); + $this->output = $output; $tmpLogFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . time() . '-artifact-log.log'; $this->logger = self::createLogger((string) $this->getName(), $output, $tmpLogFile); $remote = $input->getArgument('remote'); @@ -247,14 +247,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int $this->fsFileSystem->remove($tmpLogFile); } catch (\Exception $exception) { - $this->io->error([ - 'Deployment failed.', - $exception->getMessage(), + $this->output->writeln([ + 'Deployment failed.', + '' . $exception->getMessage() . '', ]); return Command::FAILURE; } - $this->io->success('Deployment finished successfully.'); + $this->output->writeln('Deployment finished successfully.'); return Command::SUCCESS; } @@ -276,7 +276,7 @@ protected function processArtifact(): void { $this->doPush(); } else { - $this->io->warning('Cowardly refusing to push to remote. Use --push option to perform an actual push.'); + $this->output->writeln('Cowardly refusing to push to remote. Use --push option to perform an actual push.'); } $this->result = TRUE; } @@ -356,7 +356,7 @@ protected function prepareArtifact(): void { $result = $this->commitAllChangesInGitRepository(); // Show all changes if needed. if ($this->showChanges) { - $this->io->text(sprintf('Added changes: %s', implode("\n", $result))); + $this->output->writeln(sprintf('Added changes: %s', implode("\n", $result))); $this->logNotice(sprintf('Added changes: %s', implode("\n", $result))); } } @@ -443,7 +443,7 @@ protected function doPush(): void { $this->gitRepository->push([$this->remoteName, $refSpec]); } - $this->io->success(sprintf('Pushed branch "%s" with commit message "%s"', $this->destinationBranch, $this->message)); + $this->output->writeln(sprintf('Pushed branch "%s" with commit message "%s"', $this->destinationBranch, $this->message)); } catch (\Exception $exception) { // Re-throw the message with additional context. @@ -538,7 +538,7 @@ protected function showInfo(): void { $lines[] = (' Will push: ' . ($this->needsPush ? 'Yes' : 'No')); $lines[] = ('----------------------------------------------------------------------'); - $this->io->text($lines); + $this->output->writeln($lines); foreach ($lines as $line) { $this->logNotice($line); } @@ -584,9 +584,9 @@ protected function setMode(string $mode, array $options): void { case self::modeBranch(): if (!$this->hasToken($options['branch'])) { - $this->io->warning('WARNING! Provided branch name does not have a token. + $this->output->writeln('WARNING! Provided branch name does not have a token. Pushing of the artifact into this branch will fail on second and follow up pushes to remote. - Consider adding tokens with unique values to the branch name.'); + Consider adding tokens with unique values to the branch name.'); } break; From 0a2d6740d899669feef8f69ce40f363de03a7e4c Mon Sep 17 00:00:00 2001 From: "tan.nguyen" Date: Fri, 29 Mar 2024 09:18:46 +0700 Subject: [PATCH 32/37] test. --- src/Commands/ArtifactCommand.php | 33 ++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/src/Commands/ArtifactCommand.php b/src/Commands/ArtifactCommand.php index 056f0dd..1815529 100644 --- a/src/Commands/ArtifactCommand.php +++ b/src/Commands/ArtifactCommand.php @@ -227,19 +227,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int if ($input->getOption('log')) { $output->setVerbosity(OutputInterface::VERBOSITY_DEBUG); } - // Setup io and logger. $this->output = $output; $tmpLogFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . time() . '-artifact-log.log'; $this->logger = self::createLogger((string) $this->getName(), $output, $tmpLogFile); $remote = $input->getArgument('remote'); try { - // Let resolve options into properties first. - // @phpstan-ignore-next-line - $this->resolveOptions($remote, $input->getOptions()); // Now we have all what we need. // Let process artifact function. $this->checkRequirements(); - $this->processArtifact(); + // @phpstan-ignore-next-line + $this->processArtifact($remote, $input->getOptions()); // Dump log file and clean tmp log file. if (!empty($this->logFile)) { $this->fsFileSystem->copy($tmpLogFile, $this->logFile); @@ -262,12 +259,36 @@ protected function execute(InputInterface $input, OutputInterface $output): int /** * Assemble a code artifact from your codebase. * + * @param string $remote + * Path to the remote git repository. + * @param array $options + * Options. + * + * @option $branch Destination branch with optional tokens. + * @option $debug Print debug information. + * @option $gitignore Path to gitignore file to replace current .gitignore. + * @option $message Commit message with optional tokens. + * @option $mode Mode of artifact build: branch, force-push or diff. + * Defaults to force-push. + * @option $now Internal value used to set internal time. + * @option $no-cleanup Do not cleanup after run. + * @option $push Push artifact to the remote repository. Defaults to FALSE. + * @option $report Path to the report file. + * @option $root Path to the root for file path resolution. If not + * specified, current directory is used. + * @option $show-changes Show changes made to the repo by the build in the + * output. + * @option $src Directory where source repository is located. If not + * specified, root directory is used. + * * @throws \Exception */ - protected function processArtifact(): void { + protected function processArtifact(string $remote, array $options): void { try { $error = NULL; $this->logDebug('Debug messages enabled'); + // Let resolve options into properties first. + $this->resolveOptions($remote, $options); $this->setupRemoteForRepository(); $this->showInfo(); $this->prepareArtifact(); From 60d2724db58771344cb993211d20f49885e510d5 Mon Sep 17 00:00:00 2001 From: "tan.nguyen" Date: Fri, 29 Mar 2024 09:22:00 +0700 Subject: [PATCH 33/37] test. --- src/Commands/ArtifactCommand.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Commands/ArtifactCommand.php b/src/Commands/ArtifactCommand.php index 1815529..8bc46d5 100644 --- a/src/Commands/ArtifactCommand.php +++ b/src/Commands/ArtifactCommand.php @@ -237,11 +237,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int $this->checkRequirements(); // @phpstan-ignore-next-line $this->processArtifact($remote, $input->getOptions()); + // Dump log file and clean tmp log file. - if (!empty($this->logFile)) { - $this->fsFileSystem->copy($tmpLogFile, $this->logFile); + if ($this->fsFileSystem->exists($tmpLogFile)) { + if (!empty($this->logFile)) { + $this->fsFileSystem->copy($tmpLogFile, $this->logFile); + } + $this->fsFileSystem->remove($tmpLogFile); } - $this->fsFileSystem->remove($tmpLogFile); } catch (\Exception $exception) { $this->output->writeln([ From 0ec8c1db28d2556b2fdc4706b3b5762a43960847 Mon Sep 17 00:00:00 2001 From: "tan.nguyen" Date: Fri, 29 Mar 2024 09:25:10 +0700 Subject: [PATCH 34/37] test. --- .circleci/config.yml | 10 +++++----- .github/workflows/test-php.yml | 10 +++++----- README.md | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 295dc0a..9be90bc 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -96,7 +96,7 @@ jobs: git@github.com:drevops/git-artifact-destination.git \ --branch=mode--force-push--circleci--[branch] \ --mode=force-push \ - --report=$HOME/report--mode--force-push.txt \ + --log=$HOME/report--mode--force-push.txt \ --push \ --debug @@ -121,7 +121,7 @@ jobs: git@github.com:drevops/git-artifact-destination.git \ --branch=mode--force-push--circleci--[branch] \ --mode=force-push \ - --report=$HOME/report--mode--force-push.txt \ + --log=$HOME/report--mode--force-push.txt \ --push \ --debug @@ -170,7 +170,7 @@ jobs: git@github.com:drevops/git-artifact-destination.git \ --branch=mode--branch--circleci--[branch]--[timestamp:Y-m-d_H-i] \ --mode=branch \ - --report=$HOME/report--mode--branch.txt \ + --log=$HOME/report--mode--branch.txt \ --push \ --debug @@ -187,7 +187,7 @@ jobs: git@github.com:drevops/git-artifact-destination.git \ --branch=mode--branch--circleci--[branch]--[timestamp:Y-m-d_H-i] \ --mode=branch \ - --report=$HOME/report--mode--branch.txt \ + --log=$HOME/report--mode--branch.txt \ --push \ --debug \ && { echo "Expected to fail as repeated pushes to the same branch are not allowed, but succeeded" >&2; exit 1; } || echo "Failed as expected" @@ -207,7 +207,7 @@ jobs: git@github.com:drevops/git-artifact-destination.git \ --branch=mode--branch--circleci--[branch]--[timestamp:Y-m-d_H-i-s] \ --mode=branch \ - --report=$HOME/report--mode--branch.txt \ + --log=$HOME/report--mode--branch.txt \ --push \ --debug diff --git a/.github/workflows/test-php.yml b/.github/workflows/test-php.yml index b61da39..a941f07 100644 --- a/.github/workflows/test-php.yml +++ b/.github/workflows/test-php.yml @@ -158,7 +158,7 @@ jobs: git@github.com:drevops/git-artifact-destination.git \ --branch=mode--force-push--gha--[branch] \ --mode=force-push \ - --report=$HOME/report--mode--force-push.txt \ + --log=$HOME/report--mode--force-push.txt \ --push \ --debug @@ -181,7 +181,7 @@ jobs: git@github.com:drevops/git-artifact-destination.git \ --branch=mode--force-push--gha--[branch] \ --mode=force-push \ - --report=$HOME/report--mode--force-push.txt \ + --log=$HOME/report--mode--force-push.txt \ --push \ --debug @@ -260,7 +260,7 @@ jobs: git@github.com:drevops/git-artifact-destination.git \ --branch=mode--branch--gha--[branch]--[timestamp:Y-m-d_H-i] \ --mode=branch \ - --report=$HOME/report--mode--branch.txt \ + --log=$HOME/report--mode--branch.txt \ --push \ --debug @@ -283,7 +283,7 @@ jobs: git@github.com:drevops/git-artifact-destination.git \ --branch=mode--branch--gha--[branch]--[timestamp:Y-m-d_H-i] \ --mode=branch \ - --report=$HOME/report--mode--branch.txt \ + --log=$HOME/report--mode--branch.txt \ --push \ --debug \ && { echo "Expected to fail as repeated pushes to the same branch are not allowed, but succeeded" >&2; exit 1; } || echo "Failed as expected" @@ -294,7 +294,7 @@ jobs: git@github.com:drevops/git-artifact-destination.git \ --branch=mode--branch--gha--[branch]--[timestamp:Y-m-d_H-i-s] \ --mode=branch \ - --report=$HOME/report--mode--branch.txt \ + --log=$HOME/report--mode--branch.txt \ --push \ --debug diff --git a/README.md b/README.md index 55ed662..66e45e5 100644 --- a/README.md +++ b/README.md @@ -134,7 +134,7 @@ fully-configured [example in the DrevOps project](https://github.com/drevops/dre --no-cleanup Do not cleanup after run. --now=NOW Internal value used to set internal time. --push Push artifact to the remote repository. Defaults to FALSE. - --report=REPORT Path to the report file. + --log=LOG Path to the log/report file. --root=ROOT Path to the root for file path resolution. If not specified, current directory is used. --show-changes Show changes made to the repo by the build in the output. --src=SRC Directory where source repository is located. If not specified, root directory is used. From d02afd6e72099c33ef5646815807f8e3d72040b0 Mon Sep 17 00:00:00 2001 From: "tan.nguyen" Date: Fri, 29 Mar 2024 09:29:07 +0700 Subject: [PATCH 35/37] test. --- .circleci/config.yml | 10 +++++----- .github/workflows/test-php.yml | 10 +++++----- README.md | 3 --- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 9be90bc..f3173ef 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -98,7 +98,7 @@ jobs: --mode=force-push \ --log=$HOME/report--mode--force-push.txt \ --push \ - --debug + -vvv DEPLOYED_BRANCH=$(sed -n 's/Remote branch://p' $HOME/report--mode--force-push.txt | sed 's/ //g') echo "Deployed to $DEPLOYED_BRANCH" @@ -123,7 +123,7 @@ jobs: --mode=force-push \ --log=$HOME/report--mode--force-push.txt \ --push \ - --debug + -vvv DEPLOYED_BRANCH=$(sed -n 's/Remote branch://p' $HOME/report--mode--force-push.txt | sed 's/ //g') echo "Deployed to $DEPLOYED_BRANCH" @@ -172,7 +172,7 @@ jobs: --mode=branch \ --log=$HOME/report--mode--branch.txt \ --push \ - --debug + -vvv DEPLOYED_BRANCH=$(sed -n 's/Remote branch://p' $HOME/report--mode--branch.txt | sed 's/ //g') echo "Deployed to $DEPLOYED_BRANCH" @@ -189,7 +189,7 @@ jobs: --mode=branch \ --log=$HOME/report--mode--branch.txt \ --push \ - --debug \ + -vvv \ && { echo "Expected to fail as repeated pushes to the same branch are not allowed, but succeeded" >&2; exit 1; } || echo "Failed as expected" - run: @@ -209,7 +209,7 @@ jobs: --mode=branch \ --log=$HOME/report--mode--branch.txt \ --push \ - --debug + -vvv DEPLOYED_BRANCH=$(sed -n 's/Remote branch://p' $HOME/report--mode--branch.txt | sed 's/ //g') echo "Deployed to $DEPLOYED_BRANCH" diff --git a/.github/workflows/test-php.yml b/.github/workflows/test-php.yml index a941f07..7215545 100644 --- a/.github/workflows/test-php.yml +++ b/.github/workflows/test-php.yml @@ -160,7 +160,7 @@ jobs: --mode=force-push \ --log=$HOME/report--mode--force-push.txt \ --push \ - --debug + -vvv DEPLOYED_BRANCH=$(sed -n 's/Remote branch://p' $HOME/report--mode--force-push.txt | sed 's/ //g') echo "Deployed to $DEPLOYED_BRANCH" @@ -183,7 +183,7 @@ jobs: --mode=force-push \ --log=$HOME/report--mode--force-push.txt \ --push \ - --debug + -vvv DEPLOYED_BRANCH=$(sed -n 's/Remote branch://p' $HOME/report--mode--force-push.txt | sed 's/ //g') echo "Deployed to $DEPLOYED_BRANCH" @@ -262,7 +262,7 @@ jobs: --mode=branch \ --log=$HOME/report--mode--branch.txt \ --push \ - --debug + -vvv DEPLOYED_BRANCH=$(sed -n 's/Remote branch://p' $HOME/report--mode--branch.txt | sed 's/ //g') echo "Deployed to $DEPLOYED_BRANCH" @@ -285,7 +285,7 @@ jobs: --mode=branch \ --log=$HOME/report--mode--branch.txt \ --push \ - --debug \ + -vvv \ && { echo "Expected to fail as repeated pushes to the same branch are not allowed, but succeeded" >&2; exit 1; } || echo "Failed as expected" - name: Deployment 2 - new branch @@ -296,7 +296,7 @@ jobs: --mode=branch \ --log=$HOME/report--mode--branch.txt \ --push \ - --debug + -vvv DEPLOYED_BRANCH=$(sed -n 's/Remote branch://p' $HOME/report--mode--branch.txt | sed 's/ //g') echo "Deployed to $DEPLOYED_BRANCH" diff --git a/README.md b/README.md index 66e45e5..fb8b1fc 100644 --- a/README.md +++ b/README.md @@ -144,9 +144,6 @@ fully-configured [example in the DrevOps project](https://github.com/drevops/dre --ansi Force ANSI output --no-ansi Disable ANSI output -n, --no-interaction Do not ask any interactive question - --simulate Run in simulated mode (show what would have happened). - --progress-delay=PROGRESS-DELAY Number of seconds before progress bar is displayed in long-running task collections. Default: 2s. [default: 2] - -D, --define=DEFINE Define a configuration item value. (multiple values allowed) -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug Help: From 3acaa376d3e237cef6a15d2613e234fb0b9dc2f5 Mon Sep 17 00:00:00 2001 From: "tan.nguyen" Date: Fri, 29 Mar 2024 09:48:44 +0700 Subject: [PATCH 36/37] test. --- tests/phpunit/AbstractTestCase.php | 11 ---- .../Functional/AbstractFunctionalTestCase.php | 6 -- tests/phpunit/Functional/GeneralTest.php | 64 +++++++++++++++---- tests/phpunit/Traits/CommandTrait.php | 19 +----- 4 files changed, 53 insertions(+), 47 deletions(-) diff --git a/tests/phpunit/AbstractTestCase.php b/tests/phpunit/AbstractTestCase.php index 8726a4d..931cc0f 100644 --- a/tests/phpunit/AbstractTestCase.php +++ b/tests/phpunit/AbstractTestCase.php @@ -56,7 +56,6 @@ protected function setUp(): void { $this->commandTraitSetUp( $this->fixtureDir . DIRECTORY_SEPARATOR . 'git_src', $this->fixtureDir . DIRECTORY_SEPARATOR . 'git_remote', - $this->isDebug() ); } @@ -71,14 +70,4 @@ protected function tearDown(): void { } } - /** - * Check if testing framework was ran with --debug option. - * - * @return bool - * TRUE if is in debug mode, FALSE otherwise. - */ - protected function isDebug(): bool { - return in_array('-vvv', $_SERVER['argv'], TRUE); - } - } diff --git a/tests/phpunit/Functional/AbstractFunctionalTestCase.php b/tests/phpunit/Functional/AbstractFunctionalTestCase.php index d09c6d0..be63379 100644 --- a/tests/phpunit/Functional/AbstractFunctionalTestCase.php +++ b/tests/phpunit/Functional/AbstractFunctionalTestCase.php @@ -125,12 +125,6 @@ protected function runBuild(string $args = '', bool $expectFail = FALSE): string $output = $this->runGitArtifactCommandTimestamped(sprintf('--src=%s %s %s', $this->src, $this->dst, $args), $expectFail); - if ($this->isDebug()) { - print str_pad('', 80, '+') . PHP_EOL; - print implode(PHP_EOL, $output) . PHP_EOL; - print str_pad('', 80, '+') . PHP_EOL; - } - return implode(PHP_EOL, $output); } diff --git a/tests/phpunit/Functional/GeneralTest.php b/tests/phpunit/Functional/GeneralTest.php index cf7c463..adff138 100644 --- a/tests/phpunit/Functional/GeneralTest.php +++ b/tests/phpunit/Functional/GeneralTest.php @@ -62,15 +62,65 @@ public function testNoCleanup(): void { $this->gitAssertFilesNotExist($this->dst, 'f1', $this->currentBranch); } - public function testReport(): void { + public function testDebug(): void { + $this->gitCreateFixtureCommits(1); + $output = $this->runBuild('-vvv'); + + $this->assertStringContainsString('Debug messages enabled', $output); + $this->assertStringContainsString('Artifact information', $output); + $this->assertStringContainsString('Mode: force-push', $output); + $this->assertStringContainsString('Source repository: ' . $this->src, $output); + $this->assertStringContainsString('Remote repository: ' . $this->dst, $output); + $this->assertStringContainsString('Remote branch: ' . $this->currentBranch, $output); + $this->assertStringContainsString('Gitignore file: No', $output); + $this->assertStringContainsString('Will push: No', $output); + + $this->assertStringContainsString('Artifact report', $output); + $this->assertStringContainsString(sprintf('Source repository: %s', $this->src), $output); + $this->assertStringContainsString(sprintf('Remote repository: %s', $this->dst), $output); + $this->assertStringContainsString(sprintf('Remote branch: %s', $this->currentBranch), $output); + $this->assertStringContainsString('Gitignore file: No', $output); + $this->assertStringContainsString('Push result: Success', $output); + + $this->assertStringContainsString('Cowardly refusing to push to remote. Use --push option to perform an actual push.', $output); + $this->gitAssertFilesNotExist($this->dst, 'f1', $this->currentBranch); + } + + public function testDebugLogFile(): void { $report = $this->src . DIRECTORY_SEPARATOR . 'report.txt'; $this->gitCreateFixtureCommits(1); - $this->runBuild(sprintf('--log=%s', $report)); + $commandOutput = $this->runBuild(sprintf('--log=%s', $report)); + + $this->assertStringContainsString('Debug messages enabled', $commandOutput); + $this->assertStringContainsString('Artifact information', $commandOutput); + $this->assertStringContainsString('Mode: force-push', $commandOutput); + $this->assertStringContainsString('Source repository: ' . $this->src, $commandOutput); + $this->assertStringContainsString('Remote repository: ' . $this->dst, $commandOutput); + $this->assertStringContainsString('Remote branch: ' . $this->currentBranch, $commandOutput); + $this->assertStringContainsString('Gitignore file: No', $commandOutput); + $this->assertStringContainsString('Will push: No', $commandOutput); + + $this->assertStringContainsString('Artifact report', $commandOutput); + $this->assertStringContainsString(sprintf('Source repository: %s', $this->src), $commandOutput); + $this->assertStringContainsString(sprintf('Remote repository: %s', $this->dst), $commandOutput); + $this->assertStringContainsString(sprintf('Remote branch: %s', $this->currentBranch), $commandOutput); + $this->assertStringContainsString('Gitignore file: No', $commandOutput); + $this->assertStringContainsString('Push result: Success', $commandOutput); $this->assertFileExists($report); $output = file_get_contents($report); + $this->assertStringContainsString('Debug messages enabled', (string) $output); + $this->assertStringContainsString('Artifact information', (string) $output); + $this->assertStringContainsString('Mode: force-push', (string) $output); + $this->assertStringContainsString('Source repository: ' . $this->src, (string) $output); + $this->assertStringContainsString('Remote repository: ' . $this->dst, (string) $output); + $this->assertStringContainsString('Remote branch: ' . $this->currentBranch, (string) $output); + $this->assertStringContainsString('Gitignore file: No', (string) $output); + $this->assertStringContainsString('Will push: No', (string) $output); + $this->assertStringNotContainsString('Added changes:', (string) $output); + $this->assertStringContainsString('Artifact report', (string) $output); $this->assertStringContainsString(sprintf('Source repository: %s', $this->src), (string) $output); $this->assertStringContainsString(sprintf('Remote repository: %s', $this->dst), (string) $output); @@ -79,16 +129,6 @@ public function testReport(): void { $this->assertStringContainsString('Push result: Success', (string) $output); } - public function testDebug(): void { - $this->gitCreateFixtureCommits(1); - $output = $this->runBuild('-vvv'); - - $this->assertStringContainsString('Debug messages enabled', $output); - - $this->assertStringContainsString('Cowardly refusing to push to remote. Use --push option to perform an actual push.', $output); - $this->gitAssertFilesNotExist($this->dst, 'f1', $this->currentBranch); - } - public function testDebugDisabled(): void { $this->gitCreateFixtureCommits(1); $output = $this->runBuild(); diff --git a/tests/phpunit/Traits/CommandTrait.php b/tests/phpunit/Traits/CommandTrait.php index 2efbba8..2674573 100644 --- a/tests/phpunit/Traits/CommandTrait.php +++ b/tests/phpunit/Traits/CommandTrait.php @@ -37,13 +37,6 @@ trait CommandTrait { */ protected $fs; - /** - * Flag to denote that debug information should be printed. - * - * @var bool - */ - protected $printDebug; - /** * Artifact git. */ @@ -58,12 +51,8 @@ trait CommandTrait { * Source path. * @param string $remote * Remote path. - * @param bool $printDebug - * Optional flag to print debug information when running commands. - * Defaults to FALSE. */ - protected function setUp(string $src, string $remote, bool $printDebug = FALSE): void { - $this->printDebug = $printDebug; + protected function setUp(string $src, string $remote): void { $this->fs = new Filesystem(); $this->src = $src; $this->gitInitRepo($this->src); @@ -511,17 +500,11 @@ public function runGitArtifactCommand(string $argsAndOptions, bool $expectFail = * If commands exists with non-zero status. */ protected function runCliCommand(string $command): array { - if ($this->printDebug) { - print '++ ' . $command . PHP_EOL; - } exec($command . ' 2>&1', $output, $code); if ($code !== 0) { throw new ErrorException(sprintf('Command "%s" exited with non-zero status', $command), $code, '', -1, new ErrorException(implode(PHP_EOL, $output), $code, '', -1)); } - if ($this->printDebug) { - print '++++ ' . implode(PHP_EOL, $output) . PHP_EOL; - } return $output; } From 645cce235f597125b98db49ddbe5d3f4edb8e62d Mon Sep 17 00:00:00 2001 From: "tan.nguyen" Date: Fri, 29 Mar 2024 09:50:30 +0700 Subject: [PATCH 37/37] test. --- src/Commands/ArtifactCommand.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/Commands/ArtifactCommand.php b/src/Commands/ArtifactCommand.php index 8bc46d5..5c90bae 100644 --- a/src/Commands/ArtifactCommand.php +++ b/src/Commands/ArtifactCommand.php @@ -14,7 +14,6 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Finder\Finder; @@ -128,11 +127,6 @@ class ArtifactCommand extends Command { */ protected GitArtifactGit $git; - /** - * Symfony style. - */ - protected SymfonyStyle $io; - /** * Artifact constructor. *