X Tutup
Skip to content

Commit 87a827b

Browse files
author
sebastigurin
committed
1 parent a690231 commit 87a827b

File tree

6 files changed

+1100
-0
lines changed

6 files changed

+1100
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package net.sf.j2s.ui.cmdline;
2+
3+
import org.eclipse.ui.plugin.AbstractUIPlugin;
4+
import org.osgi.framework.BundleContext;
5+
6+
/**
7+
* The activator class controls the plug-in life cycle
8+
* @author sgurin
9+
*/
10+
public class Activator extends AbstractUIPlugin {
11+
12+
// The plug-in ID
13+
public static final String PLUGIN_ID = "net.sf.j2s.ui.cmdline"; //$NON-NLS-1$
14+
15+
// The shared instance
16+
private static Activator plugin;
17+
18+
/**
19+
* The constructor
20+
*/
21+
public Activator() {
22+
}
23+
24+
/*
25+
* (non-Javadoc)
26+
* @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
27+
*/
28+
public void start(BundleContext context) throws Exception {
29+
super.start(context);
30+
plugin = this;
31+
}
32+
33+
/*
34+
* (non-Javadoc)
35+
* @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
36+
*/
37+
public void stop(BundleContext context) throws Exception {
38+
plugin = null;
39+
super.stop(context);
40+
}
41+
42+
/**
43+
* Returns the shared instance
44+
*
45+
* @return the shared instance
46+
*/
47+
public static Activator getDefault() {
48+
return plugin;
49+
}
50+
51+
}
Lines changed: 309 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,309 @@
1+
package net.sf.j2s.ui.cmdline;
2+
3+
import java.io.IOException;
4+
import java.util.HashMap;
5+
import java.util.Iterator;
6+
import java.util.LinkedList;
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
import org.eclipse.core.resources.IProject;
11+
import org.eclipse.core.resources.ResourcesPlugin;
12+
import org.eclipse.core.runtime.CoreException;
13+
import org.eclipse.equinox.app.IApplication;
14+
import org.eclipse.equinox.app.IApplicationContext;
15+
import org.eclipse.jdt.core.JavaModelException;
16+
/**
17+
* command line format definition for j2s cmdline api.
18+
*
19+
* ./eclipse -nosplash -application net.sf.j2s.ui.cmdlineApi GENERAL_PARAMETERS COMMAND_LIST_PARAMETER
20+
*
21+
* please read doc/j2s-cmdline-api.pdf for information about this eclipse application
22+
*
23+
* @author sgurin
24+
*TODO: support for reading command list options from file with an especial parameter
25+
*--read-from-file=/pàth/to/options/file
26+
*/
27+
public class Application implements IApplication {
28+
29+
private static final int STATUS_ERROR = 1, STATUS_OK=IApplication.EXIT_OK;
30+
31+
Map<String, String> generalParam;
32+
List<Command> commands;
33+
IApplicationContext appContext;
34+
private String[] args;
35+
private int firstCmdIdx;
36+
Map<String, IProject> projectNames;
37+
List<IProject> importedProjects;
38+
List<IProject> buildedProjects;
39+
40+
@Override
41+
public Object start(IApplicationContext context) throws Exception {
42+
this.appContext=context;
43+
projectNames=new HashMap<String, IProject>();
44+
importedProjects = new LinkedList<IProject>();
45+
buildedProjects = new LinkedList<IProject>();
46+
args = (String[])appContext.getArguments().get("application.args");
47+
48+
//test only
49+
doDebugInitArgs();
50+
51+
try {
52+
doTheJobWith();
53+
return STATUS_OK;
54+
} catch (Exception e) {
55+
System.out.println("net.sf.j2s.ui.cmdline Main Exception");
56+
e.printStackTrace();
57+
return STATUS_ERROR;
58+
}
59+
}
60+
61+
62+
63+
@Override
64+
public void stop() {
65+
}
66+
67+
68+
private void doTheJobWith() throws Exception {
69+
firstCmdIdx = Utils.indexOfFirstOccurence(args, Command.CMD);
70+
71+
initGeneralOptions();
72+
initCommandListOptions();
73+
74+
Utils.debug(dumpParams());
75+
76+
dispatchCommands();
77+
78+
// removeAllProjects();
79+
}
80+
81+
// private void removeAllProjects() throws CoreException {
82+
// for (Iterator<IProject> iterator = importedProjects.iterator(); iterator.hasNext();) {
83+
// IProject proj = iterator.next();
84+
// JDTUtils.projectDelete(proj);
85+
// }
86+
// }
87+
88+
private void dispatchCommands() throws Exception {
89+
for (Iterator<Command> iterator = commands.iterator(); iterator.hasNext();) {
90+
Command c = iterator.next();
91+
try {
92+
dispatch(c);
93+
} catch (Exception e) {
94+
Utils.logError("Error: "+e.getMessage()+" while dispatching command "+c);
95+
e.printStackTrace();
96+
}
97+
98+
}
99+
}
100+
private void dispatch(Command c) throws Exception {
101+
Map<String, String> params = c.getParams();
102+
String cmd = params.get(Command.CMD);
103+
if(cmd==null) {
104+
Utils.logError("malformed command: "+c);
105+
return ;
106+
}
107+
else if(cmd.equals(Command.CMD_BUILD)) {
108+
doBuild(params);
109+
}
110+
else if(cmd.equals(Command.CMD_NEW_PROJECT)) {
111+
doSetSourceFolders(getProject(params.get(Command.PATH), params.get(Command.PROJECT_NAME)), params);
112+
}
113+
else if(cmd.equals(Command.CMD_RENDER)) {
114+
doRender(params);
115+
}
116+
else if(cmd.equals(Command.CMD_SET_CLASSPATH)) {
117+
doSetClasspath(getProject(params.get(Command.PATH), params.get(Command.PROJECT_NAME)), params);
118+
}
119+
else if(cmd.equals(Command.CMD_SET_SOURCE_FOLDERS)) {
120+
doSetSourceFolders(getProject(params.get(Command.PATH), params.get(Command.PROJECT_NAME)), params);
121+
}
122+
else if(cmd.equals(Command.CMD_SET_OUTPUT_FOLDER)) {
123+
IProject proj = getProject(params.get(Command.PATH), params.get(Command.PROJECT_NAME));
124+
JDTUtils.setOuputFolder(proj, params.get(Command.OUTPUT_FOLDER));
125+
}
126+
}
127+
128+
private void doSetClasspath(IProject project, Map<String, String> params) throws CoreException {
129+
String classPath = params.get(Command.CLASSPATH);
130+
//TODO: relative paths?
131+
//TODO: set != add
132+
JDTUtils.addToClassPath(project, classPath.split(Command.FILEPATH_SEPARATOR));
133+
}
134+
135+
private void doSetSourceFolders(IProject proj, Map<String, String> params) throws JavaModelException {
136+
String srcFolders1 = params.get(Command.SOURCE_FOLDERS);
137+
if(srcFolders1!=null)
138+
JDTUtils.setSourceFolders(proj, srcFolders1.split(Command.FILEPATH_SEPARATOR));
139+
}
140+
141+
private void doRender(Map<String, String> params) throws Exception {
142+
String path = params.get(Command.PATH),
143+
name = params.get(Command.PROJECT_NAME),
144+
mainFile = params.get(Command.MAIN_TYPE),
145+
outputName = params.get(Command.OUTPUT_NAME),
146+
templatePath = params.get(Command.TEMPLATE),
147+
j2slibUrl = params.get(Command.J2SLIBURL);
148+
149+
IProject project = getProject(path, name);
150+
if(!Utils.contains(buildedProjects, project))
151+
doBuild(params);
152+
153+
try {
154+
JDTUtils.renderJ2SApp(project, mainFile, j2slibUrl, templatePath, outputName);
155+
} catch (Exception e) {
156+
System.out.println("main render exception");
157+
e.printStackTrace();
158+
throw e;
159+
}
160+
161+
}
162+
163+
164+
/**
165+
* if project doens't exists first import it, then build it
166+
*/
167+
private void doBuild(Map<String, String> params) throws CoreException, IOException {
168+
String path = params.get(Command.PATH),
169+
name = params.get(Command.PROJECT_NAME);
170+
IProject project = getProject(path, name);
171+
if(project==null)
172+
Utils.logError("doBuild: project "+name+"/"+path+" - not found");
173+
try {
174+
JDTUtils.appendJ2SBuilderTo(project);
175+
JDTUtils.build(project, params);
176+
buildedProjects.add(project);
177+
} catch (Exception e) {
178+
Utils.logError("IO exception. are you sure "+path+"/"+name+" is a valid eclipse project path? ");
179+
e.printStackTrace();
180+
}
181+
}
182+
183+
/**
184+
* get a project by name and path. If the project was not already imported, import it
185+
*/
186+
private IProject getProject(String path, String name) throws CoreException, IOException {
187+
IProject project = null;
188+
189+
if(Utils.isNull(path) && !Utils.isNull(name)) { //project already opened?
190+
project = projectNames.get(name);
191+
}
192+
else if(!Utils.isNull(path)){
193+
project = JDTUtils.newProject(path, name);
194+
importedProjects.add(project);
195+
projectNames.put(name, project);
196+
}
197+
198+
if(project==null) {
199+
Utils.logError("project "+path+" "+name+" cannot be imported. interrupted");
200+
return null;
201+
}
202+
return project;
203+
}
204+
205+
206+
//command utilities
207+
private void initCommandListOptions() throws IOException {
208+
if(firstCmdIdx==-1) {
209+
Utils.logError("incorrect parameter list. at least one "+Command.CMD+" parameter is needed");
210+
Utils.exit(STATUS_ERROR);
211+
}
212+
commands=new LinkedList<Command>();
213+
int i = firstCmdIdx, nextCmdIdx=0;
214+
215+
while(nextCmdIdx<args.length && nextCmdIdx>=0) {
216+
nextCmdIdx = Utils.indexOf(args, Command.CMD, Math.min(i+1, args.length-1), args.length);
217+
if(nextCmdIdx==i)
218+
nextCmdIdx=args.length;
219+
String[] argFragment = (String[]) Utils.subArray(args, i, nextCmdIdx==-1?args.length:nextCmdIdx);
220+
221+
Command cmd = null;
222+
try {
223+
cmd=new Command(argFragment);
224+
} catch (Exception e) {
225+
Utils.logError("invalid command syntax: "+e.getMessage());
226+
e.printStackTrace();
227+
Utils.exit(STATUS_ERROR);
228+
}
229+
commands.add(cmd);
230+
System.out.println("builded command : "+cmd);
231+
i=nextCmdIdx;
232+
}
233+
}
234+
private void initGeneralOptions() throws IOException {
235+
if(firstCmdIdx==-1) {
236+
Utils.logError("incorrect parameter list. at least one 'cmd' parameter is needed");
237+
Utils.exit(STATUS_ERROR);
238+
}
239+
generalParam=new HashMap<String, String>();
240+
for (int i = 0; i < firstCmdIdx; i++) {
241+
String arg = args[i];
242+
String [] a = arg.split("=");
243+
if(a.length==2) { //if not it is an invalid j2s cmd line api parameter
244+
String name = a[0], value = a[1];
245+
generalParam.put(name, value);
246+
}
247+
else if(a.length>2)
248+
Utils.logError("invalid general command syntax. Values cannot contain '=' char like: "+arg);
249+
else
250+
Utils.logError("invalid general command syntax. arguments must be of the for foo=var at: "+arg);
251+
}
252+
253+
}
254+
255+
256+
257+
//debug utilities
258+
private String dumpParams() {
259+
String s = "General parameters: "+Utils.toString(generalParam)+"\n" +
260+
"Command list: \n";
261+
for (Iterator<Command> iterator = commands.iterator(); iterator.hasNext();) {
262+
s+="\n\t"+ iterator.next();
263+
}
264+
return s+"\nWorkspace: "+ResourcesPlugin.getWorkspace().getRoot().getLocation();
265+
}
266+
267+
/**
268+
* set working initial parameters for launching this application with eclipse plugin launcher instead from user cmd line.
269+
* @throws IOException
270+
* @throws CoreException
271+
*/
272+
private void doDebugInitArgs() throws CoreException, IOException {
273+
if(!Utils.debug)
274+
return;
275+
276+
//parameter examples:
277+
278+
//importing and building a non eclipse project
279+
args = new String[]{
280+
"-cmd", "newProject", "-projectName", "p1", "-path", "/home/sebastian/desarrollo/j2scmdlintests/eclipsestandalonetest1",
281+
"-cmd", "setOutputFolder", "-projectName", "p1", "-outputFolder", "javascriptOutput",
282+
"-cmd", "setSourceFolders", "-projectName", "p1", "-sourceFolders", "src",
283+
"-cmd", "build", "-projectName", "p1",
284+
};
285+
286+
287+
// importing and building a non eclipse project that uses swt
288+
args = new String[]{
289+
"-cmd", "newProject", "-projectName", "test", "-path", "/home/sebastian/desarrollo/j2scmdlintests/nonEclipseSWTProject",
290+
"-cmd", "setOutputFolder", "-projectName", "test", "-outputFolder", "javascriptOutput",
291+
"-cmd", "setSourceFolders", "-projectName", "test", "-sourceFolders", "src",
292+
"-cmd", "setClassPath", "-projectName", "test", "-classPath", "/home/sebastian/Desktop/downloads/swt33/swt-debug.jar",
293+
"-cmd", "build", "-projectName", "test",
294+
295+
//and finally render
296+
"-cmd", "render", "-projectName", "test", "-mainType", "foo.test1.NewSWTApp",
297+
"-j2slibUrl", "../j2slib", "-template", "/home/sebastian/desarrollo/j2scmdlintests/nonEclipseSWTProject/simpleHtmlDocument.vm", "-outputName","${mainType}.html"
298+
};
299+
300+
301+
302+
// args = new String[]{};
303+
// Tests.UtilsTest1();
304+
305+
// System.out.println("workspace: "+ResourcesPlugin.getWorkspace().getRoot().getLocation());
306+
// System.out.println("args: "+Utils.toString(args));
307+
308+
}
309+
}

0 commit comments

Comments
 (0)
X Tutup