From aa583e7d43ace05571b0e47e612318f4424c6e0e Mon Sep 17 00:00:00 2001 From: Anthony Axenov Date: Fri, 4 Aug 2023 09:01:40 +0800 Subject: [PATCH 1/3] Fixed some mistakes while processing -d --- src/FileSystem.php | 6 +++--- src/Processor.php | 21 +++++++++++---------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/FileSystem.php b/src/FileSystem.php index e325b36..2c55272 100644 --- a/src/FileSystem.php +++ b/src/FileSystem.php @@ -65,13 +65,13 @@ class FileSystem { $path = static::normalizePath($path); if (!file_exists($path)) { - throw new DirectoryNotExistsException("output directory is not exist: $path"); + throw new DirectoryNotExistsException("directory does not exist: $path"); } if (!is_readable($path)) { - throw new DirectoryIsNotReadableException("output directory permissions are not valid: $path"); + throw new DirectoryIsNotReadableException("directory permissions are not valid: $path"); } if (!is_writable($path)) { - throw new DirectoryIsNotWriteableException("output directory permissions are not valid: $path"); + throw new DirectoryIsNotWriteableException("directory permissions are not valid: $path"); } return true; } diff --git a/src/Processor.php b/src/Processor.php index c79c00e..7450d29 100644 --- a/src/Processor.php +++ b/src/Processor.php @@ -27,7 +27,7 @@ class Processor /** * @var string[] Paths to collection files */ - protected array $collectionPaths; + protected array $collectionPaths = []; /** * @var string Output path where to put results in @@ -52,7 +52,7 @@ class Processor /** * @var object[] Collections that will be converted into choosen formats */ - protected array $collections; + protected array $collections = []; /** * Constructor @@ -80,7 +80,7 @@ class Processor switch ($arg) { case '-f': case '--file': - $path = $this->argv[$idx + 1]; + $path = FileSystem::normalizePath($this->argv[$idx + 1]); if (empty($path) || !str_ends_with($path, '.json') || !file_exists($path) || !is_readable($path)) { throw new InvalidArgumentException('a valid json-file path is expected for -f (--file)'); } @@ -99,13 +99,11 @@ class Processor throw new InvalidArgumentException('a directory path is expected for -d (--dir)'); } $path = $this->argv[$idx + 1]; - if (FileSystem::checkDir($path)) { - $files = array_filter( - FileSystem::dirContents($path), - static fn($filename) => str_ends_with($filename, '.json') - ); - $this->collectionPaths = array_unique(array_merge($this?->collectionPaths ?? [], $files)); - } + $files = array_filter( + FileSystem::dirContents($path), + static fn($filename) => str_ends_with($filename, '.json') + ); + $this->collectionPaths = array_unique(array_merge($this?->collectionPaths ?? [], $files)); break; case '-p': case '--preserve': @@ -128,6 +126,9 @@ class Processor die(implode(PHP_EOL, $this->usage()) . PHP_EOL); } } + if (empty($this->collectionPaths)) { + throw new InvalidArgumentException('there are no collections to convert'); + } if (empty($this->formats)) { $this->formats = [ConvertFormat::Http->name => ConvertFormat::Http]; } -- 2.46.GIT From 882fbe4713486b4d450f356dbe9897c36a9aa13b Mon Sep 17 00:00:00 2001 From: Anthony Axenov Date: Fri, 4 Aug 2023 09:23:17 +0800 Subject: [PATCH 2/3] Time and RAM stats after conversion --- README.md | 3 +-- pm-convert | 2 +- src/Processor.php | 41 +++++++++++++++++++++++++++++++++++++---- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 71fddae..85a1fbc 100644 --- a/README.md +++ b/README.md @@ -23,12 +23,11 @@ These formats are supported for now: `http`, `curl`, `wget`. ## Planned features - support as many as possible/necessary of authentication kinds (_currently no ones_); -- support as many as possible/necessary of body formats (_currently json and formdata_); +- support as many as possible/necessary of body formats (_currently only `json` and `formdata`_); - documentation generation support (markdown) with responce examples (if present); - maybe some another convert formats (like httpie or something...); - replace `{{vars}}` from folder; - replace `{{vars}}` from environment; -- performance measurement; - better logging; - tests, phpcs, psalm, etc.; - web version. diff --git a/pm-convert b/pm-convert index 9cf0a06..1168f11 100755 --- a/pm-convert +++ b/pm-convert @@ -21,7 +21,7 @@ foreach ($paths as $path) { is_null($file) && throw new RuntimeException('Unable to locate autoload.php file.'); try { - (new Processor($argv))->start(); + (new Processor($argv))->convert(); } catch (Exception $e) { fwrite(STDERR, sprintf('ERROR: %s%s', $e->getMessage(), PHP_EOL)); die(1); diff --git a/src/Processor.php b/src/Processor.php index 7450d29..df7d5b8 100644 --- a/src/Processor.php +++ b/src/Processor.php @@ -54,6 +54,16 @@ class Processor */ protected array $collections = []; + /** + * @var int Initial timestamp + */ + protected int $init_time; + + /** + * @var int Initial RAM usage + */ + protected int $init_ram; + /** * Constructor * @@ -61,6 +71,8 @@ class Processor */ public function __construct(protected array $argv) { + $this->init_time = hrtime(true); + $this->init_ram = memory_get_usage(true); } /** @@ -181,22 +193,43 @@ class Processor * * @throws Exception */ - public function start(): void + public function convert(): void { $this->parseArgs(); $this->initOutputDirectory(); $this->initConverters(); $this->initCollections(); + $count = count($this->collections); + $current = 1; + $success = 0; print(implode(PHP_EOL, array_merge($this->version(), $this->copyright())) . PHP_EOL . PHP_EOL); foreach ($this->collections as $collectionName => $collection) { - print("Converting '$collectionName':" . PHP_EOL); + printf("Converting '%s' (%d/%d):%s", $collectionName, $current, $count, PHP_EOL); foreach ($this->converters as $type => $exporter) { - print("\t-> " . strtolower($type)); + printf(' > %s', strtolower($type)); $outputPath = sprintf('%s%s%s', $this->outputPath, DIRECTORY_SEPARATOR, $collectionName); $exporter->convert($collection, $outputPath); - printf("\t- OK: %s%s", $exporter->getOutputPath(), PHP_EOL); + printf(' - OK: %s%s', $exporter->getOutputPath(), PHP_EOL); } + print(PHP_EOL); + ++$current; + ++$success; } + $this->printStats($success, $current); + } + + /** + * Outputs some statistics + * + * @param int $success + * @param int $count + * @return void + */ + protected function printStats(int $success, int $count): void + { + $time = (hrtime(true) - $this->init_time) / 1_000_000; + $ram = (memory_get_peak_usage(true) - $this->init_ram) / 1024 / 1024; + printf('Converted %d of %d in %.3f ms using %.3f MiB RAM', $success, $count, $time, $ram); } /** -- 2.46.GIT From b9c804b0f64e0746074309e60293536e12650b07 Mon Sep 17 00:00:00 2001 From: Anthony Axenov Date: Fri, 4 Aug 2023 09:23:48 +0800 Subject: [PATCH 3/3] Bump version --- src/Processor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Processor.php b/src/Processor.php index df7d5b8..1a93203 100644 --- a/src/Processor.php +++ b/src/Processor.php @@ -22,7 +22,7 @@ class Processor /** * Converter version */ - public const VERSION = '1.0.0'; + public const VERSION = '1.1.0'; /** * @var string[] Paths to collection files -- 2.46.GIT