Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 812217f93a1dab635cbdd25f6fc529c341e36d2d (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
/*******************************************************************************
 * Copyright (c) 2016 Ericsson.
 *
 * 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
 *******************************************************************************/

package org.eclipse.cdt.debug.application;

import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;

/**
 * A "script" that generates the config.ini used by the stand-alone debugger
 * script. This is done by parsing the debug.product file to find the plug-ins.
 */
public class ConfigGenerator {
	private static final Pattern PLUGIN_LINE_PATTERN = Pattern.compile("\\s*<plugin id=\"(\\S*)\".*"); //$NON-NLS-1$

	/** Plug-ins requiring a start level for things to work correctly */
	private static final Map<String, String> PLUGINS_WITH_START_LEVEL = new HashMap<>();
	/** Plug-ins that we don't want to load when using the script VS using the product */
	private static final Set<String> PLUGINS_NOT_IN_SCRIPT_VERSION = new HashSet<>();

	static {
		PLUGINS_WITH_START_LEVEL.put("org.apache.felix.scr", "@1\\:start"); //$NON-NLS-1$ //$NON-NLS-2$
		PLUGINS_WITH_START_LEVEL.put("org.eclipse.equinox.common", "@2\\:start"); //$NON-NLS-1$ //$NON-NLS-2$
		PLUGINS_WITH_START_LEVEL.put("org.eclipse.core.runtime", "@start"); //$NON-NLS-1$ //$NON-NLS-2$
		// We don't want the user to do "Check for updates", etc.
		PLUGINS_NOT_IN_SCRIPT_VERSION.add("org.eclipse.update.configurator"); //$NON-NLS-1$
		PLUGINS_NOT_IN_SCRIPT_VERSION.add("org.eclipse.equinox.p2.ui"); //$NON-NLS-1$
		PLUGINS_NOT_IN_SCRIPT_VERSION.add("org.eclipse.equinox.p2.ui.sdk"); //$NON-NLS-1$
	}

	public static void main(String[] args) {
		if (args.length < 4) {
			printUsage();
			System.exit(1);
		}

		String productFilePath = args[1];
		List<String> pluginList = parsePluginList(productFilePath);
		if (pluginList.isEmpty()) {
			System.err.println("No plugins. Something must have gone wrong."); //$NON-NLS-1$
			System.exit(1);
		}

		Path configOutputPath = Paths.get(args[3]);
		generateConfigIni(pluginList, configOutputPath);
		System.exit(0);
	}

	private static void generateConfigIni(List<String> pluginList, Path configOutputPath) {
		try (FileWriter r = new FileWriter(configOutputPath.toFile())) {
			r.write("osgi.install.area=file\\:$eclipse.home$\n"); //$NON-NLS-1$
			r.write("osgi.framework=file\\:$eclipse.home$/plugins/$osgi.jar$\n"); //$NON-NLS-1$
			r.write("osgi.bundles="); //$NON-NLS-1$

			// Write all plug-in names
			for (int i = 0; i < pluginList.size(); i++) {
				String pluginName = pluginList.get(i);
				if (PLUGINS_NOT_IN_SCRIPT_VERSION.contains(pluginName)) {
					continue;
				}

				r.write(pluginName);

				// Add start level if necessary
				if (PLUGINS_WITH_START_LEVEL.containsKey(pluginName)) {
					r.write(PLUGINS_WITH_START_LEVEL.get(pluginName));
				}

				r.write(',');
			}
			r.write('\n');
			r.write("osgi.configuration.cascaded=false\n"); //$NON-NLS-1$
			r.write("osgi.bundles.defaultStartLevel=4\n"); //$NON-NLS-1$
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(1);
		}
	}

	private static List<String> parsePluginList(String productPath) {
		List<String> pluginList = new ArrayList<>();
		try (Stream<String> stream = Files.lines(Paths.get(productPath))) {
			stream.forEach((line) -> {
				Matcher m = PLUGIN_LINE_PATTERN.matcher(line);
				if (m.matches()) {
					pluginList.add(m.group(1));
				}
			});
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(1);
		}
		return pluginList;
	}

	private static void printUsage() {
		System.err.println("Usage:"); //$NON-NLS-1$
		System.err.println("  ConfigGenerator -product /path/to/foo.product -out /path/to/config.ini"); //$NON-NLS-1$
	}
}

Back to the top