Skip to content

Commit 1b89e7b

Browse files
author
angelozerr
committed
Call create factory with parameters. See
angelozerr/angular-eclipse#4
1 parent 499af39 commit 1b89e7b

File tree

8 files changed

+182
-125
lines changed

8 files changed

+182
-125
lines changed

eclipse/terminal/ts.eclipse.ide.terminal.interpreter.npm/src/ts/eclipse/ide/terminal/interpreter/npm/internal/commands/NpmCommandInterpreterFactory.java

Lines changed: 7 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,65 +2,19 @@
22

33
import java.util.List;
44

5-
import org.eclipse.core.resources.IContainer;
6-
import org.eclipse.core.resources.IResource;
7-
import org.eclipse.core.resources.ResourcesPlugin;
8-
import org.eclipse.core.runtime.CoreException;
9-
import org.eclipse.core.runtime.IProgressMonitor;
10-
import org.eclipse.core.runtime.IStatus;
11-
import org.eclipse.core.runtime.Path;
12-
import org.eclipse.core.runtime.Status;
13-
import org.eclipse.jface.viewers.StructuredSelection;
14-
import org.eclipse.ui.IPageLayout;
15-
import org.eclipse.ui.IViewPart;
16-
import org.eclipse.ui.IWorkbenchPage;
17-
import org.eclipse.ui.PlatformUI;
18-
import org.eclipse.ui.part.ISetSelectionTarget;
19-
import org.eclipse.ui.progress.UIJob;
20-
215
import ts.eclipse.ide.terminal.interpreter.ICommandInterpreter;
226
import ts.eclipse.ide.terminal.interpreter.ICommandInterpreterFactory;
237

