Skip to content

Commit

Permalink
Added support for many main template to AcceleoUtil.
Browse files Browse the repository at this point in the history
  • Loading branch information
ylussaud committed Feb 28, 2024
1 parent e4a9d33 commit 443b60e
Show file tree
Hide file tree
Showing 16 changed files with 306 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -103,13 +103,12 @@ public static String getTemplateImplicitVariableName() {
* @return the {@link Template#isMain() main} {@link Template} of the given {@link Module} if any,
* <code>null</code> otherwise
*/
public static Template getMainTemplate(Module module) {
Template res = null;
public static List<Template> getMainTemplate(Module module) {
List<Template> res = new ArrayList<>();

for (ModuleElement moduleElement : module.getModuleElements()) {
if (moduleElement instanceof Template && ((Template)moduleElement).isMain()) {
res = (Template)moduleElement;
break;
res.add((Template)moduleElement);
}
}

Expand Down Expand Up @@ -171,51 +170,56 @@ private static void generate(AcceleoEvaluator evaluator, IQualifiedNameQueryEnvi
URI destination, URI logURI) {

final EObjectServices services = new EObjectServices(queryEnvironment, null, null);
final Template main = getMainTemplate(module);
// TODO more than one parameter is allowed ?
// TODO not EClass type ?
// TODO more than one EClass type ?
final String parameterName = main.getParameters().get(0).getName();
// TODO use IType ?
// TODO this is really quick and dirty
final EClassifierTypeLiteral eClassifierTypeLiteral = (EClassifierTypeLiteral)main.getParameters()
.get(0).getType().getAst();
final Collection<EClassifier> eClassifiers = queryEnvironment.getEPackageProvider().getTypes(
eClassifierTypeLiteral.getEPackageName(), eClassifierTypeLiteral.getEClassifierName());
if (!eClassifiers.isEmpty()) {
final EClass parameterType = (EClass)eClassifiers.iterator().next();
final List<EObject> values = new ArrayList<EObject>();
for (Resource model : resources) {
for (EObject root : model.getContents()) {
if (parameterType.isInstance(root)) {
values.add(root);
final Map<EClassifier, List<EObject>> valuesCache = new HashMap<>();
for (Template main : getMainTemplate(module)) {
// TODO more than one parameter is allowed ?
// TODO not EClass type ?
// TODO more than one EClass type ?
final String parameterName = main.getParameters().get(0).getName();
// TODO use IType ?
// TODO this is really quick and dirty
final EClassifierTypeLiteral eClassifierTypeLiteral = (EClassifierTypeLiteral)main.getParameters()
.get(0).getType().getAst();
final Set<EClassifier> eClassifiers = queryEnvironment.getEPackageProvider().getTypes(
eClassifierTypeLiteral.getEPackageName(), eClassifierTypeLiteral.getEClassifierName());
if (!eClassifiers.isEmpty()) {
final EClass parameterType = (EClass)eClassifiers.iterator().next();
final List<EObject> values = valuesCache.computeIfAbsent(parameterType, type -> {
final List<EObject> res = new ArrayList<EObject>();
for (Resource model : resources) {
for (EObject root : model.getContents()) {
if (parameterType.isInstance(root)) {
res.add(root);
}
res.addAll(services.eAllContents(root, parameterType));
}
}
values.addAll(services.eAllContents(root, parameterType));
return res;
});

generationStrategy.start(destination);
final Map<String, Object> variables = new HashMap<String, Object>();
for (EObject value : values) {
variables.put(parameterName, value);
evaluator.generate(module, variables, generationStrategy, destination);
}
}

generationStrategy.start(destination);
final Map<String, Object> variables = new HashMap<String, Object>();
for (EObject value : values) {
variables.put(parameterName, value);
evaluator.generate(module, variables, generationStrategy, destination);
}

if (logURI != null && evaluator.getGenerationResult().getDiagnostic()
.getSeverity() != Diagnostic.OK) {
// TODO provide Charset
try {
final IAcceleoWriter logWriter = generationStrategy.createWriterForLog(logURI,
StandardCharsets.UTF_8, parameterName);
printDiagnostic(logWriter, evaluator.getGenerationResult().getDiagnostic(), "", evaluator
.getNewLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
if (logURI != null && evaluator.getGenerationResult().getDiagnostic()
.getSeverity() != Diagnostic.OK) {
// TODO provide Charset
try {
final IAcceleoWriter logWriter = generationStrategy.createWriterForLog(logURI,
StandardCharsets.UTF_8, parameterName);
printDiagnostic(logWriter, evaluator.getGenerationResult().getDiagnostic(), "",
evaluator.getNewLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

generationStrategy.terminate();
generationStrategy.terminate();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2015, 2023 Obeo.
* Copyright (c) 2015, 2024 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -33,9 +33,9 @@ public interface IEPackageProvider {
* @param name
* the name of the requested package.
* @return the {@link Collection} of {@link EPackage} registered with the specified name
* @since 4.1
* @since 8.0.2
*/
Collection<EPackage> getEPackage(String name);
Set<EPackage> getEPackage(String name);

/**
* the classifier with the specified name in the package registered with the specified name.
Expand All @@ -45,19 +45,19 @@ public interface IEPackageProvider {
* @param classifierName
* the name of the searched classifier
* @return the list of classifiers matching the given {@link EClassifier} and {@link EPackage} names.
* @since 4.1
* @since 8.0.2
*/
Collection<EClassifier> getTypes(String name, String classifierName);
Set<EClassifier> getTypes(String name, String classifierName);

/**
* the list of classifiers known to the package provider with the specified name.
*
* @param name
* the name of the searched classifier
* @return the list of classifiers matching the given {@link EClassifier} names.
* @since 4.1
* @since 8.0.2
*/
Collection<EClassifier> getTypes(String name);
Set<EClassifier> getTypes(String name);

/**
* the classifier with the specified name in the package registered with the specified name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public class EPackageProvider implements IEPackageProvider {
private final Map<EClass, Set<EClass>> subTypes = new HashMap<EClass, Set<EClass>>();

@Override
public Collection<EPackage> getEPackage(String name) {
public Set<EPackage> getEPackage(String name) {
final Set<EPackage> res = new LinkedHashSet<EPackage>();

final Set<EPackage> set = ePackages.get(name);
Expand Down Expand Up @@ -344,7 +344,7 @@ private void registerEClassifierClass(EClassifier eCls) {
}

@Override
public Collection<EClassifier> getTypes(String name, String classifierName) {
public Set<EClassifier> getTypes(String name, String classifierName) {
final Set<EClassifier> classifiers = new LinkedHashSet<EClassifier>();

final Set<EPackage> set = ePackages.get(name);
Expand All @@ -371,8 +371,8 @@ public EClassifier getType(String name, String classifierName) {
}

@Override
public Collection<EEnumLiteral> getEnumLiterals(String name, String enumName, String literalName) {
Collection<EEnumLiteral> result = new LinkedHashSet<EEnumLiteral>();
public Set<EEnumLiteral> getEnumLiterals(String name, String enumName, String literalName) {
Set<EEnumLiteral> result = new LinkedHashSet<EEnumLiteral>();

final Set<EPackage> set = ePackages.get(name);
if (set != null) {
Expand Down Expand Up @@ -415,7 +415,7 @@ public EClassifier getType(String classifierName) {
}

@Override
public Collection<EClassifier> getTypes(String classifierName) {
public Set<EClassifier> getTypes(String classifierName) {
Set<EClassifier> result = new LinkedHashSet<EClassifier>();
for (Set<EPackage> ePkgs : ePackages.values()) {
for (EPackage ePackage : ePkgs) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
some static text.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
some static text.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

header position 0..59
module myModule
metamodel http://www.eclipse.org/emf/2002/Ecore (18..56)
[comment @main (72..78) /] (63..80)

public template myTemplate(myParam : EPackage (110..135))
@main
[file url .add(.aqlFeatureAccess(myParam, 'name'), '.txt') (148..169) mode overwrite
some static text. (newLineNeeded) (188..207) (182..209)
[/file] (141..216)
(newLineNeeded) (216..218) (137..218)
[/template] (82..229)
[comment @main (242..248) /] (233..250)

public template myTemplate2(myParam : EPackage (281..306))
@main
[file url .add(.aqlFeatureAccess(myParam, 'name'), '2.txt') (319..341) mode overwrite
some static text. (newLineNeeded) (360..379) (354..381)
[/file] (312..388)
(newLineNeeded) (388..390) (308..390)
[/template] (252..401) (0..401)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(null 0 0) null[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

header position 0..59
module myModule
metamodel http://www.eclipse.org/emf/2002/Ecore (18..56)
[comment @main (70..76) /] (61..78)

public template myTemplate(myParam : EPackage (107..132))
@main
[file url .add(.aqlFeatureAccess(myParam, 'name'), '.txt') (144..165) mode overwrite
some static text. (newLineNeeded) (183..201) (178..203)
[/file] (137..210)
(newLineNeeded) (210..211) (134..211)
[/template] (79..222)
[comment @main (233..239) /] (224..241)

public template myTemplate2(myParam : EPackage (271..296))
@main
[file url .add(.aqlFeatureAccess(myParam, 'name'), '2.txt') (308..330) mode overwrite
some static text. (newLineNeeded) (348..366) (343..368)
[/file] (301..375)
(newLineNeeded) (375..376) (298..376)
[/template] (242..387) (0..387)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(null 0 0) null[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[module myModule('http://www.eclipse.org/emf/2002/Ecore')/]

[comment @main /]
[template public myTemplate(myParam : ecore::EPackage)]
[file (myParam.name + '.txt', overwrite)]
some static text.
[/file]
[/template]

[comment @main /]
[template public myTemplate2(myParam : ecore::EPackage)]
[file (myParam.name + '2.txt', overwrite)]
some static text.
[/file]
[/template]

0 comments on commit 443b60e

Please sign in to comment.