Skip to content

Commit

Permalink
Fixed #121 Create a log file with GenerationResult.getDiagnostic().
Browse files Browse the repository at this point in the history
  • Loading branch information
ylussaud committed Feb 16, 2024
1 parent 9ff12a7 commit f85264c
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,28 @@ public class AcceleoLauncher implements IApplication {
@Option(name = "-consoleLog", usage = "Log messages in the console.")
private boolean consoleLog;

/**
* Output log URI.
*/
@Option(name = "-log", usage = "Specifies the module which main template will be executed.", metaVar = "LOG", handler = StringOptionHandler.class, required = false)
private String log;

/** List of URIs parsed from the {@link #models} argument. */
private List<URI> modelURIs;

/** Bundle containing our main module for this generation. */
private Bundle bundle;

/**
* The target {@link URI}.
*/
private URI targetURI;

/**
* The log {@link URI} if any, <code>null</code> otherwise.
*/
private URI logURI;

@Override
public Object start(IApplicationContext context) throws Exception {
String[] args = (String[])context.getArguments().get(IApplicationContext.APPLICATION_ARGS);
Expand Down Expand Up @@ -141,7 +157,9 @@ public Object doMain(String[] args) {
applicationResult = APPLICATION_ERROR;
break;
}
printDiagnostic(stream, result.getDiagnostic(), "");
if (consoleLog) {
printDiagnostic(stream, result.getDiagnostic(), "");
}
}
Set<URI> generatedFiles = result.getGeneratedFiles();
System.out.println("Generated " + generatedFiles.size() + " in " + target);
Expand Down Expand Up @@ -183,7 +201,13 @@ private void validateArguments(CmdLineParser parser) throws CmdLineException {
+ " must be available in the target platform.");
}
try {
URI.createURI(target);
targetURI = URI.createURI(target);
} catch (IllegalArgumentException e) {
throw new CmdLineException(parser, e);
}

try {
logURI = AcceleoUtil.getlogURI(targetURI, log);
} catch (IllegalArgumentException e) {
throw new CmdLineException(parser, e);
}
Expand Down Expand Up @@ -241,7 +265,8 @@ private GenerationResult launchGeneration() {
} else {
mainModule = null;
}
evaluate(evaluator, queryEnvironment, mainModule, resourceSetForModels);

