Skip to content
Snippets Groups Projects
Commit 09f2414c authored by René Schöne's avatar René Schöne
Browse files

Bugfixing, 0.2.6

parent ebe3453a
Branches
No related tags found
No related merge requests found
Pipeline #8879 passed
......@@ -4,7 +4,7 @@ plugins {
}
group 'org.jastadd.preprocessor'
version '0.2.5'
version '0.2.6'
gradlePlugin {
plugins {
......@@ -21,6 +21,7 @@ publishing {
maven(MavenPublication) {
groupId = "org.jastadd.preprocessor"
artifactId = "testing"
from components.java // jar
}
}
repositories {
......
......@@ -7,6 +7,7 @@ import org.gradle.api.tasks.OutputDirectory;
import java.io.File;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
/**
......@@ -14,24 +15,40 @@ import java.util.List;
*
* @author rschoene - Initial contribution
*/
public class JastAddConfiguration {
@Optional
@Input
String jastAddList;
public interface JastAddConfiguration {
@Input
String packageName;
String getPackageName();
void setPackageName(String packageName);
@InputFiles
List<File> inputFiles;
@Optional
@Input
String getJastAddList();
void setJastAddList(String jastAddList);
@Optional
@Input
boolean run = true;
boolean isSkipRun();
void setSkipRun(boolean skipRun);
@Optional
@Input
List<String> extraOptions;
List<String> getExtraOptions();
void setExtraOptions(List<String> extraOptions);
default List<String> getExtraOptionsOrDefault() {
return getExtraOptions() != null ? getExtraOptions() : Collections.emptyList();
}
@InputFiles
List<File> getInputFiles();
void setInputFiles(List<File> inputFiles);
@Optional
@OutputDirectory
File getOutputDir();
void setOutputDir(File outputDir);
@OutputDirectory
File outputDir = Paths.get("src", "test", "java-gen").toFile();
default File getOutputDirOrDefault() {
return getOutputDir() != null ? getOutputDir() : Paths.get("src", "test", "java-gen").toFile();
}
}
......@@ -19,36 +19,35 @@ public class PreprocessorPlugin implements Plugin<Project> {
@Override
public void apply(Project project) {
CompilerLocationExtension extension = project.getExtensions().create(
"preprocessor.testing",
"preprocessorTesting",
CompilerLocationExtension.class,
project);
Set<Task> tasks = project.getTasksByName("compileTestJava", false);
// there should be only one task "compileTestJava"
testTask = tasks.iterator().next();
// set compiler locations (if set)
project.afterEvaluate(p -> {
RelastTest.setCompilerLocation(extension.getRelastCompilerLocation().getOrNull());
RagConnectTest.setRagconnectCompilerLocation(extension.getRagconnectCompilerLocation().getOrNull());
});
// setup tasks
TaskCollection<RelastTest> relastTests = project.getTasks().withType(RelastTest.class);
relastTests.forEach(relastTest -> setupRelastTest(relastTest,
extension.getRelastCompilerLocation().getOrNull()));
// relastTests.whenTaskAdded(relastTest -> setupRelastTest(relastTest, extension.getRelastCompilerLocation().getOrNull()));
relastTests.whenTaskAdded(this::setupRelastTest);
TaskCollection<RagConnectTest> ragconnectTests = project.getTasks().withType(RagConnectTest.class);
ragconnectTests.forEach(relastTest -> setupRagconnectTest(relastTest,
extension.getRelastCompilerLocation().getOrNull(),
extension.getRagconnectCompilerLocation().getOrNull()));
ragconnectTests.whenTaskAdded(this::setupRagconnectTest);
}
private void setupRelastTest(RelastTest relastTest, String compilerLocation) {
private void setupRelastTest(RelastTest relastTest) {
testTask.dependsOn(relastTest);
relastTest.setCompilerLocation(compilerLocation);
relastTest.setGroup("verification");
relastTest.setDescription("Runs a relast test");
}
private void setupRagconnectTest(RagConnectTest ragConnectTest,
String relastCompilerLocation,
String ragconnectCompilerLocation) {
private void setupRagconnectTest(RagConnectTest ragConnectTest) {
testTask.dependsOn(ragConnectTest);
ragConnectTest.setRelastCompilerLocation(relastCompilerLocation);
ragConnectTest.setRagconnectCompilerLocation(ragconnectCompilerLocation);
ragConnectTest.setGroup("verification");
ragConnectTest.setDescription("Runs a ragconnect test");
}
......
......@@ -12,26 +12,37 @@ import java.util.List;
*
* @author rschoene - Initial contribution
*/
public class RagConnectConfiguration {
public interface RagConnectConfiguration {
@Input
String rootNode;
String getRootNode();
void setRootNode(String rootNode);
@Optional
@Input
boolean logReads;
boolean isLogReads();
void setLogReads(boolean logReads);
@Optional
@Input
boolean logWrites;
boolean isLogWrites();
void setLogWrites(boolean logWrites);
@Input
String outputDir;
String getOutputDir();
void setOutputDir(String outputDir);
@Optional
@Input
List<String> protocols;
@InputFiles
List<File> inputFiles;
boolean isVerbose();
void setVerbose(boolean verbose);
@Optional
@Input
boolean verbose = false;
List<String> getProtocols();
void setProtocols(List<String> protocols);
@InputFiles
List<File> getInputFiles();
void setInputFiles(List<File> inputFiles);
}
package org.jastadd.preprocessor.testing.plugin;
import org.gradle.api.DefaultTask;
import groovy.lang.Closure;
import org.gradle.api.Project;
import org.gradle.api.file.FileCollection;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.tasks.Nested;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.SourceSetContainer;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.*;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import static groovy.lang.Closure.DELEGATE_FIRST;
/**
* RagConnect Test Task.
*
* @author rschoene - Initial contribution
*/
public class RagConnectTest extends DefaultTask {
public abstract class RagConnectTest extends RelastTest {
@Nested
RagConnectConfiguration ragconnect;
abstract RagConnectConfiguration getRagconnect();
@Nested
RelastConfiguration relast;
private static String ragconnectCompilerLocation;
@Nested
JastAddConfiguration jastadd;
public static void setRagconnectCompilerLocation(String ragconnectCompilerLocation) {
RagConnectTest.ragconnectCompilerLocation = ragconnectCompilerLocation;
}
private String relastCompilerLocation;
private String ragconnectCompilerLocation;
@SuppressWarnings("unused")
public void ragconnect(Closure<?> c) {
c.setResolveStrategy(DELEGATE_FIRST);
c.setDelegate(getRagconnect());
c.call();
}
public void setRelastCompilerLocation(String relastCompilerLocation) {
this.relastCompilerLocation = relastCompilerLocation;
@OutputFiles
public List<File> grammarFilesGeneratedByRagconnect() {
// files are RagConnect.relast and <INPUT_GRAMMAR(s)>.relast
List<File> result = new ArrayList<>();
result.add(resolveOutputDir("RagConnect.relast"));
for (File inputFile : getRagconnect().getInputFiles()) {
String filename = inputFile.getName();
if (Utils.fileExtension(filename).equals("relast")) {
result.add(resolveOutputDir(filename));
}
}
return result;
}
public void setRagconnectCompilerLocation(String ragconnectCompilerLocation) {
this.ragconnectCompilerLocation = ragconnectCompilerLocation;
@OutputFiles
public List<File> aspectFilesGeneratedByRagconnect() {
// files are RagConnect.jadd and the handler
List<File> result = new ArrayList<>();
result.add(resolveOutputDir("RagConnect.jadd"));
List<String> protocols = getRagconnect().getProtocols();
if (protocols == null || protocols.isEmpty() || protocols.contains("mqtt")) {
result.add(resolveOutputDir("MqttHandler.jadd"));
}
if (protocols != null && protocols.contains("rest")) {
result.add(resolveOutputDir("RestHandler.jadd"));
}
if (protocols != null && protocols.contains("java")) {
result.add(resolveOutputDir("JavaHandler.jadd"));
}
return result;
}
private File resolveOutputDir(String s) {
return Paths.get(getRagconnect().getOutputDir(), s).toFile();
}
@TaskAction
......@@ -46,10 +79,15 @@ public class RagConnectTest extends DefaultTask {
}
protected void runTest() throws IOException {
// run ragconnect before
Project project = getProject();
String absoluteProjectPath = project.getProjectDir().getAbsolutePath();
// TODO maybe generated files should be deleted here?
// delete generated files
project.delete(deleteSpec -> {
deleteSpec.delete(grammarFilesGeneratedByRagconnect());
deleteSpec.delete(aspectFilesGeneratedByRagconnect());
});
// run ragconnect first
project.getPlugins().withType(JavaPlugin.class, javaPlugin -> {
SourceSetContainer sourceSets = (SourceSetContainer) project.getProperties().get("sourceSets");
FileCollection runtimeClasspath = sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME).getRuntimeClasspath();
......@@ -62,31 +100,29 @@ public class RagConnectTest extends DefaultTask {
} else {
javaExecSpec.setMain("org.jastadd.ragconnect.compiler.Compiler");
}
args.add("--o=" + ragconnect.outputDir);
args.add("--rootNode=" + ragconnect.rootNode);
if (ragconnect.logReads) {
args.add("--o=" + getRagconnect().getOutputDir());
args.add("--rootNode=" + getRagconnect().getRootNode());
if (getRagconnect().isLogReads()) {
args.add("--logReads");
}
if (ragconnect.logWrites) {
if (getRagconnect().isLogWrites()) {
args.add("--logWrites");
}
if (ragconnect.verbose) {
if (getRagconnect().isVerbose()) {
args.add("--verbose");
}
if (ragconnect.protocols != null && !ragconnect.protocols.isEmpty()) {
args.add("--protocols=" + String.join(",", ragconnect.protocols));
if (getRagconnect().getProtocols() != null && !getRagconnect().getProtocols().isEmpty()) {
args.add("--protocols=" + String.join(",", getRagconnect().getProtocols()));
}
args.addAll(ragconnect.inputFiles);
args.addAll(getJastadd().getExtraOptionsOrDefault());
args.addAll(getRagconnect().getInputFiles());
javaExecSpec.args(args);
}).assertNormalExitValue();
});
// now run relast + jastadd
RelastTest relastTest = new RelastTest();
relastTest.jastadd = jastadd;
relastTest.relast = relast;
relastTest.setCompilerLocation(relastCompilerLocation);
relastTest.runTest();
getRelast().setInputFiles(grammarFilesGeneratedByRagconnect());
super.runTest();
}
}
......@@ -12,31 +12,39 @@ import java.util.List;
*
* @author rschoene - Initial contribution
*/
public class RelastConfiguration {
public interface RelastConfiguration {
@Input
boolean useJastAddNames;
boolean isUseJastAddNames();
void setUseJastAddNames(boolean useJastAddNames);
@Input
String grammarName;
String getGrammarName();
void setGrammarName(String grammarName);
@InputFiles
@Optional
List<File> inputFiles;
List<File> getInputFiles();
void setInputFiles(List<File> inputFiles);
@Input
boolean writeToFile = true;
boolean isWriteToFile(); // = true;
void setWriteToFile(boolean writeToFile);
@Input
boolean resolverHelper = true;
boolean isResolverHelper(); // = true;
void setResolverHelper(boolean resolverHelper);
@Optional
@Input
String listClass;
String getListClass();
void setListClass(String listClass);
@Optional
@Input
String serializer;
String getSerializer();
void setSerializer(String serializer);
@Input
boolean verbose = false;
boolean isVerbose(); // = false;
void setVerbose(boolean verbose);
}
package org.jastadd.preprocessor.testing.plugin;
import groovy.lang.Closure;
import org.gradle.api.DefaultTask;
import org.gradle.api.Project;
import org.gradle.api.file.FileCollection;
......@@ -9,6 +10,7 @@ import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.SourceSetContainer;
import org.gradle.api.tasks.TaskAction;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
......@@ -18,31 +20,45 @@ import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import static groovy.lang.Closure.DELEGATE_FIRST;
/**
* RelAst Test Task
*
* @author rschoene - Initial contribution
*/
public class RelastTest extends DefaultTask {
public abstract class RelastTest extends DefaultTask {
// configuration from plugin
private String compilerLocation;
private static String compilerLocation;
@Nested
RelastConfiguration relast;
abstract RelastConfiguration getRelast();
@Nested
JastAddConfiguration jastadd;
abstract JastAddConfiguration getJastadd();
public void setCompilerLocation(String compilerLocation) {
this.compilerLocation = compilerLocation;
public static void setCompilerLocation(String compilerLocation) {
RelastTest.compilerLocation = compilerLocation;
}
private final String[] genSuffixes = {".ast", ".jadd", "RefResolver.jadd", "ResolverStubs.jrag", "Serializer.jadd"};
private static final String[] genSuffixes = {".ast", ".jadd", "RefResolver.jadd", "ResolverStubs.jrag", "Serializer.jadd"};
private Path pathToAbsoluteProject(String filename) {
return Paths.get(getProject().getProjectDir().getAbsolutePath(), filename);
}
public void relast(Closure<?> c) {
c.setResolveStrategy(DELEGATE_FIRST);
c.setDelegate(getRelast());
c.call();
}
public void jastadd(Closure<?> c) {
c.setResolveStrategy(DELEGATE_FIRST);
c.setDelegate(getJastadd());
c.call();
}
@TaskAction
void taskAction() throws IOException {
runTest();
......@@ -50,28 +66,28 @@ public class RelastTest extends DefaultTask {
protected void runTest() throws IOException {
Project project = getProject();
if (relast.verbose) {
if (getRelast().isVerbose()) {
System.out.println("Running relast test in " + project.getDisplayName());
System.out.println("relast files: " + relast.inputFiles);
System.out.println("relast files: " + getRelast().getInputFiles());
System.out.println("Deleting files");
}
// first, delete generated files
List<Path> genFiles = new ArrayList<>();
for (String suffix : genSuffixes) {
genFiles.add(pathToAbsoluteProject(relast.grammarName + suffix));
genFiles.add(pathToAbsoluteProject(getRelast().getGrammarName() + suffix));
}
if (relast.verbose) {
if (getRelast().isVerbose()) {
System.out.println("gen files: " + genFiles);
}
project.delete(deleteSpec -> {
deleteSpec.delete(genFiles);
// deleteSpec.delete(Paths.get(jastadd.outputDir, jastadd.packageName));
deleteSpec.delete(jastadd.outputDir.toPath().resolve(jastadd.packageName));
deleteSpec.delete(getJastadd().getOutputDirOrDefault().toPath().resolve(getJastadd().getPackageName()));
});
// create output directories, if not existing
createDirectory(pathToAbsoluteProject(jastadd.outputDir.getName()));
createDirectory(pathToAbsoluteProject(relast.grammarName).getParent());
if (relast.verbose) {
createDirectory(pathToAbsoluteProject(getJastadd().getOutputDirOrDefault().getName()));
createDirectory(pathToAbsoluteProject(getRelast().getGrammarName()).getParent());
if (getRelast().isVerbose()) {
System.out.println("Pre processing, running relast");
}
// then, run relast pre processing
......@@ -79,43 +95,49 @@ public class RelastTest extends DefaultTask {
SourceSetContainer sourceSets = (SourceSetContainer) project.getProperties().get("sourceSets");
FileCollection runtimeClasspath = sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME).getRuntimeClasspath();
project.javaexec(javaExecSpec -> {
List<Object> args = new ArrayList<>();
List<String> args = new ArrayList<>();
javaExecSpec.setClasspath(runtimeClasspath);
if (compilerLocation != null) {
if (RelastTest.compilerLocation != null) {
javaExecSpec.setMain("-jar");
args.add(compilerLocation);
args.add(RelastTest.compilerLocation);
} else {
javaExecSpec.setMain("org.jastadd.relast.compiler.Compiler");
}
args.addAll(relast.inputFiles.stream().map(filename -> pathToAbsoluteProject(filename.getName())).collect(Collectors.toList()));
for (File file : getRelast().getInputFiles()) {
args.add(file.getAbsolutePath());
}
args.add("--quiet");
if (relast.writeToFile) {
if (getRelast().isWriteToFile()) {
args.add("--file");
}
if (relast.useJastAddNames) {
if (getRelast().isUseJastAddNames()) {
args.add("--useJastAddNames");
}
if (relast.resolverHelper) {
if (getRelast().isResolverHelper()) {
args.add("--resolverHelper");
}
if (jastadd.jastAddList != null) {
args.add("--jastAddList=" + jastadd.jastAddList);
if (getJastadd().getJastAddList() != null) {
args.add("--jastAddList=" + getJastadd().getJastAddList());
}
if (relast.listClass != null) {
args.add("--listClass=" + relast.listClass);
if (getRelast().getListClass() != null) {
args.add("--listClass=" + getRelast().getListClass());
}
if (relast.serializer != null) {
args.add("--serializer=" + relast.serializer);
if (getRelast().getSerializer() != null) {
args.add("--serializer=" + getRelast().getSerializer());
}
args.add("--grammarName=" + pathToAbsoluteProject(relast.grammarName));
if (relast.verbose) {
System.out.println("Start relast with args: " + args);
args.add("--grammarName=" + pathToAbsoluteProject(getRelast().getGrammarName()));
if (getRelast().isVerbose()) {
System.out.println("Start relast with args: " + args + " and main: " + javaExecSpec.getMain());
}
javaExecSpec.args(args);
}).assertNormalExitValue();
});
if (jastadd.run) {
if (relast.verbose) {
if (getJastadd().isSkipRun()) {
if (getRelast().isVerbose()) {
System.out.println("Skipping run of JastAdd");
}
} else {
if (getRelast().isVerbose()) {
System.out.println("Compile with JastAdd");
}
// check which files were actually generated
......@@ -128,15 +150,15 @@ public class RelastTest extends DefaultTask {
javaExecSpec.setClasspath(runtimeClasspath);
javaExecSpec.setMain("org.jastadd.JastAdd");
List<Object> args = new ArrayList<>();
args.add("--o=" + pathToAbsoluteProject(jastadd.outputDir.getName()));
args.add("--package=" + jastadd.packageName);
if (jastadd.jastAddList != null) {
args.add("--List=" + jastadd.jastAddList);
args.add("--o=" + pathToAbsoluteProject(getJastadd().getOutputDirOrDefault().getName()));
args.add("--package=" + getJastadd().getPackageName());
if (getJastadd().getJastAddList() != null) {
args.add("--List=" + getJastadd().getJastAddList());
}
args.addAll(jastadd.extraOptions);
args.addAll(getJastadd().getExtraOptionsOrDefault());
args.addAll(genFiles);
args.addAll(jastadd.inputFiles);
if (relast.verbose) {
args.addAll(getJastadd().getInputFiles());
if (getRelast().isVerbose()) {
System.out.println("Start JastAdd with args: " + args);
}
javaExecSpec.args(args);
......@@ -147,7 +169,7 @@ public class RelastTest extends DefaultTask {
private boolean verboseFileNotExists(Path path) {
boolean fileDoesNotExist = !Files.exists(path);
if (fileDoesNotExist && relast.verbose) {
if (fileDoesNotExist && getRelast().isVerbose()) {
System.out.println("Do not include " + path);
}
return fileDoesNotExist;
......@@ -157,7 +179,7 @@ public class RelastTest extends DefaultTask {
if (Files.exists(path) && Files.isDirectory(path)) {
return;
}
if (relast.verbose) {
if (getRelast().isVerbose()) {
System.out.println("Creating " + path.toAbsolutePath());
}
try {
......
package org.jastadd.preprocessor.testing.plugin;
/**
* Utility methods.
*
* @author rschoene - Initial contribution
*/
public class Utils {
public static String fileExtension(String filename) {
int indexOfDot = filename.lastIndexOf('.');
return indexOfDot == -1 ? filename : filename.substring(indexOfDot + 1);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment