Coder Social home page Coder Social logo

yegor256 / xcop Goto Github PK

View Code? Open in Web Editor NEW
28.0 6.0 5.0 130 KB

Command Line Style Checker of XML Documents: to make sure they are always formatted nicely

Home Page: https://www.yegor256.com/2017/08/29/xcop.html

License: MIT License

Ruby 90.45% Gherkin 9.55%
xml ruby static-analysis xml-format xslt xsl html

xcop's Introduction

XCOP logo

EO principles respected here DevOps By Rultor.com We recommend RubyMine

rake PDD status Gem Version License Maintainability Test Coverage Lines of code Hits-of-Code

This command line tool validates your XML files for proper formatting. If they are not formatted correctly, it prints the difference and exits with an error. You can use it two ways: 1) to fail your build if any XML-ish files (for example, XML, XSD, XSL, or XHTML) are not formatted correctly, and 2) to format them correctly using --fix option.

Read this blog post first: XCOP—XML Style Checker.

Make sure you have Ruby installed and then install the tool:

$ gem install xcop

Run it locally and read its output:

$ xcop --help

To validate formatting of your XML files just pass their names as arguments:

$ xcop file1.xml file2.xml

If your files are not formatted correctly and xcop complains, you can ask it to "beautify" them, using --fix option:

$ xcop --fix broken-file.xml

