-
Notifications
You must be signed in to change notification settings - Fork 3
/
AcceleoLauncher.java
277 lines (246 loc) · 10.2 KB
/
AcceleoLauncher.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*******************************************************************************
* Copyright (c) 2020, 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
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.acceleo.aql.launcher;
import java.io.File;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.eclipse.acceleo.Module;
import org.eclipse.acceleo.aql.AcceleoUtil;
import org.eclipse.acceleo.aql.evaluation.AcceleoEvaluator;
import org.eclipse.acceleo.aql.evaluation.GenerationResult;
import org.eclipse.acceleo.aql.evaluation.writer.DefaultGenerationStrategy;
import org.eclipse.acceleo.aql.evaluation.writer.IAcceleoGenerationStrategy;
import org.eclipse.acceleo.aql.parser.AcceleoParser;
import org.eclipse.acceleo.aql.parser.ModuleLoader;
import org.eclipse.acceleo.query.AQLUtils;
import org.eclipse.acceleo.query.ide.QueryPlugin;
import org.eclipse.acceleo.query.ide.runtime.impl.namespace.OSGiQualifiedNameResolver;
import org.eclipse.acceleo.query.runtime.namespace.IQualifiedNameQueryEnvironment;
import org.eclipse.acceleo.query.runtime.namespace.IQualifiedNameResolver;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.Option;
import org.kohsuke.args4j.spi.StringArrayOptionHandler;
import org.kohsuke.args4j.spi.StringOptionHandler;
import org.osgi.framework.Bundle;
/**
* Application class for the Acceleo Launcher.
* <p>
* Parses the arguments and starts the specified Acceleo generation.
* </p>
*
* @author lgoubet
*/
public class AcceleoLauncher implements IApplication {
/** Return code for the application in case of error. */
private static final Integer APPLICATION_ERROR = Integer.valueOf(-1);
/**
* Input models for the generation. Cannot be empty.
*/
@Option(name = "-models", usage = "Specifies the input models for this generation.", metaVar = "INPUT", handler = StringArrayOptionHandler.class, required = true)
private String[] models = new String[0];
/**
* Identifier of the bundle containing the main module for this generation.
*/
@Option(name = "-bundle", usage = "The identifier of the bundle containing the main module of the generation to start.", metaVar = "BUNDLE", handler = StringOptionHandler.class, required = true)
private String bundleIdentifier;
/**
* Input module for the generation. This needs to the qualified name of the module containing an
* "@main"-annotated template.
*/
@Option(name = "-module", usage = "Specifies the module which main template will be executed.", metaVar = "MODULE", handler = StringOptionHandler.class, required = true)
private String moduleQualifiedName;
/**
* The destination URI for this generation. Generated files will use this folder as their root to resolve
* against.
*/
@Option(name = "-target", usage = "Specifies the destination for the generation.", metaVar = "TARGET", handler = StringOptionHandler.class, required = true)
private String target;
/**
* Workspace location. This argument is here only to mimic the OSGi applications common arguments so that
* they are displayed in usage.
*/
@Option(name = "-data", usage = "Specify the folder which will keep the workspace.", metaVar = "FOLDER")
private File dataFolder;
/**
* consoleLog. This argument is here only to mimic the OSGi applications common arguments so that they are
* displayed in usage.
*/
@Option(name = "-consoleLog", usage = "Log messages in the console.")
private boolean consoleLog;
/** List of URIs parsed from the {@link #models} argument. */
private List<URI> modelURIs;
/** Bundle containing our main module for this generation. */
private Bundle bundle;
@Override
public Object start(IApplicationContext context) throws Exception {
String[] args = (String[])context.getArguments().get(IApplicationContext.APPLICATION_ARGS);
return new AcceleoLauncher().doMain(args);
}
/**
* Main logic for this launcher.
*
* @param args
* Application parameters passed by the Equinox framework.
* @return The return value of this application.
*/
public Object doMain(String[] args) {
CmdLineParser parser = new CmdLineParser(this);
Object applicationResult = IApplication.EXIT_OK;
try {
parser.parseArgument(args);
validateArguments(parser);
GenerationResult result = launchGeneration();
if (result.getDiagnostic().getSeverity() > Diagnostic.INFO) {
PrintStream stream;
switch (result.getDiagnostic().getSeverity()) {
case Diagnostic.WARNING:
stream = System.out;
stream.println("WARNING");
break;
case Diagnostic.ERROR:
// Fall-through
default:
// Shouldn't happen as we only show warnings and errors
stream = System.err;
stream.println("ERROR");
applicationResult = APPLICATION_ERROR;
break;
}
printDiagnostic(stream, result.getDiagnostic(), "");
}
Set<URI> generatedFiles = result.getGeneratedFiles();
System.out.println("Generated " + generatedFiles.size() + " in " + target);
if (result.getGeneratedFiles().size() <= 10) {
for (URI generated : result.getGeneratedFiles()) {
System.out.println(generated);
}
}
} catch (CmdLineException e) {
parser.printUsage(System.err);
System.err.println();
AcceleoLauncherPlugin.INSTANCE.log(e);
applicationResult = APPLICATION_ERROR;
}
return applicationResult;
}
private void validateArguments(CmdLineParser parser) throws CmdLineException {
if (models.length == 0 || models[0].length() == 0) {
throw new CmdLineException(parser, "The input model path cannot be empty.");
}
if (bundleIdentifier.length() == 0) {
throw new CmdLineException(parser,
"The main module's containing bundle identifier cannot be empty.");
}
if (moduleQualifiedName.length() == 0) {
throw new CmdLineException(parser, "The main module's qualified name cannot be empty.");
}
if (target.length() == 0) {
throw new CmdLineException(parser, "The target folder path cannot be empty.");
}
modelURIs = convertToURIs(parser, models);
bundle = Platform.getBundle(bundleIdentifier);
if (bundle == null || bundle.getState() == Bundle.UNINSTALLED) {
throw new CmdLineException(parser, "The Bundle " + bundleIdentifier
+ " must be available in the target platform.");
}
try {
URI.createURI(target);
} catch (IllegalArgumentException e) {
throw new CmdLineException(parser, e);
}
}
private List<URI> convertToURIs(CmdLineParser parser, String... paths) throws CmdLineException {
List<URI> uris = new ArrayList<>();
for (String path : paths) {
URI pathURI = null;
try {
pathURI = URI.createURI(path, true);
} catch (IllegalArgumentException e) {
throw new CmdLineException(parser, e);
}
if (pathURI != null && !pathURI.hasAbsolutePath()) {
pathURI = URI.createFileURI(path);
}
if (pathURI != null) {
uris.add(pathURI);
}
}
return uris;
}
private GenerationResult launchGeneration() {
IQualifiedNameResolver resolver = new OSGiQualifiedNameResolver(bundle,
AcceleoParser.QUALIFIER_SEPARATOR);
// TODO get options form the launch configuration
final Map<String, String> options = new LinkedHashMap<>();
final ArrayList<Exception> exceptions = new ArrayList<>();
final ResourceSet resourceSetForModels = AQLUtils.createResourceSetForModels(exceptions, resolver,
new ResourceSetImpl(), options);
// TODO report exceptions
for (URI modelURI : modelURIs) {
resourceSetForModels.getResource(modelURI, true);
}
final IQualifiedNameQueryEnvironment queryEnvironment = AcceleoUtil.newAcceleoQueryEnvironment(
options, resolver, resourceSetForModels, false);
try {
final String newLine = options.getOrDefault(AcceleoUtil.NEW_LINE_OPTION, System.lineSeparator());
AcceleoEvaluator evaluator = new AcceleoEvaluator(queryEnvironment.getLookupEngine(), newLine);
resolver.addLoader(new ModuleLoader(new AcceleoParser(), evaluator));
resolver.addLoader(QueryPlugin.getPlugin().createJavaLoader(AcceleoParser.QUALIFIER_SEPARATOR,
false));
final Object resolved = resolver.resolve(moduleQualifiedName);
final Module mainModule;
if (resolved instanceof Module) {
mainModule = (Module)resolved;
} else {
mainModule = null;
}
evaluate(evaluator, queryEnvironment, mainModule, resourceSetForModels);
return evaluator.getGenerationResult();
} finally {
AQLUtils.cleanResourceSetForModels(resolver, resourceSetForModels);
AcceleoUtil.cleanServices(queryEnvironment, resourceSetForModels);
}
}
private void evaluate(AcceleoEvaluator evaluator, IQualifiedNameQueryEnvironment queryEnvironment,
Module mainModule, ResourceSet modelResourceSet) {
final IAcceleoGenerationStrategy strategy = new DefaultGenerationStrategy(modelResourceSet
.getURIConverter());
AcceleoUtil.generate(evaluator, queryEnvironment, mainModule, modelResourceSet, strategy, URI
.createURI(target));
}
private void printDiagnostic(PrintStream stream, Diagnostic diagnostic, String indentation) {
String nextIndentation = indentation;
if (diagnostic.getMessage() != null) {
stream.print(indentation);
stream.println(diagnostic.getMessage());
nextIndentation += "\t";
}
for (Diagnostic child : diagnostic.getChildren()) {
printDiagnostic(stream, child, nextIndentation);
}
}
@Override
public void stop() {
// Nothing to do on application stop
}
}