Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4c07b152058e8bb3d981fc1c33208a46c99634f8 (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
274
275
276
277
278
279
280
281
282
283
284
/*****************************************************************************
 * Copyright (c) 2011 CEA LIST.
 * 
 * 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:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *  Vincent Lorenzo (CEA-LIST) vincent.lorenzo@cea.fr
 *****************************************************************************/
package org.eclipse.papyrus.eclipse.project.editors.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.Set;

import org.eclipse.core.resources.IProject;
import org.eclipse.papyrus.eclipse.project.editors.Activator;
import org.eclipse.papyrus.eclipse.project.editors.interfaces.IBuildEditor;
import org.eclipse.papyrus.infra.widgets.util.FileUtil;

public class BuildEditor extends AbstractFileEditor implements IBuildEditor {

	/** the build config */
	private Properties buildConfig;

	/** the buidl file */
	private File buildFile;

	/**
	 * The build key to edit
	 * Defaults to "bin.includes" (The eclipse standard binary build)
	 */
	private String buildKey = "bin.includes"; //$NON-NLS-1$


	/**
	 * 
	 * Constructor.
	 * 
	 * @param project
	 *        the eclipse project
	 */
	public BuildEditor(final IProject project) {
		super(project);
	}

	/**
	 * 
	 * Constructor.
	 * 
	 * @param project
	 *        the eclipse project
	 * @param buildKey
	 *        the build key to edit. If null, the "bin.includes" key will be used
	 * 
	 * @see IBuildEditor#BINARY_BUILD
	 * @see IBuildEditor#SOURCE_BUILD
	 */
	public BuildEditor(final IProject project, String buildKey) {
		super(project);
		if(buildKey != null) {
			this.buildKey = buildKey;
		}
	}

	/**
	 * 
	 * @see org.eclipse.papyrus.eclipse.project.editors.project.ProjectEditor#init()
	 * 
	 *      {@inheritDoc}
	 */
	@Override
	public void init() {
		this.buildConfig = new Properties();
		this.buildFile = getBuildProperties();
		if(this.buildFile != null && this.buildFile.exists()) {
			try {
				this.buildConfig.load(new FileInputStream(this.buildFile));
			} catch (FileNotFoundException e) {
				Activator.log.error(e);
			} catch (IOException e) {
				Activator.log.error(e);
			}
		}
	}


	private File getBuildProperties() {
		return this.buildFile = FileUtil.getWorkspaceFile("/" + getProject().getName() + "/" + BUILD_PROPERTIES_FILE); //$NON-NLS-1$ //$NON-NLS-2$
	}

	/**
	 * 
	 * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IBuildEditor#addToBuild(java.lang.String)
	 * 
	 *      {@inheritDoc}
	 */
	public void addToBuild(final String path) {
		if(exists()) {
			String currentValue = this.buildConfig.getProperty(buildKey);
			if(Arrays.asList(getElementsInBuild()).contains(path)) {
				return;
			}
			if(currentValue == null || currentValue.trim().equals("")) { //$NON-NLS-1$
				this.buildConfig.setProperty(buildKey, path);
			} else {
				this.buildConfig.setProperty(buildKey, currentValue + "," + path); //$NON-NLS-1$
			}
		}
	}

	/**
	 * 
	 * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IBuildEditor#removeFromBuild(java.lang.String)
	 * 
	 *      {@inheritDoc}
	 */
	public void removeFromBuild(String path) {
		if(isRegisteredSourceFolder(path)) {
			//Get the files from the build
			List<String> allFiles = Arrays.asList(getElementsInBuild());

			//Clear the build
			this.buildConfig.setProperty(buildKey, "");

			//Recreate the build without the removed files
			for(String filePath : allFiles) {
				if(!filePath.equals(path)) {
					addToBuild(filePath);
				}
			}
		}
	}

	/**
	 * 
	 * @throws Throwable
	 * @see org.eclipse.papyrus.eclipse.project.editors.project.ProjectEditor#save()
	 * 
	 *      {@inheritDoc}
	 */
	public void save() throws Throwable {
		if(exists()) {
			this.buildConfig.store(new FileOutputStream(this.buildFile), ""); //$NON-NLS-1$
		}
	}


	/**
	 * 
	 * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IBuildEditor#registerSourceFolder(java.lang.String)
	 * 
	 *      {@inheritDoc}
	 */
	public void registerSourceFolder(final String path) {
		if(exists() && !isRegisteredSourceFolder(path)) {
			String currentValue = this.buildConfig.getProperty(SOURCE_FOLDER_KEY);
			if(currentValue == null || currentValue.trim().equals("")) { //$NON-NLS-1$
				this.buildConfig.setProperty(SOURCE_FOLDER_KEY, path);
			} else {
				this.buildConfig.setProperty(SOURCE_FOLDER_KEY, currentValue + "," + path); //$NON-NLS-1$
			}
		}
	}

	/**
	 * 
	 * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IBuildEditor#isRegistred(java.lang.String)
	 * 
	 *      {@inheritDoc}
	 */
	public boolean isRegisteredSourceFolder(final String path) {
		return Arrays.asList(getSourceFolders()).contains(path);
	}

	/**
	 * 
	 * @see org.eclipse.papyrus.eclipse.project.editors.file.AbstractFileEditor#getMissingFiles()
	 * 
	 *      {@inheritDoc}
	 */
	@Override
	public Set<String> getMissingFiles() {
		Set<String> files = super.getMissingFiles();
		if(!getBuildProperties().exists()) {
			files.add(BUILD_PROPERTIES_FILE);
		}
		return files;
	}

	/**
	 * 
	 * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IBuildEditor#getSourceFolders()
	 * 
	 *      {@inheritDoc}
	 */
	public String[] getSourceFolders() {
		if(exists()) {
			String currentValue = this.buildConfig.getProperty(SOURCE_FOLDER_KEY, "").replaceAll("\t|\r|\n", "").trim();
			String[] values = currentValue.split(","); //$NON-NLS-1$
			return values;
		}
		return new String[0];
	}

	/**
	 * 
	 * @see org.eclipse.papyrus.eclipse.project.editors.project.AbstractProjectEditor#exists()
	 * 
	 *      {@inheritDoc}
	 */
	@Override
	public boolean exists() {
		return this.buildFile.getParentFile().exists() && this.buildFile.exists() && super.exists();
	}

	/**
	 * 
	 * @see org.eclipse.papyrus.eclipse.project.editors.project.ProjectEditor#createFiles(Set)
	 * 
	 *      {@inheritDoc}
	 */
	public void createFiles(final Set<String> files) {
		if(files.contains(BUILD_PROPERTIES_FILE)) {
			if(!this.buildFile.exists()) {
				if(!this.buildFile.getParentFile().exists()) {
					this.buildFile.getParentFile().mkdirs();
				}
				try {
					this.buildFile.createNewFile();
					init();
				} catch (IOException e) {
					Activator.log.error(e);
				}
			}
		}
	}

	/**
	 * 
	 * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IBuildEditor#getElementsInBuild()
	 * 
	 *      {@inheritDoc}
	 */
	public String[] getElementsInBuild() {
		String value = this.buildConfig.getProperty(buildKey);
		return splitValues(value);
	}

	private String[] splitValues(String value) {
		if(value == null) {
			return new String[0];
		}
		return value.replace("\t|\r|\n", "").split(","); //$NON-NLS-1$ //$NON-NLS-2$
	}

	public void registerBinFolder(String binFolder) {
		if(isRegisteredBinFolder(binFolder)) {
			return;
		}

		String value = this.buildConfig.getProperty(BIN_KEY, ""); //$NON-NLS-1$
		if(value.trim().equals("")) { //$NON-NLS-1$
			value = binFolder;
		} else {
			value = value + "," + binFolder;
		}
		this.buildConfig.setProperty(BIN_KEY, value);
	}

	public boolean isRegisteredBinFolder(String binFolder) {
		List<String> folders = Arrays.asList(splitValues(this.buildConfig.getProperty(BIN_KEY, ""))); //$NON-NLS-1$
		return folders.contains(binFolder);
	}
}

Back to the top