Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5796a4a13e8d95f971c390884a69e7ca8cca715c (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2011 protos software gmbh (http://www.protos.de).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 * 
 * CONTRIBUTORS:
 * 		Henrik Rentz-Reichert (initial contribution)
 * 
 *******************************************************************************/

package org.eclipse.etrice.generator.launch;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.core.variables.IStringVariableManager;
import org.eclipse.core.variables.VariablesPlugin;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.ui.RefreshTab;
import org.eclipse.etrice.generator.base.AbstractGeneratorOptions;
import org.eclipse.etrice.generator.base.io.ILineOutput;
import org.eclipse.etrice.generator.base.setup.GeneratorApplicationOptions;
import org.eclipse.etrice.generator.ui.preferences.PreferenceConstants;
import org.eclipse.jdt.launching.AbstractJavaLaunchConfigurationDelegate;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleConstants;
import org.eclipse.ui.console.IConsoleManager;
import org.eclipse.ui.console.IConsoleView;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;
import org.eclipse.ui.preferences.ScopedPreferenceStore;

import com.google.common.collect.Lists;

/**
 * @author Henrik Rentz-Reichert (initial contribution)
 *
 */
public abstract class GeneratorLaunchConfigurationDelegate extends AbstractJavaLaunchConfigurationDelegate{
	
	@Override
	public void launch(ILaunchConfiguration configuration, String mode,
			ILaunch launch, IProgressMonitor monitor) throws CoreException {
		
		if (monitor == null) {
			monitor = new NullProgressMonitor();
		}
		
		monitor.beginTask(configuration.getName()+"...", 3); //$NON-NLS-1$
		
		if (monitor.isCanceled()) {
			return;
		}
		try {
			StringBuffer argString = new StringBuffer();

			// constructing program arguments
			addModels(configuration, argString);
			addArguments(configuration, argString);

			String pgmArgs = argString.toString().trim();
			pgmArgs = VariablesPlugin.getDefault().getStringVariableManager()
					.performStringSubstitution(pgmArgs);

			// split at single spaces but keep strings in double quotes as single argument
			// (with double quotes removed)
			ArrayList<String> res = new ArrayList<String>();
			int begin = 0;
			int end = pgmArgs.indexOf(' ');
			boolean inQuotes = false;
			while (end>0) {
				if (pgmArgs.charAt(begin)=='\"')
					inQuotes = true;
				if ((inQuotes && pgmArgs.charAt(end-1)=='\"')) {
					inQuotes = false;
				}
				
				if (!inQuotes) {
					res.add(pgmArgs.substring(begin, end).replace("\"", ""));
					begin = end+1;
				}
				end = pgmArgs.indexOf(' ', end+1);
			}
			res.add(pgmArgs.substring(begin).replace("\"", ""));
			
			String[] args = new String[res.size()];
			res.toArray(args);

			final MessageConsole myConsole = findConsole(getConsoleName());
			Display.getDefault().syncExec(new Runnable() {
				@Override
				public void run() {
					try {
						IWorkbench wb = PlatformUI.getWorkbench();
						IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
						IWorkbenchPage page = win.getActivePage();
						String id = IConsoleConstants.ID_CONSOLE_VIEW;
						IConsoleView view = (IConsoleView) page.showView(id);
						view.display(myConsole);
					} catch (PartInitException e) {
						e.printStackTrace();
					}
				}
			});
			MessageConsoleStream out = myConsole.newMessageStream();
			out.getConsole().clearConsole();
			ConsoleOutput output = new ConsoleOutput(out);
			runGenerator(args, output);

			// check for cancellation
			if (monitor.isCanceled()) {
				return;
			}
		} finally {
			monitor.done();
		}
		
		launchRefreshJob(configuration);
	}

	private void launchRefreshJob(final ILaunchConfiguration configuration) {
		Job job = new Job("refresh resources after code generation") {
			public IStatus run(IProgressMonitor monitor) {
				try {
					RefreshTab.refreshResources(configuration, monitor);
				} catch (CoreException e) {
					e.printStackTrace();
					return e.getStatus();
				}
				return Status.OK_STATUS;
			}
		};
		job.schedule();
	}
	
	private MessageConsole findConsole(String name) {
		ConsolePlugin plugin = ConsolePlugin.getDefault();
		IConsoleManager conMan = plugin.getConsoleManager();
		IConsole[] existing = conMan.getConsoles();
		for (int i = 0; i < existing.length; i++)
			if (name.equals(existing[i].getName()))
				return (MessageConsole) existing[i];
		
		// no console found, so create a new one
		MessageConsole myConsole = new MessageConsole(name, null);
		conMan.addConsoles(new IConsole[] { myConsole });
		return myConsole;
	}

	protected void addModels(ILaunchConfiguration configuration, StringBuffer argString) throws CoreException {
		IStringVariableManager variableManager = VariablesPlugin.getDefault().getStringVariableManager();
		
		List<String> models = Lists.newArrayList();
		for(String model : getModels(configuration)) {
			models.add(variableManager.performStringSubstitution(model));
		}
		if(configuration.getAttribute(GeneratorConfigTab.GEN_DEPS_WITHIN_PROJECT, true)) {
			// generate all dependencies within project for .etmap
			models = Lists.newArrayList(GeneratorLaunchHelper.getAllDependenciesWithinProjects(models));
		}
		for(String model : models) {
			argString.append(" \""+model+"\"");
		}		
	}
	
	@SuppressWarnings("unchecked")
	protected List<String> getModels(ILaunchConfiguration configuration) throws CoreException {
		return configuration.getAttribute("ModelFiles", Collections.EMPTY_LIST);
	}
	
	/**
	 * assemble the command line by adding further parameters
	 * 
	 * @param configuration
	 * @param argString
	 * @throws CoreException
	 */
	@SuppressWarnings("deprecation")	// keep compatible
	protected void addArguments(ILaunchConfiguration configuration, StringBuffer argString) throws CoreException {
		if (configuration.getAttribute(GeneratorConfigTab.LIB, false)) {
			argString.append(" -"+AbstractGeneratorOptions.LIB.getName());
		}
		if (configuration.getAttribute(GeneratorConfigTab.SAVE_GEN_MODEL, false)) {
			argString.append(" -"+AbstractGeneratorOptions.SAVE_GEN_MODEL.getName());
			argString.append(" "+configuration.getAttribute(GeneratorConfigTab.GEN_MODEL_PATH, "?"));
		}
		if (!configuration.getAttribute(GeneratorConfigTab.MAIN_METHOD_NAME, AbstractGeneratorOptions.MAIN_NAME.getDefaultValue()).equals(AbstractGeneratorOptions.MAIN_NAME.getDefaultValue())) {
			argString.append(" -"+AbstractGeneratorOptions.MAIN_NAME.getName());
			argString.append(" "+configuration.getAttribute(GeneratorConfigTab.MAIN_METHOD_NAME, AbstractGeneratorOptions.MAIN_NAME.getDefaultValue()));
		}
		if (configuration.getAttribute(GeneratorConfigTab.GEN_DOCUMENTATION, false)
				|| configuration.getAttribute(GeneratorConfigTab.GEN_INSTANCE_DIAGRAM, false))
			argString.append(" -"+AbstractGeneratorOptions.DOCUMENTATION.getName());
		if (configuration.getAttribute(GeneratorConfigTab.DEBUG, false)) {
			argString.append(" -"+GeneratorApplicationOptions.LOGLEVEL.getName());
			argString.append(" debug");
		}
		if (configuration.getAttribute(GeneratorConfigTab.MSC_INSTR, false)) {
			argString.append(" -"+AbstractGeneratorOptions.MSC_INSTR.getName());
		}
		if (configuration.getAttribute(GeneratorConfigTab.DATA_INSTR, false)) {
			argString.append(" -"+AbstractGeneratorOptions.DATA_INSTR.getName());
		}
		if (configuration.getAttribute(GeneratorConfigTab.VERBOSE, false)) {
			argString.append(" -"+AbstractGeneratorOptions.VERBOSE_RT.getName());
		}
		if (!configuration.getAttribute(GeneratorConfigTab.USE_TRAANSLATION, true)) {
			argString.append(" -"+AbstractGeneratorOptions.NOTRANSLATE.getName());
		}
		
		ScopedPreferenceStore prefStore = new ScopedPreferenceStore(InstanceScope.INSTANCE, "org.eclipse.etrice.generator.ui");
		if (prefStore.getBoolean(PreferenceConstants.GEN_INCREMENTAL)) {
			argString.append(" -"+GeneratorApplicationOptions.GEN_INCREMENTAL.getName());
		}
		
		boolean override = configuration.getAttribute(GeneratorConfigTab.OVERRIDE_DIRECTORIES, false);
		String srcgenDir = prefStore.getString(PreferenceConstants.GEN_DIR);
		String infoDir = prefStore.getString(PreferenceConstants.GEN_INFO_DIR);
		String docDir = prefStore.getString(PreferenceConstants.GEN_DOC_DIR);
		if (override) {
			srcgenDir = configuration.getAttribute(GeneratorConfigTab.SRCGEN_PATH, srcgenDir);
			infoDir = configuration.getAttribute(GeneratorConfigTab.INFO_PATH, infoDir);
			docDir = configuration.getAttribute(GeneratorConfigTab.DOC_PATH, docDir);
		}
		argString.append(" -"+GeneratorApplicationOptions.GEN_DIR.getName());
		argString.append(" "+srcgenDir);
		
		argString.append(" -"+GeneratorApplicationOptions.GEN_INFO_DIR.getName());
		argString.append(" "+infoDir);
		
		argString.append(" -"+AbstractGeneratorOptions.GEN_DOC_DIR.getName());
		argString.append(" "+docDir);
	}
	
	/**
	 * call the generator main method
	 * 
	 * @param args the command line arguments
	 * @param out line wise output to console
	 */
	protected abstract void runGenerator(String[] args, ILineOutput out);
	
	/**
	 * @return the name of the console for the generator output
	 */
	protected abstract String getConsoleName();
	
	@Override
	public boolean buildForLaunch(ILaunchConfiguration configuration, String mode, IProgressMonitor monitor) throws CoreException {
		// never build before launch
		return false;
	}
}

Back to the top