code-lts/cli-tools

用于管理输出错误格式化的CLI工具,支持junit、checkstyle、teamcity、gitlab和github错误格式

v1.6.0 2024-08-20 10:39 UTC

This package is auto-updated.

Last update: 2024-09-20 10:50:42 UTC


README

codecov Version License

用于管理输出错误格式化的CLI工具,支持junit、checkstyle、teamcity、gitlab和github错误格式。

动机

拥有可以在项目中重复使用的错误格式化工具。部分代码来自phpstan。请参阅 #4122

示例用法

格式化错误

use CodeLts\CliTools\Error;
use CodeLts\CliTools\AnalysisResult;
use CodeLts\CliTools\OutputFormat;
use CodeLts\CliTools\Exceptions\FormatNotFoundException;

// if the value is from an user input
// Will throw FormatNotFoundException
// See valid formats at OutputFormat::VALID_OUTPUT_FORMATS
OutputFormat::checkOutputFormatIsValid('notValidFormat');

$fileSpecificErrors = [
    new Error(
        'My error message',
        '/file/path/to/File.php',
        15 // Line number
    ),
];

$notFileSpecificErrors = [
    'Just an error message',
];

$internalErrors = [
    'Oops the internal engine did crash !',
];

$warnings = [
    'I warn you that most of this code is tested',
    'Contributions are very welcome',
];

$analysisResult = new AnalysisResult(
    $fileSpecificErrors,
    $notFileSpecificErrors,
    $internalErrors,
    $warnings
);

OutputFormat::displayUserChoiceFormat(
    OutputFormat::OUTPUT_FORMAT_TABLE,// The phpstan table error format, you can have a lot more formats in OutputFormat::VALID_OUTPUT_FORMATS
    $analysisResult,
    $this->sourceRootDirectory,// can be null, if null the error paths will not be pretty and will be displayed as given
    $this->output // Implement CodeLts\CliTools\Output or use CodeLts\CliTools\Symfony\SymfonyOutput
);

读取/写入文件

use CodeLts\CliTools\File\FileReader;
use CodeLts\CliTools\File\FileWriter;

$fileName = 'myFile.txt';

FileWriter::write(
    $fileName,
    'Some data, blob, blob, blob.'
);

FileReader::read($fileName);// -> Some data, blob, blob, blob.

ANSI代码

use CodeLts\CliTools\File\AnsiEscapeSequences;

$this->output->writeFormatted(AnsiEscapeSequences::ERASE_TO_LINE_END);

检测CI

use CodeLts\CliTools\Utils;

// See supported CIs at https://github.com/OndraM/ci-detector#supported-continuous-integration-servers

Utils::isCiDetected(); // true of false