1 11 package org.eclipse.ui.externaltools.internal.model; 12 13 import java.util.HashMap ; 14 import java.util.Map ; 15 import java.util.StringTokenizer ; 16 17 import org.eclipse.core.resources.ICommand; 18 import org.eclipse.core.resources.IFile; 19 import org.eclipse.core.resources.IFolder; 20 import org.eclipse.core.resources.IProject; 21 import org.eclipse.core.resources.IResource; 22 import org.eclipse.core.resources.IncrementalProjectBuilder; 23 import org.eclipse.core.resources.ResourcesPlugin; 24 import org.eclipse.core.runtime.CoreException; 25 import org.eclipse.core.runtime.IConfigurationElement; 26 import org.eclipse.core.runtime.IExtensionPoint; 27 import org.eclipse.core.runtime.IPath; 28 import org.eclipse.core.runtime.IStatus; 29 import org.eclipse.core.runtime.NullProgressMonitor; 30 import org.eclipse.core.runtime.Path; 31 import org.eclipse.core.runtime.Platform; 32 import org.eclipse.debug.core.DebugPlugin; 33 import org.eclipse.debug.core.ILaunchConfiguration; 34 import org.eclipse.debug.core.ILaunchConfigurationType; 35 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; 36 import org.eclipse.debug.core.ILaunchManager; 37 import org.eclipse.jface.dialogs.MessageDialog; 38 import org.eclipse.swt.widgets.Shell; 39 import org.eclipse.ui.externaltools.internal.registry.ExternalToolMigration; 40 41 44 public class BuilderUtils { 45 46 public static final String LAUNCH_CONFIG_HANDLE = "LaunchConfigHandle"; 48 51 public static final String BUILDER_FOLDER_NAME= ".externalToolBuilders"; 55 public static final String PROJECT_TAG= "<project>"; 57 public static final String VERSION_1_0= "1.0"; public static final String VERSION_2_1= "2.1"; public static final String VERSION_3_0_interim= "3.0.interim"; public static final String VERSION_3_0_final= "3.0"; 64 private static final String TAG_CONFIGURATION_MAP= "configurationMap"; private static final String TAG_SOURCE_TYPE= "sourceType"; private static final String TAG_BUILDER_TYPE= "builderType"; 69 private static final String BUILD_TYPE_SEPARATOR = ","; private static final int[] DEFAULT_BUILD_TYPES= new int[] { 71 IncrementalProjectBuilder.INCREMENTAL_BUILD, 72 IncrementalProjectBuilder.FULL_BUILD}; 73 74 83 public static ILaunchConfiguration configFromBuildCommandArgs(IProject project, Map commandArgs, String [] version) { 84 String configHandle = (String ) commandArgs.get(LAUNCH_CONFIG_HANDLE); 85 if (configHandle == null) { 86 version[0]= VERSION_1_0; 88 return ExternalToolMigration.configFromArgumentMap(commandArgs); 89 } 90 ILaunchManager manager= DebugPlugin.getDefault().getLaunchManager(); 91 ILaunchConfiguration configuration= null; 92 if (configHandle.startsWith(PROJECT_TAG)) { 93 version[0]= VERSION_3_0_final; 94 IPath path= new Path(configHandle); 95 IFile file= project.getFile(path.removeFirstSegments(1)); 96 if (file.exists()) { 97 configuration= manager.getLaunchConfiguration(file); 98 } 99 } else { 100 IPath path= new Path(BUILDER_FOLDER_NAME).append(configHandle); 103 IFile file= project.getFile(path); 104 if (file.exists()) { 105 version[0]= VERSION_3_0_interim; 106 configuration= manager.getLaunchConfiguration(file); 107 } else { 108 try { 109 configuration = manager.getLaunchConfiguration(configHandle); 112 } catch (CoreException e) { 113 } 114 if (configuration != null) { 115 version[0]= VERSION_2_1; 116 } 117 } 118 } 119 return configuration; 120 } 121 122 130 public static ICommand commandFromLaunchConfig(IProject project, ILaunchConfiguration config) { 131 ICommand newCommand = null; 132 try { 133 newCommand = project.getDescription().newCommand(); 134 newCommand = toBuildCommand(project, config, newCommand); 135 configureTriggers(config, newCommand); 136 } catch (CoreException exception) { 137 Shell shell= ExternalToolsPlugin.getActiveWorkbenchShell(); 138 if (shell != null) { 139 MessageDialog.openError(shell, ExternalToolsModelMessages.BuilderUtils_5, ExternalToolsModelMessages.BuilderUtils_6); 140 } 141 return null; 142 } 143 return newCommand; 144 } 145 146 public static void configureTriggers(ILaunchConfiguration config, ICommand newCommand) throws CoreException { 147 newCommand.setBuilding(IncrementalProjectBuilder.FULL_BUILD, false); 148 newCommand.setBuilding(IncrementalProjectBuilder.INCREMENTAL_BUILD, false); 149 newCommand.setBuilding(IncrementalProjectBuilder.AUTO_BUILD, false); 150 newCommand.setBuilding(IncrementalProjectBuilder.CLEAN_BUILD, false); 151 String buildKinds= config.getAttribute(IExternalToolConstants.ATTR_RUN_BUILD_KINDS, (String )null); 152 int[] triggers= BuilderUtils.buildTypesToArray(buildKinds); 153 for (int i = 0; i < triggers.length; i++) { 154 switch (triggers[i]) { 155 case IncrementalProjectBuilder.FULL_BUILD: 156 newCommand.setBuilding(IncrementalProjectBuilder.FULL_BUILD, true); 157 break; 158 case IncrementalProjectBuilder.INCREMENTAL_BUILD: 159 newCommand.setBuilding(IncrementalProjectBuilder.INCREMENTAL_BUILD, true); 160 break; 161 case IncrementalProjectBuilder.AUTO_BUILD: 162 newCommand.setBuilding(IncrementalProjectBuilder.AUTO_BUILD, true); 163 break; 164 case IncrementalProjectBuilder.CLEAN_BUILD: 165 newCommand.setBuilding(IncrementalProjectBuilder.CLEAN_BUILD, true); 166 break; 167 } 168 } 169 if (!config.getAttribute(IExternalToolConstants.ATTR_TRIGGERS_CONFIGURED, false)) { 170 ILaunchConfigurationWorkingCopy copy= config.getWorkingCopy(); 171 copy.setAttribute(IExternalToolConstants.ATTR_TRIGGERS_CONFIGURED, true); 172 copy.doSave(); 173 } 174 } 175 176 186 public static boolean isUnmigratedConfig(ILaunchConfiguration config) { 187 return config.isWorkingCopy() && ((ILaunchConfigurationWorkingCopy) config).getOriginal() == null; 188 } 189 190 196 public static ICommand toBuildCommand(IProject project, ILaunchConfiguration config, ICommand command) throws CoreException { 197 Map args= null; 198 if (isUnmigratedConfig(config)) { 199 ICommand[] commands= project.getDescription().getBuildSpec(); 203 for (int i = 0; i < commands.length; i++) { 204 ICommand projectCommand = commands[i]; 205 String name= ExternalToolMigration.getNameFromCommandArgs(projectCommand.getArguments()); 206 if (name != null && name.equals(config.getName())) { 207 args= projectCommand.getArguments(); 208 break; 209 } 210 } 211 } else { 212 if (config instanceof ILaunchConfigurationWorkingCopy) { 213 ILaunchConfigurationWorkingCopy workingCopy= (ILaunchConfigurationWorkingCopy) config; 214 if (workingCopy.getOriginal() != null) { 215 config= workingCopy.getOriginal(); 216 } 217 } 218 args= new HashMap (); 219 StringBuffer buffer= new StringBuffer (PROJECT_TAG); 221 buffer.append('/').append(config.getFile().getFullPath().removeFirstSegments(1)); 223 args.put(LAUNCH_CONFIG_HANDLE, buffer.toString()); 224 } 225 command.setBuilderName(ExternalToolBuilder.ID); 226 command.setArguments(args); 227 return command; 228 } 229 230 235 public static ILaunchConfigurationType getConfigurationDuplicationType(ILaunchConfiguration config) throws CoreException { 236 IExtensionPoint ep= Platform.getExtensionRegistry().getExtensionPoint(IExternalToolConstants.PLUGIN_ID, IExternalToolConstants.EXTENSION_POINT_CONFIGURATION_DUPLICATION_MAPS); 237 IConfigurationElement[] elements = ep.getConfigurationElements(); 238 String sourceType= config.getType().getIdentifier(); 239 String builderType= null; 240 for (int i= 0; i < elements.length; i++) { 241 IConfigurationElement element= elements[i]; 242 if (element.getName().equals(TAG_CONFIGURATION_MAP) && sourceType.equals(element.getAttribute(TAG_SOURCE_TYPE))) { 243 builderType= element.getAttribute(TAG_BUILDER_TYPE); 244 break; 245 } 246 } 247 if (builderType != null) { 248 ILaunchConfigurationType type= DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurationType(builderType); 249 if (type != null) { 250 return type; 251 } 252 } 253 return config.getType(); 254 } 255 256 260 public static IFolder getBuilderFolder(IProject project, boolean create) { 261 IFolder folder = project.getFolder(BUILDER_FOLDER_NAME); 262 if (!folder.exists() && create) { 263 try { 264 folder.create(true, true, new NullProgressMonitor()); 265 } catch (CoreException e) { 266 return null; 267 } 268 } 269 return folder; 270 } 271 272 278 public static ILaunchConfiguration duplicateConfiguration(IProject project, ILaunchConfiguration config) throws CoreException { 279 Map attributes= config.getAttributes(); 280 String newName= new StringBuffer (config.getName()).append(ExternalToolsModelMessages.BuilderUtils_7).toString(); 281 newName= DebugPlugin.getDefault().getLaunchManager().generateUniqueLaunchConfigurationNameFrom(newName); 282 ILaunchConfigurationType newType= getConfigurationDuplicationType(config); 283 ILaunchConfigurationWorkingCopy newWorkingCopy= newType.newInstance(getBuilderFolder(project, true), newName); 284 newWorkingCopy.setAttributes(attributes); 285 return newWorkingCopy.doSave(); 286 } 287 288 304 public static ILaunchConfiguration migrateBuilderConfiguration(IProject project, ILaunchConfigurationWorkingCopy workingCopy) throws CoreException { 305 workingCopy.setContainer(getBuilderFolder(project, true)); 306 String name= workingCopy.getName(); 308 name= name.replace('/', '.'); 309 if (name.charAt(0) == ('.')) { 310 name = name.substring(1); 311 } 312 IStatus status = ResourcesPlugin.getWorkspace().validateName(name, IResource.FILE); 313 if (!status.isOK()) { 314 name = "ExternalTool"; } 316 name = DebugPlugin.getDefault().getLaunchManager().generateUniqueLaunchConfigurationNameFrom(name); 317 workingCopy.rename(name); 318 return workingCopy.doSave(); 319 } 320 321 328 public static int[] buildTypesToArray(String buildTypes) { 329 if (buildTypes == null || buildTypes.length() == 0) { 330 return DEFAULT_BUILD_TYPES; 331 } 332 333 int count = 0; 334 boolean incremental = false; 335 boolean full = false; 336 boolean auto = false; 337 boolean clean= false; 338 339 StringTokenizer tokenizer = new StringTokenizer (buildTypes, BUILD_TYPE_SEPARATOR); 340 while (tokenizer.hasMoreTokens()) { 341 String token = tokenizer.nextToken(); 342 if (IExternalToolConstants.BUILD_TYPE_INCREMENTAL.equals(token)) { 343 if (!incremental) { 344 incremental = true; 345 count++; 346 } 347 } else if (IExternalToolConstants.BUILD_TYPE_FULL.equals(token)) { 348 if (!full) { 349 full = true; 350 count++; 351 } 352 } else if (IExternalToolConstants.BUILD_TYPE_AUTO.equals(token)) { 353 if (!auto) { 354 auto = true; 355 count++; 356 } 357 } else if (IExternalToolConstants.BUILD_TYPE_CLEAN.equals(token)) { 358 if (!clean) { 359 clean = true; 360 count++; 361 } 362 } 363 } 364 365 int[] results = new int[count]; 366 count = 0; 367 if (incremental) { 368 results[count] = IncrementalProjectBuilder.INCREMENTAL_BUILD; 369 count++; 370 } 371 if (full) { 372 results[count] = IncrementalProjectBuilder.FULL_BUILD; 373 count++; 374 } 375 if (auto) { 376 results[count] = IncrementalProjectBuilder.AUTO_BUILD; 377 count++; 378 } 379 if (clean) { 380 results[count] = IncrementalProjectBuilder.CLEAN_BUILD; 381 count++; 382 } 383 384 return results; 385 } 386 } 387 | Popular Tags |