-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2f84974
commit 7cf4bd2
Showing
4 changed files
with
166 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
69 changes: 69 additions & 0 deletions
69
formatter-maven-plugin/src/main/java/com/regnosys/rosetta/formatter/TestMojo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters