Skip to content

Commit b735488

Browse files
author
angelozerr
committed
Clean API for command interpreter. See
angelozerr/angular-eclipse#4
1 parent 1b89e7b commit b735488

File tree

10 files changed

+175
-110
lines changed

10 files changed

+175
-110
lines changed

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,11 @@
77

88
public class NpmCommandInterpreterFactory implements ICommandInterpreterFactory {
99

10-
private static final ICommandInterpreter NPM_INSTALL_INTERPRETER = new NpmInstallCommandInterpreter();
11-
1210
@Override
13-
public ICommandInterpreter create(List<String> parameters) {
11+
public ICommandInterpreter create(List<String> parameters, String workingDir) {
1412
if (parameters.contains("install")) {
15-
return NPM_INSTALL_INTERPRETER;
13+
return new NpmInstallCommandInterpreter(parameters, workingDir);
1614
}
1715
return null;
1816
}
19-
2017
}

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

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
import java.util.List;
44

55
import org.eclipse.core.resources.IContainer;
6-
import org.eclipse.core.resources.IResource;
76
import org.eclipse.core.resources.ResourcesPlugin;
8-
import org.eclipse.core.runtime.CoreException;
97
import org.eclipse.core.runtime.IProgressMonitor;
108
import org.eclipse.core.runtime.IStatus;
119
import org.eclipse.core.runtime.Path;
@@ -18,9 +16,13 @@
1816
import org.eclipse.ui.part.ISetSelectionTarget;
1917
import org.eclipse.ui.progress.UIJob;
2018

21-
import ts.eclipse.ide.terminal.interpreter.ICommandInterpreter;
19+
import ts.eclipse.ide.terminal.interpreter.AbstractCommandInterpreter;
2220

23-
public class NpmInstallCommandInterpreter implements ICommandInterpreter {
21+
public class NpmInstallCommandInterpreter extends AbstractCommandInterpreter {
22+
23+
public NpmInstallCommandInterpreter(List<String> parameters, String workingDir) {
24+
super(parameters, workingDir);
25+
}
2426

2527
@Override
2628
public void execute(List<String> parameters, String workingDir) {
@@ -31,27 +33,23 @@ public void execute(List<String> parameters, String workingDir) {
3133

3234
@Override
3335
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();
36+
// try {
37+
// container.refreshLocal(IResource.DEPTH_INFINITE,
38+
// monitor);
39+
if (container.exists(new Path("node_modules"))) {
40+
IWorkbenchPage page = PlatformUI.getWorkbench().getWorkbenchWindows()[0].getActivePage();
41+
final IViewPart view = page.findView(IPageLayout.ID_PROJECT_EXPLORER);
42+
((ISetSelectionTarget) view)
43+
.selectReveal(new StructuredSelection(container.getFolder(new Path("node_modules"))));
4544
}
45+
// } catch (CoreException e) {
46+
// // TODO Auto-generated catch block
47+
// e.printStackTrace();
48+
// }
4649
return Status.OK_STATUS;
4750
}
4851
}.schedule();
4952
}
5053
}
5154

52-
@Override
53-
public void addLine(String line) {
54-
// Do nothing
55-
}
56-
5755
}

eclipse/terminal/ts.eclipse.ide.terminal.interpreter/plugin.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@
2626
</delegate>
2727
</extension>
2828

29-
<extension
29+
<!--<extension
3030
point="ts.eclipse.ide.terminal.interpreter.commandInterpreterFactories">
3131
<factory
3232
id="ts.eclipse.ide.terminal.interpreter.commands.CdCommandInterpreterFactory"
3333
name="%interpreter.cd.name"
34-
commands="cd,chdir"
34+
commands="#"
3535
class="ts.eclipse.ide.terminal.interpreter.internal.commands.CdCommandInterpreterFactory">
3636
</factory>
37-
</extension>
37+
</extension>
38+
-->
3839

3940
</plugin>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
*/
11+
package ts.eclipse.ide.terminal.interpreter;
12+
13+
import java.util.List;
14+
15+
/**
16+
* Abstract class for {@link ICommandInterpreter}.
17+
*
18+
*/
19+
public abstract class AbstractCommandInterpreter implements ICommandInterpreter {
20+
21+
private final List<String> parameters;
22+
private final String workingDir;
23+
24+
public AbstractCommandInterpreter(List<String> parameters, String workingDir) {
25+
this.parameters = parameters;
26+
this.workingDir = workingDir;
27+
}
28+
29+
@Override
30+
public void execute() {
31+
execute(parameters, workingDir);
32+
}
33+
34+
@Override
35+
public void onTrace(String line) {
36+
// Do nothing
37+
}
38+
39+
/**
40+
* Execute the command interpreter with the given parameters and working
41+
* directory.
42+
*
43+
* @param parameters
44+
* @param workingDir
45+
*/
46+
protected abstract void execute(List<String> parameters, String workingDir);
47+
48+
}
Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,31 @@
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;
212

