Skip to content

Commit

Permalink
feat(core): Java method and field extraction with the 'javasource' in…
Browse files Browse the repository at this point in the history
…clude macro
  • Loading branch information
nandorholozsnyak committed Feb 5, 2023
1 parent fe06f72 commit 858dafe
Show file tree
Hide file tree
Showing 22 changed files with 706 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- name: install-java
uses: actions/setup-java@v3
with:
java-version: 8
java-version: 11
distribution: temurin
cache: maven
- name: Build & Verify Project
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,13 @@ jobs:
- name: Setup Java
uses: actions/setup-java@v3
with:
java-version: 8
java-version: 11
distribution: 'temurin'
cache: 'maven'
- name: Stage Files for Release
run: |
mvn versions:set -DnewVersion=$RELEASE_VERSION
mvn -Prelease deploy -DaltDeploymentRepository=local::file:./target/staging-deploy
./scripts/jbang-version-release.sh $RELEASE_VERSION
env:
RELEASE_VERSION: ${{ inputs.releaseVersion }}
- name: Run JReleaser - Full Release
Expand Down
2 changes: 1 addition & 1 deletion .sdkmanrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Enable auto-env through the sdkman_auto_env config
# Add key=value pairs of SDKs to use below
java=8.0.332-tem
java=11.0.10-open
4 changes: 2 additions & 2 deletions README.adoc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
= Single Module Project
= AsciiDoctorJ Extensions
ifndef::env-github[]
:icons: font
endif::[]
Expand All @@ -14,7 +14,7 @@ endif::[]
:toclevels: 4

[.text-center]
image:https://img.shields.io/maven-central/v/org.rodnansol/single-module-project.svg[Maven Central]
image:https://img.shields.io/maven-central/v/org.rodnansol/asciidoctorj-extensions.svg[Maven Central]
image:https://img.shields.io/badge/License-Apache_2.0-blue.svg[Apache 2.0]
image:https://img.shields.io/twitter/url/https/twitter.com/rodnansol.svg?style=social&label=Follow%20%40RodnanSol[]
image:https://dcbadge.vercel.app/api/server/USyh6XUjvP[Discord]
Expand Down
35 changes: 27 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
<modelVersion>4.0.0</modelVersion>

<groupId>org.rodnansol</groupId>
<artifactId>single-module-project</artifactId>
<artifactId>asciidoctorj-extensions</artifactId>
<version>999-SNAPSHOT</version>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.copyright>Apache-2.0</project.copyright>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven-compiler-plugin.version>3.10.1</maven-compiler-plugin.version>
Expand Down Expand Up @@ -42,11 +42,14 @@
<maven-resources-plugin.version>3.3.0</maven-resources-plugin.version>
<jreleaser-github-release.draft>false</jreleaser-github-release.draft>
<jreleaser-nexus-deploy.active>ALWAYS</jreleaser-nexus-deploy.active>
<roaster.version>2.28.0.Final</roaster.version>
<asciidoctorj.version>2.5.7</asciidoctorj.version>
<org.eclipse.jdt.core.version>3.31.0</org.eclipse.jdt.core.version>
</properties>

<name>Single Module Project</name>
<description>Single Module Project Description</description>
<url>https://github.com/rodnansol/single-module-project</url>
<name>AsciiDoctorJ Extensions</name>
<description>AsciiDoctorJ Extensions</description>
<url>https://github.com/rodnansol/asciidoctorj-extensions</url>
<inceptionYear>2023</inceptionYear>
<developers>
<developer>
Expand All @@ -69,7 +72,7 @@
<scm>
<connection>scm:git:git@${project.scm.url}</connection>
<developerConnection>scm:git:${project.scm.url}</developerConnection>
<url>[email protected]:rodnansol/single-module-project.git</url>
<url>[email protected]:rodnansol/asciidoctorj-extensions.git</url>
<tag>HEAD</tag>
</scm>

