Skip to content

Gherkin-Utils - Java - Address issue #1662 - add a Java pretty print gherkin module #1725

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 41 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
b0501c1
some initial commits towards https://github.com/cucumber/common/issue…
nddipiazza Sep 2, 2021
3e85f37
finish the Pretty document stuff.
nddipiazza Sep 3, 2021
7cdbe75
add some gitignores
nddipiazza Sep 3, 2021
ef4f7c5
Merge branch 'main' of github.com:cucumber/common into address_issue_…
nddipiazza Sep 3, 2021
9a73928
code formatting.
nddipiazza Sep 3, 2021
ad5887f
maven-itize the project
nddipiazza Sep 3, 2021
c36c37d
add more of the other files
nddipiazza Sep 3, 2021
f9e6ca2
javadoc
nddipiazza Sep 3, 2021
1242fc1
Merge branch 'main' into address_issue_1662
nddipiazza Sep 3, 2021
d13d33e
Merge branch 'main' into address_issue_1662
aslakhellesoy Sep 6, 2021
97d5a6c
Merge branch 'main' into address_issue_1662
aslakhellesoy Sep 8, 2021
fb9be45
add comments.
Oct 19, 2021
da7aa10
Merge branch 'main' into address_issue_1662
aslakhellesoy Oct 20, 2021
a527c67
the formatting of numbers on the left side caused differences with in…
Oct 21, 2021
c588f2b
Merge remote-tracking branch 'origin/address_issue_1662' into address…
Oct 21, 2021
85b4f9e
Merge branch 'main' into address_issue_1662
jamietanna Oct 21, 2021
992cf4f
pr review
nddipiazza Jan 5, 2022
5201f42
handle end of file comments.
nddipiazza Jan 7, 2022
cd76a13
Merge branch 'main' into address_issue_1662
nddipiazza May 17, 2022
2ed7730
fix broken test
nddipiazza May 17, 2022
c356fe3
Merge branch 'address_issue_1662' of github.com:nddipiazza/common int…
nddipiazza May 17, 2022
a9a30cb
fix ci issue
nddipiazza May 17, 2022
ed00d2a
add entry for gherkin-util/java in ci config.yaml
nddipiazza May 17, 2022
4f627cc
try to fix config error
nddipiazza May 17, 2022
30d9b53
try to fix config error
nddipiazza May 17, 2022
4bcc2bd
try removing it
nddipiazza May 17, 2022
19fe741
Revert "fix ci issue"
nddipiazza May 17, 2022
15f7e70
more fixes
nddipiazza May 17, 2022
f992a61
fix typo in name
nddipiazza May 17, 2022
95f59ef
add commited files for ci
nddipiazza May 17, 2022
26417cd
push untracked files
nddipiazza May 17, 2022
4b89272
fix gherkin patch m2
nddipiazza May 17, 2022
c804fa1
fix package names to match project
nddipiazza May 17, 2022
2e3d3b2
push source
nddipiazza May 17, 2022
3ed72e1
Merge branch 'main' into address_issue_1662
nddipiazza May 17, 2022
6625780
fix remaining issues with make
nddipiazza May 17, 2022
330371d
push source
nddipiazza May 17, 2022
b90afc0
Merge branch 'address_issue_1662' of github.com:nddipiazza/common int…
nddipiazza May 17, 2022
cb68862
push untracked files
nddipiazza May 17, 2022
e85f043
push untracked files
nddipiazza May 17, 2022
c5e73bf
Merge branch 'main' into address_issue_1662
nddipiazza May 18, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,23 @@ jobs:
paths:
- 'io/cucumber/gherkin'

gherkin-utils-java:
executor: docker-circleci-openjdk
steps:
- attach_workspace:
at: '~/cucumber'
- attach_workspace:
at: '~/.m2/repository'
- run:
name: gherkin-utils-java
command: |
cd gherkin-utils/java
make
- persist_to_workspace:
root: '~/.m2/repository'
paths:
- 'io/cucumber/gherkin'

json-to-messages-java-testdata:
executor: docker-circleci-openjdk
steps:
Expand Down Expand Up @@ -734,6 +751,9 @@ workflows:
- gherkin-java:
requires:
- messages-java
- gherkin-utils-java:
requires:
- messages-java
- json-to-messages-java-testdata:
requires:
- prepare-parallel
Expand Down
6 changes: 3 additions & 3 deletions .templates/java/scripts/check-jar.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ set -uf -o pipefail
check_jar() {
jar="$1"
module_name=$(xmlstarlet sel -N pom="http://maven.apache.org/POM/4.0.0" -t -m "//pom:project.Automatic-Module-Name" -v . pom.xml)
module_path=$(echo $module_name | sed "s/\./\\\\\//g")
echo "Checking contents of ${jar}"
module_path=$(echo $module_name | sed "s/\./\\\\\//g" | sed "s/-/\\\\\//g")
echo "Checking contents of ${jar} to see if it matches pattern: ${module_path}"
unshaded_classes=$(unzip -l ${jar} | grep -e "\.class" | rev | cut -d' ' -f1 | rev | grep -v "^$module_path")
if [[ "${unshaded_classes}" != "" ]]; then
echo "Some classes in ${jar} are not in the io.cucumber package. Rename the classes or change the maven-shade-plugin configuration."
echo "Some classes in ${jar} are not in the expected package matching pattern ${module_path}. Rename the classes or change the maven-shade-plugin configuration."
Comment on lines +11 to +15
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain those changes here?

Copy link
Contributor Author

@nddipiazza nddipiazza May 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I added the new module, I noticed that the message was misleading. It mentioned "io.cucumber" needs to be the package, but it's actually checking for io.cucumber + {sub-module}. After spinning my tires on that, I updated the message to make it clear.

And sub-modules with - in them need to be replaced with / just like you are already doing with . that way gherkin-utils becomes io/cucumber/gherkin/utils

echo
echo "${unshaded_classes}"
exit 1
Expand Down
5 changes: 5 additions & 0 deletions gherkin-utils/java/.github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
PLEASE DO NOT CREATE ISSUES IN THIS REPO.
THIS REPO IS A READ-ONLY MIRROR.

Create your issue in the Cucumber monorepo instead:
https://github.com/cucumber/cucumber/issues
5 changes: 5 additions & 0 deletions gherkin-utils/java/.github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
PLEASE DO NOT CREATE PULL REAUESTS IN THIS REPO.
THIS REPO IS A READ-ONLY MIRROR.

Create your pull request in the Cucumber monorepo instead:
https://github.com/cucumber/cucumber/pulls
17 changes: 17 additions & 0 deletions gherkin-utils/java/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.idea/
*.iml
target/
release.properties
pom.xml.releaseBackup
pom.xml.versionsBackup
dependency-reduced-pom.xml
.classpath
.deps
.project
.settings/
.tested*
.compared
.built
.codegen
# Approval tests
acceptance/
4 changes: 4 additions & 0 deletions gherkin-utils/java/.rsync
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
../LICENSE LICENSE
../../.templates/github/ .github/
../../.templates/java/ .
../testdata/ testdata/
14 changes: 14 additions & 0 deletions gherkin-utils/java/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Please read [CONTRIBUTING](https://github.com/cucumber/common/blob/main/CONTRIBUTING.md) first. You should clone
the [cucumber/cucumber](https://github.com/cucumber/cucumber) repo if you want to contribute.

## Run tests

### Using make

Just run `make` from this directory.

### Using just Maven

Just run `mvn clean test` from this directory.

Keep in mind that this will only run unit tests. The acceptance tests are only run when you build with `make`.
21 changes: 21 additions & 0 deletions gherkin-utils/java/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) Cucumber Ltd, Nicholas DiPiazza

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
1 change: 1 addition & 0 deletions gherkin-utils/java/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include default.mk
5 changes: 5 additions & 0 deletions gherkin-utils/java/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Gherkin Utilties for Java

This contains a Java library with some common Utilties

See [GherkinTest](src/test/java/io/cucumber/gherkin/utils/PrettyPrintGherkinDocumentTest.java) for sample usage.
77 changes: 77 additions & 0 deletions gherkin-utils/java/default.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Please update /.templates/java/default.mk and sync:
#
# source scripts/functions.sh && rsync_files
#
SHELL := /usr/bin/env bash
JAVA_SOURCE_FILES = $(shell find . -name "*.java")
IS_TESTDATA = $(findstring -testdata,${CURDIR})

# https://stackoverflow.com/questions/2483182/recursive-wildcards-in-gnu-make
rwildcard=$(foreach d,$(wildcard $(1:=/*)),$(call rwildcard,$d,$2) $(filter $(subst *,%,$2),$d))

default: .tested
.PHONY: default

.codegen:
touch $@

.tested: .tested-jar-check

.tested-jar-check: .deps .built
./scripts/check-jar.sh $(JAR)
touch $@

.built: pom.xml $(JAVA_SOURCE_FILES) .codegen
mvn install
touch $@

.deps:
touch $@

update-dependencies:
mvn versions:force-releases
mvn versions:use-latest-versions -Dmaven.version.rules=file://$(shell pwd)/maven-versions-rules.xml
ruby scripts/set_version_ranges.rb
.PHONY: update-dependencies

pre-release: update-version update-dependencies clean default
.PHONY: pre-release

update-version:
ifdef NEW_VERSION
mvn versions:set -DnewVersion=$(NEW_VERSION) -DgenerateBackupPoms=false
else
@echo -e "\033[0;31mNEW_VERSION is not defined. Can't update version :-(\033[0m"
exit 1
endif
.PHONY: update-version

publish: .deps
ifeq ($(IS_TESTDATA),-testdata)
# no-op
else
mvn deploy -Psign-source-javadoc -DskipTests=true
endif
.PHONY: publish

post-release:
scripts/post-release.sh
.PHONY: post-release

clean: clean-java
.PHONY: clean

clean-java:
rm -rf target .deps .tested* .built acceptance .codegen
mvn clean
.PHONY: clean-java

### COMMON stuff for all platforms

BERP_VERSION = 1.3.0
BERP_GRAMMAR = gherkin.berp

define berp-generate-parser =
-! dotnet tool list --tool-path /usr/bin | grep "berp\s*$(BERP_VERSION)" && dotnet tool update Berp --version $(BERP_VERSION) --tool-path /usr/bin
berp -g $(BERP_GRAMMAR) -t $< -o $@ --noBOM
endef
9 changes: 9 additions & 0 deletions gherkin-utils/java/maven-versions-rules.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<ruleset comparisonMethod="maven"
xmlns="http://mojo.codehaus.org/versions-maven-plugin/rule/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://mojo.codehaus.org/versions-maven-plugin/rule/2.0.0 http://mojo.codehaus.org/versions-maven-plugin/xsd/rule-2.0.0.xsd">
<ignoreVersions>
<ignoreVersion type="regex">.*pr.*</ignoreVersion>
<ignoreVersion type="regex">.*-rc.*</ignoreVersion>
<ignoreVersion type="regex">.*-M\d+</ignoreVersion>
</ignoreVersions>
</ruleset>
56 changes: 56 additions & 0 deletions gherkin-utils/java/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.cucumber</groupId>
<artifactId>gherkin-utils</artifactId>
<version>22.0.0</version>
<packaging>jar</packaging>
<name>Gherkin Utils</name>
<description>Gherkin utils for doing things such as walking a Gherkin tree or pretty printing Gherkin files.
</description>
<url>https://github.com/cucumber/common</url>

<properties>
<project.Automatic-Module-Name>io.cucumber.gherkin-utils</project.Automatic-Module-Name>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>

<scm>
<connection>scm:git:git://github.com/cucumber/common.git</connection>
<developerConnection>scm:git:[email protected]:cucumber/common.git</developerConnection>
<url>git://github.com/cucumber/common.git</url>
<tag>HEAD</tag>
</scm>

<dependencies>
<dependency>
<groupId>io.cucumber</groupId>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be test scope.

Please add a runtime dependency on messages

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i could not make this change, it caused project not to compile due to missing dependencies during compile time.

<artifactId>gherkin</artifactId>
<version>21.0.0</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
</build>

</project>
24 changes: 24 additions & 0 deletions gherkin-utils/java/scripts/check-jar.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash
#
# Verifies that the jar doesn't have any non-"io.cucumber" classes. This might happen
# if some dependencies are shaded, but some are forgotten.
#
set -uf -o pipefail

check_jar() {
jar="$1"
module_name=$(xmlstarlet sel -N pom="http://maven.apache.org/POM/4.0.0" -t -m "//pom:project.Automatic-Module-Name" -v . pom.xml)
module_path=$(echo $module_name | sed "s/\./\\\\\//g" | sed "s/-/\\\\\//g")
echo "Checking contents of ${jar} to see if it matches pattern: ${module_path}"
unshaded_classes=$(unzip -l ${jar} | grep -e "\.class" | rev | cut -d' ' -f1 | rev | grep -v "^$module_path")
if [[ "${unshaded_classes}" != "" ]]; then
echo "Some classes in ${jar} are not in the expected package matching pattern ${module_path}. Rename the classes or change the maven-shade-plugin configuration."
echo
echo "${unshaded_classes}"
exit 1
fi
}

find . -name "*.jar" | while read jar; do
check_jar "${jar}"
done
21 changes: 21 additions & 0 deletions gherkin-utils/java/scripts/post-release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash
#
# Bumps the patch version and appends -SNAPSHOT
#
set -uf -o pipefail

# Finding current version in Changelog is way simpler, as there may be multiple <version> nodes in pom.xml
# Also extracting version with maven itself is a bit tedious
if [[ $(cat "../CHANGELOG.md") =~ \[([0-9]+).([0-9]+).([0-9]+)\] ]]; then
MAJOR="${BASH_REMATCH[1]}"
MINOR="${BASH_REMATCH[2]}"
PATCH="${BASH_REMATCH[3]}"

NEW_VERSION="$MAJOR.$MINOR.$(( PATCH + 1))-SNAPSHOT"
mvn versions:set -DnewVersion=$NEW_VERSION -DgenerateBackupPoms=false
echo "Updated version in pom.xml to $NEW_VERSION"
else
echo "Unable to find current version in CHANGELOG, please update pom.xml manually"
echo "Bump the patch version in the `pom.xml` and append `-SNAPSHOT` to it."
exit 1
fi
12 changes: 12 additions & 0 deletions gherkin-utils/java/scripts/set_version_ranges.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'nokogiri'

pom = File.open("pom.xml") { |f| Nokogiri::XML(f) }

versions = pom.xpath("//xmlns:dependency/xmlns:version[../xmlns:groupId/text() = 'io.cucumber']")
versions.each do |version|
return version unless version.content =~ /^\d+\.\d+\.\d+$/
major, minor, patch = version.content.split('.').map {|i| i.to_i}
version.content = "[#{major}.#{minor}.#{patch},#{major+1}.0.0)"
end

File.open("pom.xml", "w:UTF-8") { |f| f.write(pom.to_xml) }
Loading