-
Notifications
You must be signed in to change notification settings - Fork 20
/
IProjectUtils.java
231 lines (212 loc) · 7.37 KB
/
IProjectUtils.java
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
/*******************************************************************************
* Copyright (c) 2017 Inria 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:
* Inria - initial API and implementation
*******************************************************************************/
package org.eclipse.gemoc.commons.eclipse.core.resources;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
public class IProjectUtils {
/**
* Create recursively folders in the project.
* Do nothing if folder already exists.
* @param project
* @param nameOrPath
* @return
* @throws CoreException
*/
public static IFolder createFolder(IProject project, String nameOrPath) throws CoreException {
return createFolder(project, nameOrPath, new NullProgressMonitor());
}
/**
* Create recursively folders in the project.
* Do nothing if folder already exists.
* @param project
* @param nameOrPath
* @param monitor
* @return
* @throws CoreException
*/
public static IFolder createFolder(IProject project, String nameOrPath, IProgressMonitor monitor) throws CoreException {
return createFolder(project, new Path(nameOrPath), monitor);
}
/**
* Create recursively folders in the project.
* Do nothing if folder already exists.
* @param project
* @param path
* @param monitor
* @return
* @throws CoreException
*/
public static IFolder createFolder(IProject project, IPath path, IProgressMonitor monitor) throws CoreException {
IFolder folder = project.getFolder(path);
if (!folder.exists()) {
if (path.segmentCount() > 1) {
IPath currentFolderPath = new Path("");
for (int i=0; i<path.segmentCount()-1; i++) {
currentFolderPath = currentFolderPath.append(path.segment(i));
createFolder(project, currentFolderPath, monitor);
}
}
folder.create(true, true, monitor);
}
return folder;
}
/**
* Create a file in the project with default content.
* Do nothing if file already exists.
* @param project
* @param nameOrPath
* @param content
* @param monitor
* @return
* @throws CoreException
*/
public static IFile createFile(IProject project, String nameOrPath, String content, IProgressMonitor monitor) throws CoreException {
ByteArrayInputStream contentStream = new ByteArrayInputStream(content.getBytes());
IFile file = createFile(project, new Path(nameOrPath), contentStream, monitor);
return file;
}
/**
* Create a file in the project with default content.
* Do nothing if file already exists.
* @param project
* @param nameOrPath
* @param contentStream
* @param monitor
* @return
* @throws CoreException
*/
public static IFile createFile(IProject project, String nameOrPath, InputStream contentStream, IProgressMonitor monitor) throws CoreException {
return createFile(project, new Path(nameOrPath), contentStream, monitor);
}
/**
* Create a file in the project with default content.
* Do nothing if file already exists.
* @param project
* @param path
* @param contentStream
* @param monitor
* @return
* @throws CoreException
*/
public static IFile createFile(IProject project, IPath path, InputStream contentStream, IProgressMonitor monitor) throws CoreException {
IFile file = project.getFile(path);
return createFile(project, file, contentStream, monitor);
}
public static IFile createFile(IProject project, IFile file, String content) throws CoreException {
return createFile(project, file, content, new NullProgressMonitor());
}
public static IFile createFile(IProject project, IFile file, String content, IProgressMonitor monitor) throws CoreException {
ByteArrayInputStream contentStream = new ByteArrayInputStream(content.getBytes());
return createFile(project, file, contentStream, new NullProgressMonitor());
}
public static IFile createFile(IProject project, IFile file, InputStream contentStream, IProgressMonitor monitor) throws CoreException {
if (!file.exists())
{
IPath path = file.getProjectRelativePath();
if (path.segmentCount() > 1) {
IPath currentFolderPath = new Path("");
for (int i=0; i<path.segmentCount()-1; i++) {
currentFolderPath = currentFolderPath.append(path.segment(i));
createFolder(project, currentFolderPath, monitor);
}
}
try
{
file.create(contentStream, true, monitor);
}
finally
{
try {
contentStream.close();
} catch (IOException e) {
throw new CoreException(new Status(Status.ERROR, "", "Could not close stream for file " + file.getFullPath(), e));
}
}
}
return file;
}
public static void addNature(IProject project, String natureId) throws CoreException
{
IProjectDescription description = project.getDescription();
String[] natures = description.getNatureIds();
if (!project.hasNature(natureId))
{
String[] newNatures = new String[natures.length + 1];
System.arraycopy(natures, 0, newNatures, 1, natures.length);
newNatures[0] = natureId;
description.setNatureIds(newNatures);
project.setDescription(description, null);
}
}
public static void removeNature(IProject project, String natureId) throws CoreException
{
IProjectDescription description = project.getDescription();
ArrayList<String> natures = new ArrayList<String>(Arrays.asList(description.getNatureIds()));
natures.remove(natureId);
String[] newNatures = natures.toArray(new String[natures.size()]);
description.setNatureIds(newNatures);
project.setDescription(description, null);
}
public static NatureToggling toggleNature(IProject project, String natureId) throws CoreException
{
if (!project.hasNature(natureId))
{
addNature(project, natureId);
return NatureToggling.Added;
}
else
{
removeNature(project, natureId);
return NatureToggling.Removed;
}
}
public static void setFileContent(IProject project, String filePath, String fileContent) throws CoreException
{
setFileContent(project, new Path(filePath), fileContent);
}
public static void setFileContent(IProject project, IPath filePath, String fileContent) throws CoreException
{
IFile file = project.getFile(filePath);
setFileContent(project, file, fileContent, new NullProgressMonitor());
}
public static void setFileContent(IProject project, IFile file, String fileContent, IProgressMonitor monitor) throws CoreException
{
if (file.exists())
{
ByteArrayInputStream contentStream = new ByteArrayInputStream(fileContent.getBytes());
try
{
file.setContents(contentStream, true, true, monitor);
}
finally
{
try {
contentStream.close();
} catch (IOException e) {
throw new CoreException(new Status(Status.ERROR, "", "Could not close stream for file " + file.getFullPath(), e));
}
}
}
}
}