Repository navigation
Expand file tree
/
Copy pathExpressProjectWizard.java
More file actions
324 lines (287 loc) · 10.3 KB
/
Copy pathExpressProjectWizard.java
File metadata and controls
324 lines (287 loc) · 10.3 KB
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package org.nodeclipse.ui.wizards;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkingSet;
import org.eclipse.ui.dialogs.IOverwriteQuery;
import org.eclipse.ui.ide.undo.CreateProjectOperation;
import org.eclipse.ui.ide.undo.WorkspaceUndoUtil;
import org.eclipse.ui.wizards.datatransfer.ImportOperation;
import org.eclipse.wst.jsdt.internal.ui.workingsets.JavaWorkingSetUpdater;
import org.nodeclipse.ui.Activator;
import org.nodeclipse.ui.npm.InstallLaunchShortcut;
import org.nodeclipse.ui.preferences.PreferenceConstants;
import org.nodeclipse.ui.util.Constants;
import org.nodeclipse.ui.util.LogUtil;
import org.nodeclipse.ui.util.NodeclipseConsole;
import org.nodeclipse.ui.util.ProcessUtils;
import org.osgi.framework.Bundle;
/**
* @author Tomoyuki Inagaki
*/
public class ExpressProjectWizard extends AbstractNodeProjectWizard {
private final String WINDOW_TITLE = "New Express Project";
private ExpressProjectWizardPage mainPage;
public ExpressProjectWizard() {
super();
setWindowTitle(WINDOW_TITLE);
}
@Override
public void addPages() {
mainPage = new ExpressProjectWizardPage("ExpressNewProjectPage") { //$NON-NLS-1$
/*
* (non-Javadoc)
*
* @see
* org.eclipse.ui.dialogs.WizardNewProjectCreationPage#createControl
* (org.eclipse.swt.widgets.Composite)
*/
public void createControl(Composite parent) {
super.createControl(parent);
createWorkingSetGroup(
(Composite) getControl(),
getSelection(),
new String[] { JavaWorkingSetUpdater.ID, "org.eclipse.ui.resourceWorkingSetPage" }); //$NON-NLS-1$
Dialog.applyDialogFont(getControl());
}
};
mainPage.setTitle("Create an Express Project");
mainPage.setDescription("Create a new Node.js Express project (using `express` CLI underneath,\n"
+"that should be installed before with `npm install -g express`\n"
+"Used `Express` location can be configured in Preferences \"Express path\").");
addPage(mainPage);
}
@Override
protected IProject createNewProject() {
final IProject newProjectHandle = mainPage.getProjectHandle();
URI location = null;
if (!mainPage.useDefaults()) {
location = mainPage.getLocationURI();
}
/*
IWorkspace workspace = ResourcesPlugin.getWorkspace();
final IProjectDescription description = workspace
.newProjectDescription(newProjectHandle.getName());
description.setLocationURI(location);
String[] natures = description.getNatureIds();
String[] newNatures = new String[natures.length + 1];
System.arraycopy(natures, 0, newNatures, 0, natures.length);
newNatures[natures.length] = NodeNature.NATURE_ID;
description.setNatureIds(newNatures);
*/
final IProjectDescription pd = createProjectDescription(newProjectHandle, location);
final boolean exists = isExistingProjectFolder(pd);
final String projectName = mainPage.getProjectName();
final String templateEngine = mainPage.getSelectedTemplateEngine();
final String stylesheetEngine = mainPage.getSelectedStylesheetEngine();
IRunnableWithProgress op = new IRunnableWithProgress() {
@Override
public void run(IProgressMonitor monitor)
throws InvocationTargetException, InterruptedException {
createProject(pd, monitor);
if(exists == true) {
return;
}
String tempdir = System.getProperty("java.io.tmpdir")
+ "/express-work/"
+ new Long(System.currentTimeMillis()).toString();
File workingDirectory = new File(tempdir);
if (workingDirectory.mkdirs() == false) {
throw new InvocationTargetException(new IOException(
"failed creation of working directory."));
}
generateExpressApplication(monitor, projectName, newProjectHandle,
templateEngine, stylesheetEngine, workingDirectory);
importExpressApplication(monitor, newProjectHandle,
workingDirectory);
monitor.beginTask("Generating Express Project...", 3);
deleteFiles(monitor, workingDirectory);
monitor.worked(1);
try {
// universal method for all templates
generateTemplates("templates/common-templates", newProjectHandle);
rewriteFile("README.md", newProjectHandle);
rewritePackageJson(monitor, newProjectHandle);
boolean addTernNature = store.getBoolean(PreferenceConstants.ADD_TERN_NATURE);
if (addTernNature){
generateTemplates("templates/tern-node", newProjectHandle);
}
// JSHint support
runJSHint(newProjectHandle);
} catch (CoreException e1) {
throw new InvocationTargetException(e1);
}
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
try {
launchNpmInstall(newProjectHandle);
} catch (Exception e) {
// throw new InvocationTargetException(e);
}
}
});
try {
newProjectHandle.refreshLocal(1, monitor);
} catch (CoreException e) {
throw new InvocationTargetException(e);
}
monitor.done();
}
};
try {
getContainer().run(true, true, op);
} catch (InvocationTargetException e) {
LogUtil.error(e);
} catch (InterruptedException e) {
}
if (newProjectHandle != null) {
// add to workingsets
IWorkingSet[] workingSets = mainPage.getSelectedWorkingSets();
getWorkbench().getWorkingSetManager().addToWorkingSets(
newProjectHandle, workingSets);
}
return newProjectHandle;
}
private void createProject(IProjectDescription description,
IProgressMonitor monitor) throws InvocationTargetException {
CreateProjectOperation op = new CreateProjectOperation(description,
WINDOW_TITLE);
try {
op.execute(monitor, WorkspaceUndoUtil.getUIInfoAdapter(getShell()));
} catch (ExecutionException e) {
throw new InvocationTargetException(e);
}
}
private void generateExpressApplication(IProgressMonitor monitor,
String projectName, IProject projectHandle, String templateEngine, String stylesheetEngine, File workingDirectory)
throws InvocationTargetException {
Bundle bundle = Activator.getDefault().getBundle();
if (bundle == null) {
throw new InvocationTargetException(
new CoreException(
new Status(IStatus.ERROR, Activator.PLUGIN_ID, "bundle not found")));
}
List<String> cmdLine = new ArrayList<String>();
cmdLine.add(ProcessUtils.getNodePath());
cmdLine.add(ProcessUtils.getExpressPath());
cmdLine.add(workingDirectory.getAbsolutePath() + "/" + projectName);
if (!templateEngine.equals(Constants.BLANK_STRING)) {
int ver = ProcessUtils.getExpressMajorVersion();
if (ver < 3) {
cmdLine.add("-t");
cmdLine.add(templateEngine);
} else {
cmdLine.add("--" + templateEngine);
}
}
if (!stylesheetEngine.equals(Constants.BLANK_STRING)) {
cmdLine.add("--css " + stylesheetEngine);
}
for(String s : cmdLine) NodeclipseConsole.write(s+" ");
NodeclipseConsole.write("\n");
ProcessUtils.exec(cmdLine, workingDirectory);
}
private void importExpressApplication(IProgressMonitor monitor,
IProject projectHandle, File workingDirectory)
throws InvocationTargetException, InterruptedException {
String projectName = projectHandle.getName();
File[] children = workingDirectory.listFiles();
File root = null;
for (File child : children) {
if (child.getName().equals(projectName)) {
root = child;
break;
}
}
if (root == null) {
throw new InvocationTargetException(new IOException("Express app("
+ projectName + ") is not found. "));
}
RelativityFileSystemStructureProvider structureProvider = new RelativityFileSystemStructureProvider(
workingDirectory);
ImportOperation operation = new ImportOperation(
projectHandle.getFullPath(), root, structureProvider,
new IOverwriteQuery() {
@Override
public String queryOverwrite(String pathString) {
return IOverwriteQuery.ALL;
}
}, structureProvider.collectFiles(root));
operation.setCreateContainerStructure(false);
operation.setContext(getShell());
operation.run(monitor);
}
private void deleteFiles(IProgressMonitor monitor, File f) {
if (f.exists() == false) {
return;
}
if (f.isFile()) {
f.delete();
}
if (f.isDirectory()) {
File[] files = f.listFiles();
for (int i = 0; i < files.length; i++) {
deleteFiles(monitor, files[i]);
}
f.delete();
}
}
private void rewritePackageJson(IProgressMonitor monitor, IProject projectHandle) throws CoreException {
String newLine = System.getProperty("line.separator");
IFile file = projectHandle.getFile("package.json");
if(!file.exists()) {
throw new CoreException(new Status(IStatus.ERROR,
Activator.PLUGIN_ID, "package.json not found"));
}
InputStreamReader ir = new InputStreamReader(file.getContents());
BufferedReader br = new BufferedReader(ir);
StringBuilder sb = new StringBuilder();
String line;
try {
while((line = br.readLine()) != null) {
if(line.contains("application-name")) {
line = line.replace("application-name", projectHandle.getName());
}
sb.append(line);
sb.append(newLine);
}
ByteArrayInputStream source = new ByteArrayInputStream(sb.toString().getBytes());
file.setContents(source, true, true, null);
} catch (IOException e) {
throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Cannot read package.json"));
} finally {
try {
ir.close();
br.close();
} catch (IOException e) {
}
ir = null;
br = null;
}
}
private void launchNpmInstall(IProject projectHandle) throws InvocationTargetException, CoreException {
InstallLaunchShortcut launcher = new InstallLaunchShortcut();
IFile path = projectHandle.getFile(Constants.PACKAGE_JSON);
launcher.launchFile(path, ILaunchManager.DEBUG_MODE);
}
}