Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor EclipseCollectionsCodeGenerator #1322

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
package org.eclipse.collections.codegenerator.maven;

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.List;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
Expand Down Expand Up @@ -43,16 +39,14 @@ public void execute() throws MojoExecutionException
this.getLog().info("Generating sources in " + this.mavenProject.getArtifactId() + " to " + this.getOutputDirectory());
}

List<URL> urls = Arrays.asList(((URLClassLoader) this.getClass().getClassLoader()).getURLs());

boolean[] error = new boolean[1];
ErrorListener errorListener = string -> {
this.getLog().error(string);
error[0] = true;
};

EclipseCollectionsCodeGenerator codeGenerator = new EclipseCollectionsCodeGenerator(
this.getTemplateDirectory(),
urls,
errorListener,
this.getOutputDirectory(),
this.getFileExtension());
Expand Down
22 changes: 22 additions & 0 deletions eclipse-collections-code-generator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,28 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.github.classgraph</groupId>
<artifactId>classgraph</artifactId>
<version>4.8.146</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.javaparser</groupId>
<artifactId>javaparser-core</artifactId>
<version>3.24.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.nio.file.Paths;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;
import java.util.zip.CRC32;

import org.eclipse.collections.codegenerator.model.Primitive;
Expand All @@ -27,8 +28,7 @@

public class EclipseCollectionsCodeGenerator
{
private final String templateDirectory;
private final List<URL> classPathURLs;
private final Pattern resourcePattern;
private final ErrorListener errorListener;
private final File outputDirectory;
private final String fileExtension;
Expand All @@ -37,13 +37,12 @@ public class EclipseCollectionsCodeGenerator

public EclipseCollectionsCodeGenerator(
String templateDirectory,
List<URL> classPathURLs,
ErrorListener errorListener,
File outputDirectory,
String fileExtension)
{
this.templateDirectory = Objects.requireNonNull(templateDirectory);
this.classPathURLs = Objects.requireNonNull(classPathURLs);
Objects.requireNonNull(templateDirectory);
this.resourcePattern = Pattern.compile(templateDirectory + "/.*\\.stg");
this.errorListener = Objects.requireNonNull(errorListener);
this.outputDirectory = Objects.requireNonNull(outputDirectory);
this.fileExtension = Objects.requireNonNull(fileExtension);
Expand All @@ -56,10 +55,9 @@ public EclipseCollectionsCodeGenerator(
*/
public int generateFiles()
{
List<URL> allTemplateFilesFromClassPath =
FileUtils.getAllTemplateFilesFromClasspath(this.templateDirectory, this.classPathURLs);
List<URL> templateFiles = FileUtils.findTemplateFiles(this.resourcePattern);

for (URL url : allTemplateFilesFromClassPath)
for (URL url : templateFiles)
{
STGroupFile templateFile = new STGroupFile(url, "UTF-8", '<', '>');
if (!templateFile.isDefined("fileName"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,15 @@

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
import java.util.regex.Pattern;

import io.github.classgraph.ClassGraph;

public final class FileUtils
{
Expand Down Expand Up @@ -77,88 +75,11 @@ public static void writeToFile(String data, File outputFile, boolean outputFileM
}
}

public static List<URL> getAllTemplateFilesFromClasspath(String templateDirectory, List<URL> classPathURLs)
{
List<URL> files = new ArrayList<>();
try
{
for (URL url : classPathURLs)
{
recurseURL(url, files, templateDirectory);
}
}
catch (IOException | URISyntaxException e)
{
throw new RuntimeException(e);
}
return files;
}

private static void recurseURL(URL url, List<URL> files, String templateDirectory) throws URISyntaxException, IOException
public static List<URL> findTemplateFiles(Pattern resourcePattern)
{
if ("file".equals(url.getProtocol()))
{
recurse(new File(url.toURI()), new File(url.toURI()), files, templateDirectory);
}
else
{
if (url.getPath().endsWith(".jar"))
{
JarInputStream stream = new JarInputStream(url.openStream());
processJar(stream, files, templateDirectory);
stream.close();
}
}
}

private static void recurse(File rootDirectory, File file, List<URL> files, String templateDirectory) throws IOException
{
if (file.isDirectory())
{
File[] children = file.listFiles();
if (children != null)
{
for (File child : children)
{
recurse(rootDirectory, child, files, templateDirectory);
}
}
}
else
{
if (file.getName().endsWith(".jar"))
{
try (JarInputStream stream = new JarInputStream(new FileInputStream(file)))
{
processJar(stream, files, templateDirectory);
}
}
else
{
String rootPath = rootDirectory.getAbsolutePath();
String filePath = file.getAbsolutePath();
if (filePath.contains(templateDirectory) && !rootPath.equals(filePath) && isTemplateFile(filePath))
{
files.add(new URL("file:" + filePath));
}
}
}
}

private static void processJar(
JarInputStream stream,
List<URL> files,
String templateDirectory) throws IOException
{
JarEntry entry;
while ((entry = stream.getNextJarEntry()) != null)
{
String entryName = entry.getName();
if (isTemplateFile(entryName) && entryName.startsWith(templateDirectory))
{
files.add(FileUtils.class.getClassLoader().getResource(entryName));
}
}
return new ClassGraph().scan()
.getResourcesMatchingPattern(resourcePattern)
.getURLs();
}

public static void createDirectory(File path)
Expand All @@ -173,11 +94,6 @@ public static void createDirectory(File path)
}
}

private static boolean isTemplateFile(String filePath)
{
return filePath.endsWith(".stg");
}

public static String readFile(Path path)
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import "primitiveEquals.stg"
import "primitiveHashCode.stg"
import "primitiveLiteral.stg"

skipBoolean() ::= "true"

targetPath() ::= "org/eclipse/collections/impl/bag/mutable/primitive"

fileName(primitive) ::= "<primitive.name>HashBag"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import "primitiveEquals.stg"
import "primitiveEqualsWithDelta.stg"
import "specialPredicates.stg"

skipBoolean() ::= "true"

targetPath() ::= "org/eclipse/collections/impl/block/factory/primitive"

fileName(primitive) ::= "<primitive.name>Predicates"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import "primitiveEquals.stg"
import "primitiveHashCode.stg"
import "primitiveLiteral.stg"

skipBoolean() ::= "true"

targetPath() ::= "org/eclipse/collections/impl/list/immutable/primitive"

fileName(primitive) ::= "Immutable<primitive.name>ArrayList"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import "primitiveEquals.stg"
import "primitiveHashCode.stg"
import "primitiveLiteral.stg"

skipBoolean() ::= "true"

targetPath() ::= "org/eclipse/collections/impl/list/mutable/primitive"

fileName(primitive) ::= "<primitive.name>ArrayList"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import "primitiveEquals.stg"
import "primitiveHashCode.stg"
import "primitiveLiteral.stg"

skipBoolean() ::= "true"

targetPath() ::= "org/eclipse/collections/impl/set/mutable/primitive"

fileName(primitive) ::= "<primitive.name>HashSet"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import "copyrightAndOthers.stg"
import "primitiveEquals.stg"
import "primitiveLiteral.stg"

skipBoolean() ::= "true"

isTest() ::= "true"

targetPath() ::= "org/eclipse/collections/impl/bag/immutable/primitive"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import "primitiveEquals.stg"
import "primitiveHashCode.stg"
import "primitiveLiteral.stg"

skipBoolean() ::= "true"

isTest() ::= "true"

targetPath() ::= "org/eclipse/collections/impl/bag/immutable/primitive"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import "copyright.stg"
import "primitiveEquals.stg"
import "primitiveLiteral.stg"

skipBoolean() ::= "true"

isTest() ::= "true"

targetPath() ::= "org/eclipse/collections/impl/bag/immutable/primitive"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import "primitiveEquals.stg"
import "primitiveHashCode.stg"
import "primitiveLiteral.stg"

skipBoolean() ::= "true"

isTest() ::= "true"

targetPath() ::= "org/eclipse/collections/impl/bag/mutable/primitive"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import "primitiveEquals.stg"
import "primitiveHashCode.stg"
import "primitiveLiteral.stg"

skipBoolean() ::= "true"

isTest() ::= "true"

targetPath() ::= "org/eclipse/collections/impl/bag/mutable/primitive"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import "copyright.stg"
import "primitiveHashCode.stg"
import "primitiveLiteral.stg"

skipBoolean() ::= "true"

isTest() ::= "true"

targetPath() ::= "org/eclipse/collections/impl/bag/mutable/primitive"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import "copyrightAndOthers.stg"
import "primitiveHashCode.stg"
import "primitiveLiteral.stg"

skipBoolean() ::= "true"

isTest() ::= "true"

targetPath() ::= "org/eclipse/collections/impl/bag/mutable/primitive"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import "copyright.stg"
import "primitiveLiteral.stg"

skipBoolean() ::= "true"

isTest() ::= "true"

targetPath() ::= "org/eclipse/collections/impl/block/factory/primitive"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import "primitiveEquals.stg"
import "primitiveHashCode.stg"
import "primitiveLiteral.stg"

skipBoolean() ::= "true"

isTest() ::= "true"

targetPath() ::= "org/eclipse/collections/impl/collection/immutable/primitive"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import "primitiveEquals.stg"
import "primitiveHashCode.stg"
import "primitiveLiteral.stg"

skipBoolean() ::= "true"

isTest() ::= "true"

targetPath() ::= "org/eclipse/collections/impl/collection/mutable/primitive"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import "primitiveEquals.stg"
import "primitiveHashCode.stg"
import "primitiveLiteral.stg"

skipBoolean() ::= "true"

isTest() ::= "true"

targetPath() ::= "org/eclipse/collections/impl/collection/mutable/primitive"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import "copyright.stg"
import "primitiveHashCode.stg"
import "primitiveLiteral.stg"

skipBoolean() ::= "true"

isTest() ::= "true"

targetPath() ::= "org/eclipse/collections/impl/factory/primitive"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import "copyrightAndOthers.stg"
import "primitiveHashCode.stg"
import "primitiveLiteral.stg"

skipBoolean() ::= "true"

isTest() ::= "true"

targetPath() ::= "org/eclipse/collections/impl/factory/primitive"
Expand Down
Loading