Skip to content
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

Add first iteration of rule exportation tool #303

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
60 changes: 60 additions & 0 deletions .github/workflows/export_rules.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: Export rules

on:
push:
branches:
- main
paths-ignore:
- '*.md'
- '.github/**/*.yml'
tags:
- '[0-9]+.[0-9]+.[0-9]+'

jobs:
export:
name: Export
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up JDK 11
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: 11
cache: 'maven'

- name: Build
run: mvn -e -B install

- name: Retrieve current version
id: version
run: |
echo "version=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)" >> $GITHUB_OUTPUT

- name: Export
run: |
mkdir -p dist && \
java -jar ./tools/rule-exporter/target/rule-exporter-${{ steps.version.outputs.version }}.jar \
./ecocode-rules-specifications/target/ecocode-rules-specifications-${{ steps.version.outputs.version }}.jar \
./dist/rules.json

- name: Archive artifacts
uses: actions/upload-pages-artifact@v3
with:
path: ./dist

deploy:
needs: export
permissions:
pages: write # to deploy to Pages
id-token: write # to verify the deployment originates from an appropriate source
environment:
name: rules
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>io.ecocode</groupId>
Expand Down Expand Up @@ -80,6 +81,7 @@

<modules>
<module>ecocode-rules-specifications</module>
<module>tools/rule-exporter</module>
</modules>

<scm>
Expand All @@ -94,14 +96,12 @@
</issueManagement>

<properties>

<encoding>UTF-8</encoding>
<project.build.sourceEncoding>${encoding}</project.build.sourceEncoding>
<project.reporting.outputEncoding>${encoding}</project.reporting.outputEncoding>

</properties>

<!-- <build>
<build>
<plugins>
<plugin>
<groupId>com.mycila</groupId>
Expand Down Expand Up @@ -132,7 +132,7 @@
</executions>
</plugin>
</plugins>
</build> -->
</build>

<profiles>
<profile>
Expand Down
106 changes: 106 additions & 0 deletions tools/rule-exporter/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.ecocode</groupId>
<artifactId>ecocode-parent</artifactId>
<version>1.6.3-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

<artifactId>rule-exporter</artifactId>

<name>ecoCode Rule Exporter</name>
<description>Export all rules to JSON files usable by the website</description>
<url>https://github.com/green-code-initiative/ecoCode/tree/main/tools</url>
<inceptionYear>2024</inceptionYear>

<properties>
<java.version>11</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<maven.deploy.skip>true</maven.deploy.skip>
<maven.install.skip>true</maven.install.skip>

<version.jakarta-json-api>2.1.3</version.jakarta-json-api>
<version.jakarta-json>2.0.1</version.jakarta-json>
<version.junit>5.10.1</version.junit>
<version.assertj>3.25.3</version.assertj>
</properties>

<dependencies>
<dependency>
<groupId>jakarta.json</groupId>
<artifactId>jakarta.json-api</artifactId>
<version>${version.jakarta-json-api}</version>
</dependency>

<dependency>
<groupId>org.glassfish</groupId>
<artifactId>jakarta.json</artifactId>
<version>${version.jakarta-json}</version>
<classifier>module</classifier>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${version.junit}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${version.assertj}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>io.ecocode.tools.exporter.RuleExporter</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.5.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>false</shadedArtifactAttached>
<minimizeJar>true</minimizeJar>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* ecoCode Rule Exporter - Export all rules to JSON files usable by the website
* Copyright © 2024 Green Code Initiative (https://www.ecocode.io)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package io.ecocode.tools.exporter;

import io.ecocode.tools.exporter.infra.RuleReader;
import io.ecocode.tools.exporter.infra.RuleWriter;

import java.io.IOException;

public class RuleExporter {

public static void main(String[] args) throws IOException {
if (args.length != 2) {
System.out.println("Usage: java -jar rule-exporter.jar <input jar> <output json>");
System.exit(1);
}

RuleReader reader = new RuleReader(args[0]);
RuleWriter writer = new RuleWriter(args[1]);
writer.writeRules(reader.readRules());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* ecoCode Rule Exporter - Export all rules to JSON files usable by the website
* Copyright © 2024 Green Code Initiative (https://www.ecocode.io)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package io.ecocode.tools.exporter.domain;

import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RuleId implements Comparable<RuleId> {

private static final Pattern PATTERN = Pattern.compile("^EC(\\d+)$");

private final String naturalId;

private final long id;

public RuleId(String naturalId) {
Matcher matcher = PATTERN.matcher(naturalId);
if (matcher.find()) {
this.naturalId = naturalId;
this.id = Long.parseLong(matcher.group(1));
} else {
throw new IllegalArgumentException("Invalid rule id: " + naturalId);
}
}

@Override
public String toString() {
return this.naturalId;
}

@Override
public int compareTo(RuleId o) {
return Long.compare(this.id, o.id);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
RuleId ruleId = (RuleId) o;
return id == ruleId.id;
}

@Override
public int hashCode() {
return Objects.hash(id);
}

}
Loading
Loading