Skip to content

Commit

Permalink
Initialize rule exporter tooling module
Browse files Browse the repository at this point in the history
  • Loading branch information
utarwyn committed Jul 18, 2024
1 parent 89a9670 commit 4af3932
Show file tree
Hide file tree
Showing 17 changed files with 876 additions and 5 deletions.
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
74 changes: 74 additions & 0 deletions tools/rule-exporter/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?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.5.4-SNAPSHOT</version>
</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>
<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);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* 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;

public class RuleMetadata {

private final RuleId id;
private final String technology;
private final String title;
private final RuleSeverity severity;
private final RuleStatus status;

private RuleMetadata(String id, String technology, String title, RuleSeverity severity, RuleStatus status) {
this.id = new RuleId(id);
this.technology = technology;
this.title = title;
this.severity = severity;
this.status = status;
}

public RuleId getId() {
return this.id;
}

public String getTechnology() {
return this.technology;
}

public String getTitle() {
return this.title;
}

public RuleSeverity getSeverity() {
return this.severity;
}

public RuleStatus getStatus() {
return this.status;
}

@Override
public String toString() {
return "RuleMetadata{" +
"id='" + id + '\'' +
", language='" + technology + '\'' +
", title='" + title + '\'' +
", severity=" + severity +
", status=" + status +
'}';
}

public static class Builder {

private String id;
private String technology;
private String title;
private RuleSeverity severity;
private RuleStatus status;

public Builder id(String id) {
this.id = id;
return this;
}

public Builder technology(String technology) {
this.technology = technology;
return this;
}

public Builder title(String title) {
this.title = title;
return this;
}

public Builder severity(RuleSeverity severity) {
this.severity = severity;
return this;
}

public Builder status(RuleStatus status) {
this.status = status;
return this;
}

public RuleMetadata build() {
return new RuleMetadata(id, technology, title, severity, status);
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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;

public enum RuleSeverity {
INFO, MINOR, MAJOR, CRITICAL
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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;

public enum RuleStatus {
READY, DEPRECATED
}
Loading

0 comments on commit 4af3932

Please sign in to comment.