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

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

View File

@ -58,7 +58,7 @@ abstract class AbstractConverter implements ConverterContract
*/
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);
}
@ -132,7 +132,7 @@ abstract class AbstractConverter implements ConverterContract
static $dir_tree;
foreach ($item->item as $subitem) {
$dir_tree[] = $item->name;
$path = implode(DIRECTORY_SEPARATOR, $dir_tree);
$path = implode(DS, $dir_tree);
if ($this->isItemFolder($subitem)) {
$this->convertItem($subitem);
} else {
@ -181,9 +181,9 @@ abstract class AbstractConverter implements ConverterContract
*/
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);
$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);
return file_put_contents($filepath, $content) > 0;
}

View File

@ -92,7 +92,7 @@ abstract class AbstractRequest implements Stringable, RequestContract
*/
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()
);
$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->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
{
$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;
}

View File

@ -57,7 +57,7 @@ class Postman21Converter extends AbstractConverter implements ConverterContract
protected function writeCollection(): bool
{
$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;
}

View File

@ -89,6 +89,6 @@ class WgetRequest extends AbstractRequest
$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
{
$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);
$records = array_diff(@scandir($path) ?: [], ['.', '..']);
foreach ($records as &$record) {
$record = sprintf('%s%s%s', $path, DIRECTORY_SEPARATOR, $record);
$record = sprintf('%s%s%s', $path, DS, $record);
}
return $records;
}

View File

@ -78,6 +78,11 @@ class Processor
*/
protected Environment $env;
/**
* @var bool Flag to output some debug-specific messages
*/
protected bool $devMode = false;
/**
* Constructor
*
@ -98,7 +103,7 @@ class Processor
protected function parseArgs(): void
{
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) {
switch ($arg) {
@ -108,7 +113,7 @@ class Processor
$normpath = FileSystem::normalizePath($rawpath);
if (!FileSystem::isCollectionFile($normpath)) {
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];
@ -179,11 +184,15 @@ class Processor
case '-v':
case '--version':
die(implode(PHP_EOL, $this->version()) . PHP_EOL);
die(implode(EOL, $this->version()) . EOL);
case '-h':
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)) {
@ -277,22 +286,26 @@ class Processor
$this->initCollections();
$this->initEnv();
$count = count($this->collections);
$current = 0;
$success = 0;
print(implode(PHP_EOL, array_merge($this->version(), $this->copyright())) . PHP_EOL . PHP_EOL);
$current = $success = 0;
print(implode(EOL, array_merge($this->version(), $this->copyright())) . EOL . EOL);
foreach ($this->collections as $collectionName => $collection) {
++$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) {
printf('> %s%s', strtolower($type), PHP_EOL);
$outputPath = sprintf('%s%s%s', $this->outputPath, DIRECTORY_SEPARATOR, $collectionName);
printf('> %s%s', strtolower($type), EOL);
$outputPath = sprintf('%s%s%s', $this->outputPath, DS, $collectionName);
if (!empty($this->env)) {
$exporter->withEnv($this->env);
}
$exporter->convert($collection, $outputPath);
printf(' OK: %s%s', $exporter->getOutputPath(), PHP_EOL);
try {
$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;
}
unset($this->converters, $type, $exporter, $outputPath, $this->collections, $collectionName, $collection);
@ -315,7 +328,7 @@ class Processor
$timeFmt = 'sec';
}
$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);
}
/**