imakerplugin/com.nokia.s60tools.imaker/src/com/nokia/s60tools/imaker/internal/wrapper/IMakerWrapper.java
changeset 1 7ff23301fe22
parent 0 61163b28edca
child 2 a91cb670dd8e
equal deleted inserted replaced
0:61163b28edca 1:7ff23301fe22
    76 	private boolean runVerdict;
    76 	private boolean runVerdict;
    77 	private String dProduct = null;
    77 	private String dProduct = null;
    78 //	private String dTarget = null;
    78 //	private String dTarget = null;
    79 	private Pattern blockBegin = Pattern.compile("-{30,}");
    79 	private Pattern blockBegin = Pattern.compile("-{30,}");
    80 
    80 
    81 	private OutputStream outputStream;
       
    82 	private List<String> tool;
    81 	private List<String> tool;
    83 	private IMakerCoreExecutionException lastImakerException;
    82 	private IMakerCoreExecutionException lastImakerException;
    84 
    83 
    85 	/**
    84 	/**
    86 	 * Default constructor
    85 	 * Default constructor
   100 	 */
    99 	 */
   101 	public String getIMakerCoreVersion() throws IMakerCoreNotFoundException, IMakerCoreExecutionException {
   100 	public String getIMakerCoreVersion() throws IMakerCoreNotFoundException, IMakerCoreExecutionException {
   102 		verifyExists();
   101 		verifyExists();
   103 		params.clear();
   102 		params.clear();
   104 		params.add(IMakerWrapperPreferences.CMD_FETCH_VERSION);
   103 		params.add(IMakerWrapperPreferences.CMD_FETCH_VERSION);
   105 		List<String> version = executeCommand(params);
   104 		List<String> version = executeCommand(params,null);
   106 		String vers = "";
   105 		String vers = "";
   107 		for (String line : version) {
   106 		for (String line : version) {
   108 			if(line.startsWith("iMaker")) {
   107 			if(line.startsWith("iMaker")) {
   109 				vers = line;
   108 				vers = line;
   110 				break;
   109 				break;
   119 			String newMessage = e.getMessage() +" "+ Messages.getString("IMakerWrapper.27").replace("xxx", tool.get(0));
   118 			String newMessage = e.getMessage() +" "+ Messages.getString("IMakerWrapper.27").replace("xxx", tool.get(0));
   120 			throw new IMakerCoreNotFoundException(newMessage);
   119 			throw new IMakerCoreNotFoundException(newMessage);
   121 		}
   120 		}
   122 	}
   121 	}
   123 
   122 
       
   123 	private void verifyCompatibility() throws IMakerCoreNotFoundException, IMakerCoreExecutionException {
       
   124 		//verify exists
       
   125 		String minVersion = Messages.getString("PreferencesTab.22"); //$NON-NLS-1$
       
   126 		String curVersion = IMakerUtils.parseIMakerVersion(getIMakerCoreVersion());
       
   127 		if(curVersion==null) throw new IMakerCoreExecutionException("Unable to query version information!");
       
   128 		if(minVersion.compareTo(curVersion)>0) {
       
   129 			String msg = Messages.getString("Error.3");
       
   130 			msg = msg.replace("xxx", minVersion);
       
   131 			msg = msg.replace("yyy", curVersion);			
       
   132 			throw new IMakerCoreExecutionException(msg);
       
   133 		}
       
   134 	}
       
   135 
   124 	/* (non-Javadoc)
   136 	/* (non-Javadoc)
   125 	 * @see com.nokia.s60tools.imakerplugin.wrapper.IIMakerWrapper#getConfigurations(java.lang.String, org.eclipse.core.runtime.IProgressMonitor)
   137 	 * @see com.nokia.s60tools.imakerplugin.wrapper.IIMakerWrapper#getConfigurations(java.lang.String, org.eclipse.core.runtime.IProgressMonitor)
   126 	 */
   138 	 */
   127 	public List<UIConfiguration> getConfigurations(IProgressMonitor monitor) 
   139 	public List<UIConfiguration> getConfigurations(IProgressMonitor monitor) 
   128 	throws IMakerCoreExecutionException, IMakerCoreNotFoundException {
   140 	throws IMakerCoreExecutionException, IMakerCoreNotFoundException {
   129 		this.monitor = monitor;
   141 		this.monitor = monitor;
   130 		verifyExists();
   142 		verifyCompatibility();
   131 		List<UIConfiguration> configurations = getConfigurations((String)null);
   143 		List<UIConfiguration> configurations = getConfigurations((String)null);
   132 		return configurations;
   144 		return configurations;
   133 	}
   145 	}
   134 
   146 
   135 	private List<UIConfiguration> getConfigurations(String makefile) throws IMakerCoreExecutionException {
   147 	private List<UIConfiguration> getConfigurations(String makefile) throws IMakerCoreExecutionException {
   164 		if(dProduct!=null) {
   176 		if(dProduct!=null) {
   165 			for(UIConfiguration config: uiConfigs) {
   177 			for(UIConfiguration config: uiConfigs) {
   166 				String pname = config.getConfigurationName();
   178 				String pname = config.getConfigurationName();
   167 				if(pname != null && pname.equals(dProduct)) {
   179 				if(pname != null && pname.equals(dProduct)) {
   168 					config.setDefaultConfig(true);
   180 					config.setDefaultConfig(true);
   169 //					config.setDefaultTarget(dTarget);
       
   170 					break;
   181 					break;
   171 				}
   182 				}
   172 			}			
   183 			}			
   173 		}
   184 		}
   174 		return uiConfigs;
   185 		return uiConfigs;
   181 		params.clear();
   192 		params.clear();
   182 		params.add(IMakerWrapperPreferences.DEFAULT_DATA);// + configPath
   193 		params.add(IMakerWrapperPreferences.DEFAULT_DATA);// + configPath
   183 		Pattern product = Pattern.compile("\\s*IMAKER_CONFMK.*image_conf_(.*)\\.mk.\\s*");
   194 		Pattern product = Pattern.compile("\\s*IMAKER_CONFMK.*image_conf_(.*)\\.mk.\\s*");
   184 //		Pattern target  = Pattern.compile("\\s*TARGET_DEFAULT\\s*=\\s*.(.*).\\s*");
   195 //		Pattern target  = Pattern.compile("\\s*TARGET_DEFAULT\\s*=\\s*.(.*).\\s*");
   185 		try {
   196 		try {
   186 			List<String> lines = executeCommand(params);
   197 			List<String> lines = executeCommand(params,null);
   187 			for (String line : lines) {
   198 			for (String line : lines) {
   188 				Matcher matcher = product.matcher(line);
   199 				Matcher matcher = product.matcher(line);
   189 				if(matcher.matches()) {
   200 				if(matcher.matches()) {
   190 					String pr = matcher.group(1);
   201 					String pr = matcher.group(1);
   191 					if(!pr.equals("")) {
   202 					if(!pr.equals("")) {
   210 
   221 
   211 	private List<String> queryMakefiles() throws IMakerCoreExecutionException {
   222 	private List<String> queryMakefiles() throws IMakerCoreExecutionException {
   212 		List<String> makeFiles;
   223 		List<String> makeFiles;
   213 		params.clear();
   224 		params.clear();
   214 		params.add(IMakerWrapperPreferences.CMD_FETCH_CONFIGURATIONS);// + configPath
   225 		params.add(IMakerWrapperPreferences.CMD_FETCH_CONFIGURATIONS);// + configPath
   215 		makeFiles = executeCommand(params);
   226 		makeFiles = executeCommand(params,null);
   216 		
   227 		
   217 		// Remove redundant lines of information from makeFile list
   228 		// Remove redundant lines of information from makeFile list
   218 		if ((makeFiles != null) && (makeFiles.size() > 0)) {
   229 		if ((makeFiles != null) && (makeFiles.size() > 0)) {
   219 			List<String> tmpMakeFiles = new ArrayList<String>();
   230 			List<String> tmpMakeFiles = new ArrayList<String>();
   220 			
   231 			
   233 
   244 
   234 	public UIConfiguration getConfiguration(List<String> parameters,
   245 	public UIConfiguration getConfiguration(List<String> parameters,
   235 			IProgressMonitor mon) throws IMakerCoreExecutionException,
   246 			IProgressMonitor mon) throws IMakerCoreExecutionException,
   236 			IMakerCoreNotFoundException {
   247 			IMakerCoreNotFoundException {
   237 		this.monitor = mon;
   248 		this.monitor = mon;
   238 		verifyExists();
   249 		verifyCompatibility();
   239 		if(parameters==null||parameters.size()<2) {
   250 		if(parameters==null||parameters.size()<2) {
   240 			return null;
   251 			return null;
   241 		}
   252 		}
   242 		if (monitor != null) {
   253 		if (monitor != null) {
   243 			monitor.beginTask(Messages.getString("IMakerWrapper.4"), 2); //$NON-NLS-1$
   254 			monitor.beginTask(Messages.getString("IMakerWrapper.4"), 2); //$NON-NLS-1$
   250 		params.add(makefile);
   261 		params.add(makefile);
   251 		params.add(IMakerWrapperPreferences.CMD_FETCH_CONFIGURATION);
   262 		params.add(IMakerWrapperPreferences.CMD_FETCH_CONFIGURATION);
   252 		params.addAll(parameters);
   263 		params.addAll(parameters);
   253 		
   264 		
   254 		builderRunning=true;
   265 		builderRunning=true;
   255 		List<String> settingsList = executeCommand(params);
   266 		List<String> settingsList = executeCommand(params,null);
   256 		builderRunning=false;
   267 		builderRunning=false;
   257 		
   268 		
   258 		ArrayList<ConfigurationElement> elements = new ArrayList<ConfigurationElement>();
   269 		ArrayList<ConfigurationElement> elements = new ArrayList<ConfigurationElement>();
   259 		ArrayList<Setting> settings = new ArrayList<Setting>();
   270 		ArrayList<Setting> settings = new ArrayList<Setting>();
   260 		
   271 		
   287 	/* (non-Javadoc)
   298 	/* (non-Javadoc)
   288 	 * @see com.nokia.s60tools.imakerplugin.wrapper.IIMakerWrapper#buildImage(java.util.List)
   299 	 * @see com.nokia.s60tools.imakerplugin.wrapper.IIMakerWrapper#buildImage(java.util.List)
   289 	 */
   300 	 */
   290 	public boolean buildImage(List<String> cmdParams, OutputStream out) throws IMakerCoreExecutionException, 
   301 	public boolean buildImage(List<String> cmdParams, OutputStream out) throws IMakerCoreExecutionException, 
   291 	IMakerCoreAlreadyRunningException, IMakerCoreNotFoundException {
   302 	IMakerCoreAlreadyRunningException, IMakerCoreNotFoundException {
   292 		this.outputStream = out;
   303 		verifyCompatibility();
   293 		verifyExists();
       
   294 		builderRunning = true;
   304 		builderRunning = true;
   295 		List<String> ret = executeCommand(cmdParams);
   305 		List<String> ret = executeCommand(cmdParams,out);
   296 		builderRunning = false;
   306 		builderRunning = false;
   297 		return getReturnStatus(ret);
   307 		return getReturnStatus(ret);
   298 	}
   308 	}
   299 
   309 
   300 	private boolean getReturnStatus(List<String> ret) {
   310 	private boolean getReturnStatus(List<String> ret) {
   341 		return xmi;
   351 		return xmi;
   342 	}
   352 	}
   343 
   353 
   344 	/**
   354 	/**
   345 	 * Calls the iMaker on a command line and reads the output.
   355 	 * Calls the iMaker on a command line and reads the output.
       
   356 	 * @param out 
   346 	 * 
   357 	 * 
   347 	 * @param  cmd additional arguments to be passed to the iMaker core.
   358 	 * @param  cmd additional arguments to be passed to the iMaker core.
   348 	 * @return Output of iMaker as list of Strings.
   359 	 * @return Output of iMaker as list of Strings.
   349 	 */
   360 	 */
   350 	private List<String> executeCommand(List<String> params) throws IMakerCoreExecutionException {
   361 	private List<String> executeCommand(List<String> params, OutputStream out) throws IMakerCoreExecutionException {
   351 		List<String> cmd = prepareCommand(params);
   362 		List<String> cmd = prepareCommand(params);
   352 
   363 
   353 		// Try to start a process to execute iMaker
   364 		// Try to start a process to execute iMaker
   354 		try {
   365 		try {
   355 			// Finally build a new ProcessBuilder with cmds.
   366 			// Finally build a new ProcessBuilder with cmds.
   367 
   378 
   368 			builder.redirectErrorStream(true);
   379 			builder.redirectErrorStream(true);
   369 			Process process = builder.start();
   380 			Process process = builder.start();
   370 
   381 
   371 			// Start reading the output
   382 			// Start reading the output
   372 			StreamGobbler gobbler = new StreamGobbler(process.getInputStream(), outputStream);
   383 			StreamGobbler gobbler = new StreamGobbler(process.getInputStream(), out);
   373 			gobbler.start();
   384 			gobbler.start();
   374 			
   385 			
   375 			ArrayList<String> output;
   386 			ArrayList<String> output;
   376 			try {
   387 			try {
   377 				exitValue = process.waitFor();
   388 				exitValue = process.waitFor();
   620 				params.add(IMakerWrapperPreferences.CMD_FETCH_TARGETS);
   631 				params.add(IMakerWrapperPreferences.CMD_FETCH_TARGETS);
   621 				params.add(IMakerWrapperPreferences.ARG_FETCH_CONFIGURATION);
   632 				params.add(IMakerWrapperPreferences.ARG_FETCH_CONFIGURATION);
   622 				params.add(makeFile);
   633 				params.add(makeFile);
   623 				List<String> prodTargets; 
   634 				List<String> prodTargets; 
   624 				try {
   635 				try {
   625 					prodTargets = executeCommand(params);
   636 					prodTargets = executeCommand(params,null);
   626 				} catch (IMakerCoreExecutionException e) {
   637 				} catch (IMakerCoreExecutionException e) {
   627 					this.lastImakerException = e;
   638 					this.lastImakerException = e;
   628 					if(monitor != null) {
   639 					if(monitor != null) {
   629 						monitor.worked(1);
   640 						monitor.worked(1);
   630 					}
   641 					}
   657 					monitor.subTask(Messages.getString("IMakerWrapper.20")+": "+makeFile); //$NON-NLS-1$
   668 					monitor.subTask(Messages.getString("IMakerWrapper.20")+": "+makeFile); //$NON-NLS-1$
   658 				}
   669 				}
   659 				params.set(0, IMakerWrapperPreferences.CMD_FETCH_CONFIGURATION);
   670 				params.set(0, IMakerWrapperPreferences.CMD_FETCH_CONFIGURATION);
   660 				List<String> settingsList;
   671 				List<String> settingsList;
   661 				try {
   672 				try {
   662 					settingsList = executeCommand(params);
   673 					settingsList = executeCommand(params,null);
   663 				} catch (IMakerCoreExecutionException e) {
   674 				} catch (IMakerCoreExecutionException e) {
   664 					this.lastImakerException = e;
   675 					this.lastImakerException = e;
   665 					if(monitor != null) {
   676 					if(monitor != null) {
   666 						monitor.worked(1);
   677 						monitor.worked(1);
   667 					}
   678 					}
   758 	}
   769 	}
   759 
   770 
   760 	public String getWorkdir(String makefile) throws IMakerCoreNotFoundException, IMakerCoreAlreadyRunningException, IMakerCoreExecutionException {
   771 	public String getWorkdir(String makefile) throws IMakerCoreNotFoundException, IMakerCoreAlreadyRunningException, IMakerCoreExecutionException {
   761 		String target = "help-variable-WORKDIR*-value";
   772 		String target = "help-variable-WORKDIR*-value";
   762 		String retValue = null;
   773 		String retValue = null;
   763 		verifyExists();
   774 		verifyCompatibility();
   764 		List<String> parameters = new ArrayList<String>();
   775 		List<String> parameters = new ArrayList<String>();
   765 		parameters.add("-f");
   776 		parameters.add("-f");
   766 		parameters.add(makefile);
   777 		parameters.add(makefile);
   767 		parameters.add(target);
   778 		parameters.add(target);
   768 		
   779 		
   769 		List<String> ret = executeCommand(parameters);
   780 		List<String> ret = executeCommand(parameters,null);
   770 		String output = ret.get(0);
   781 		String output = ret.get(0);
   771 		for (String str : ret) {
   782 		for (String str : ret) {
   772 			if(str.startsWith("WORK")) {
   783 			if(str.startsWith("WORK")) {
   773 				output = str;
   784 				output = str;
   774 				break;
   785 				break;
   815 			if(monitor!=null) {
   826 			if(monitor!=null) {
   816 				monitor.worked(1);
   827 				monitor.worked(1);
   817 				monitor.subTask(Messages.getString("IMakerWrapper.26"));
   828 				monitor.subTask(Messages.getString("IMakerWrapper.26"));
   818 			}
   829 			}
   819 			StringBuilder sb = new StringBuilder();
   830 			StringBuilder sb = new StringBuilder();
   820 			List<String> ret = executeCommand(cmd);
   831 			List<String> ret = executeCommand(cmd,null);
   821 			for (String str : ret) {
   832 			for (String str : ret) {
   822 				Matcher matcher = pattern.matcher(str);
   833 				Matcher matcher = pattern.matcher(str);
   823 				if(matcher.find()) {
   834 				if(matcher.find()) {
   824 					int end = matcher.end(1);
   835 					int end = matcher.end(1);
   825 					int start = matcher.start(1);
   836 					int start = matcher.start(1);
   848 			throws IMakerCoreNotFoundException, IMakerCoreExecutionException,
   859 			throws IMakerCoreNotFoundException, IMakerCoreExecutionException,
   849 			IMakerCoreAlreadyRunningException {
   860 			IMakerCoreAlreadyRunningException {
   850 		if(impFile==null||!impFile.exists()) {
   861 		if(impFile==null||!impFile.exists()) {
   851 			throw new IMakerCoreExecutionException("Invalid imp file given!");
   862 			throw new IMakerCoreExecutionException("Invalid imp file given!");
   852 		}
   863 		}
   853 		this.outputStream = out;
   864 		verifyCompatibility();
   854 		verifyExists();
       
   855 		
   865 		
   856 		params.clear();
   866 		params.clear();
   857 		params.add("-f");
   867 		params.add("-f");
   858 		params.add("\""+impFile.getAbsolutePath()+"\"");
   868 		params.add(getFilePath(impFile));
   859 		builderRunning = true;
   869 		builderRunning = true;
   860 		List<String> ret = executeCommand(params);
   870 		List<String> ret = executeCommand(params,out);
   861 		builderRunning = false;
   871 		builderRunning = false;
   862 		return getReturnStatus(ret);
   872 		return getReturnStatus(ret);
       
   873 	}
       
   874 
       
   875 	private String getFilePath(File impFile) {
       
   876 		String path = impFile.getAbsolutePath();
       
   877 		if (path.contains(" ")) {
       
   878 			return "\""+path+"\"";			
       
   879 		} else {
       
   880 			return path;
       
   881 		}
       
   882 	}
       
   883 
       
   884 	public String getBuildCommand(File impFile) {
       
   885 		StringBuffer sb = new StringBuffer();
       
   886 		sb.append(tool.get(0) + " -f " + getFilePath(impFile));
       
   887 		return sb.toString();
   863 	}	
   888 	}	
   864 }
   889 }