evaluate(evaluator, queryEnvironment, mainModule, resourceSetForModels, targetURI, logURI);
return evaluator.getGenerationResult();
} finally {
AQLUtils.cleanResourceSetForModels(resolver, resourceSetForModels);
Expand All @@ -250,11 +275,11 @@ private GenerationResult launchGeneration() {
}

private void evaluate(AcceleoEvaluator evaluator, IQualifiedNameQueryEnvironment queryEnvironment,
Module mainModule, ResourceSet modelResourceSet) {
Module mainModule, ResourceSet modelResourceSet, URI targetURI, URI logURI) {
final IAcceleoGenerationStrategy strategy = new DefaultGenerationStrategy(modelResourceSet
.getURIConverter());
AcceleoUtil.generate(evaluator, queryEnvironment, mainModule, modelResourceSet, strategy, URI
.createURI(target));
AcceleoUtil.generate(evaluator, queryEnvironment, mainModule, modelResourceSet, strategy, targetURI,
logURI);
}

private void printDiagnostic(PrintStream stream, Diagnostic diagnostic, String indentation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ public void initializeFrom(ILaunchConfiguration configuration) {
AcceleoDebugger.OPTION_MAP_TYPE);
} else {
options = new LinkedHashMap<>();
if (!options.containsKey(AcceleoUtil.NEW_LINE_OPTION)) {
options.put(AcceleoUtil.NEW_LINE_OPTION, System.lineSeparator());
}
options.put(AcceleoUtil.NEW_LINE_OPTION, System.lineSeparator());
// TODO add GUI
options.put(AcceleoUtil.LOG_URI_OPTION, "acceleo.log");
}
optionsString = GSON.toJson(options);
initialOptionsString = optionsString;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void run() {
final IAcceleoGenerationStrategy strategy = new DefaultGenerationStrategy(model
.getResourceSet().getURIConverter());
AcceleoUtil.generate(evaluator, queryEnvironment, module, model, strategy,
getDestination());
getDestination(), logURI);
}
} finally {
// FIXME workaround: UI jobs are coming from core.debug even if the gen has finished,
Expand Down Expand Up @@ -251,6 +251,11 @@ public Object doSwitch(EObject eObject) {
*/
private String newLine;

/**
* The log {@link URI} if any, <code>null</code> otherwise.
*/
private URI logURI;

/**
* The {@link AcceleoDebugEvaluator}.
*/
Expand Down Expand Up @@ -297,6 +302,7 @@ public void initialize(boolean noDebug, Map<String, Object> arguments) {
AcceleoParser.QUALIFIER_SEPARATOR, false);

newLine = options.getOrDefault(AcceleoUtil.NEW_LINE_OPTION, System.lineSeparator());
logURI = AcceleoUtil.getlogURI(destination, options.get(AcceleoUtil.LOG_URI_OPTION));
final ArrayList<Exception> exceptions = new ArrayList<>();
resourceSetForModels = AQLUtils.createResourceSetForModels(exceptions, this, new ResourceSetImpl(),
options);
Expand Down Expand Up @@ -371,8 +377,8 @@ protected void generateNoDebug(IQualifiedNameQueryEnvironment environment, Modul

final IAcceleoGenerationStrategy strategy = new DefaultGenerationStrategy(modelResource
.getResourceSet().getURIConverter());
AcceleoUtil.generate(noDebugEvaluator, environment, module, modelResource, strategy,
getDestination());
AcceleoUtil.generate(noDebugEvaluator, environment, module, modelResource, strategy, getDestination(),
logURI);

if (profiler != null) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -30,6 +31,7 @@
import org.eclipse.acceleo.Template;
import org.eclipse.acceleo.aql.evaluation.AcceleoEvaluator;
import org.eclipse.acceleo.aql.evaluation.writer.IAcceleoGenerationStrategy;
import org.eclipse.acceleo.aql.evaluation.writer.IAcceleoWriter;
import org.eclipse.acceleo.query.AQLUtils;
import org.eclipse.acceleo.query.ast.ASTNode;
import org.eclipse.acceleo.query.ast.EClassifierTypeLiteral;
Expand All @@ -41,6 +43,7 @@
import org.eclipse.acceleo.query.services.EObjectServices;
import org.eclipse.acceleo.query.services.configurator.IServicesConfigurator;
import org.eclipse.acceleo.util.AcceleoSwitch;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
Expand All @@ -65,6 +68,11 @@ public final class AcceleoUtil {
*/
public static final String NEW_LINE_OPTION = "newLine";

/**
* The log URI {@link URI} {@link String} option name.
*/
public static final String LOG_URI_OPTION = "logURI";

/**
* "self".
*/
Expand Down Expand Up @@ -123,11 +131,14 @@ public static Template getMainTemplate(Module module) {
* the {@link IAcceleoGenerationStrategy}
* @param destination
* destination {@link URI}
* @param logURI
* the {@link URI} for loggin if nay, <code>null</code> otherwise
*/
public static void generate(AcceleoEvaluator evaluator, IQualifiedNameQueryEnvironment queryEnvironment,
Module module, Resource model, IAcceleoGenerationStrategy generationStrategy, URI destination) {
Module module, Resource model, IAcceleoGenerationStrategy generationStrategy, URI destination,
URI logURI) {
generate(evaluator, queryEnvironment, module, Collections.singletonList(model), generationStrategy,
destination);
destination, logURI);
}

/**
Expand All @@ -145,17 +156,19 @@ public static void generate(AcceleoEvaluator evaluator, IQualifiedNameQueryEnvir
* the {@link IAcceleoGenerationStrategy}
* @param destination
* the destination {@link URI}
* @param logURI
* the {@link URI} for loggin if nay, <code>null</code> otherwise
*/
public static void generate(AcceleoEvaluator evaluator, IQualifiedNameQueryEnvironment queryEnvironment,
Module module, ResourceSet resourceSet, IAcceleoGenerationStrategy generationStrategy,
URI destination) {
URI destination, URI logURI) {
generate(evaluator, queryEnvironment, module, resourceSet.getResources(), generationStrategy,
destination);
destination, logURI);
}

private static void generate(AcceleoEvaluator evaluator, IQualifiedNameQueryEnvironment queryEnvironment,
Module module, List<Resource> resources, IAcceleoGenerationStrategy generationStrategy,
URI destination) {
URI destination, URI logURI) {

final EObjectServices services = new EObjectServices(queryEnvironment, null, null);
final Template main = getMainTemplate(module);
Expand Down Expand Up @@ -187,10 +200,38 @@ private static void generate(AcceleoEvaluator evaluator, IQualifiedNameQueryEnvi
variables.put(parameterName, value);
evaluator.generate(module, variables, generationStrategy, destination);
}

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

generationStrategy.terminate();
}
}

private static void printDiagnostic(IAcceleoWriter writer, Diagnostic diagnostic, String indentation,
String newLine) {
String nextIndentation = indentation;
if (diagnostic.getMessage() != null) {
writer.append(indentation);
writer.append(diagnostic.getMessage() + newLine);
nextIndentation += "\t";
}
for (Diagnostic child : diagnostic.getChildren()) {
printDiagnostic(writer, child, nextIndentation, newLine);
}
}

/**
* Provides the concrete Acceleo {@link EClass EClasses} that are, inherit or extend the given Acceleo
* {@link EClass}. This is useful to use an {@link AcceleoSwitch} on non-instantiable EClasses.
Expand Down Expand Up @@ -333,4 +374,36 @@ public static void cleanServices(IReadOnlyQueryEnvironment queryEnvironment,
AQLUtils.cleanServices(LANGUAGE_NAME, queryEnvironment, resourceSetForModels);
}

/**
* Gets the log {@link URI} for the given target {@link URI} and log {@link String}.
*
* @param targetURI
* the target {@link URI}
* @param log
* the log {@link String}
* @return the log {@link URI} for the given target {@link URI} and log {@link String}
* @throws IllegalArgumentException
* if {@link URI} convertion fails
*/
public static URI getlogURI(URI targetURI, String log) throws IllegalArgumentException {
final URI res;

if (log != null) {
final URI uri = URI.createURI(log);
if (uri.isRelative()) {
if (targetURI.isRelative()) {
res = targetURI.appendSegments(uri.segments());
} else {
res = uri.resolve(targetURI);
}
} else {
res = uri;
}
} else {
res = null;
}

return res;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -157,19 +157,6 @@ public class AcceleoEvaluator extends AcceleoSwitch<Object> {
*/
private String newLine;

/**
* Constructor.
*
* @param other
* the other {@link AcceleoEvaluator}.
*/
public AcceleoEvaluator(AcceleoEvaluator other) {
this(other.lookupEngine, other.newLine);
destination = other.destination;
generationStrategy = other.generationStrategy;
generationResult = other.generationResult;
}

/**
* Constructor.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2008, 2023 Obeo.
* Copyright (c) 2008, 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 All @@ -19,7 +19,6 @@
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -44,7 +43,7 @@
public class DefaultGenerationStrategy implements IAcceleoGenerationStrategy {

/** Used to call URIConverter methods with no options. */
private static final Map<String, Object> EMPTY_OPTION_MAP = new HashMap<>();
private static final Map<String, Object> EMPTY_OPTION_MAP = Collections.emptyMap();

/**
* Keeps track of the contents of all protected areas from the files we've created writers for.
Expand Down Expand Up @@ -115,6 +114,12 @@ public Map<String, List<String>> consumeAllProtectedAreas(URI uri) {
return res;
}

@Override
public IAcceleoWriter createWriterForLog(URI uri, Charset charset, String lineDelimiter)
throws IOException {
return createWriterFor(uri, OpenModeKind.APPEND, charset, lineDelimiter);
}

@Override
public IAcceleoWriter createWriterForLostContent(URI uri, String protectedAreaID, Charset charset,
String lineDelimiter) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2008, 2023 Obeo.
* Copyright (c) 2008, 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 @@ -32,6 +32,7 @@
* @author <a href="mailto:laurent.goubet@obeo.fr">Laurent Goubet</a>
*/
public interface IAcceleoGenerationStrategy {

/** The start of user code String. Marks the start of a protected area. */
String USER_CODE_START = AcceleoMessages.getString("usercode.start"); //$NON-NLS-1$

Expand Down Expand Up @@ -72,6 +73,21 @@ public interface IAcceleoGenerationStrategy {
*/
Map<String, List<String>> consumeAllProtectedAreas(URI uri);

/**
* Creates an {@link IAcceleoWriter} for the log content for the given destination {@link URI}.
*
* @param uri
* the log {@link URI}
* @param charset
* the {@link Charset} of the stream that's to be saved
* @param lineDelimiter
* the line delimiter that was demanded by the user for this writer.
* @throws IOException
* if the writer can't be created
* @return the created writer. It can't be <code>null</code> use {@link NullWriter} instead.
*/
IAcceleoWriter createWriterForLog(URI uri, Charset charset, String lineDelimiter) throws IOException;

/**
* Creates an {@link IAcceleoWriter} for the lost content for the given generated {@link URI} with the
* given protected area ID.
Expand All @@ -83,6 +99,7 @@ public interface IAcceleoGenerationStrategy {
* @param charset
* the {@link Charset} of the stream that's to be saved
* @param lineDelimiter
* the line delimiter that was demanded by the user for this writer.
* @throws IOException
* if the writer can't be created
* @return the created writer. It can't be <code>null</code> use {@link NullWriter} instead.
Expand Down
Loading

0 comments on commit f85264c

Please sign in to comment.