|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 package com.nokia.s60tools.imaker.internal.console; |
|
20 |
|
21 import java.io.File; |
|
22 import java.io.IOException; |
|
23 import java.io.PipedInputStream; |
|
24 import java.io.PipedOutputStream; |
|
25 import java.util.List; |
|
26 import java.util.regex.Matcher; |
|
27 import java.util.regex.Pattern; |
|
28 |
|
29 import org.eclipse.core.runtime.IProgressMonitor; |
|
30 import org.eclipse.core.runtime.IStatus; |
|
31 import org.eclipse.core.runtime.Status; |
|
32 import org.eclipse.core.runtime.jobs.Job; |
|
33 import org.eclipse.jface.text.BadLocationException; |
|
34 import org.eclipse.jface.text.IDocument; |
|
35 import org.eclipse.swt.program.Program; |
|
36 import org.eclipse.ui.console.IHyperlink; |
|
37 import org.eclipse.ui.console.MessageConsole; |
|
38 import org.eclipse.ui.console.MessageConsoleStream; |
|
39 |
|
40 import com.nokia.s60tools.imaker.IIMakerWrapper; |
|
41 import com.nokia.s60tools.imaker.IMakerConsoleLogger; |
|
42 import com.nokia.s60tools.imaker.IMakerKeyConstants; |
|
43 import com.nokia.s60tools.imaker.IMakerUtils; |
|
44 import com.nokia.s60tools.imaker.Messages; |
|
45 import com.nokia.s60tools.imaker.StatusHandler; |
|
46 import com.nokia.s60tools.imaker.exceptions.IMakerCoreAlreadyRunningException; |
|
47 import com.nokia.s60tools.imaker.exceptions.IMakerCoreExecutionException; |
|
48 import com.nokia.s60tools.imaker.exceptions.IMakerCoreNotFoundException; |
|
49 |
|
50 public class IMakerJob extends Job { |
|
51 private List<String> command; |
|
52 public volatile boolean done = false; |
|
53 private IIMakerWrapper wrapper; |
|
54 private String epocroot; |
|
55 |
|
56 public static final int DEFAULT_THREAD_SLEEP_TIME = 20; |
|
57 public static final String BUILD_START_MESSAGE = Messages.getString("ImageBuilder.0"); //$NON-NLS-1$ |
|
58 public static final String BUILD_CMD = Messages.getString("ImageBuilder.1"); //$NON-NLS-1$ |
|
59 public static final String BUILD_SUCCESS_MESSAGE = Messages.getString("ImageBuilder.2"); //$NON-NLS-1$ |
|
60 public static final String BUILD_FAILURE_MESSAGE = Messages.getString("ImageBuilder.3"); //$NON-NLS-1$ |
|
61 |
|
62 public static final Pattern VARIABLE_PATTERN1 = Pattern.compile(".*\\s*=\\s*.([\\\\/].*)."); |
|
63 public static final Pattern VARIABLE_PATTERN2 = Pattern.compile(".*\\s*=\\s*.(.:.*)."); |
|
64 |
|
65 public IMakerJob(String name, List<String> cmd, IIMakerWrapper wrapper) { |
|
66 super(name); |
|
67 this.command = cmd; |
|
68 this.wrapper = wrapper; |
|
69 this.epocroot = IMakerUtils.getLocationDrive(wrapper.getTool().get(0)); |
|
70 } |
|
71 |
|
72 @Override |
|
73 protected IStatus run(IProgressMonitor monitor) { |
|
74 monitor.beginTask(Messages.getString("ImageBuilder.4"), IProgressMonitor.UNKNOWN); |
|
75 IMakerConsole console = IMakerConsole.getDefault(); |
|
76 console.clear(); |
|
77 MessageConsoleStream infoStream = console.getNewMessageConsoleStream(IMakerConsole.MSG_INFORMATION); |
|
78 MessageConsoleStream errorStream = console.getNewMessageConsoleStream(IMakerConsole.MSG_ERROR); |
|
79 |
|
80 //print command |
|
81 infoStream.println(BUILD_START_MESSAGE); |
|
82 infoStream.print(BUILD_CMD); |
|
83 infoStream.println(wrapper.getBuildCommand(command)); |
|
84 |
|
85 try { |
|
86 PipedInputStream pin = new PipedInputStream(); |
|
87 PipedOutputStream pout = new PipedOutputStream(pin); |
|
88 |
|
89 IMakerConsoleLogger logger = new IMakerConsoleLogger(pin); |
|
90 logger.start(); |
|
91 |
|
92 boolean success = false; |
|
93 success = wrapper.buildImage(command,pout); |
|
94 |
|
95 // Stop the thread. |
|
96 logger.done = true; |
|
97 if (success) { |
|
98 infoStream.println(BUILD_SUCCESS_MESSAGE); |
|
99 } else { |
|
100 errorStream.println(BUILD_FAILURE_MESSAGE); |
|
101 } |
|
102 |
|
103 pout.close(); |
|
104 pin.close(); |
|
105 infoStream.close(); |
|
106 errorStream.close(); |
|
107 Thread.sleep(IMakerKeyConstants.DEFAULT_THREAD_SLEEP_TIME); |
|
108 highlightSummary(console.getMessageConsole()); |
|
109 monitor.done(); |
|
110 } catch (IOException e1) { |
|
111 e1.printStackTrace(); |
|
112 } catch (IMakerCoreNotFoundException e) { |
|
113 StatusHandler.handle(IStatus.ERROR,Messages.getString("Error.2"),e); |
|
114 } catch (IMakerCoreExecutionException e) { |
|
115 StatusHandler.handle(IStatus.ERROR,Messages.getString("Error.1"),e); |
|
116 e.printStackTrace(); |
|
117 } catch (IMakerCoreAlreadyRunningException e) { |
|
118 StatusHandler.handle(IStatus.WARNING,Messages.getString("Flashmenu.13"),e); |
|
119 e.printStackTrace(); |
|
120 } catch (InterruptedException e) { |
|
121 e.printStackTrace(); |
|
122 } |
|
123 return Status.OK_STATUS; |
|
124 } |
|
125 |
|
126 private void highlightSummary(MessageConsole console) { |
|
127 IDocument doc = console.getDocument(); |
|
128 int numberOfLines = doc.getNumberOfLines(); |
|
129 boolean begin = true; |
|
130 int startLine = 0; |
|
131 int endLine = 0; |
|
132 for (int i = numberOfLines-1; i >= 0; i--) { |
|
133 try { |
|
134 String line = doc.get(doc.getLineOffset(i), doc.getLineLength(i)); |
|
135 if(isSummaryDelimeter(line)) { |
|
136 if(!begin) { |
|
137 endLine = i; |
|
138 break; |
|
139 } else { |
|
140 startLine = i; |
|
141 begin = false; |
|
142 } |
|
143 } |
|
144 } catch (BadLocationException e) { |
|
145 e.printStackTrace(); |
|
146 } |
|
147 } |
|
148 |
|
149 for (int i = startLine-1; i > endLine; i--) { |
|
150 highlightLine(doc,i,console); |
|
151 } |
|
152 } |
|
153 |
|
154 |
|
155 private void highlightLine(IDocument doc, int line, MessageConsole console) { |
|
156 try { |
|
157 int lineOffset = doc.getLineOffset(line); |
|
158 String content = doc.get(lineOffset, doc.getLineLength(line)); |
|
159 Matcher matcher1 = VARIABLE_PATTERN1.matcher(content); |
|
160 Matcher matcher2 = VARIABLE_PATTERN2.matcher(content); |
|
161 if(matcher1.find()) { |
|
162 int start = matcher1.start(1); |
|
163 int end = matcher1.end(1); |
|
164 makeLink(epocroot,doc, console, lineOffset, start, end); |
|
165 } else if(matcher2.find()) { |
|
166 int start = matcher2.start(1); |
|
167 int end = matcher2.end(1); |
|
168 makeLink("",doc, console, lineOffset, start, end); |
|
169 } |
|
170 } catch (BadLocationException e) { |
|
171 e.printStackTrace(); |
|
172 } |
|
173 } |
|
174 |
|
175 private void makeLink(String root, IDocument doc, MessageConsole console, |
|
176 int lineOffset, int start, int end) throws BadLocationException { |
|
177 int length = end-start; |
|
178 String link = doc.get(lineOffset+start, length); |
|
179 console.addHyperlink(new Link(root,link), lineOffset+start, length); |
|
180 } |
|
181 |
|
182 private boolean isSummaryDelimeter(String line) { |
|
183 return line.startsWith(IMakerKeyConstants.SUMMARY_DELIMETER); |
|
184 } |
|
185 |
|
186 private class Link implements IHyperlink { |
|
187 |
|
188 private String link; |
|
189 private String eroot; |
|
190 |
|
191 public Link(String eroot, String link) { |
|
192 this.link = link; |
|
193 this.eroot = eroot; |
|
194 } |
|
195 // @Override |
|
196 public void linkActivated() { |
|
197 String path = eroot+link; |
|
198 File f = new File(path); |
|
199 while(f!=null && !f.exists()) { |
|
200 f=f.getParentFile(); |
|
201 } |
|
202 if(f!=null) { |
|
203 if(f.isFile()) { |
|
204 f = f.getParentFile(); |
|
205 } |
|
206 Program.launch(f.getPath()); |
|
207 } |
|
208 } |
|
209 |
|
210 // @Override |
|
211 public void linkEntered() {} |
|
212 |
|
213 // @Override |
|
214 public void linkExited() {} |
|
215 |
|
216 } |
|
217 } |