Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: deba8a4f2cd08d03edc35354f96c79fec7fddd85 (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
/*******************************************************************************
* Copyright (c) 2018 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 is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* CONTRIBUTORS:
*           Jan Belle (initial contribution)
*
 *******************************************************************************/

package org.eclipse.etrice.generator.base;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;

import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.etrice.generator.base.args.Arguments;
import org.eclipse.etrice.generator.base.args.Options;
import org.eclipse.etrice.generator.base.cli.CommandLineParseException;
import org.eclipse.etrice.generator.base.cli.ICommandLineParser;
import org.eclipse.etrice.generator.base.cli.IHelpFormatter;
import org.eclipse.etrice.generator.base.io.GeneratorFileIO;
import org.eclipse.etrice.generator.base.io.ILineOutput;
import org.eclipse.etrice.generator.base.io.IGeneratorResourceLoader;
import org.eclipse.etrice.generator.base.io.LineOutput;
import org.eclipse.etrice.generator.base.logging.Logger;
import org.eclipse.etrice.generator.base.logging.Loglevel;
import org.eclipse.etrice.generator.base.setup.GeneratorApplicationOptions;
import org.eclipse.etrice.generator.base.setup.IGeneratorOptions;
import org.eclipse.etrice.generator.base.validation.IGeneratorResourceValidator;

import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.Provider;

/**
 * Executes a generator.
 */
public class GeneratorApplication {

	/**
	 * Creates a new generator instance using the specified generator module.
	 * 
	 * @param module the generator module
	 * @return the new generator application
	 */
	public static GeneratorApplication create(Module module) {
		Injector injector = Guice.createInjector(module);
		GeneratorApplication genAppl = injector.getInstance(GeneratorApplication.class);
		return genAppl;
	}

	/**
	 * Creates a new generator instance using the specified generator module.
	 * 
	 * @param moduleName the name of the class of the generator module
	 * @return the new generator application
	 */
	public static GeneratorApplication create(String moduleName) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
		Module module = (Module) Class.forName(moduleName).newInstance();
		return create(module);
	}

	private final Options options;
	private ICommandLineParser commandLineParser;
	private IHelpFormatter helpFormatter;
	private Provider<Logger> loggerProvider;
	private Provider<GeneratorFileIO> fileIOProvider;
	private Provider<IGenerator> generatorProvider;
	private IGeneratorResourceLoader resourceLoader;
	private IGeneratorResourceValidator resourceValidator;

	@Inject
	public GeneratorApplication(IGeneratorOptions optionsModule, ICommandLineParser commandLineParser,
			IHelpFormatter helpFormatter, Provider<Logger> loggerProvider,
			Provider<GeneratorFileIO> fileIOProvider, Provider<IGenerator> generatorProvider,
			IGeneratorResourceLoader resourceLoader, IGeneratorResourceValidator resourceValidator) {
		this.options = new Options(new GeneratorApplicationOptions(), optionsModule);
		this.commandLineParser = commandLineParser;
		this.helpFormatter = helpFormatter;
		this.loggerProvider = loggerProvider;
		this.fileIOProvider = fileIOProvider;
		this.generatorProvider = generatorProvider;
		this.resourceLoader = resourceLoader;
		this.resourceValidator = resourceValidator;
	}

	/**
	 * Runs the generator with the specified command line arguments.
	 * 
	 * @param args the command line arguments
	 */
	public void run(String[] args) throws GeneratorException {
		run(args, new LineOutput());
	}

	/**
	 * Runs the generator with the specified command line arguments and output.
	 * 
	 * @param args the command line arguments
	 * @param out the output
	 */
	public void run(String[] args, ILineOutput out) throws GeneratorException {
		try {
			Arguments arguments = commandLineParser.parseArgs(options, GeneratorApplicationOptions.FILES, args);

			if(arguments.get(GeneratorApplicationOptions.HELP)) {
				printHelp(out);
			}
			else {
				run(arguments, out);
			}
		}
		catch (CommandLineParseException e) {
			out.println("Error: " + e.getMessage());
			printHelp(out);
			throw new GeneratorException(e);
		}
	}

	/**
	 * Runs the generator using the specified arguments and output.
	 * 
	 * @param arguments the generator arguments
	 * @param out the output
	 */
	public void run(Arguments arguments, ILineOutput out) throws GeneratorException {
		Logger logger = createLogger(arguments, out);
		
		try {
			GeneratorFileIO fileIO = createGeneratorFileIO(arguments, logger);
			
			logArguments(arguments, fileIO, logger);

			List<Resource> resources = load(arguments, logger);

			validate(resources, arguments, logger);

			generate(resources, arguments, fileIO, logger);
			
			cleanOutputDirectory(arguments, fileIO, logger);
		}
		catch (Exception e) {
			logException(e, logger);
			throw e;
		}
	}

	/**
	 * Returns the options of this generator.
	 * 
	 * @return the options
	 */
	public final Options getOptions() {
		return options;
	}
	
	/**
	 * Creates a new set of arguments for this generator.
	 * 
	 * @return the default arguments
	 */
	public Arguments createArguments() {
		return new Arguments(getOptions());
	}

	private void printHelp(ILineOutput out) {
		String help = helpFormatter.getHelp(options, GeneratorApplicationOptions.FILES);
		out.println(help);
	}

	private Logger createLogger(Arguments arguments, ILineOutput out) {
		Logger logger = loggerProvider.get();
		logger.setLoglevel(arguments.get(GeneratorApplicationOptions.LOGLEVEL));
		logger.setOutput(out);
		return logger;
	}

	private GeneratorFileIO createGeneratorFileIO(Arguments arguments, Logger logger) {
		GeneratorFileIO fileIO = fileIOProvider.get();
		fileIO.setOutputDirectory(arguments.get(GeneratorApplicationOptions.GEN_DIR));
		fileIO.setLogger(logger);
		return fileIO;
	}

	private List<Resource> load(Arguments arguments, Logger logger) {
		List<String> files = arguments.get(GeneratorApplicationOptions.FILES);
		return resourceLoader.load(files, arguments, logger);
	}

	private void validate(List<Resource> models, Arguments arguments, Logger logger) {
		resourceValidator.validate(models, arguments, logger);
	}

	private void generate(List<Resource> resources, Arguments arguments, GeneratorFileIO fileIO, Logger logger) {
		// Create new generator to avoid problems with static states in eTrice AbstractGenerator
		IGenerator generator = generatorProvider.get();
		generator.generate(resources, arguments, fileIO, logger);
	}
	
	private void cleanOutputDirectory(Arguments arguments, GeneratorFileIO fileIO, Logger logger) {
		if(arguments.get(GeneratorApplicationOptions.CLEAN)) {
			fileIO.cleanOutputDirectory();
		}
	}
	
	private void logArguments(Arguments arguments, GeneratorFileIO fileIO, Logger logger) {
		logger.logDebug("Arguments: " + arguments);
		logger.logDebug("Output directory: " + fileIO.getOutputDirectory().toAbsolutePath());
	}
	
	private void logException(Exception e, Logger logger) {
		if(Loglevel.DEBUG.compareTo(logger.getLoglevel()) >= 0) {
			StringWriter sw = new StringWriter();
			PrintWriter pw = new PrintWriter(sw);
			e.printStackTrace(pw);
			logger.logDebug(sw.toString());
		}
	}
}

Back to the top