Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: be393f810c1a9a3fc6b64a2341458ae2fc29d60c (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
/*******************************************************************************
 * Copyright (c) 2013 Red Hat Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Neil Guzman - initial API and implementation
 *******************************************************************************/
package org.eclipse.linuxtools.internal.rpm.createrepo;

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

import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.linuxtools.rpm.createrepo.CreaterepoPreferenceConstants;
import org.eclipse.linuxtools.rpm.createrepo.ICreaterepoConstants;

/**
 * Class to create and format the commands.
 */
public class CreaterepoCommandCreator {

	// commands that are either used or not used
	private static final String[] BOOLEAN_COMMANDS = {
		// general
		CreaterepoPreferenceConstants.PREF_UNIQUE_MD_NAME,
		CreaterepoPreferenceConstants.PREF_GENERATE_DB,
		CreaterepoPreferenceConstants.PREF_IGNORE_SYMLINKS,
		CreaterepoPreferenceConstants.PREF_PRETTY_XML,
		CreaterepoPreferenceConstants.PREF_CHECK_TS,
	};

	// commands that determine used state by arguments passed with it
	private static final String[] STRING_COMMANDS = {
		// general
		CreaterepoPreferenceConstants.PREF_CHECKSUM,
		CreaterepoPreferenceConstants.PREF_COMPRESSION_TYPE,
	};

	private static final String[] STRING_META_COMMANDS = {
		// metadata
		CreaterepoPreferenceConstants.PREF_REVISION,
		CreaterepoPreferenceConstants.PREF_DISTRO_TAG,
		CreaterepoPreferenceConstants.PREF_CONTENT_TAG,
		CreaterepoPreferenceConstants.PREF_REPO_TAG
	};

	// commands that determine used state by int passed with it
	private static final String[] INT_COMMANDS = {
		// general
		CreaterepoPreferenceConstants.PREF_WORKERS,
		CreaterepoPreferenceConstants.PREF_CHANGELOG_LIMIT,
	};

	private static final String[] INT_DELTA_COMMANDS = {
		// deltas
		CreaterepoPreferenceConstants.PREF_MAX_DELTA_SIZE,
		CreaterepoPreferenceConstants.PREF_NUM_DELTAS,
	};

	private IEclipsePreferences projectPreferences;
	private IPreferenceStore preferenceStore;
	private boolean project;
	private boolean delta;

	public CreaterepoCommandCreator(IEclipsePreferences projectPreferences) {
		project = Activator.isProjectPrefEnabled();
		delta = Activator.isDeltaPrefEnabled();
		this.projectPreferences = projectPreferences;
		preferenceStore = Activator.getDefault().getPreferenceStore();
	}

	/**
	 * Get all the command arguments.
	 *
	 * @return A list of all the command arguments.
	 */
	public List<String> getCommands() {
		List<String> commands = new ArrayList<String>();
		commands.addAll(prepareBooleanCommands());
		commands.addAll(prepareStringCommands());
		commands.addAll(prepareIntCommands());
		return commands;
	}

	/**
	 * These commands are either added to the execution or not, depending
	 * on the enabled status from the preferences.
	 *
	 * @return The command options to add.
	 */
	public List<String> prepareBooleanCommands() {
		List<String> commands = new ArrayList<String>();
		if (delta) {
			commands.add(ICreaterepoConstants.DASH.concat(CreaterepoPreferenceConstants.PREF_DELTA_ENABLE));
		}
		for (String arg : BOOLEAN_COMMANDS) {
			// if project preferences are enabled, use the preferences from there
			boolean value = project ? projectPreferences.getBoolean(arg, preferenceStore.getDefaultBoolean(arg))
					: preferenceStore.getBoolean(arg);
			// if the value returned is true, that means the switch should be added
			if (value) {
				if (arg.equals(CreaterepoPreferenceConstants.PREF_UNIQUE_MD_NAME)) {
					arg = ICreaterepoConstants.DASH.concat("unique-").concat(arg); //$NON-NLS-1$
				} else {
					arg = ICreaterepoConstants.DASH.concat(arg);
				}
				commands.add(arg);
			} else {
				// only add the switch if its one of these options
				if (arg.equals(CreaterepoPreferenceConstants.PREF_UNIQUE_MD_NAME)) {
					arg = ICreaterepoConstants.DASH.concat("simple-").concat(arg); //$NON-NLS-1$
					commands.add(arg);
				} else if (arg.equals(CreaterepoPreferenceConstants.PREF_GENERATE_DB)) {
					arg = ICreaterepoConstants.DASH.concat("no-").concat(arg); //$NON-NLS-1$
					commands.add(arg);
				}
			}
		}
		return commands;
	}

	/**
	 * Prepare the commands that require a string passed with them
	 * when executing.
	 *
	 * @return The command options to add.
	 */
	public List<String> prepareStringCommands() {
		List<String> commands = new ArrayList<String>();
		for (String arg : STRING_COMMANDS) {
			String value = project ? projectPreferences.get(arg, preferenceStore.getDefaultString(arg))
					: preferenceStore.getString(arg);
			arg = ICreaterepoConstants.DASH.concat(arg);
			if (!value.isEmpty()) {
				commands.add(arg);
				commands.add(value);
			}
		}
		for (String arg : STRING_META_COMMANDS) {
			String value = projectPreferences.get(arg, preferenceStore.getDefaultString(arg));
			arg = ICreaterepoConstants.DASH.concat(arg);
			for (String tag : value.split(ICreaterepoConstants.DELIMITER)) {
				if (!tag.isEmpty()) {
					commands.add(arg);
					commands.add(tag);
				}
			}
		}
		return commands;
	}

	/**
	 * Prepare the commands that require an integer passed with them
	 * when executing. Differs from prepareStringCommands() by how it
	 * retrieves the values from the preferences.
	 *
	 * @return The command options to add.
	 */
	public List<String> prepareIntCommands() {
		List<String> commands = new ArrayList<String>();
		if (delta) {
			for (String arg : INT_DELTA_COMMANDS) {
				int value = projectPreferences.getInt(arg, preferenceStore.getDefaultInt(arg));
				arg = ICreaterepoConstants.DASH.concat(arg);
				commands.add(arg);
				commands.add(Integer.toString(value));
			}
		} else {
			for (String arg : INT_COMMANDS) {
				// if project preferences are enabled, use the preferences from there
				int value = project ? projectPreferences.getInt(arg, preferenceStore.getDefaultInt(arg))
						: preferenceStore.getInt(arg);
				arg = ICreaterepoConstants.DASH.concat(arg);
				commands.add(arg);
				commands.add(Integer.toString(value));
			}
		}
		return commands;
	}

}

Back to the top