Skip to content

Commit

Permalink
Maven plugin
Browse files Browse the repository at this point in the history
This completes the Maven plugin.

Affects: #30
  • Loading branch information
io7m committed Nov 14, 2022
1 parent 265c1ec commit e6cd462
Show file tree
Hide file tree
Showing 5 changed files with 251 additions and 9 deletions.
39 changes: 35 additions & 4 deletions com.io7m.cedarbridge.examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,12 @@
<configuration>
<outputDirectory>${project.build.directory}/generated-sources/annotations</outputDirectory>
<files>
<file>src/main/resources/com/io7m/cedarbridge/examples/pastebin.cbs</file>
<file>${project.build.resources[0].directory}/com/io7m/cedarbridge/examples/pastebin.cbs</file>
</files>
<language>Java 17+</language>
<languageName>Java 17+</languageName>
</configuration>
</execution>

<execution>
<id>generate-chat</id>
<phase>generate-sources</phase>
Expand All @@ -122,9 +123,39 @@
<configuration>
<outputDirectory>${project.build.directory}/generated-sources/annotations</outputDirectory>
<files>
<file>src/main/resources/com/io7m/cedarbridge/examples/chat.cbs</file>
<file>${project.build.resources[0].directory}/com/io7m/cedarbridge/examples/chat.cbs</file>
</files>
<languageName>Java 17+</languageName>
</configuration>
</execution>

<execution>
<id>generate-paste-doc</id>
<phase>generate-sources</phase>
<goals>
<goal>document</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/bridgedoc</outputDirectory>
<files>
<file>${project.build.resources[0].directory}/com/io7m/cedarbridge/examples/pastebin.cbs</file>
</files>
<languageName>XHTML</languageName>
</configuration>
</execution>

<execution>
<id>generate-chat-doc</id>
<phase>generate-sources</phase>
<goals>
<goal>document</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/bridgedoc</outputDirectory>
<files>
<file>${project.build.resources[0].directory}/com/io7m/cedarbridge/examples/chat.cbs</file>
</files>
<language>Java 17+</language>
<languageName>XHTML</languageName>
</configuration>
</execution>
</executions>
Expand Down
14 changes: 14 additions & 0 deletions com.io7m.cedarbridge.maven_plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>com.io7m.jaffirm</groupId>
<artifactId>com.io7m.jaffirm.core</artifactId>
</dependency>
<dependency>
<groupId>com.io7m.jsx</groupId>
<artifactId>com.io7m.jsx.core</artifactId>
</dependency>
<dependency>
<groupId>com.io7m.jsx</groupId>
<artifactId>com.io7m.jsx.parser</artifactId>
</dependency>

<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.annotation.bundle</artifactId>
Expand Down Expand Up @@ -122,6 +135,7 @@
<failOnWarning>true</failOnWarning>
<ignoredUnusedDeclaredDependencies>
<ignoredUnusedDeclaredDependency>com.io7m.cedarbridge:*</ignoredUnusedDeclaredDependency>
<ignoredUnusedDeclaredDependency>com.io7m.jsx:*</ignoredUnusedDeclaredDependency>
</ignoredUnusedDeclaredDependencies>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.io7m.cedarbridge.schema.compiler.api.CBSchemaCompilerFactoryType;
import com.io7m.cedarbridge.schema.compiler.internal.CBServices;
import com.io7m.cedarbridge.schema.core_types.CBCore;
import com.io7m.jaffirm.core.Postconditions;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
Expand All @@ -49,7 +50,7 @@ public final class CBCompileMojo extends AbstractMojo
private List<String> files = List.of();

@Parameter(
name = "include",
name = "includes",
required = false
)
private List<String> includes = List.of();
Expand All @@ -58,7 +59,7 @@ public final class CBCompileMojo extends AbstractMojo
name = "outputDirectory",
required = true
)
private String output;
private String outputDirectory;

