daniillgolovin / difrences-files
生成diff
1.0.1
2024-07-15 10:02 UTC
Requires
- php: >=8.1.0
- docopt/docopt: ^1.0
- funct/funct: ^1.5
- symfony/yaml: ^5.2
Requires (Dev)
- phpstan/phpstan: ^1.11
- phpunit/phpunit: ^9
- squizlabs/php_codesniffer: ^3.10
README
描述
「差异计算器」 — 一个用于确定两个数据结构之间差异的程序。这是一个常见的任务,许多在线服务可以解决,例如 JSON Diff。类似机制用于显示测试或自动跟踪配置文件中的更改。
在此项目中实现了创建 AST(抽象语法树),基于此,现有的格式化器(stylish
、plain
、json
)可以输出平铺和嵌套文件的差异(使用 递归)。
特点
- 支持以下读取格式:
JSON
、YAML
。 - 实现了以下格式化器:
stylish
(默认)、plain
、json
。 - 可以作为 库 或
CLI
命令使用。
安装
⚠️ Перед установкой проекта проверьте наличие установленных php, composer!
要使用此项目,您需要按照以下步骤进行安装
- 使用以下其中一个控制台命令克隆仓库
# HTTPS >> git clone https://github.com/DaniillGolovin/Difference-Generator.git # SSH >> git clone git@github.com:DaniillGolovin/Difference-Generator.git
- 安装项目
>> make install
- 运行以下示例命令之一
执行全局安装命令
$ composer global require daniillgolovin/difrences-files
使用
CLI命令
该项目可以作为 命令行工具 使用。使用说明请参考 helper。
>> gendiff -h
gendiff -h Generate diff Usage: gendiff (-h|--help) gendiff (-v|--version) gendiff [--format <fmt>] <firstFilePath> <secondFilePath> Options: -h --help Show this screen -v --version Show version --format <fmt> Report format [default: stylish] Examples: stylish, plain, json
格式stylish
此格式化器在输出两个文件之间的差异时考虑以下特点
- 如果属性被 添加、删除 或 更改其值,则分别使用
+
和-
标记。 - 在其他情况下,属性要么 未更改,要么在两个文件中都具有作为值的 对象(是嵌套的)。
示例
>> gendiff file1.json file2.json
{ - follow: false host: hexlet.io - proxy: 123.234.53.22 - timeout: 50 + timeout: 20 + verbose: true }
平铺文件(JSON)格式(STYLISH)之间的差异
平铺文件(YAML)格式(STYLISH)之间的差异
嵌套文件(JSON)格式(STYLISH)之间的差异
嵌套文件(YAML)格式(STYLISH)之间的差异
格式plain
此格式化器在输出两个文件之间的差异时考虑以下特点
- 如果属性具有 “复杂值”(对象、数组),则输出
[complex value]
。 - 如果属性是嵌套的,则它 不被考虑:只保存其路径,该路径在输出其他 “平铺” 属性时使用,位于其中。
- 如果属性 未更改,则它 不输出。
示例
>> gendiff file5.json file6.json --format plain
Property 'common.follow' was added with value: false Property 'common.setting2' was removed Property 'common.setting3' was updated. From true to null Property 'common.setting4' was added with value: 'blah blah' Property 'common.setting5' was added with value: [complex value] Property 'common.setting6.doge.wow' was updated. From '' to 'so much' Property 'common.setting6.ops' was added with value: 'vops' Property 'group1.baz' was updated. From 'bas' to 'bars' Property 'group1.nest' was updated. From [complex value] to 'str' Property 'group2' was removed Property 'group3' was added with value: [complex value]
平铺文件(JSON)格式(PLAIN)之间的差异
平铺文件(YAML)格式(PLAIN)之间的差异
嵌套文件(JSON)格式(PLAIN)之间的差异
嵌套文件(YAML)格式(PLAIN)之间的差异
格式json
此格式化器在输出两个文件之间的差异时考虑以下特点
- 如果属性不是嵌套的或“复杂”的,则指定其 名称、描述符、旧值、新值、子项,格式为:
{ state: '状态', type: '类型', oldValue: 'file1中的值' oldValue: 'file2中的值' children: '子项' }
。
示例
>> gendiff file1.yaml file2.yaml --format json
{ { "key":"follow", "type":"removed", "oldValue":null, "newValue":false }, } { "key":"host", "type":"not updated", "oldValue":"hexlet.io", "newValue":"hexlet.io" }, { "key":"proxy","type":"removed", "oldValue":null, "newValue":"123.234.53.22" }, { "key":"timeout", "type":"updated", "oldValue":50, "newValue":20 }, { "key":"verbose", "type":"added", "oldValue":null, "newValue":true } }
平铺文件(JSON)格式(JSON)之间的差异
平铺文件(YAML)格式(JSON)之间的差异
嵌套文件(JSON)格式(JSON)之间的差异
嵌套文件(YAML)格式(JSON)之间的差异
项目结构
.. ├── Makefile ├── README.md ├── bin │ └── gendiff ├── composer.json ├── composer.lock ├── coverage.txt ├── file.txt ├── phpunit.xml ├── src │ ├── Differ.php │ ├── Formatters │ │ ├── Json.php │ │ ├── Plain.php │ │ └── Stylish.php │ ├── Formatters.php │ └── Parsers.php └── tests ├── GenDiffTest.php └── fixtures ├── diff.txt ├── diffJson.txt ├── diffPlain.txt ├── diffStylish.txt ├── file1.json ├── file1.yml ├── file2.json ├── file2.yml ├── fileNest1.json ├── fileNest1.yml ├── fileNest2.json └── fileNest2.yml 5 directories, 27 files