3-
import java.util.List;
4-
13+
/**
14+
* Command interpreter API.
15+
*
16+
*/
517
public interface ICommandInterpreter {
618

719
/**
8-
* Execute the command interpreter with the given parameters and working
9-
* directory.
10-
*
11-
* @param parameters
12-
* @param workingDir
20+
* Execute the command.
1321
*/
14-
void execute(List<String> parameters, String workingDir);
22+
void execute();
1523

16-
void addLine(String line);
24+
/**
25+
* Call when apply of command trace some logs in the shell.
26+
*
27+
* @param trace
28+
*/
29+
void onTrace(String trace);
1730

1831
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,34 @@
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;
212

313
import java.util.List;
414

15+
/**
16+
* Command interpreter factory API.
17+
*
18+
*/
519
public interface ICommandInterpreterFactory {
620

7-
ICommandInterpreter create(List<String> parameters);
21+
/**
22+
* Create a command interpreter according the given parameters and null
23+
* otherwise.
24+
*
25+
* @param parameters
26+
* of the command.
27+
* @param workingDir
28+
* working directory where command is executed.
29+
* @return a command interpreter according the given parameters and null
30+
* otherwise.
31+
*/
32+
ICommandInterpreter create(List<String> parameters, String workingDir);
833

934
}

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

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,26 @@
2121

2222
import ts.eclipse.ide.terminal.interpreter.ICommandInterpreter;
2323
import ts.eclipse.ide.terminal.interpreter.ICommandInterpreterFactory;
24+
import ts.eclipse.ide.terminal.interpreter.internal.commands.CdCommandInterpreter;
2425

2526
public class CommandInterpreterProcessor implements ITerminalServiceOutputStreamMonitorListener {
2627

2728
private final ICommandInterpreter NULL_INTERPRETER = new ICommandInterpreter() {
2829

2930
@Override
30-
public void execute(List<String> parameters, String workingDir) {
31+
public void execute() {
3132

3233
}
3334

3435
@Override
35-
public void addLine(String line) {
36+
public void onTrace(String line) {
3637

3738
}
3839
};
3940
private final Map<String, Object> properties;
4041
private ICommandInterpreter interpreter;
4142

42-
private String workingDir;
43+
private String lineInput;
4344
private String encoding;
4445
private String originalWorkingDir;
4546

@@ -49,7 +50,6 @@ public void addLine(String line) {
4950

5051
private String cmd;
5152
private String cmdWithParameters;
52-
private List<String> cmdParameters;
5353
private String workingDirEnd;
5454

5555
public CommandInterpreterProcessor(Map<String, Object> properties) {
@@ -73,8 +73,9 @@ public void onContentReadFromStream(byte[] byteBuffer, int bytesRead) {
7373
if (interpreter == null) {
7474
ICommandInterpreterFactory factory = CommandInterpreterManager.getInstance().getFactory(cmd);
7575
if (factory != null) {
76-
cmdParameters = getParameters(cmdWithParameters);
77-
interpreter = factory.create(cmdParameters);
76+
String workingDir = getWorkingDir(lineInput);
77+
List<String> parameters = getParameters(cmdWithParameters);
78+
interpreter = factory.create(parameters, workingDir);
7879
}
7980
if (interpreter == null) {
8081
interpreter = NULL_INTERPRETER;
@@ -83,27 +84,33 @@ public void onContentReadFromStream(byte[] byteBuffer, int bytesRead) {
8384

8485
List<String> l = lines.getLines();
8586
String lastLine = lines.getLastLine();
86-
if (isEndCommand(lastLine)) {
87+
if (isEndCommand(lastLine)) {
8788
for (int i = 0; i < l.size() - 1; i++) {
88-
interpreter.addLine(l.get(i));
89+
interpreter.onTrace(l.get(i));
8990
}
90-
endCommand();
91-
this.workingDir = lastLine;
91+
boolean workingDirChanged = !this.lineInput.equals(lastLine);
92+
if (workingDirChanged) {
93+
String workingDir = getWorkingDir(lineInput);
94+
List<String> parameters = getParameters(cmdWithParameters);
95+
new CdCommandInterpreter(parameters, workingDir).execute();
96+
}
97+
endCommand();
98+
this.lineInput = lastLine;
9299
} else {
93100
for (String line : lines.getLines()) {
94-
interpreter.addLine(line);
101+
interpreter.onTrace(line);
95102
}
96103
}
97104
}
98105
} else {
99106
// Terminal was opened, get the last lines which is the working
100107
// dir.
101-
if (workingDir == null) {
102-
this.workingDir = lines.getLastLine();
103-
if (workingDir != null) {
108+
if (lineInput == null) {
109+
this.lineInput = lines.getLastLine();
110+
if (lineInput != null) {
104111
String originalWorkingDir = getOriginalWorkingDir();
105-
if (workingDir.startsWith(originalWorkingDir)) {
106-
this.workingDirEnd = workingDir.substring(originalWorkingDir.length(), workingDir.length());
112+
if (lineInput.startsWith(originalWorkingDir)) {
113+
this.workingDirEnd = lineInput.substring(originalWorkingDir.length(), lineInput.length());
107114
}
108115
}
109116
} else {
@@ -114,7 +121,7 @@ public void onContentReadFromStream(byte[] byteBuffer, int bytesRead) {
114121
// with parameters
115122
// ex: "C:\User>cd a"
116123
// get the command and their parameters
117-
this.cmdWithParameters = lineCmd.substring(workingDir.length(), lineCmd.length()).trim();
124+
this.cmdWithParameters = lineCmd.substring(lineInput.length(), lineCmd.length()).trim();
118125
// here cmdWithParameters is equals to "cd a"
119126
// get the first token to retrieve the command (ex:
120127
// "cd")
@@ -129,7 +136,7 @@ private boolean isEndCommand(String line) {
129136
if (line == null) {
130137
return false;
131138
}
132-
if (workingDir.equals(line)) {
139+
if (lineInput.equals(line)) {
133140
return true;
134141
}
135142
if (!line.endsWith(workingDirEnd)) {
@@ -145,13 +152,11 @@ private boolean isEndCommand(String line) {
145152

146153
private void endCommand() {
147154
if (interpreter != null) {
148-
String dir = getWorkingDir(workingDir);
149-
interpreter.execute(cmdParameters, dir);
155+
interpreter.execute();
150156
}
151157
processingCommand = false;
152158
cmd = null;
153159
cmdWithParameters = null;
154-
cmdParameters = null;
155160
interpreter = null;
156161
}
157162

0 commit comments

Comments
 (0)