@Parameter(
name = "noCore",
Expand All @@ -68,7 +69,7 @@ public final class CBCompileMojo extends AbstractMojo
private boolean noCore;

@Parameter(
name = "language",
name = "languageName",
required = true
)
private String languageName;
Expand Down Expand Up @@ -140,13 +141,21 @@ public void execute()
compiler.execute();

final var codeGeneratorConfiguration =
new CBCodeGeneratorConfiguration(Path.of(this.output));
new CBCodeGeneratorConfiguration(Path.of(this.outputDirectory));

final var codeGenerator =
codeGeneratorFactory.createGenerator(codeGeneratorConfiguration);

for (final var packV : compilation.compiledPackages()) {
codeGenerator.execute(packV);
final var result = codeGenerator.execute(packV);
final var created = result.createdFiles();
created.forEach(path -> {
this.getLog().info("create %s".formatted(path));
});
Postconditions.checkPostconditionV(
!created.isEmpty(),
"Must have created at least one file."
);
}
} catch (final CBSchemaCompilerException | CBCodeGeneratorException e) {
throw new MojoExecutionException(e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
/*
* Copyright © 2022 Mark Raynsford <code@io7m.com> https://www.io7m.com
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

package com.io7m.cedarbridge.maven_plugin;

import com.io7m.cedarbridge.bridgedoc.api.CBDocGeneratorConfiguration;
import com.io7m.cedarbridge.bridgedoc.api.CBDocGeneratorException;
import com.io7m.cedarbridge.bridgedoc.api.CBDocGenerators;
import com.io7m.cedarbridge.schema.compiler.api.CBSchemaCompilerConfiguration;
import com.io7m.cedarbridge.schema.compiler.api.CBSchemaCompilerException;
import com.io7m.cedarbridge.schema.compiler.api.CBSchemaCompilerFactoryType;
import com.io7m.cedarbridge.schema.compiler.internal.CBServices;
import com.io7m.cedarbridge.schema.core_types.CBCore;
import com.io7m.jaffirm.core.Postconditions;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

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

import static org.apache.maven.plugins.annotations.LifecyclePhase.GENERATE_SOURCES;

/**
* The "document" mojo.
*/

@Mojo(name = "document", defaultPhase = GENERATE_SOURCES)
public final class CBDocumentMojo extends AbstractMojo
{
@Parameter(
name = "files"
)
private List<String> files = List.of();

@Parameter(
name = "includes"
)
private List<String> includes = List.of();

@Parameter(
name = "outputDirectory",
required = true
)
private String outputDirectory;

@Parameter(
name = "noCore"
)
private boolean noCore;

@Parameter(
name = "languageName",
required = true
)
private String languageName;

@Parameter(
name = "customStyle",
required = false
)
private String customStyle;

@Parameter(
required = false,
name = "skip",
property = "cedarbridge.skip",
defaultValue = "false"
)
private boolean skip;

/**
* The "document" mojo.
*/

public CBDocumentMojo()
{

}

@Override
public void execute()
throws MojoExecutionException
{
if (this.skip) {
return;
}

final var docGenerators =
new CBDocGenerators();
final var compilers =
CBServices.findService(CBSchemaCompilerFactoryType.class);

final var docGeneratorFactory =
docGenerators.findByLanguageName(this.languageName)
.orElseThrow(() -> new IllegalArgumentException(String.format(
"No documentation generator available for the language '%s'",
this.languageName)
));

final var compileFiles =
this.files.stream()
.map(Path::of)
.map(Path::toAbsolutePath)
.collect(Collectors.toList());

final var includeDirectories =
this.includes.stream()
.map(Path::of)
.map(Path::toAbsolutePath)
.collect(Collectors.toList());

final var configuration =
new CBSchemaCompilerConfiguration(
includeDirectories,
compileFiles
);

final var compiler =
compilers.createCompiler(configuration);

if (!this.noCore) {
compiler.loader().register(CBCore.get());
}

try {
final var compilation =
compiler.execute();

final var outputPath =
Path.of(this.outputDirectory).toAbsolutePath();

Files.createDirectories(outputPath);

final var docGeneratorConfiguration =
new CBDocGeneratorConfiguration(
outputPath,
Optional.ofNullable(this.customStyle)
);

final var docGenerator =
docGeneratorFactory.createGenerator(docGeneratorConfiguration);

final var created = new HashSet<Path>();
for (final var packV : compilation.compiledPackages()) {
final var result = docGenerator.execute(packV);
created.addAll(result.createdFiles());
}

created.forEach(path -> {
this.getLog().info("create %s".formatted(path));
});
Postconditions.checkPostconditionV(
!created.isEmpty(),
"Must have created at least one file."
);
} catch (final CBSchemaCompilerException
| CBDocGeneratorException
| IOException e) {
throw new MojoExecutionException(e);
}
}
}
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,12 @@
<suppressionsLocation>checkstyle-filter.xml</suppressionsLocation>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.6.4</version>
</plugin>
</plugins>
</pluginManagement>

Expand Down

0 comments on commit e6cd462

Please sign in to comment.