-
Notifications
You must be signed in to change notification settings - Fork 3
/
NewModuleWizard.java
401 lines (358 loc) · 14.9 KB
/
NewModuleWizard.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*******************************************************************************
* Copyright (c) 2023, 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.ide.ui.wizard;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.eclipse.acceleo.AcceleoPackage;
import org.eclipse.acceleo.Block;
import org.eclipse.acceleo.Comment;
import org.eclipse.acceleo.CommentBody;
import org.eclipse.acceleo.Documentation;
import org.eclipse.acceleo.Expression;
import org.eclipse.acceleo.FileStatement;
import org.eclipse.acceleo.Metamodel;
import org.eclipse.acceleo.Module;
import org.eclipse.acceleo.ModuleDocumentation;
import org.eclipse.acceleo.ModuleElement;
import org.eclipse.acceleo.ModuleElementDocumentation;
import org.eclipse.acceleo.OpenModeKind;
import org.eclipse.acceleo.Query;
import org.eclipse.acceleo.Template;
import org.eclipse.acceleo.TextStatement;
import org.eclipse.acceleo.Variable;
import org.eclipse.acceleo.VisibilityKind;
import org.eclipse.acceleo.aql.AcceleoUtil;
import org.eclipse.acceleo.aql.ide.ui.Activator;
import org.eclipse.acceleo.aql.parser.AcceleoAstSerializer;
import org.eclipse.acceleo.aql.parser.AcceleoParser;
import org.eclipse.acceleo.query.parser.AstResult;
import org.eclipse.acceleo.query.runtime.impl.QueryBuilderEngine;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.ide.IDE;
/**
* The new {@link Module} wizard.
*
* @author <a href="mailto:yvan.lussaud@obeo.fr">Yvan Lussaud</a>
*/
public class NewModuleWizard extends Wizard implements INewWizard {
/**
* The ok status message.
*/
private static final String OK_MESSAGE = "Acceleo 4 module %s created.";
/**
* The new line {@link String}.
*/
private static final String NEW_LINE = System.lineSeparator();
/**
* The {@link Job} writing the {@link Module} file.
*
* @author <a href="mailto:yvan.lussaud@obeo.fr">Yvan Lussaud</a>
*/
private class FinishJob extends Job {
/**
* The {@link ModuleConfiguration}.
*/
private ModuleConfiguration moduleConfiguration;
public FinishJob(ModuleConfiguration moduleConfiguration) {
super("Creating Acceleo 4 module: " + moduleConfiguration.getModuleFile().getFullPath());
this.moduleConfiguration = moduleConfiguration;
}
@Override
protected IStatus run(IProgressMonitor monitor) {
final IFile moduleFile = moduleConfiguration.getModuleFile();
IStatus res = new Status(IStatus.OK, Activator.PLUGIN_ID, String.format(OK_MESSAGE, moduleFile
.getFullPath()));
final org.eclipse.acceleo.Module module = AcceleoPackage.eINSTANCE.getAcceleoFactory()
.createModule();
module.setName(moduleConfiguration.getModuleName());
final ModuleDocumentation moduleDocumentation = AcceleoPackage.eINSTANCE.getAcceleoFactory()
.createModuleDocumentation();
module.setDocumentation(moduleDocumentation);
moduleDocumentation.setDocumentedElement(module);
final CommentBody moduleDocumentationBody = AcceleoPackage.eINSTANCE.getAcceleoFactory()
.createCommentBody();
moduleDocumentationBody.setValue(NEW_LINE + " * The documentation of the module " + module
.getName() + "." + NEW_LINE);
final String username = System.getProperty("user.name");
if (username != null && !"".equals(username)) {
moduleDocumentationBody.setValue(moduleDocumentationBody.getValue() + " * "
+ AcceleoParser.AUTHOR_TAG + username + NEW_LINE);
}
moduleDocumentationBody.setValue(moduleDocumentationBody.getValue() + " * "
+ AcceleoParser.VERSION_TAG + "1.0.0" + NEW_LINE);
moduleDocumentationBody.setValue(moduleDocumentationBody.getValue() + " * "
+ AcceleoParser.SINCE_TAG + "1.0.0" + NEW_LINE);
moduleDocumentationBody.setValue(moduleDocumentationBody.getValue() + " *");
moduleDocumentation.setBody(moduleDocumentationBody);
module.setDocumentation(moduleDocumentation);
for (String nsURI : moduleConfiguration.getNsURIs()) {
final Metamodel metamodel = AcceleoPackage.eINSTANCE.getAcceleoFactory().createMetamodel();
final EPackage ePkg = EPackage.Registry.INSTANCE.getEPackage(nsURI);
metamodel.setReferencedPackage(ePkg);
module.getMetamodels().add(metamodel);
}
if (moduleConfiguration.getModuleElementEClass() == AcceleoPackage.eINSTANCE.getTemplate()) {
final Template template = AcceleoPackage.eINSTANCE.getAcceleoFactory().createTemplate();
template.setName(moduleConfiguration.getModuleElementName());
template.setVisibility(VisibilityKind.PUBLIC);
template.getParameters().add(createVariable(moduleConfiguration
.getModuleElementParameterType()));
if (moduleConfiguration.isGenerateDocumentation()) {
template.setDocumentation(createModuleElementDocumentation(template));
if (moduleConfiguration.isMainTemplate()) {
final CommentBody body = template.getDocumentation().getBody();
body.setValue(body.getValue() + " " + AcceleoParser.MAIN_TAG + NEW_LINE + " *");
}
} else if (moduleConfiguration.isMainTemplate()) {
final Comment mainComment = AcceleoPackage.eINSTANCE.getAcceleoFactory().createComment();
final CommentBody mainCommentBody = AcceleoPackage.eINSTANCE.getAcceleoFactory()
.createCommentBody();
mainCommentBody.setValue(AcceleoParser.MAIN_TAG);
mainComment.setBody(mainCommentBody);
module.getModuleElements().add(mainComment);
}
template.setBody(createTemplateBody(moduleConfiguration));
module.getModuleElements().add(template);
} else if (moduleConfiguration.getModuleElementEClass() == AcceleoPackage.eINSTANCE.getQuery()) {
final Query query = AcceleoPackage.eINSTANCE.getAcceleoFactory().createQuery();
query.setName(moduleConfiguration.getModuleElementName());
query.setVisibility(VisibilityKind.PUBLIC);
query.getParameters().add(createVariable(moduleConfiguration
.getModuleElementParameterType()));
final QueryBuilderEngine queryBuilder = new QueryBuilderEngine();
final AstResult astResultReturnType = queryBuilder.build(moduleConfiguration
.getModuleElementParameterType());
query.setType(astResultReturnType);
query.setTypeAql(astResultReturnType.getAst());
query.setBody(createExpression(query.getParameters().get(0).getName()));
if (moduleConfiguration.isGenerateDocumentation()) {
query.setDocumentation(createModuleElementDocumentation(query));
}
module.getModuleElements().add(query);
}
final AcceleoAstSerializer serializer = new AcceleoAstSerializer(NEW_LINE);
// TODO at some point we should reuse the parser constants for this.
final String moduleString = "[comment encoding = UTF-8 /]" + NEW_LINE + serializer.serialize(
module);
try {
final ByteArrayInputStream moduleInputStream = new ByteArrayInputStream(moduleString.getBytes(
"UTF-8"));
moduleFile.create(moduleInputStream, true, monitor);
} catch (CoreException e) {
res = new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Can't write " + moduleFile
.getFullPath(), e);
} catch (UnsupportedEncodingException e) {
res = new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Can't write " + moduleFile
.getFullPath(), e);
}
return res;
}
/**
* Creates the {@link Block} template body from the given {@link ModuleConfiguration}.
*
* @param configuration
* the {@link ModuleConfiguration}
* @return the created the {@link Block} template body from the given {@link ModuleConfiguration}
*/
private Block createTemplateBody(ModuleConfiguration configuration) {
final Block res = AcceleoPackage.eINSTANCE.getAcceleoFactory().createBlock();
final String initialContent;
if (configuration.isIsInitialized()) {
IFile initialContentFile = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(
configuration.getInitializationPath()));
String fileContent;
try {
fileContent = AcceleoUtil.getContent(initialContentFile.getContents(), initialContentFile
.getCharset());
} catch (IOException e) {
fileContent = "";//$NON-NLS-1$
Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
"unable to load intial file content.", e));
e.printStackTrace();
} catch (CoreException e) {
fileContent = "";//$NON-NLS-1$
Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
"unable to load intial file content.", e));
}
initialContent = fileContent;
} else {
initialContent = "";//$NON-NLS-1$
}
res.setInlined(false);
final TextStatement text = AcceleoPackage.eINSTANCE.getAcceleoFactory().createTextStatement();
if (configuration.isGenerateFile()) {
text.setValue(NEW_LINE);
} else {
text.setValue(initialContent.replaceAll("\\r\\n|\\n", NEW_LINE + " ") + NEW_LINE);
}
if (configuration.isGenerateFile()) {
final FileStatement fileStatement = AcceleoPackage.eINSTANCE.getAcceleoFactory()
.createFileStatement();
fileStatement.setUrl(createExpression("'file.txt'"));
fileStatement.setMode(OpenModeKind.OVERWRITE);
fileStatement.setCharset(createExpression("'UTF-8'"));
final Block fileBlock = AcceleoPackage.eINSTANCE.getAcceleoFactory().createBlock();
fileBlock.setInlined(false);
final TextStatement fileText = AcceleoPackage.eINSTANCE.getAcceleoFactory()
.createTextStatement();
fileText.setValue(initialContent.replaceAll("\\r\\n|\\n", NEW_LINE + " ") + NEW_LINE
+ " ");
fileBlock.getStatements().add(fileText);
fileStatement.setBody(fileBlock);
res.getStatements().add(fileStatement);
}
res.getStatements().add(text);
return res;
}
/**
* Creates the {@link ModuleElement}'s {@link Documentation}.
*
* @param moduleElement
* the {@link ModuleElement}
* @return created the {@link ModuleElement}'s {@link Documentation}
*/
private Documentation createModuleElementDocumentation(
org.eclipse.acceleo.ModuleElement moduleElement) {
final ModuleElementDocumentation res = AcceleoPackage.eINSTANCE.getAcceleoFactory()
.createModuleElementDocumentation();
final CommentBody documentationBody = AcceleoPackage.eINSTANCE.getAcceleoFactory()
.createCommentBody();
res.setBody(documentationBody);
if (moduleElement instanceof Template) {
final Template template = (Template)moduleElement;
documentationBody.setValue(NEW_LINE + " * The documentation of the template " + template
.getName() + "." + NEW_LINE + " *");
documentationBody.setValue(documentationBody.getValue() + " @param " + template
.getParameters().get(0).getName() + NEW_LINE + " *");
res.setDocumentedElement(template);
} else if (moduleElement instanceof Query) {
final Query query = (Query)moduleElement;
documentationBody.setValue(NEW_LINE + " * The documentation of the query " + query.getName()
+ "." + NEW_LINE + " *");
documentationBody.setValue(documentationBody.getValue() + " @param " + query.getParameters()
.get(0).getName() + NEW_LINE + " *");
res.setDocumentedElement(query);
}
return res;
}
/**
* Creates an {@link Expression} for the given expression {@link String}.
*
* @param expression
* the expression {@link String}
* @return the created {@link Expression} for the given expression {@link String}
*/
private Expression createExpression(String expression) {
final Expression res = AcceleoPackage.eINSTANCE.getAcceleoFactory().createExpression();
final QueryBuilderEngine queryBuilder = new QueryBuilderEngine();
final AstResult astResult = queryBuilder.build(expression);
res.setAst(astResult);
res.setAql(astResult.getAst());
return res;
}
/**
* Creates a {@link Variable} with the given parameter type.
*
* @param moduleElementParameterType
* the parameter type
* @return the created {@link Variable} with the given parameter type
*/
private Variable createVariable(String moduleElementParameterType) {
final Variable res = AcceleoPackage.eINSTANCE.getAcceleoFactory().createVariable();
final QueryBuilderEngine queryBuilder = new QueryBuilderEngine();
final AstResult astResult = queryBuilder.build(moduleElementParameterType);
res.setName("parameter");
res.setType(astResult);
res.setTypeAql(astResult.getAst());
return res;
}
}
/**
* The destination {@link IContainer}.
*/
private String container;
/**
* The {@link ModulePage}.
*/
private ModulePage modulePage;
/**
* The active {@link IWorkbenchPage}.
*/
private IWorkbenchPage activePage;
@Override
public void init(IWorkbench workbench, IStructuredSelection selection) {
IContainer firstContainer = null;
if (selection != null && !selection.isEmpty()) {
IStructuredSelection aSelection = selection;
if (aSelection.size() > 0) {
Object element = aSelection.getFirstElement();
if (element instanceof IAdaptable) {
element = ((IAdaptable)element).getAdapter(IResource.class);
}
if (element instanceof IContainer) {
firstContainer = (IContainer)element;
} else if (element instanceof IResource) {
firstContainer = ((IResource)element).getParent();
}
}
}
if (firstContainer != null) {
container = firstContainer.getFullPath().toString();
} else {
container = "";
}
activePage = workbench.getActiveWorkbenchWindow().getActivePage();
}
@Override
public void addPages() {
modulePage = new ModulePage(container);
addPage(modulePage);
}
@Override
public boolean performFinish() {
final FinishJob job = new FinishJob(modulePage.getModuleConfiguration());
job.setRule(ResourcesPlugin.getWorkspace().getRoot());
job.schedule();
try {
job.join();
// Open the module created
final IFile moduleFile = modulePage.getModuleConfiguration().getModuleFile();
if (moduleFile != null && moduleFile.isAccessible()) {
try {
IDE.openEditor(activePage, moduleFile);
} catch (PartInitException e) {
// nothing to do here
}
}
} catch (InterruptedException e) {
// nothing to do here
}
return true;
}
}