Expand All @@ -86,6 +89,22 @@
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctorj</artifactId>
<version>${asciidoctorj.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.forge.roaster</groupId>
<artifactId>roaster-api</artifactId>
<version>${roaster.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.forge.roaster</groupId>
<artifactId>roaster-jdt</artifactId>
<version>${roaster.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
Expand Down Expand Up @@ -286,7 +305,7 @@
<id>release</id>
<properties>
<ossrh.url>https://s01.oss.sonatype.org</ossrh.url>
<release.tags>#tags</release.tags>
<release.tags>#asciidoctorj #java</release.tags>
</properties>
<distributionManagement>
<snapshotRepository>
Expand Down
Empty file.
9 changes: 0 additions & 9 deletions src/main/java/org/rodnansol/SingleModule.java

This file was deleted.

24 changes: 24 additions & 0 deletions src/main/java/org/rodnansol/asciidoctorj/CodeBlockProcessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.rodnansol.asciidoctorj;

import org.asciidoctor.ast.Document;
import org.asciidoctor.extension.PreprocessorReader;

import java.util.Map;

/**
* Interface describes a block processor blueprint that can handle different attributes from the 'javasource' include macro.
*
* @author nandorholozsnyak
* @since 0.1.0
*/
public interface CodeBlockProcessor {

boolean isActive(Map<String, Object> attributes);

void process(ExtractCommand extractCommand,
Document document,
PreprocessorReader reader,
String target,
Map<String, Object> attributes);

}
50 changes: 50 additions & 0 deletions src/main/java/org/rodnansol/asciidoctorj/ExtractCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.rodnansol.asciidoctorj;

import java.util.Objects;

/**
* Class that wraps an extraction command.
*
* @author nandorholozsnyak
* @since 0.1.0
*/
public class ExtractCommand {

private final String sourceCodePath;
private final int spaceSize;
private final boolean withJavaDoc;
private final int lineLength;

public ExtractCommand(String sourceCodePath, int spaceSize, boolean withJavaDoc, int lineLength) {
this.sourceCodePath = Objects.requireNonNull(sourceCodePath, "sourceCodePath is NULL");
this.spaceSize = spaceSize;
this.withJavaDoc = withJavaDoc;
this.lineLength = lineLength;
}

public String getSourceCodePath() {
return sourceCodePath;
}

public int getSpaceSize() {
return spaceSize;
}

public boolean isWithJavaDoc() {
return withJavaDoc;
}

@Override
public String toString() {
return "ExtractCommand{" +
"sourceCodePath='" + sourceCodePath + '\'' +
", spaceSize=" + spaceSize +
", withJavaDoc=" + withJavaDoc +
", lineLength=" + lineLength +
'}';
}

public int getLineLength() {
return lineLength;
}
}
42 changes: 42 additions & 0 deletions src/main/java/org/rodnansol/asciidoctorj/ExtractFieldCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.rodnansol.asciidoctorj;

import java.util.Objects;

/**
* Class that wraps parameters for field extraction.
*
* @author nandorholozsnyak
* @since 0.1.0
*/
public class ExtractFieldCommand extends ExtractCommand {

private final String fieldName;

public ExtractFieldCommand(ExtractCommand extractCommand, String fieldName) {
super(extractCommand.getSourceCodePath(), extractCommand.getSpaceSize(), extractCommand.isWithJavaDoc(), extractCommand.getLineLength());
this.fieldName = Objects.requireNonNull(fieldName, "fieldName is NULL");
}

/**
* @param sourceCodePath Java source file's path.
* @param fieldName field name to extract.
* @param spaceSize space size for indentation.
* @param withJavaDoc
* @param lineLength
*/
public ExtractFieldCommand(String sourceCodePath, String fieldName, int spaceSize, boolean withJavaDoc, int lineLength) {
super(sourceCodePath, spaceSize, withJavaDoc, lineLength);
this.fieldName = Objects.requireNonNull(fieldName, "fieldName is NULL");
}

public String getFieldName() {
return fieldName;
}

@Override
public String toString() {
return "ExtractFieldCommand{" +
"fieldName='" + fieldName + '\'' +
"} " + super.toString();
}
}
53 changes: 53 additions & 0 deletions src/main/java/org/rodnansol/asciidoctorj/ExtractMethodCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.rodnansol.asciidoctorj;

import java.util.Arrays;
import java.util.Objects;

/**
* Class that wraps parameters for method extraction.
*
* @author nandorholozsnyak
* @since 0.1.0
*/
public class ExtractMethodCommand extends ExtractCommand {


private final String methodName;
private final String[] paramTypes;

public ExtractMethodCommand(ExtractCommand extractCommand, String methodName,String... paramTypes) {
super(extractCommand.getSourceCodePath(), extractCommand.getSpaceSize(), extractCommand.isWithJavaDoc(), extractCommand.getLineLength());
this.methodName = Objects.requireNonNull(methodName, "methodName is NULL");
this.paramTypes = paramTypes;
}

/**
* @param sourceCodePath Java source file's path.
* @param spaceSize space size for indentation.
* @param methodName method's name that should be extracted from the source code.
* @param withJavaDoc if the JavaDoc should be included or not.
* @param lineLength
* @param paramTypes list of the method argument types in String values.
*/
public ExtractMethodCommand(String sourceCodePath, int spaceSize, String methodName, boolean withJavaDoc, int lineLength, String... paramTypes) {
super(sourceCodePath, spaceSize, withJavaDoc, lineLength);
this.methodName = Objects.requireNonNull(methodName, "methodName is NULL");
this.paramTypes = paramTypes;
}

public String getMethodName() {
return methodName;
}

public String[] getParamTypes() {
return paramTypes;
}

@Override
public String toString() {
return "ExtractMethodCommand{" +
"methodName='" + methodName + '\'' +
", paramTypes=" + Arrays.toString(paramTypes) +
"} " + super.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.rodnansol.asciidoctorj;

import org.asciidoctor.ast.Document;
import org.asciidoctor.extension.PreprocessorReader;

import java.io.File;
import java.io.IOException;
import java.util.Map;

/**
* Code block processor that extracts the field from the given source path.
*
* @author nandorholozsnyak
* @since 0.1.0
*/
public class JavaFieldCodeBlockProcessor implements CodeBlockProcessor {

private static final String KEY_FIELD = "field";

@Override
public boolean isActive(Map<String, Object> attributes) {
return attributes.containsKey(KEY_FIELD);
}

@Override
public void process(ExtractCommand extractCommand, Document document, PreprocessorReader reader, String target, Map<String, Object> attributes) {
String field = (String) attributes.get(KEY_FIELD);
if (field != null && !field.isEmpty()) {
try {
String fullField = JavaSourceHelper.getField(new ExtractFieldCommand(extractCommand, field));
reader.pushInclude(
fullField,
target,
new File(".").getAbsolutePath(),
1,
attributes);
} catch (IOException e) {
throw new JavaSourceCodeExtractionException("Unable to extract field with name:[" + field + "]", e);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.rodnansol.asciidoctorj;

import org.asciidoctor.ast.Document;
import org.asciidoctor.extension.PreprocessorReader;

import java.io.File;
import java.io.IOException;
import java.util.Map;

/**
* Code block processor that extract the method from the given source path.
*
* @author nandorholozsnyak
* @since 0.1.0
*/
public class JavaMethodCodeBlockProcessor implements CodeBlockProcessor {

private static final String KEY_METHOD = "method";
private static final String KEY_METHOD_PARAM_TYPES = "types";

@Override
public boolean isActive(Map<String, Object> attributes) {
return attributes.containsKey(KEY_METHOD);
}

@Override
public void process(ExtractCommand extractCommand, Document document, PreprocessorReader reader, String target, Map<String, Object> attributes) {
String method = (String) attributes.get(KEY_METHOD);
try {
String paramTypeList = (String) attributes.getOrDefault(KEY_METHOD_PARAM_TYPES, "");
String[] paramTypes = paramTypeList.split(",");
String fullMethod = JavaSourceHelper.getMethod(new ExtractMethodCommand(extractCommand, method, paramTypes));

reader.pushInclude(
fullMethod,
target,
new File(".").getAbsolutePath(),
1,
attributes);
} catch (IOException e) {
throw new JavaSourceCodeExtractionException("Unable to extract method with name:[" + method + "]", e);
}
}
}
Loading

0 comments on commit 858dafe

Please sign in to comment.