To fix all files in the directory you can do (won't work if your file names contain spaces):

$ xcop --fix $(find . -name '*.xml')

Defaults

You can put command line options into .xcop file in the directory where you start xcop. Each option should take a single line in the file. They all will be added to the list of options you specify. For example, as it was suggested in this blog post:

--license=LICENSE.txt
--nocolor
--quiet
--include=**/*
--exclude=**/*.xsl
--exclude=**/*.html

You can also create ~/.xcop file (in your personal home directory), which will also be read and added to the command line options.

How to use in Rakefile?

This is what you need there:

require 'xcop/rake_task'
desc 'Run XCop on all XML/XSL files in all directories'
Xcop::RakeTask.new(:xcop) do |task|
  task.license = 'LICENSE.txt' # no license by default
  task.quiet = true # FALSE by default
  task.includes = ['**/*.xml', '**/*.xsl'] # xml|xsd|xhtml|xsl|html by default
  task.excludes = ['target/**/*'] # empty by default
end

How to use as GitHub action?

Create new workflow file in repository under .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

To customize license location or files pattern use action inputs license and files:

- uses: g4s8/xcop-action@master
  with:
    license: MY_LICENSE.txt
    files: "src/*.xml"

How to use in Maven pom.xml?

You can integrate it with the help of maven-antrun-plugin:

<project>
  [...]
  <build>
    [...]
    <plugins>
      [...]
      <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.8</version>
        <executions>
          <execution>
            <phase>verify</phase>
            <configuration>
              <target>
                <apply executable="xcop" failonerror="true">
                  <arg value="--license"/>
                  <arg value="LICENSE.txt"/>
                  <fileset dir=".">
                    <include name="**/*.xml"/>
                    <include name="**/*.xsd"/>
                    <exclude name="target/**/*"/>
                    <exclude name=".idea/**/*"/>
                  </fileset>
                </apply>
              </target>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

How to use in Ant project.xml?

Something like this should work:

<project>
  [...]
  <target name="xcop">
    <apply executable="xcop" failonerror="true">
      <arg value="--license"/>
      <arg value="LICENSE.txt"/>
      <fileset dir=".">
        <include name="**/*.xml"/>
        <include name="**/*.xsd"/>
        <exclude name="target/**/*"/>
        <exclude name=".idea/**/*"/>
      </fileset>
    </apply>
  </target>
</project>

How to contribute

Read these guidelines. Make sure you build is green before you contribute your pull request. You will need to have Ruby 2.3+ and Bundler installed. Then:

$ bundle update
$ bundle exec rake

If it's clean and you don't see any error messages, submit your pull request.

xcop's People

Contributors

dgroup avatar g4s8 avatar llorllale avatar renovate[bot] avatar rultor avatar yegor256 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

xcop's Issues

Could not find 'racc' (~> 1.4)

I use xcop through maven-antrun-plugin. To be clear I use jcabi-parent which has the following definition of xcop:

<plugin>
  <artifactId>maven-antrun-plugin</artifactId>
  <version>3.1.0</version>
  <executions>
    <execution>
      <phase>verify</phase>
      <id>jcabi-xcop</id>
      <configuration>
        <target>
          <taskdef resource="net/sf/antcontrib/antcontrib.properties" classpathref="maven.plugin.classpath"/>
          <property environment="env"/>
          <available file="xcop" filepath="${env.PATH}" property="xcop.present"/>
          <if>
            <equals arg1="${xcop.present}" arg2="true"/>
            <then>
              <patternset id="xcop.main">
                <include name="**/*.xml"/>
                <include name="**/*.xsl"/>
                <include name="**/*.xsd"/>
                <exclude name="**/target/**/*"/>
                <exclude name=".idea/**/*"/>
              </patternset>
              <condition property="xcop.excludes.defined">
                <isreference refid="xcop.excludes"/>
              </condition>
              <if>
                <equals arg1="${xcop.excludes.defined}" arg2="true"/>
                <then>
                  <fileset dir="." id="files">
                    <patternset refid="xcop.main"/>
                    <patternset refid="xcop.excludes"/>
                  </fileset>
                  <pathconvert refid="files" property="converted" pathsep=" "/>
                </then>
                <else>
                  <fileset dir="." id="files">
                    <patternset refid="xcop.main"/>
                  </fileset>
                  <pathconvert refid="files" property="converted" pathsep=" "/>
                </else>
              </if>
              <condition property="os.windows">
                <os family="windows"/>
              </condition>
              <if>
                <equals arg1="${os.windows}" arg2="true"/>
                <then>
                  <exec executable="cmd" failonerror="true">
                    <arg line="/c xcop"/>
                    <arg value="--license"/>
                    <arg value="LICENSE.txt"/>
                    <arg line="${converted}"/>
                  </exec>
                </then>
                <else>
                  <exec executable="xcop" failonerror="true">
                    <arg value="--license"/>
                    <arg value="LICENSE.txt"/>
                    <arg line="${converted}"/>
                  </exec>
                </else>
              </if>
            </then>
            <else>
              <echo message="XCOP is not available in PATH"/>
            </else>
          </if>
        </target>
      </configuration>
      <goals>
        <goal>run</goal>
      </goals>
    </execution>
  </executions>
</plugin>

When I run mvn clean install, I get the following output:

[INFO] --- antrun:3.1.0:run (jcabi-xcop) @ jeo-maven-plugin ---
[INFO] Executing tasks
[INFO]      [exec] /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/dependency.rb:311:in `to_specs': Could not find 'racc' (~> 1.4) among 116 total gem(s) (Gem::MissingSpecError)
[INFO]      [exec] Checked in 'GEM_PATH=/Users/lombrozo/.gem/ruby/3.1.3:/Users/lombrozo/.rubies/ruby-3.1.3/lib/ruby/gems/3.1.0', execute `gem env` for more information
[INFO]      [exec]      from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/specification.rb:1449:in `block in activate_dependencies'
[INFO]      [exec]      from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/specification.rb:1438:in `each'
[INFO]      [exec]      from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/specification.rb:1438:in `activate_dependencies'
[INFO]      [exec]      from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/specification.rb:1420:in `activate'
[INFO]      [exec]      from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/specification.rb:1452:in `block in activate_dependencies'
[INFO]      [exec]      from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/specification.rb:1438:in `each'
[INFO]      [exec]      from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/specification.rb:1438:in `activate_dependencies'
[INFO]      [exec]      from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/specification.rb:1420:in `activate'
[INFO]      [exec]      from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems.rb:304:in `block in activate_bin_path'
[INFO]      [exec]      from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems.rb:303:in `synchronize'
[INFO]      [exec]      from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems.rb:303:in `activate_bin_path'
[INFO]      [exec]      from /usr/local/bin/xcop:23:in `<main>'
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------

How can I solve this?

absent license causes exception

xcop --fix --license LICENSE $(find . -name '*.xsl')
/Users/yegor/.rvm/gems/ruby-2.3.3/gems/xcop-0.5.7/bin/xcop:57:in `read': No such file or directory @ rb_sysopen - LICENSE (Errno::ENOENT)
	from /Users/yegor/.rvm/gems/ruby-2.3.3/gems/xcop-0.5.7/bin/xcop:57:in `<top (required)>'
	from /Users/yegor/.rvm/gems/ruby-2.3.3/bin/xcop:22:in `load'
	from /Users/yegor/.rvm/gems/ruby-2.3.3/bin/xcop:22:in `<main>'
	from /Users/yegor/.rvm/gems/ruby-2.3.3/bin/ruby_executable_hooks:15:in `eval'
	from /Users/yegor/.rvm/gems/ruby-2.3.3/bin/ruby_executable_hooks:15:in `<main>'

Instead, we should say something more meaningful, like "file not found"

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

bundler
Gemfile
github-actions
.github/workflows/pdd.yml
  • actions/checkout v3
  • ubuntu 20.04
.github/workflows/rake.yml
  • actions/checkout v3
  • ruby/setup-ruby v1
.github/workflows/xcop.yml
  • actions/checkout v3
  • ubuntu 20.04

  • Check this box to trigger a request for Renovate to run again on this repository

fixed/untouched to show in log

Let's show fixed or untouched in the log, when we fix the files. Currently we are always showing fixed, which is misleading.

Configuration file for xcop

Let's add ability to configure xcop via an .xcop file. The default behavior for xcop should be to look for this file in the current directory.

(this is a continuation of this discussion)

undefined method `before' for nil:NilClass

Traceback (most recent call last):
	10: from /Users/yegor/.rvm/gems/ruby-2.7.0/bin/ruby_executable_hooks:24:in `<main>'
	 9: from /Users/yegor/.rvm/gems/ruby-2.7.0/bin/ruby_executable_hooks:24:in `eval'
	 8: from /Users/yegor/.rvm/gems/ruby-2.7.0/bin/xcop:23:in `<main>'
	 7: from /Users/yegor/.rvm/gems/ruby-2.7.0/bin/xcop:23:in `load'
	 6: from /Users/yegor/.rvm/gems/ruby-2.7.0/gems/xcop-0.7.1/bin/xcop:94:in `<top (required)>'
	 5: from /Users/yegor/.rvm/gems/ruby-2.7.0/gems/xcop-0.7.1/lib/xcop/cli.rb:59:in `fix'
	 4: from /Users/yegor/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0/set.rb:328:in `each'
	 3: from /Users/yegor/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0/set.rb:328:in `each_key'
	 2: from /Users/yegor/.rvm/gems/ruby-2.7.0/gems/xcop-0.7.1/lib/xcop/cli.rb:60:in `block in fix'
	 1: from /Users/yegor/.rvm/gems/ruby-2.7.0/gems/xcop-0.7.1/lib/xcop/document.rb:59:in `fix'
/lib/nokogiri/xml/node_set.rb:63:in `before': undefined method `before' for nil:NilClass (NoMethodError)

Extra \n added in schars function

Make sure the title of the issue explains the problem you are having. Also, the description of the issue must clearly explain what is broken, not what you want us to implement. Go through this checklist and make sure you answer "YES" to all points:

  • You have all pre-requisites listed in README.md installed
  • You are sure that you are not reporting a duplicate (search all issues)
  • You say "is broken" or "doesn't work" in the title
  • You tell us what you are trying to do
  • You explain the results you are getting
  • You suggest an alternative result you would like to see

This article will help you understand what we are looking for: http://www.yegor256.com/2014/11/24/principles-of-bug-tracking.html

Thank you for your contribution!

`--fix` for all files in directory

@yegor256, can you check, does it ($ xcop --fix $(find . -name '*.xml')) works well?
In my case, it didn't work and I have applied xcop directly to each file in directory.
My directory:
image

May be it is required to add additional Junit-tests.

Also, it would be great if we can apply xcop for directories tree --- i.e. for XML-files that occurs not only in current directory, but also in directories inside of the current.

--fix option

Let's make it possible to fix XML documents, not only validate them.

complex file name breaks xcop

/Users/yegor/.rvm/gems/ruby-2.3.3/gems/xcop-0.5.7/lib/xcop.rb:95:in `initialize': No such file or directory @ rb_sysopen - ./target/surefire-reports/TEST-[modifies_vacation_mode (Errno::ENOENT)
	from /Users/yegor/.rvm/gems/ruby-2.3.3/gems/xcop-0.5.7/lib/xcop.rb:95:in `open'
	from /Users/yegor/.rvm/gems/ruby-2.3.3/gems/xcop-0.5.7/lib/xcop.rb:95:in `fix'
	from /Users/yegor/.rvm/gems/ruby-2.3.3/gems/xcop-0.5.7/lib/xcop.rb:62:in `block in fix'
	from /Users/yegor/.rvm/gems/ruby-2.3.3/gems/xcop-0.5.7/lib/xcop.rb:61:in `each'
	from /Users/yegor/.rvm/gems/ruby-2.3.3/gems/xcop-0.5.7/lib/xcop.rb:61:in `fix'
	from /Users/yegor/.rvm/gems/ruby-2.3.3/gems/xcop-0.5.7/bin/xcop:60:in `<top (required)>'
	from /Users/yegor/.rvm/gems/ruby-2.3.3/bin/xcop:22:in `load'
	from /Users/yegor/.rvm/gems/ruby-2.3.3/bin/xcop:22:in `<main>'
	from /Users/yegor/.rvm/gems/ruby-2.3.3/bin/ruby_executable_hooks:15:in `eval'
	from /Users/yegor/.rvm/gems/ruby-2.3.3/bin/ruby_executable_hooks:15:in `<main>'

undefined method `text' for nil:NilClass

xcop can fail when running from Rakefile with error:

undefined method `text' for nil:NilClass

see this PR for details: zerocracy/datum#110

If I start xcop $(find . -path ./.idea -prune -name '*.xml') command it will finish successfully.
If I remove '**/*.xml' from task includes it will finish without errors too.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.