14 Commits

Author SHA1 Message Date
3734327357 Merge pull request 'v1.2.4' (#9) from dev into master
Reviewed-on: #9
2023-08-13 15:44:53 +00:00
af9c360684 Bump version 2023-08-13 23:43:21 +08:00
44f437eaf3 Misc edits in texts and output 2023-08-13 23:42:24 +08:00
1ebdffe2a6 Fixed again non-existent env when interpolating vars 2023-08-13 23:41:54 +08:00
d854732143 Merge pull request 'dev' (#7) from dev into master
Reviewed-on: #7
2023-08-13 15:12:22 +00:00
9b021296eb Bump version 2023-08-13 23:11:52 +08:00
d581afa793 Fixed non-existent env when interpolating vars 2023-08-13 23:11:37 +08:00
95ca655eb0 Merge pull request 'v1.2.2' (#5) from dev into master
Reviewed-on: #5
2023-08-13 03:04:03 +00:00
25887a47d3 Bump version 2023-08-13 11:02:59 +08:00
1f7816c917 When env was no set then dont try to collect vars from it
(cherry picked from commit fa8ad15fdd691fb4ea5db431062e89b512a9d526)
2023-08-13 11:02:08 +08:00
fb7b4baa32 Fixed wget file format
(cherry picked from commit 0febfd38aa3047555d34f7e125adb3773ee8a2da)
2023-08-13 11:02:07 +08:00
2c4eedbf8b When collection has no vars then dont try to collect them
(cherry picked from commit ee7528186f69e811b1b4db8e69ea2e4ec0ea2613)
2023-08-13 11:02:07 +08:00
c790adf611 Merge pull request 'v1.2.1' (#4) from dev into master
Reviewed-on: #4
2023-08-13 02:51:31 +00:00
c6094566b4 Fixed error when -o is missied 2023-08-13 10:50:52 +08:00
7 changed files with 37 additions and 30 deletions

View File

@@ -32,7 +32,7 @@ These formats are supported for now: `http`, `curl`, `wget`.
- tests, phpcs, psalm, etc.; - tests, phpcs, psalm, etc.;
- web version. - web version.
## Installation ## Install and upgrade
``` ```
composer global r axenov/pm-convert composer global r axenov/pm-convert

View File

@@ -1,7 +1,7 @@
{ {
"name": "axenov/pm-convert", "name": "axenov/pm-convert",
"type": "library", "type": "library",
"description": "Postman collection coverter", "description": "Postman collection converter",
"license": "MIT", "license": "MIT",
"homepage": "https://axenov.dev/", "homepage": "https://axenov.dev/",
"authors": [ "authors": [

View File

@@ -1,7 +1,7 @@
#!/usr/bin/env php #!/usr/bin/env php
<?php <?php
declare(strict_types = 1); declare(strict_types=1);
use PmConverter\Processor; use PmConverter\Processor;
@@ -20,8 +20,13 @@ foreach ($paths as $path) {
is_null($file) && throw new RuntimeException('Unable to locate autoload.php file.'); is_null($file) && throw new RuntimeException('Unable to locate autoload.php file.');
$processor = new Processor($argv);
try { try {
(new Processor($argv))->convert(); $processor->convert();
} catch (InvalidArgumentException $e) {
fwrite(STDERR, sprintf('ERROR: %s%s', $e->getMessage(), PHP_EOL));
print(implode(PHP_EOL, $processor->usage()));
die(1);
} catch (Exception $e) { } catch (Exception $e) {
fwrite(STDERR, sprintf('ERROR: %s%s', $e->getMessage(), PHP_EOL)); fwrite(STDERR, sprintf('ERROR: %s%s', $e->getMessage(), PHP_EOL));
die(1); die(1);

View File

@@ -32,9 +32,9 @@ abstract class AbstractConverter implements ConverterContract
protected array $vars = []; protected array $vars = [];
/** /**
* @var Environment * @var Environment|null
*/ */
protected Environment $env; protected ?Environment $env = null;
/** /**
* Sets an environment with vars * Sets an environment with vars
@@ -71,7 +71,7 @@ abstract class AbstractConverter implements ConverterContract
*/ */
protected function setVariables(): static protected function setVariables(): static
{ {
if ($this->collection->variable) { if (isset($this->collection?->variable)) {
foreach ($this->collection->variable as $var) { foreach ($this->collection->variable as $var) {
$this->vars["{{{$var->key}}}"] = $var->value; $this->vars["{{{$var->key}}}"] = $var->value;
} }
@@ -173,11 +173,11 @@ abstract class AbstractConverter implements ConverterContract
*/ */
protected function interpolate(string $content): string protected function interpolate(string $content): string
{ {
if (empty($this->vars) && $this->env->hasVars()) { if (empty($this->vars) && !$this->env?->hasVars()) {
return $content; return $content;
} }
$matches = []; $matches = [];
if (preg_match_all('/\{\{[a-zA-Z][a-zA-Z0-9]+}}/', $content, $matches, PREG_PATTERN_ORDER) > 0) { if (preg_match_all('/\{\{[a-zA-Z][a-zA-Z0-9]*}}/m', $content, $matches, PREG_PATTERN_ORDER) > 0) {
foreach ($matches[0] as $key => $var) { foreach ($matches[0] as $key => $var) {
if (str_contains($content, $var)) { if (str_contains($content, $var)) {
$content = str_replace($var, $this->vars[$var] ?? $this->env[$var] ?? $var, $content); $content = str_replace($var, $this->vars[$var] ?? $this->env[$var] ?? $var, $content);

View File

@@ -53,7 +53,7 @@ class WgetRequest extends AbstractRequest
$output[] = http_build_query($params); $output[] = http_build_query($params);
return $output; return $output;
default: default:
return ["\t$this->body"]; return ["\t'$this->body' \ "];
} }
} }
@@ -75,7 +75,6 @@ class WgetRequest extends AbstractRequest
$this->prepareHeaders(), $this->prepareHeaders(),
$this->prepareBody() $this->prepareBody()
); );
$output[] = rtrim(array_pop($output), '\ ');
$output[] = "\t'$this->url'"; $output[] = "\t'$this->url'";
return implode(PHP_EOL, array_merge($output, [''])); return implode(PHP_EOL, array_merge($output, ['']));
} }

View File

@@ -1,6 +1,6 @@
<?php <?php
declare(strict_types = 1); declare(strict_types=1);
namespace PmConverter; namespace PmConverter;

View File

@@ -7,22 +7,21 @@ namespace PmConverter;
use Exception; use Exception;
use InvalidArgumentException; use InvalidArgumentException;
use JsonException; use JsonException;
use PmConverter\Exceptions\CannotCreateDirectoryException; use PmConverter\Exceptions\{
use PmConverter\Exceptions\DirectoryIsNotReadableException; CannotCreateDirectoryException,
use PmConverter\Exceptions\DirectoryIsNotWriteableException; DirectoryIsNotReadableException,
use PmConverter\Exceptions\DirectoryNotExistsException; DirectoryIsNotWriteableException,
use PmConverter\Exporters\ConverterContract; DirectoryNotExistsException};
use PmConverter\Exporters\ConvertFormat; use PmConverter\Exporters\{
ConverterContract,
ConvertFormat};
/**
*
*/
class Processor class Processor
{ {
/** /**
* Converter version * Converter version
*/ */
public const VERSION = '1.2.0'; public const VERSION = '1.2.4';
/** /**
* @var string[] Paths to collection files * @var string[] Paths to collection files
@@ -89,9 +88,6 @@ class Processor
* Parses an array of arguments came from cli * Parses an array of arguments came from cli
* *
* @return void * @return void
* @throws DirectoryIsNotWriteableException
* @throws DirectoryNotExistsException
* @throws DirectoryIsNotReadableException
*/ */
protected function parseArgs(): void protected function parseArgs(): void
{ {
@@ -167,6 +163,9 @@ class Processor
if (empty($this->collectionPaths)) { if (empty($this->collectionPaths)) {
throw new InvalidArgumentException('there are no collections to convert'); throw new InvalidArgumentException('there are no collections to convert');
} }
if (empty($this->outputPath)) {
throw new InvalidArgumentException('-o is required');
}
if (empty($this->formats)) { if (empty($this->formats)) {
$this->formats = [ConvertFormat::Http->name => ConvertFormat::Http]; $this->formats = [ConvertFormat::Http->name => ConvertFormat::Http];
} }
@@ -226,6 +225,9 @@ class Processor
*/ */
protected function initEnv(): void protected function initEnv(): void
{ {
if (!isset($this->envFile)) {
return;
}
$content = file_get_contents(FileSystem::normalizePath($this->envFile)); $content = file_get_contents(FileSystem::normalizePath($this->envFile));
$content = json_decode($content, flags: JSON_THROW_ON_ERROR); $content = json_decode($content, flags: JSON_THROW_ON_ERROR);
if (!property_exists($content, 'environment') || empty($content?->environment)) { if (!property_exists($content, 'environment') || empty($content?->environment)) {
@@ -279,13 +281,13 @@ class Processor
{ {
$time = (hrtime(true) - $this->initTime) / 1_000_000; $time = (hrtime(true) - $this->initTime) / 1_000_000;
$ram = (memory_get_peak_usage(true) - $this->initRam) / 1024 / 1024; $ram = (memory_get_peak_usage(true) - $this->initRam) / 1024 / 1024;
printf('Converted %d of %d in %.3f ms using %.3f MiB RAM', $success, $count, $time, $ram); printf('Converted %d of %d in %.3f ms using %.3f MiB RAM%s', $success, $count, $time, $ram, PHP_EOL);
} }
/** /**
* @return string[] * @return string[]
*/ */
protected function version(): array public function version(): array
{ {
return ["Postman collection converter v" . self::VERSION]; return ["Postman collection converter v" . self::VERSION];
} }
@@ -293,7 +295,7 @@ class Processor
/** /**
* @return string[] * @return string[]
*/ */
protected function copyright(): array public function copyright(): array
{ {
return [ return [
'Anthony Axenov (c) ' . date('Y') . ", MIT license", 'Anthony Axenov (c) ' . date('Y') . ", MIT license",
@@ -304,7 +306,7 @@ class Processor
/** /**
* @return array * @return array
*/ */
protected function usage(): array public function usage(): array
{ {
return array_merge($this->version(), [ return array_merge($this->version(), [
'Usage:', 'Usage:',
@@ -315,7 +317,7 @@ class Processor
'', '',
'Possible ARGUMENTS:', 'Possible ARGUMENTS:',
"\t-f, --file - a PATH to single collection located in PATH to convert from", "\t-f, --file - a PATH to single collection located in PATH to convert from",
"\t-d, --dir - a directory with collections located in COLLECTION_FILEPATH to convert from", "\t-d, --dir - a directory with collections located in PATH to convert from",
"\t-o, --output - a directory OUTPUT_PATH to put results in", "\t-o, --output - a directory OUTPUT_PATH to put results in",
"\t-e, --env - use environment file with variable values to replace in request", "\t-e, --env - use environment file with variable values to replace in request",
"\t-p, --preserve - do not delete OUTPUT_PATH (if exists)", "\t-p, --preserve - do not delete OUTPUT_PATH (if exists)",
@@ -335,6 +337,7 @@ class Processor
"\t--http - generate raw *.http files (default)", "\t--http - generate raw *.http files (default)",
"\t--curl - generate shell scripts with curl command", "\t--curl - generate shell scripts with curl command",
"\t--wget - generate shell scripts with wget command", "\t--wget - generate shell scripts with wget command",
'',
'If no FORMATS specified then --http implied.', 'If no FORMATS specified then --http implied.',
'Any of FORMATS can be specified at the same time.', 'Any of FORMATS can be specified at the same time.',
'', '',