yegor256/xcop
GitHub: yegor256/xcop
xcop 是一款 Ruby 编写的 XML 文档风格检查器,用于校验和自动修正 XML、XSD、XSL、XHTML 等文件的格式规范。
Stars: 31 | Forks: 8
# XML 文件风格检查器 (Linter)
[](https://www.elegantobjects.org)
[](https://www.rultor.com/p/yegor256/xcop)
[](https://www.jetbrains.com/ruby/)
[](https://github.com/yegor256/xcop/actions/workflows/rake.yml)
[](https://www.0pdd.com/p?name=yegor256/xcop)
[](https://badge.fury.io/rb/xcop)
[](https://github.com/yegor256/xcop/blob/master/LICENSE.txt)
[](https://codecov.io/github/yegor256/xcop?branch=master)
[](https://hitsofcode.com/view/github/yegor256/xcop)
此命令行工具用于验证您的 XML 文件格式是否正确。
如果格式不正确,它将输出差异
并以错误状态退出。
您可以通过两种方式使用它:1)如果任何类似 XML 的文件
(例如,XML、XSD、XSL 或 XHTML)格式不正确,则使构建失败;
2)使用 `--fix` 选项将其正确格式化。
请先阅读这篇博客文章:[_XCOP—XML 风格检查器_][blog]。
确保您已安装 [Ruby],然后安装该工具:
```
gem install xcop
```
在本地运行它并阅读其输出:
```
xcop --help
```
要验证 XML 文件的格式,只需将它们的文件名
作为参数传递:
```
xcop file1.xml file2.xml
```
如果您的文件格式不正确且 `xcop` 报错,您可以
使用 `--fix` 选项让其进行“美化”:
```
xcop --fix broken-file.xml
```
您也可以将目录作为参数传递。在这种情况下,`xcop` 将
递归查找其中的每一个 `.xml`、`.xsd`、`.xhtml`、`.xsl` 和 `.html`
文件:
```
xcop .
xcop --fix src/resources
```
## 默认配置
您可以将命令行选项放入启动 `xcop` 的目录下的 `.xcop` 文件中。
每个选项在文件中应单独占一行。
它们都会被_添加_到您指定的选项列表中。例如,
正如[这篇博客文章](https://www.yegor256.com/2022/07/20/command-line-defaults.html)中所建议的:
```
--nocolor
--quiet
--include=**/*
--exclude=**/*.xsl
--exclude=**/*.html
```
您也可以创建 `~/.xcop` 文件(在您的个人主目录中),
该文件也会被读取并_添加_到命令行选项中。
## 如何在 `Rakefile` 中使用?
这是您需要的内容:
```
require 'xcop/rake_task'
desc 'Run XCop on all XML/XSL files in all directories'
Xcop::RakeTask.new(:xcop) do |task|
task.quiet = true # FALSE by default
task.includes = ['**/*.xml', '**/*.xsl'] # xml|xsd|xhtml|xsl|html by default
task.excludes = ['target/**/*'] # empty by default
end
```
## 如何作为 GitHub action 使用?
在您的代码库的 `.github/workflows/xcop.yml` 下创建一个新的工作流文件:
```
---
name: XCOP
"on":
# run on push to master events
push:
branches:
- master
# run on pull requests to master
pull_request:
branches:
- master
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: g4s8/xcop-action@master
```
要自定义文件匹配模式,请使用 `files` 参数:
```
- uses: g4s8/xcop-action@master
with:
files: "src/*.xml"
```
## 如何在 Maven `pom.xml` 中使用?
您可以借助
[maven-antrun-plugin](https://maven.apache.org/plugins/maven-antrun-plugin/) 进行集成:
```
[...]
[...]
[...]
maven-antrun-plugin
1.8
verify
run
```
## 如何在 Ant `project.xml` 中使用?
类似这样的配置应该可以工作:
```
[...]
```
## 如何贡献
请阅读[这些指南](https://www.yegor256.com/2014/04/15/github-guidelines.html)。
在提交您的 pull request 之前,请确保您的构建是绿色的(通过状态)。
您需要安装 [Ruby](https://www.ruby-lang.org/en/) 2.3+ 和
[Bundler](https://bundler.io/)。然后:
```
bundle update
bundle exec rake
```
如果一切正常且您没有看到任何错误消息,请提交您的 pull request。
标签:Apache Flink, Ruby, SOC Prime, XML, 代码格式化, 代码规范检查, 开发工具, 知识库