From 9613697ada55b4dd13fd8b49869a26f5f1c940f2 Mon Sep 17 00:00:00 2001 From: Anthony Axenov Date: Wed, 9 Aug 2023 21:31:08 +0800 Subject: [PATCH 1/3] Fixed count bug --- src/Processor.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Processor.php b/src/Processor.php index 1a93203..945c434 100644 --- a/src/Processor.php +++ b/src/Processor.php @@ -200,10 +200,11 @@ class Processor $this->initConverters(); $this->initCollections(); $count = count($this->collections); - $current = 1; + $current = 0; $success = 0; print(implode(PHP_EOL, array_merge($this->version(), $this->copyright())) . PHP_EOL . PHP_EOL); foreach ($this->collections as $collectionName => $collection) { + ++$current; printf("Converting '%s' (%d/%d):%s", $collectionName, $current, $count, PHP_EOL); foreach ($this->converters as $type => $exporter) { printf(' > %s', strtolower($type)); @@ -212,7 +213,6 @@ class Processor printf(' - OK: %s%s', $exporter->getOutputPath(), PHP_EOL); } print(PHP_EOL); - ++$current; ++$success; } $this->printStats($success, $current); -- 2.46.GIT From 1b3dd8b677250aa051b9d01fb39d7f66e85fdd3e Mon Sep 17 00:00:00 2001 From: Anthony Axenov Date: Wed, 9 Aug 2023 22:02:00 +0800 Subject: [PATCH 2/3] Improved detection of collections (affected on -f and -d) --- src/FileSystem.php | 36 +++++++++++++++++++++++++++++++++--- src/Processor.php | 25 ++++++++++++++++++------- 2 files changed, 51 insertions(+), 10 deletions(-) diff --git a/src/FileSystem.php b/src/FileSystem.php index 2c55272..c137425 100644 --- a/src/FileSystem.php +++ b/src/FileSystem.php @@ -10,8 +10,17 @@ use PmConverter\Exceptions\{ DirectoryIsNotWriteableException, DirectoryNotExistsException}; +/** + * Helper class to work with files and directories + */ class FileSystem { + /** + * Normalizes a given path + * + * @param string $path + * @return string + */ public static function normalizePath(string $path): string { $path = str_replace('~', $_SERVER['HOME'], $path); @@ -19,6 +28,8 @@ class FileSystem } /** + * Recursively creates a new directory by given path + * * @param string $path * @return string * @throws CannotCreateDirectoryException @@ -38,6 +49,8 @@ class FileSystem } /** + * Recursively removes a given directory + * * @param string $path * @return void * @throws DirectoryIsNotReadableException @@ -77,11 +90,10 @@ class FileSystem } /** + * Returns content of given directory path + * * @param string $path * @return array - * @throws DirectoryIsNotReadableException - * @throws DirectoryIsNotWriteableException - * @throws DirectoryNotExistsException */ public static function dirContents(string $path): array { @@ -92,4 +104,22 @@ class FileSystem } return $records; } + + /** + * Checks if a given file is a valid collection json file + * + * @param string $path + * @return bool + */ + public static function isCollectionFile(string $path): bool + { + $path = static::normalizePath($path); + return !empty($path = trim($path)) + && str_ends_with($path, '.postman_collection.json') + && file_exists($path) + && is_readable($path) + && ($json = json_decode(file_get_contents($path), true)) + && json_last_error() === JSON_ERROR_NONE + && isset($json['collection']['info']['name']); + } } diff --git a/src/Processor.php b/src/Processor.php index 945c434..382296e 100644 --- a/src/Processor.php +++ b/src/Processor.php @@ -92,47 +92,58 @@ class Processor switch ($arg) { case '-f': case '--file': - $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)'); + $rawpath = $this->argv[$idx + 1]; + $normpath = FileSystem::normalizePath($rawpath); + if (!FileSystem::isCollectionFile($normpath)) { + throw new InvalidArgumentException( + sprintf("this is not a valid collection file:%s\t%s %s", PHP_EOL, $arg, $rawpath) + ); } $this->collectionPaths[] = $this->argv[$idx + 1]; break; + case '-o': case '--output': if (empty($this->argv[$idx + 1])) { - throw new InvalidArgumentException('-o expected'); + throw new InvalidArgumentException('-o is required'); } $this->outputPath = $this->argv[$idx + 1]; break; + case '-d': case '--dir': if (empty($this->argv[$idx + 1])) { throw new InvalidArgumentException('a directory path is expected for -d (--dir)'); } - $path = $this->argv[$idx + 1]; + $normpath = $this->argv[$idx + 1]; $files = array_filter( - FileSystem::dirContents($path), - static fn($filename) => str_ends_with($filename, '.json') + FileSystem::dirContents($normpath), + static fn($filename) => FileSystem::isCollectionFile($filename) ); $this->collectionPaths = array_unique(array_merge($this?->collectionPaths ?? [], $files)); break; + case '-p': case '--preserve': $this->preserveOutput = true; break; + case '--http': $this->formats[ConvertFormat::Http->name] = ConvertFormat::Http; break; + case '--curl': $this->formats[ConvertFormat::Curl->name] = ConvertFormat::Curl; break; + case '--wget': $this->formats[ConvertFormat::Wget->name] = ConvertFormat::Wget; break; + case '-v': case '--version': die(implode(PHP_EOL, $this->version()) . PHP_EOL); + case '-h': case '--help': die(implode(PHP_EOL, $this->usage()) . PHP_EOL); -- 2.46.GIT From 2081aa935a5a1ff979ea5daa7fdc9fa15da17110 Mon Sep 17 00:00:00 2001 From: Anthony Axenov Date: Thu, 10 Aug 2023 13:31:23 +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 382296e..3a39e0c 100644 --- a/src/Processor.php +++ b/src/Processor.php @@ -22,7 +22,7 @@ class Processor /** * Converter version */ - public const VERSION = '1.1.0'; + public const VERSION = '1.1.1'; /** * @var string[] Paths to collection files -- 2.46.GIT