Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 55fbc1d04862c56ad2098d01f6cc7608613b9118 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*******************************************************************************
 * Copyright (c) 2007, 2011 Symbian Software Limited and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Bala Torati (Symbian) - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.managedbuilder.templateengine.processes;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.cdt.core.templateengine.TemplateCore;
import org.eclipse.cdt.core.templateengine.process.ProcessArgument;
import org.eclipse.cdt.core.templateengine.process.ProcessFailureException;
import org.eclipse.cdt.core.templateengine.process.ProcessRunner;
import org.eclipse.cdt.core.templateengine.process.processes.Messages;
import org.eclipse.cdt.managedbuilder.core.IConfiguration;
import org.eclipse.cdt.managedbuilder.core.IManagedBuildInfo;
import org.eclipse.cdt.managedbuilder.core.IManagedProject;
import org.eclipse.cdt.managedbuilder.core.IResourceConfiguration;
import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceProxy;
import org.eclipse.core.resources.IResourceProxyVisitor;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;


/**
 * Exclude Resources from a CDT project. This takes three arguments
 * <ul>
 *   <li> <b>projectName</b> the name of the project to exclude resources for. Must be a CDT Managed project.
 *   <li> <b>configIdPattern</b> a regular expression in java.util.regex.Pattern syntax for matching against the project configuration ids. The resources that
 *   match any of the regular expressions given in the <i>filePatterns</i> argument will be excluded for all matching project configurations.
 *   <li> <b>filePatterns</b> an array of regular expressions in java.util.regex.Pattern syntax for matching against project resources. The paths that
 *   will be matched against are workspace relative (include the project folder) and use forward slash as the file separator. That this argument is an
 *   array is purely to allow logically separate patterns to be given separately )rather than as one big string). If any of the regular expressions matches
 *   then the resource in question will be excluded for the matching configuration(s).
 *   <li> <b>invertConfigMatching</b> if this is set to "true" then the set of configurations for which resources matching any of the specified file patterns will
 *   be inverted. This enables you to specify which resources the files should not be excluded for without having to know what other configurations may exist.
 * </ul>
 *
 * @noextend This class is not intended to be subclassed by clients.
 * @noinstantiate This class is not intended to be instantiated by clients.
 */
public class ExcludeResources extends ProcessRunner {

	@Override
	public void process(TemplateCore template, final ProcessArgument[] args, String processId, IProgressMonitor monitor) throws ProcessFailureException {
		String projectName = args[0].getSimpleValue();
		String configIdPattern = args[1].getSimpleValue();
		final String[] filePatterns = args[2].getSimpleArrayValue();
		String invertConfigMatching = args[3].getSimpleValue();

		boolean invert = Boolean.valueOf(invertConfigMatching).booleanValue();

		IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);

		IManagedBuildInfo info = ManagedBuildManager.getBuildInfo(project);
		if(info==null) {
			throw new ProcessFailureException(Messages.getString("ExcludeResources.0")); //$NON-NLS-1$
		}
		IManagedProject managedProject = info.getManagedProject();

		/*
		 * Determine which configurations to exclude for
		 */
		IConfiguration[] allConfigs = managedProject.getConfigurations();
		List<IConfiguration> matchingConfigs = new ArrayList<IConfiguration>();
		for(int i=0; i<allConfigs.length; i++) {
			IConfiguration config = allConfigs[i];
			if(config.getId().matches(configIdPattern)) {
				matchingConfigs.add(config);
			}
		}

		if(invert) {
			List<IConfiguration> invertedConfigs = new ArrayList<IConfiguration>(Arrays.asList(allConfigs));
			invertedConfigs.removeAll(matchingConfigs);
			matchingConfigs = invertedConfigs;
		}

		/*
		 * Visit project resources and exclude them if they match any pattern
		 */
		final List<IConfiguration> configsToProcess = matchingConfigs;
		IResourceProxyVisitor visitor = new IResourceProxyVisitor() {
			@Override
			public boolean visit(IResourceProxy proxy) throws CoreException {
				IPath lPath = proxy.requestFullPath();

				if(proxy.getType() == IResource.FILE) { /* CDT does not support directory resource configurations */
					boolean isDerived = false;
					for(IResource res = proxy.requestResource(); res!=null; res = res.getParent()) {
						isDerived |= res.isDerived();
					}

					if(!isDerived) {
						for (IConfiguration config : configsToProcess) {
							IResourceConfiguration resourceConfig = config.getResourceConfiguration(lPath.toString());
							// Only add a resource configuration if the file pattern matches something that
							//is actually supposed to be excluded. Adding a resrouce configuration for all files
							// regardless of wheter they need it or not mess up the makefile generation.
							for (String filePattern : filePatterns) {
								if(lPath.toString().matches(filePattern)) {
									if(resourceConfig==null) {
										IFile file = (IFile) proxy.requestResource();
											resourceConfig = config.createResourceConfiguration(file);
									}

									if (resourceConfig != null){
										resourceConfig.setExclude(true);
									}

									break;

								}
							}

						}
					}
				}
				return true;
			}
		};

		try {
			project.accept(visitor, IResource.DEPTH_INFINITE);
			if (info.isDirty()){
				ManagedBuildManager.saveBuildInfo(project, true);
			}
		} catch (CoreException ce) {
			throw new ProcessFailureException(ce);
		}
	}
}

Back to the top