Error stacktrace with --dev enabled while conversion is in progress

Also shorter constants.
This commit is contained in:
Anthony Axenov 2023-10-03 09:01:37 +08:00
parent 771fe4931a
commit 5666702ccc
Signed by: anthony
GPG Key ID: EA9EC32FF7CCD4EC
10 changed files with 44 additions and 29 deletions

View File

@ -2,6 +2,8 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
const EOL = PHP_EOL;
const DS = DIRECTORY_SEPARATOR;
use PmConverter\Processor; use PmConverter\Processor;
@ -24,10 +26,10 @@ $processor = new Processor($argv);
try { try {
$processor->convert(); $processor->convert();
} catch (InvalidArgumentException $e) { } catch (InvalidArgumentException $e) {
fwrite(STDERR, sprintf('ERROR: %s%s', $e->getMessage(), PHP_EOL)); fwrite(STDERR, sprintf('ERROR: %s%s', $e->getMessage(), EOL));
print(implode(PHP_EOL, $processor->usage())); print(implode(EOL, $processor->usage()));
die(1); 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(), EOL));
die(1); die(1);
} }

View File

@ -58,7 +58,7 @@ abstract class AbstractConverter implements ConverterContract
*/ */
protected function prepareOutputDir(string $outputPath): void protected function prepareOutputDir(string $outputPath): void
{ {
$outputPath = sprintf('%s%s%s', $outputPath, DIRECTORY_SEPARATOR, static::OUTPUT_DIR); $outputPath = sprintf('%s%s%s', $outputPath, DS, static::OUTPUT_DIR);
$this->outputPath = FileSystem::makeDir($outputPath); $this->outputPath = FileSystem::makeDir($outputPath);
} }
@ -132,7 +132,7 @@ abstract class AbstractConverter implements ConverterContract
static $dir_tree; static $dir_tree;
foreach ($item->item as $subitem) { foreach ($item->item as $subitem) {
$dir_tree[] = $item->name; $dir_tree[] = $item->name;
$path = implode(DIRECTORY_SEPARATOR, $dir_tree); $path = implode(DS, $dir_tree);
if ($this->isItemFolder($subitem)) { if ($this->isItemFolder($subitem)) {
$this->convertItem($subitem); $this->convertItem($subitem);
} else { } else {
@ -181,9 +181,9 @@ abstract class AbstractConverter implements ConverterContract
*/ */
protected function writeRequest(RequestContract $request, string $subpath = null): bool protected function writeRequest(RequestContract $request, string $subpath = null): bool
{ {
$filedir = sprintf('%s%s%s', $this->outputPath, DIRECTORY_SEPARATOR, $subpath); $filedir = sprintf('%s%s%s', $this->outputPath, DS, $subpath);
$filedir = FileSystem::makeDir($filedir); $filedir = FileSystem::makeDir($filedir);
$filepath = sprintf('%s%s%s.%s', $filedir, DIRECTORY_SEPARATOR, $request->getName(), static::FILE_EXT); $filepath = sprintf('%s%s%s.%s', $filedir, DS, $request->getName(), static::FILE_EXT);
$content = $this->interpolate((string)$request); $content = $this->interpolate((string)$request);
return file_put_contents($filepath, $content) > 0; return file_put_contents($filepath, $content) > 0;
} }

View File

@ -92,7 +92,7 @@ abstract class AbstractRequest implements Stringable, RequestContract
*/ */
public function getName(): string public function getName(): string
{ {
return str_replace(DIRECTORY_SEPARATOR, '_', $this->name); return str_replace(DS, '_', $this->name);
} }
/** /**

View File

@ -78,6 +78,6 @@ class CurlRequest extends AbstractRequest
$this->prepareBody() $this->prepareBody()
); );
$output[] = rtrim(array_pop($output), '\ '); $output[] = rtrim(array_pop($output), '\ ');
return implode(PHP_EOL, array_merge($output, [''])); return implode(EOL, array_merge($output, ['']));
} }
} }

View File

@ -69,6 +69,6 @@ class HttpRequest extends AbstractRequest
$this->prepareHeaders(), $this->prepareHeaders(),
$this->prepareBody() $this->prepareBody()
); );
return implode(PHP_EOL, $output); return implode(EOL, $output);
} }
} }

View File

@ -57,7 +57,7 @@ class Postman20Converter extends AbstractConverter implements ConverterContract
protected function writeCollection(): bool protected function writeCollection(): bool
{ {
$filedir = FileSystem::makeDir($this->outputPath); $filedir = FileSystem::makeDir($this->outputPath);
$filepath = sprintf('%s%s%s.%s', $filedir, DIRECTORY_SEPARATOR, $this->collection->name(), static::FILE_EXT); $filepath = sprintf('%s%s%s.%s', $filedir, DS, $this->collection->name(), static::FILE_EXT);
return file_put_contents($filepath, $this->collection) > 0; return file_put_contents($filepath, $this->collection) > 0;
} }

View File

@ -57,7 +57,7 @@ class Postman21Converter extends AbstractConverter implements ConverterContract
protected function writeCollection(): bool protected function writeCollection(): bool
{ {
$filedir = FileSystem::makeDir($this->outputPath); $filedir = FileSystem::makeDir($this->outputPath);
$filepath = sprintf('%s%s%s.%s', $filedir, DIRECTORY_SEPARATOR, $this->collection->name(), static::FILE_EXT); $filepath = sprintf('%s%s%s.%s', $filedir, DS, $this->collection->name(), static::FILE_EXT);
return file_put_contents($filepath, $this->collection) > 0; return file_put_contents($filepath, $this->collection) > 0;
} }

View File

@ -89,6 +89,6 @@ class WgetRequest extends AbstractRequest
$output[] = sprintf("\t%s", $this->getUrl()); $output[] = sprintf("\t%s", $this->getUrl());
} }
} }
return implode(PHP_EOL, array_merge($output, [''])); return implode(EOL, array_merge($output, ['']));
} }
} }

View File

@ -25,7 +25,7 @@ class FileSystem
public static function normalizePath(string $path): string public static function normalizePath(string $path): string
{ {
$path = str_replace('~', $_SERVER['HOME'], $path); $path = str_replace('~', $_SERVER['HOME'], $path);
return rtrim($path, DIRECTORY_SEPARATOR); return rtrim($path, DS);
} }
/** /**
@ -101,7 +101,7 @@ class FileSystem
$path = static::normalizePath($path); $path = static::normalizePath($path);
$records = array_diff(@scandir($path) ?: [], ['.', '..']); $records = array_diff(@scandir($path) ?: [], ['.', '..']);
foreach ($records as &$record) { foreach ($records as &$record) {
$record = sprintf('%s%s%s', $path, DIRECTORY_SEPARATOR, $record); $record = sprintf('%s%s%s', $path, DS, $record);
} }
return $records; return $records;
} }

View File

@ -78,6 +78,11 @@ class Processor
*/ */
protected Environment $env; protected Environment $env;
/**
* @var bool Flag to output some debug-specific messages
*/
protected bool $devMode = false;
/** /**
* Constructor * Constructor
* *
@ -98,7 +103,7 @@ class Processor
protected function parseArgs(): void protected function parseArgs(): void
{ {
if (count($this->argv) < 2) { if (count($this->argv) < 2) {
die(implode(PHP_EOL, $this->usage()) . PHP_EOL); die(implode(EOL, $this->usage()) . EOL);
} }
foreach ($this->argv as $idx => $arg) { foreach ($this->argv as $idx => $arg) {
switch ($arg) { switch ($arg) {
@ -108,7 +113,7 @@ class Processor
$normpath = FileSystem::normalizePath($rawpath); $normpath = FileSystem::normalizePath($rawpath);
if (!FileSystem::isCollectionFile($normpath)) { if (!FileSystem::isCollectionFile($normpath)) {
throw new InvalidArgumentException( throw new InvalidArgumentException(
sprintf("not a valid collection:%s\t%s %s", PHP_EOL, $arg, $rawpath) sprintf("not a valid collection:%s\t%s %s", EOL, $arg, $rawpath)
); );
} }
$this->collectionPaths[] = $this->argv[$idx + 1]; $this->collectionPaths[] = $this->argv[$idx + 1];
@ -179,11 +184,15 @@ class Processor
case '-v': case '-v':
case '--version': case '--version':
die(implode(PHP_EOL, $this->version()) . PHP_EOL); die(implode(EOL, $this->version()) . EOL);
case '-h': case '-h':
case '--help': case '--help':
die(implode(PHP_EOL, $this->usage()) . PHP_EOL); die(implode(EOL, $this->usage()) . EOL);
case '--dev':
$this->devMode = true;
break;
} }
} }
if (empty($this->collectionPaths)) { if (empty($this->collectionPaths)) {
@ -277,22 +286,26 @@ class Processor
$this->initCollections(); $this->initCollections();
$this->initEnv(); $this->initEnv();
$count = count($this->collections); $count = count($this->collections);
$current = 0; $current = $success = 0;
$success = 0; print(implode(EOL, array_merge($this->version(), $this->copyright())) . EOL . EOL);
print(implode(PHP_EOL, array_merge($this->version(), $this->copyright())) . PHP_EOL . PHP_EOL);
foreach ($this->collections as $collectionName => $collection) { foreach ($this->collections as $collectionName => $collection) {
++$current; ++$current;
printf("Converting '%s' (%d/%d):%s", $collectionName, $current, $count, PHP_EOL); printf("Converting '%s' (%d/%d):%s", $collectionName, $current, $count, EOL);
foreach ($this->converters as $type => $exporter) { foreach ($this->converters as $type => $exporter) {
printf('> %s%s', strtolower($type), PHP_EOL); printf('> %s%s', strtolower($type), EOL);
$outputPath = sprintf('%s%s%s', $this->outputPath, DIRECTORY_SEPARATOR, $collectionName); $outputPath = sprintf('%s%s%s', $this->outputPath, DS, $collectionName);
if (!empty($this->env)) { if (!empty($this->env)) {
$exporter->withEnv($this->env); $exporter->withEnv($this->env);
} }
$exporter->convert($collection, $outputPath); try {
printf(' OK: %s%s', $exporter->getOutputPath(), PHP_EOL); $exporter->convert($collection, $outputPath);
printf(' OK: %s%s', $exporter->getOutputPath(), EOL);
} catch (Exception $e) {
printf(' ERROR %s: %s%s', $e->getCode(), $e->getMessage(), EOL);
$this->devMode && array_map(static fn ($line) => printf(" %s%s", $line, EOL), $e->getTrace());
}
} }
print(PHP_EOL); print(EOL);
++$success; ++$success;
} }
unset($this->converters, $type, $exporter, $outputPath, $this->collections, $collectionName, $collection); unset($this->converters, $type, $exporter, $outputPath, $this->collections, $collectionName, $collection);
@ -315,7 +328,7 @@ class Processor
$timeFmt = 'sec'; $timeFmt = 'sec';
} }
$ram = (memory_get_peak_usage(true) - $this->initRam) / 1024 / 1024; $ram = (memory_get_peak_usage(true) - $this->initRam) / 1024 / 1024;
printf("Converted %d/%d in %.2f $timeFmt using up to %.2f MiB RAM%s", $success, $count, $time, $ram, PHP_EOL); printf("Converted %d/%d in %.2f $timeFmt using up to %.2f MiB RAM%s", $success, $count, $time, $ram, EOL);
} }
/** /**