24-
public class NpmCommandInterpreterFactory implements ICommandInterpreter, ICommandInterpreterFactory {
25-
26-
@Override
27-
public void process(List<String> parameters, String workingDir) {
28-
if (!parameters.contains("install")) {
29-
return;
30-
}
31-
final IContainer[] c = ResourcesPlugin.getWorkspace().getRoot().findContainersForLocation(new Path(workingDir));
32-
if (c != null && c.length > 0) {
33-
final IContainer container = c[0];
34-
new UIJob("Refresh npm project") {
35-
36-
@Override
37-
public IStatus runInUIThread(IProgressMonitor monitor) {
38-
try {
39-
container.refreshLocal(IResource.DEPTH_INFINITE, monitor);
40-
if (container.exists(new Path("node_modules"))) {
41-
IWorkbenchPage page = PlatformUI.getWorkbench().getWorkbenchWindows()[0].getActivePage();
42-
final IViewPart view = page.findView(IPageLayout.ID_PROJECT_EXPLORER);
43-
((ISetSelectionTarget) view).selectReveal(
44-
new StructuredSelection(container.getFolder(new Path("node_modules"))));
45-
}
46-
} catch (CoreException e) {
47-
// TODO Auto-generated catch block
48-
e.printStackTrace();
49-
}
50-
return Status.OK_STATUS;
51-
}
52-
}.schedule();
53-
}
54-
}
8+
public class NpmCommandInterpreterFactory implements ICommandInterpreterFactory {
559

56-
@Override
57-
public void addLine(String line) {
58-
// Do nothing
59-
}
10+
private static final ICommandInterpreter NPM_INSTALL_INTERPRETER = new NpmInstallCommandInterpreter();
6011

6112
@Override
62-
public ICommandInterpreter create() {
63-
return this;
13+
public ICommandInterpreter create(List<String> parameters) {
14+
if (parameters.contains("install")) {
15+
return NPM_INSTALL_INTERPRETER;
16+
}
17+
return null;
6418
}
6519

6620
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package ts.eclipse.ide.terminal.interpreter.npm.internal.commands;
2+
3+
import java.util.List;
4+
5+
import org.eclipse.core.resources.IContainer;
6+
import org.eclipse.core.resources.IResource;
7+
import org.eclipse.core.resources.ResourcesPlugin;
8+
import org.eclipse.core.runtime.CoreException;
9+
import org.eclipse.core.runtime.IProgressMonitor;
10+
import org.eclipse.core.runtime.IStatus;
11+
import org.eclipse.core.runtime.Path;
12+
import org.eclipse.core.runtime.Status;
13+
import org.eclipse.jface.viewers.StructuredSelection;
14+
import org.eclipse.ui.IPageLayout;
15+
import org.eclipse.ui.IViewPart;
16+
import org.eclipse.ui.IWorkbenchPage;
17+
import org.eclipse.ui.PlatformUI;
18+
import org.eclipse.ui.part.ISetSelectionTarget;
19+
import org.eclipse.ui.progress.UIJob;
20+
21+
import ts.eclipse.ide.terminal.interpreter.ICommandInterpreter;
22+
23+
public class NpmInstallCommandInterpreter implements ICommandInterpreter {
24+
25+
@Override
26+
public void execute(List<String> parameters, String workingDir) {
27+
final IContainer[] c = ResourcesPlugin.getWorkspace().getRoot().findContainersForLocation(new Path(workingDir));
28+
if (c != null && c.length > 0) {
29+
final IContainer container = c[0];
30+
new UIJob("Refresh npm project") {
31+
32+
@Override
33+
public IStatus runInUIThread(IProgressMonitor monitor) {
34+
try {
35+
container.refreshLocal(IResource.DEPTH_INFINITE, monitor);
36+
if (container.exists(new Path("node_modules"))) {
37+
IWorkbenchPage page = PlatformUI.getWorkbench().getWorkbenchWindows()[0].getActivePage();
38+
final IViewPart view = page.findView(IPageLayout.ID_PROJECT_EXPLORER);
39+
((ISetSelectionTarget) view).selectReveal(
40+
new StructuredSelection(container.getFolder(new Path("node_modules"))));
41+
}
42+
} catch (CoreException e) {
43+
// TODO Auto-generated catch block
44+
e.printStackTrace();
45+
}
46+
return Status.OK_STATUS;
47+
}
48+
}.schedule();
49+
}
50+
}
51+
52+
@Override
53+
public void addLine(String line) {
54+
// Do nothing
55+
}
56+
57+
}

eclipse/terminal/ts.eclipse.ide.terminal.interpreter/src/ts/eclipse/ide/terminal/interpreter/ICommandInterpreter.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@
44

55
public interface ICommandInterpreter {
66

7-
void process(List<String> parameters, String workingDir);
7+
/**
8+
* Execute the command interpreter with the given parameters and working
9+
* directory.
10+
*
11+
* @param parameters
12+
* @param workingDir
13+
*/
14+
void execute(List<String> parameters, String workingDir);
815

916
void addLine(String line);
1017

Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package ts.eclipse.ide.terminal.interpreter;
22

3+
import java.util.List;
4+
35
public interface ICommandInterpreterFactory {
46

5-
ICommandInterpreter create();
7+
ICommandInterpreter create(List<String> parameters);
68

79
}

eclipse/terminal/ts.eclipse.ide.terminal.interpreter/src/ts/eclipse/ide/terminal/interpreter/internal/CommandInterpreterManager.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
/**
2+
* Copyright (c) 2015-2016 Angelo ZERR.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Angelo Zerr <[email protected]> - initial API and implementation
10+
*/
111
package ts.eclipse.ide.terminal.interpreter.internal;
212

313
import java.util.HashMap;
@@ -24,13 +34,9 @@ public static CommandInterpreterManager getInstance() {
2434
return INSTANCE;
2535
}
2636

27-
public ICommandInterpreter getCommand(String cmd) {
37+
public ICommandInterpreterFactory getFactory(String cmd) {
2838
populateCache();
29-
ICommandInterpreterFactory factory = factories.get(cmd.toUpperCase());
30-
if (factory != null) {
31-
return factory.create();
32-
}
33-
return null;
39+
return factories.get(cmd.toUpperCase());
3440
}
3541

3642
/**

eclipse/terminal/ts.eclipse.ide.terminal.interpreter/src/ts/eclipse/ide/terminal/interpreter/internal/CommandInterpreterProcessor.java

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
/**
2+
* Copyright (c) 2015-2016 Angelo ZERR.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Angelo Zerr <[email protected]> - initial API and implementation
10+
*/
111
package ts.eclipse.ide.terminal.interpreter.internal;
212

313
import java.io.File;
@@ -10,13 +20,14 @@
1020
import org.eclipse.tm.terminal.view.core.interfaces.constants.ITerminalsConnectorConstants;
1121

1222
import ts.eclipse.ide.terminal.interpreter.ICommandInterpreter;
23+
import ts.eclipse.ide.terminal.interpreter.ICommandInterpreterFactory;
1324

1425
public class CommandInterpreterProcessor implements ITerminalServiceOutputStreamMonitorListener {
1526

1627
private final ICommandInterpreter NULL_INTERPRETER = new ICommandInterpreter() {
1728

1829
@Override
19-
public void process(java.util.List<String> parameters, String workingDir) {
30+
public void execute(List<String> parameters, String workingDir) {
2031

2132
}
2233

@@ -38,6 +49,7 @@ public void addLine(String line) {
3849

3950
private String cmd;
4051
private String cmdWithParameters;
52+
private List<String> cmdParameters;
4153
private String workingDirEnd;
4254

4355
public CommandInterpreterProcessor(Map<String, Object> properties) {
@@ -59,7 +71,11 @@ public void onContentReadFromStream(byte[] byteBuffer, int bytesRead) {
5971
} else {
6072
// Initialize interpreter if needed
6173
if (interpreter == null) {
62-
interpreter = CommandInterpreterManager.getInstance().getCommand(cmd);
74+
ICommandInterpreterFactory factory = CommandInterpreterManager.getInstance().getFactory(cmd);
75+
if (factory != null) {
76+
cmdParameters = getParameters(cmdWithParameters);
77+
interpreter = factory.create(cmdParameters);
78+
}
6379
if (interpreter == null) {
6480
interpreter = NULL_INTERPRETER;
6581
}
@@ -78,23 +94,6 @@ public void onContentReadFromStream(byte[] byteBuffer, int bytesRead) {
7894
interpreter.addLine(line);
7995
}
8096
}
81-
//
82-
// if (lineCmd.contains("cd") || lineCmd.contains("chdir"))
83-
// {
84-
// queryCursorPosition = false;
85-
// System.err.println("Finish!" + lineCmd);
86-
// lineCmd = null;
87-
// workingDir = lines.getLastLine();
88-
// } else if (workingDir.equals(lines.getLastLine())) {
89-
// //
90-
// queryCursorPosition = false;
91-
// System.err.println("Finish!" + lineCmd);
92-
// lineCmd = null;
93-
// } else {
94-
// for (String line : lines.getLines()) {
95-
// System.err.println("Add to " + lineCmd + ": " + line);
96-
// }
97-
// }
9897
}
9998
} else {
10099
// Terminal was opened, get the last lines which is the working
@@ -147,11 +146,12 @@ private boolean isEndCommand(String line) {
147146
private void endCommand() {
148147
if (interpreter != null) {
149148
String dir = getWorkingDir(workingDir);
150-
interpreter.process(getParameters(cmdWithParameters), dir);
149+
interpreter.execute(cmdParameters, dir);
151150
}
152151
processingCommand = false;
153152
cmd = null;
154153
cmdWithParameters = null;
154+
cmdParameters = null;
155155
interpreter = null;
156156
}
157157

@@ -205,6 +205,11 @@ private String getCmd(String cmdWithParameters) {
205205
return cmdWithParameters;
206206
}
207207

208+
/**
209+
* Returns the encoding of the terminal.
210+
*
211+
* @return the encoding of the terminal.
212+
*/
208213
private String getEncoding() {
209214
if (encoding != null) {
210215
return encoding;
@@ -213,6 +218,11 @@ private String getEncoding() {
213218
return encoding;
214219
}
215220

221+
/**
222+
* Returns the original working directory of the terminal.
223+
*
224+
* @return the original working directory of the terminal.
225+
*/
216226
private String getOriginalWorkingDir() {
217227
if (originalWorkingDir != null) {
218228
return originalWorkingDir;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package ts.eclipse.ide.terminal.interpreter.internal.commands;
2+
3+
import java.util.List;
4+
5+
import org.eclipse.core.resources.IContainer;
6+
import org.eclipse.core.resources.ResourcesPlugin;
7+
import org.eclipse.core.runtime.Path;
8+
import org.eclipse.jface.viewers.StructuredSelection;
9+
import org.eclipse.swt.widgets.Display;
10+
import org.eclipse.ui.IPageLayout;
11+
import org.eclipse.ui.IViewPart;
12+
import org.eclipse.ui.IWorkbenchPage;
13+
import org.eclipse.ui.PlatformUI;
14+
import org.eclipse.ui.part.ISetSelectionTarget;
15+
16+
import ts.eclipse.ide.terminal.interpreter.ICommandInterpreter;
17+
18+
public class CdCommandInterpreter implements ICommandInterpreter {
19+
20+
@Override
21+
public void execute(List<String> parameters, String workingDir) {
22+
if (parameters.size() < 1) {
23+
return;
24+
}
25+
String path = parameters.get(0);
26+
try {
27+
final IContainer[] c = ResourcesPlugin.getWorkspace().getRoot()
28+
.findContainersForLocation(new Path(workingDir + "/" + path));
29+
if (c != null && c.length > 0) {
30+
IWorkbenchPage page = PlatformUI.getWorkbench().getWorkbenchWindows()[0].getActivePage();
31+
final IViewPart view = page.findView(IPageLayout.ID_PROJECT_EXPLORER);
32+
33+
Display.getDefault().syncExec(new Runnable() {
34+
35+
@Override
36+
public void run() {
37+
((ISetSelectionTarget) view).selectReveal(new StructuredSelection(c));
38+
}
39+
});
40+
}
41+
} catch (Throwable e) {
42+
e.printStackTrace();
43+
}
44+
}
45+
46+
@Override
47+
public void addLine(String line) {
48+
// Do nothing
49+
}
50+
51+
}

0 commit comments

Comments
 (0)