定制 mistralys/changelog-parser 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mistralys/changelog-parser

Composer 安装命令:

composer require mistralys/changelog-parser

包简介

PHP library to parse Markdown-formatted change log files.

README 文档

README

PHP library to parse Markdown-formatted change log files.

Requirements

Installation

Add the package to your composer.json file with the following command:

composer require mistralys/changelog-parser

Also see the Packagist page.

Supported changelog formats

The parser expects all versions to be listed as headers with the same header level, and individual changes to be added as a list. Both of these examples will work:

# v1.2.3
- Made some changes

# v1.2.2
- Lotsa code changes
- Added some documentation
### v1.2.3
- Made some changes

### v1.2.2
- Lotsa code changes
- Added some documentation

Version heading formats

All the following headings are valid formats that the parser will recognize:

# v1

# v1.2

# v1.2.3

# 1

# 1.2

# 1.2.3

# 1.2.3-ALPHA

# 1.2.3-BranchName-SNAPSHOT

# 5.0 - Optional version label

# 5.0 | Optional version label

# 5.0 ~ Optional version label

For more information on the spectrum of version strings that can be used, have a look at the supported formats in the mistralys/version-parser package, which is used to read them.

Nesting the changelog in a document

The way the parser analyzes the Markdown document means that the changelog can be nested anywhere. The heading level will be inferred from the first version heading it encounters.

In this example, the changelog is not a separate document, but is nested in a subsection.

# Application name

## Usage

Learn how to use the application with this documentation.

## Change log

### v1.2.3
- Made some changes

### v1.2.2
- Lotsa code changes
- Added some documentation

## Credits

Many people contributed to the application.

The changelog parser will stick to the first changelog it finds in the document, meaning that only the first of multiple, separate version lists will be used, even if they have the same heading level.

Subheaders within versions

The parser will recognize subheaders within a version entry, and add collect these as plain text to be accessed again later. This makes it possible to further document things like breaking changes for example.

# v2.0.0 - Complete rework (breaking)
- Code entirely refurbished
- Documentation rewritten

## Breaking changes
- Renamed all methods
- Renamed all files

Only the items below the version will be considered changes in the version. The "Breaking changes" subheader and any additional content is captured as text, which can be accessed via getFreeformText():

use Mistralys\ChangelogParser\ChangelogParser;

$version = ChangelogParser::parseMarkdownFile('changelog.md')->getVersionByNumber('2.0.0');

echo $version->getFreeformText();

This will output:

## Breaking changes
- Renamed all methods
- Renamed all files

Categorized changes

Introduction

The parser also supports categorized changes, which are a way to mark changes by type. Entries can be prepended with symbols that define what kind of change they are, with two criticality levels: Optional and mandatory.

The idea is to mark changes that require users to take action with a new version, declare which ones are optional, and categorize them by the components that were modified.

Supported symbols

The parser handles the following symbols:

  • { } - Global change, optional
  • ( ) - Global change, mandatory
  • {C} - Component change, optional
  • (C) - Component change, mandatory

For component-related changes, the name of the component is expected to come directly after the symbol, and the description of the change separated by a colon.

Example

# v1.1.0 Tweaks
- ( ) The supported image format was changed to JPG only.
- (C) Image Reader: Added support for JPG images.
- {C} Image Reader: Added PNG image conversion. 

This version contains three changes, two of which are mandatory, requiring special attention when updating. The third is optional.

Note: Optional changes do not necessarily need to be marked with a symbol, but it has the advantage of using the component name to group changes by component.

Backticks for symbols

To make the changelogs nicer to read, the parser supports using backticks for the symbols. With a monospaced font, they are better aligned and easier to read.

# v1.1.0 Tweaks
- `( )` The supported image format was changed to JPG only.
- `(C)` Image Reader: Added support for JPG images.
- `{C}` Image Reader: Added PNG image conversion. 

Usage examples

Fetch all versions

use Mistralys\ChangelogParser\ChangelogParser;

$versions = ChangelogParser::parseMarkdownFile('changelog.md')->getVersions();

foreach($versions as $version)
{
    echo $version->getNumber().PHP_EOL;
}

Fetch the latest version

use Mistralys\ChangelogParser\ChangelogParser;

$parser = ChangelogParser::parseMarkdownFile('changelog.md');

$latest = $parser->getLatestVersion();

Get a version by number

use Mistralys\ChangelogParser\ChangelogParser;

$parser = ChangelogParser::parseMarkdownFile('changelog.md');

$version = $parser->getVersionByNumber('5.2.0');

This will throw an exception if the version is not found. To check if a version number exists beforehand

Check if a version exists

use Mistralys\ChangelogParser\ChangelogParser;

$parser = ChangelogParser::parseMarkdownFile('changelog.md');

if($parser->versionExists('5.2.0'))
{
    $version = $parser->getVersionByNumber('5.2.0');
}

Note that this requires the exact version number to be known (major, minor and patch version numbers). For a more flexible way to find versions, the version info is best used instead.

For example, to find all versions matching v4.2.x:

use Mistralys\ChangelogParser\ChangelogParser;

$versions = ChangelogParser::parseMarkdownFile('changelog.md')->getVersions();

foreach($versions as $version)
{
    $info = $version->getVersionInfo();
    
    if($info->getMajorVersion() === 4 && $info->getMinorVersion() === 2) 
    {
        // Matches v4.2
    }
}

Go through individual changes in a version

use Mistralys\ChangelogParser\ChangelogParser;

$version = ChangelogParser::parseMarkdownFile('changelog.md')->requireLatestVersion();

$changes = $version->getChanges();

echo "Changes in version ".$version->getNumber().":".PHP_EOL;

foreach($changes as $change)
{
    echo '- '.$change->getText().PHP_EOL;
}

Note the use of the requireLatestVersion() method: This will throw an exception instead of NULL if no versions are found in the change log. Handy to avoid checking for a null value.

Persisting and caching

To easily store or transmit changelog information, the parser offers the possibility to serialize the data to JSON. This can be decoded again later instead of parsing the source file each time.

use Mistralys\ChangelogParser\ChangelogParser;

if(!file_exists('changelog.json'))
{
    $changelog = ChangelogParser::parseMarkdownFile('changelog.md');
    $changelog->toJSONFile('changelog.json');
}
else
{
    $changelog = ChangelogParser::parseJSONFile('changelog.json');
}

This example will automatically create a JSON cache file, which performs better than parsing the source Markdown file each time, especially for large files.

mistralys/changelog-parser 适用场景与选型建议

mistralys/changelog-parser 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.32k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 04 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 mistralys/changelog-parser 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 mistralys/changelog-parser 我们能提供哪些服务?
定制开发 / 二次开发

基于 mistralys/changelog-parser 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3.32k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 2
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-04-19