Skip to content

Commit

Permalink
Starting code for formatter plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
maria77102 committed Nov 7, 2024
1 parent 2f84974 commit 7cf4bd2
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 20 deletions.
29 changes: 29 additions & 0 deletions formatter-maven-plugin/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>formatter-maven-plugin</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>net.sf.eclipsecs.core.CheckstyleBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>net.sf.eclipsecs.core.CheckstyleNature</nature>
</natures>
</projectDescription>
54 changes: 54 additions & 0 deletions formatter-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<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>com.regnosys.rosetta</groupId>
<artifactId>com.regnosys.rosetta.parent</artifactId>
<version>0.0.0.main-SNAPSHOT</version>
</parent>

<artifactId>formatter-maven-plugin</artifactId>
<packaging>maven-plugin</packaging>

<name>Xtext Resource Formatter</name>
<description>A plugin that formats Xtext resources</description>

<dependencies>
<!-- Inter-project dependencies -->
<dependency>
<groupId>com.regnosys.rosetta</groupId>
<artifactId>com.regnosys.rosetta</artifactId>
<version>${project.version}</version>
</dependency>

<!-- External dependencies -->
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.xtext</groupId>
<artifactId>xtext-maven-plugin</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.regnosys.rosetta.formatter;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.Injector;
import com.regnosys.rosetta.RosettaStandaloneSetup;
import com.regnosys.rosetta.formatting2.ResourceFormatterService;

@Mojo(name = "format", defaultPhase = LifecyclePhase.INITIALIZE)
public class TestMojo extends AbstractMojo{
private static Logger LOGGER = LoggerFactory.getLogger(TestMojo.class);

@Parameter(property = "path", required = true)
private String path;

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
Path directory = Paths.get(path);
LOGGER.info("Mojo running on path:" + directory.toString());

Injector inj = new RosettaStandaloneSetup().createInjectorAndDoEMFRegistration();
ResourceSet resourceSet = inj.getInstance(ResourceSet.class);
ResourceFormatterService formatterService = inj.getInstance(ResourceFormatterService.class);

try {
// Find all .rosetta files in the directory and load them from disk
List<Resource> resources = Files.walk(directory)
.filter(path -> path.toString().endsWith(".rosetta"))
.map(file -> resourceSet.getResource(URI.createFileURI(file.toString()), true))
.collect(Collectors.toList());

// format resources
formatterService.formatCollection(resources, null);

LOGGER.info(resources.toString());

// save each resource
resources.forEach(resource -> {
try {
resource.save(null);
LOGGER.info("Successfully formatted and saved file at location " + resource.getURI());
} catch (IOException e) {
LOGGER.error("Error saving file at location " + resource.getURI() + ": "+ e.getMessage());
}
});
} catch (IOException e) {
LOGGER.error("Error processing files: " + e.getMessage());
}

}

}
34 changes: 14 additions & 20 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
<!--
~ Copyright 2024 REGnosys
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<!-- ~ Copyright 2024 REGnosys ~ ~ Licensed under the Apache License, Version
2.0 (the "License"); ~ you may not use this file except in compliance with
the License. ~ You may obtain a copy of the License at ~ ~ http://www.apache.org/licenses/LICENSE-2.0
~ ~ Unless required by applicable law or agreed to in writing, software ~
distributed under the License is distributed on an "AS IS" BASIS, ~ WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ~ See the
License for the specific language governing permissions and ~ limitations
under the License. -->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Expand Down Expand Up @@ -145,6 +138,7 @@
<module>rosetta-testing</module>
<module>rosetta-maven-plugin</module>
<module>rosetta-profiling</module>
<module>formatter-maven-plugin</module>
</modules>

<dependencyManagement>
Expand Down Expand Up @@ -327,8 +321,8 @@
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- Xtext uses Log4j directly. The following dependency will
redirect Log4j messages to slf4j. -->
<!-- Xtext uses Log4j directly. The following dependency will redirect
Log4j messages to slf4j. -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
Expand Down Expand Up @@ -427,8 +421,7 @@
<executions>
<execution>
<id>Check style</id>
<!-- This needs to run /after/ xtend (test) source
generation is done. -->
<!-- This needs to run /after/ xtend (test) source generation is done. -->
<phase>process-test-sources</phase>
<goals>
<goal>check</goal>
Expand Down Expand Up @@ -596,7 +589,8 @@
</execution>
</executions>
</plugin>
<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
<!--This plugin's configuration is used to store Eclipse m2e settings
only. It has no influence on the Maven build itself. -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
Expand Down

0 comments on commit 7cf4bd2

Please sign in to comment.