core/com.nokia.carbide.cpp.leavescan/src/com/nokia/carbide/cpp/internal/leavescan/popup/actions/LeavescanAction.java
branchC3_BUILDER_WORK
changeset 1657 03f5f8bf29b4
parent 1656 d1edeecb12af
parent 1655 e17ab99b8f30
child 1659 48d0d704d5e6
equal deleted inserted replaced
1656:d1edeecb12af 1657:03f5f8bf29b4
     1 /*
       
     2 * Copyright (c) 2007-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 the License "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 */
       
    13 package com.nokia.carbide.cpp.internal.leavescan.popup.actions;
       
    14 
       
    15 import java.util.ArrayList;
       
    16 import java.util.HashMap;
       
    17 import java.util.Iterator;
       
    18 import java.util.List;
       
    19 
       
    20 import org.eclipse.core.resources.IFile;
       
    21 import org.eclipse.core.resources.IProject;
       
    22 import org.eclipse.core.resources.IResource;
       
    23 import org.eclipse.core.runtime.CoreException;
       
    24 import org.eclipse.core.runtime.IPath;
       
    25 import org.eclipse.core.runtime.IProgressMonitor;
       
    26 import org.eclipse.core.runtime.IStatus;
       
    27 import org.eclipse.core.runtime.Path;
       
    28 import org.eclipse.core.runtime.Status;
       
    29 import org.eclipse.core.runtime.jobs.Job;
       
    30 import org.eclipse.jface.action.IAction;
       
    31 import org.eclipse.jface.preference.IPreferenceStore;
       
    32 import org.eclipse.jface.viewers.ISelection;
       
    33 import org.eclipse.jface.viewers.IStructuredSelection;
       
    34 import org.eclipse.ui.IActionDelegate;
       
    35 import org.eclipse.ui.IObjectActionDelegate;
       
    36 import org.eclipse.ui.IWorkbenchPart;
       
    37 
       
    38 import com.nokia.carbide.cdt.builder.CarbideBuilderPlugin;
       
    39 import com.nokia.carbide.cdt.builder.EpocEngineHelper;
       
    40 import com.nokia.carbide.cdt.builder.builder.CarbideCPPBuilder;
       
    41 import com.nokia.carbide.cdt.builder.builder.CarbideCommandLauncher;
       
    42 import com.nokia.carbide.cdt.builder.project.ICarbideBuildConfiguration;
       
    43 import com.nokia.carbide.cdt.builder.project.ICarbideProjectInfo;
       
    44 import com.nokia.carbide.cpp.internal.leavescan.LeavescanPlugin;
       
    45 import com.nokia.carbide.cpp.internal.leavescan.ui.LeavescanPreferenceConstants;
       
    46 import com.nokia.cpp.internal.api.utils.core.HostOS;
       
    47 
       
    48 public class LeavescanAction implements IObjectActionDelegate {
       
    49 	
       
    50 	private ISelection selection;
       
    51 	
       
    52 	// id definied from plugin.xml
       
    53 	public static final String LEAVE_SCAN_ACTION_POP_UP_ID = "com.nokia.carbide.cpp.leavescan.leaveScanAction";
       
    54 	public static final String LEAVE_SCAN_ACTION_MMP_POP_UP_ID = "com.nokia.carbide.cpp.leavescan.leaveScanActionOnMMP";
       
    55 
       
    56 	
       
    57 	/**
       
    58 	 * Constructor for Action1.
       
    59 	 */
       
    60 	public LeavescanAction() {
       
    61 		super();
       
    62 	}
       
    63 
       
    64 	/**
       
    65 	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
       
    66 	 */
       
    67 	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
       
    68 	}
       
    69 
       
    70 	/**
       
    71 	 * @see IActionDelegate#run(IAction)
       
    72 	 */
       
    73 	public void run(IAction action) {
       
    74 		if (action.getId().equals(LEAVE_SCAN_ACTION_POP_UP_ID)){
       
    75 			// scan the selected source file(s)
       
    76 			handleLeaveScanAction(action);
       
    77 		} else if (action.getId().equals(LEAVE_SCAN_ACTION_MMP_POP_UP_ID)){
       
    78 			// scan the selected source file(s)
       
    79 			handleLeaveScanActionOnMMP(action);
       
    80 		}
       
    81 	}
       
    82 
       
    83 	/**
       
    84 	 * @see IActionDelegate#selectionChanged(IAction, ISelection)
       
    85 	 */
       
    86 	public void selectionChanged(IAction action, ISelection selection) {
       
    87 		this.selection = selection;
       
    88 	}
       
    89 	
       
    90 	/**
       
    91 	 * Run leavescan on source file selection(s)
       
    92 	 * @param action
       
    93 	 */
       
    94 	private void handleLeaveScanAction(IAction action){
       
    95 		// get the project each file belongs to...
       
    96 		HashMap<IProject, List<IPath>> projectSourceMap = new HashMap<IProject, List<IPath>>();
       
    97 		if (selection != null && selection instanceof IStructuredSelection) {
       
    98 			Iterator iter = ((IStructuredSelection)selection).iterator();
       
    99 			while (iter.hasNext()) {
       
   100 				Object selItem = iter.next();
       
   101 				if (selItem instanceof IFile) {
       
   102 					IProject currProject = ((IResource)selItem).getProject();
       
   103 					if (projectSourceMap.get(currProject) == null){
       
   104 						// project is not a key, create a new key/value entry
       
   105 						List<IPath> pathList = new ArrayList<IPath>();
       
   106 						pathList.add(((IResource)selItem).getLocation());
       
   107 						projectSourceMap.put(currProject, pathList);
       
   108 
       
   109 					} else {
       
   110 						// this key already exists, update the source list
       
   111 						List<IPath> pathList  = projectSourceMap.get(currProject);
       
   112 						pathList.add(((IResource)selItem).getLocation());
       
   113 						projectSourceMap.put(currProject, pathList);  // 2+ paths exist for this project
       
   114 					}
       
   115 				}
       
   116 			}
       
   117 		}
       
   118 		
       
   119 		// Get the leavescan preferences...
       
   120 		IPreferenceStore store = LeavescanPlugin.getLeaveScanPrefsStore();
       
   121 		final boolean noisyOutput =  store.getBoolean(LeavescanPreferenceConstants.LEAVESCAN_NOISY_OUTPUT);
       
   122 		final String leaveScanFolder = store.getString(LeavescanPreferenceConstants.LEAVESCAN_FOLDER);
       
   123 		
       
   124 		for (final IProject project : projectSourceMap.keySet()){
       
   125 			final List<IPath> finalPathList = projectSourceMap.get(project);	
       
   126 			// Run a job on each project. The arguments for leavescan is:
       
   127 			// leavescan [-h|-n|-v|-N] <iFilename.cpp> [<iFilename.cpp> ...]
       
   128 			//
       
   129 			//            -h: This help.
       
   130 			//            -n: Noisy output - provides diagnostics (if available).
       
   131 			//            -N: Very noisy output - provides diagnostics (if available).
       
   132 			//            -v: Displays version (for build & automation systems).
       
   133 			Job buildJob = new Job("Running Leave Scan on Project: " + project.getName()) { //$NON-NLS-1$
       
   134 				protected IStatus run(IProgressMonitor monitor){
       
   135 					
       
   136 					final String[] leaveScanParserIds = new String[] {
       
   137 				        "com.nokia.carbide.cpp.leavescan.LeaveScanErrorParser"
       
   138 				        };
       
   139 					
       
   140 			        ICarbideProjectInfo cpi = CarbideBuilderPlugin.getBuildManager().getProjectInfo(project);
       
   141 			        try {
       
   142 			        	CarbideCPPBuilder.removeAllMarkers(project);
       
   143 			        } catch (CoreException e){
       
   144 			        	e.printStackTrace();
       
   145 			        }
       
   146 			        IPath workingDir = cpi.getINFWorkingDirectory();
       
   147 					CarbideCommandLauncher cmdLauncher = new CarbideCommandLauncher(project, monitor, leaveScanParserIds, workingDir);
       
   148 					cmdLauncher.startTimingStats();
       
   149 					int fileCount = 1;
       
   150 					int listSize = finalPathList.size();
       
   151 					for (IPath currPath : finalPathList){
       
   152 						
       
   153 						//System.out.print("\nProject: " + project.getName() + " | Source File: " + currPath.toOSString()); // Debug
       
   154 						
       
   155 						ICarbideBuildConfiguration defaultConfig = cpi.getDefaultConfiguration();
       
   156 						monitor.beginTask("Running leavescan.", 100);
       
   157 						
       
   158 						double dWorked = ((double)fileCount / (double)listSize) * 100;
       
   159 						monitor.worked((int)dWorked);
       
   160 						String taskName = "Running leavescan on file \"" + currPath.toOSString() + "\" for project \"" + project.getName() + ".";
       
   161 						monitor.setTaskName(taskName);
       
   162 						cmdLauncher.writeToConsole("\n***" + taskName + "\n");
       
   163 						// Construct the leavescan arguments
       
   164 						List<String> leaveScanArgList = new ArrayList<String>();
       
   165 						leaveScanArgList.add("/c");
       
   166 						
       
   167 						if (leaveScanFolder.length() > 0){
       
   168 							leaveScanArgList.add(leaveScanFolder + "leavescan" + HostOS.EXE_EXT);
       
   169 						} else {
       
   170 							leaveScanArgList.add("leavescan" + HostOS.EXE_EXT);
       
   171 						}
       
   172 
       
   173 						if (noisyOutput) {
       
   174 							leaveScanArgList.add("-N");
       
   175 						} else {
       
   176 							leaveScanArgList.add("-n");
       
   177 						}
       
   178 						
       
   179 						leaveScanArgList.add(currPath.toOSString());
       
   180 						String[] args = new String[leaveScanArgList.size()];
       
   181 						leaveScanArgList.toArray(args);
       
   182 						cmdLauncher.showCommand(true);
       
   183 						
       
   184 						// executeCommand, a special extension to the regular execute which will handle
       
   185 						// writing the console output, error parsing, and creating error markers.
       
   186 						cmdLauncher.executeCommand(CarbideCommandLauncher.getCmdExeLocation(), args, CarbideCPPBuilder.getResolvedEnvVars(defaultConfig), workingDir);
       
   187 						
       
   188 						fileCount++;
       
   189 					}
       
   190 					cmdLauncher.writeToConsole(cmdLauncher.getTimingStats());
       
   191 					return new Status(IStatus.OK, LeavescanPlugin.PLUGIN_ID, IStatus.OK, "LeaveScan Complete", null); 
       
   192 				}
       
   193 			};
       
   194 			
       
   195 			buildJob.setPriority(Job.BUILD);
       
   196 			buildJob.schedule();
       
   197 		}
       
   198 	}
       
   199 	
       
   200 	/**
       
   201 	 * Run leavescan on all the sources in the current project MMP file.
       
   202 	 * The sources files will be build configuration specifc (i.e. MMP is preprocessed) to return
       
   203 	 * platform specific source list.
       
   204 	 * @param action - The IAction interface from the eclipse core
       
   205 	 */
       
   206 	private void handleLeaveScanActionOnMMP(IAction action){
       
   207 		// get the project each file belongs to...
       
   208 		IProject project = null;
       
   209 		IPath mmpFile = null;
       
   210 		if (selection != null && selection instanceof IStructuredSelection) {
       
   211 			Iterator iter = ((IStructuredSelection)selection).iterator();
       
   212 			while (iter.hasNext()) {
       
   213 				Object selItem = iter.next();
       
   214 				if (selItem instanceof IFile) {
       
   215 					project = ((IResource)selItem).getProject();
       
   216 					mmpFile = ((IResource)selItem).getLocation();
       
   217 					//System.out.print("\nMMP File Selected: " + ((IResource)selItem).getLocation());
       
   218 				}
       
   219 			}
       
   220 		}
       
   221 		
       
   222 		if (project == null){
       
   223 			return;
       
   224 		}
       
   225 		
       
   226 		// Get the leavescan preferences...
       
   227 		IPreferenceStore store = LeavescanPlugin.getLeaveScanPrefsStore();
       
   228 		final boolean noisyOutput =  store.getBoolean(LeavescanPreferenceConstants.LEAVESCAN_NOISY_OUTPUT);
       
   229 		final String leaveScanFolder = store.getString(LeavescanPreferenceConstants.LEAVESCAN_FOLDER);
       
   230 		
       
   231 		ICarbideProjectInfo cpi = CarbideBuilderPlugin.getBuildManager().getProjectInfo(project);
       
   232 		if (cpi.getDefaultConfiguration() == null)
       
   233         	return;
       
   234 		
       
   235 		List<IPath> sourceFileList = new ArrayList<IPath>();
       
   236 		// Get the list of source files for the default configuration this MMP is associated with.
       
   237 		sourceFileList = EpocEngineHelper.getSourceFilesForConfiguration(cpi.getDefaultConfiguration(), mmpFile);
       
   238 		
       
   239 		final List<IPath> finalPathList = new ArrayList<IPath>(sourceFileList);	
       
   240 		final IProject finalProject = project;
       
   241 		final Path finalMMPPath = new Path(mmpFile.toOSString());
       
   242 		Job buildJob = new Job("Running Leave Scan on MMP: " + mmpFile.toOSString()) { //$NON-NLS-1$
       
   243 			protected IStatus run(IProgressMonitor monitor){
       
   244 				
       
   245 				final String[] leaveScanParserIds = new String[] {
       
   246 			        "com.nokia.carbide.cpp.leavescan.LeaveScanErrorParser"
       
   247 			        };
       
   248 				
       
   249 		        ICarbideProjectInfo cpi = CarbideBuilderPlugin.getBuildManager().getProjectInfo(finalProject);
       
   250 		        try {
       
   251 		        	CarbideCPPBuilder.removeAllMarkers(finalProject);
       
   252 		        } catch (CoreException e){
       
   253 		        	e.printStackTrace();
       
   254 		        }
       
   255 		        IPath workingDir = cpi.getINFWorkingDirectory();
       
   256 				CarbideCommandLauncher cmdLauncher = new CarbideCommandLauncher(finalProject, monitor, leaveScanParserIds, workingDir);
       
   257 				cmdLauncher.startTimingStats();
       
   258 				int fileCount = 1;
       
   259 				int listSize = finalPathList.size();
       
   260 				for (IPath currPath : finalPathList){
       
   261 					
       
   262 					//System.out.print("\nMMP Project: " + finalMMPPath.lastSegment() + " | Source File: " + currPath.toOSString()); // Debug
       
   263 					
       
   264 					ICarbideBuildConfiguration defaultConfig = cpi.getDefaultConfiguration();
       
   265 					monitor.beginTask("Running leavescan.", 100);
       
   266 					
       
   267 					double dWorked = ((double)fileCount / (double)listSize) * 100;
       
   268 					monitor.worked((int)dWorked);
       
   269 					String taskName = "Running leavescan on file \"" + currPath.toOSString() + "\" for MMP \"" + finalMMPPath.lastSegment() + ".";
       
   270 					monitor.setTaskName(taskName);
       
   271 					cmdLauncher.writeToConsole("\n***" + taskName + "\n");
       
   272 					// Construct the leavescan arguments
       
   273 					List<String> leaveScanArgList = new ArrayList<String>();
       
   274 					leaveScanArgList.add("/c");
       
   275 					
       
   276 					if (leaveScanFolder.length() > 0){
       
   277 						leaveScanArgList.add(leaveScanFolder + "leavescan" + HostOS.EXE_EXT);
       
   278 					} else {
       
   279 						leaveScanArgList.add("leavescan" + HostOS.EXE_EXT);
       
   280 					}
       
   281 
       
   282 					if (noisyOutput) {
       
   283 						leaveScanArgList.add("-N");
       
   284 					} else {
       
   285 						leaveScanArgList.add("-n");
       
   286 					}
       
   287 					
       
   288 					leaveScanArgList.add(currPath.toOSString());
       
   289 					String[] args = new String[leaveScanArgList.size()];
       
   290 					leaveScanArgList.toArray(args);
       
   291 					cmdLauncher.showCommand(true);
       
   292 					
       
   293 					// executeCommand, a special extension to the regular execute which will handle
       
   294 					// writing the console output, error parsing, and creating error markers.
       
   295 					cmdLauncher.executeCommand(CarbideCommandLauncher.getCmdExeLocation(), args, CarbideCPPBuilder.getResolvedEnvVars(defaultConfig), workingDir);
       
   296 					
       
   297 					fileCount++;
       
   298 				}
       
   299 				cmdLauncher.writeToConsole(cmdLauncher.getTimingStats());
       
   300 				return new Status(IStatus.OK, LeavescanPlugin.PLUGIN_ID, IStatus.OK, "LeaveScan Complete", null); 
       
   301 			}
       
   302 		};
       
   303 		
       
   304 		buildJob.setPriority(Job.BUILD);
       
   305 		buildJob.schedule();
       
   306 		
       
   307 	}
       
   308 
       
   309 }