Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 53e18924397a8b15e3a8414775571f29f23bfeda (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
/*******************************************************************************
 * Copyright (c) 2007, 2013 Matthew Conway 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:
 *     Matthew Conway - initial API and implementation
 *     IBM Corporation - bug fixing
 *******************************************************************************/
package org.eclipse.ui.externaltools.internal.variables;

import java.util.LinkedHashSet;
import java.util.Set;

import org.eclipse.core.externaltools.internal.model.ExternalToolBuilder;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IResourceDeltaVisitor;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.variables.IDynamicVariable;
import org.eclipse.core.variables.IDynamicVariableResolver;

public class BuildFilesResolver implements IDynamicVariableResolver
{
    private static final char ARG_REMOVED = 'r';
    private static final char ARG_CHANGED = 'c';
    private static final char ARG_ADDED = 'a';
    private static final char ARG_DIRS = 'd';
    private static final char ARG_FILES = 'f';

    // Use a space as a separator as this is a more natural fit for sending a
    // list of files to a unix command
    private static final String FILE_LIST_SEPARATOR = " "; //$NON-NLS-1$

	@Override
	public String resolveValue(IDynamicVariable variable, String argument) throws CoreException
    {
        String result = null;

        IResourceDelta buildDelta = ExternalToolBuilder.getBuildDelta();
        if (buildDelta != null)
        {
            final StringBuffer fileList = new StringBuffer();
			final Set<String> changedResources = new LinkedHashSet<String>();

            // Use the argument to determine which deltas to visit - if none,
            // then defaults to all
            int deltas = 0;
            boolean dirs = false, files = false;
            if (argument != null)
            {
                // Check delta kinds
                if (argument.indexOf(ARG_ADDED) > -1)
                {
                    deltas |= IResourceDelta.ADDED;
                }
                if (argument.indexOf(ARG_CHANGED) > -1)
                {
                    deltas |= IResourceDelta.CHANGED;
                }
                if (argument.indexOf(ARG_REMOVED) > -1)
                {
                    deltas |= IResourceDelta.REMOVED;
                }

                // Check wether to include files and/or directories
                if (argument.indexOf(ARG_DIRS) > -1)
                {
                    dirs = true;
                }
                if (argument.indexOf(ARG_FILES) > -1)
                {
                    files = true;
                }

            }
            if (deltas == 0)
            {
                deltas = IResourceDelta.ADDED | IResourceDelta.CHANGED | IResourceDelta.REMOVED;
            }
            if (!dirs && !files)
            {
                dirs = true;
                files = true;
            }
            final int trackDeltas = deltas;
            final boolean trackDirs = dirs;
            final boolean trackFiles = files;


            buildDelta.accept(new IResourceDeltaVisitor()
            {
                @Override
				public boolean visit(IResourceDelta delta) throws CoreException
                {
                    IResource resource = delta.getResource();

                    // Only track files with the right kind of delta
                    boolean isTracked = (delta.getKind() & trackDeltas) > 0;
                    if (isTracked)
                    {
                        // Only track dirs if desired
                        isTracked = trackDirs && resource.getType() != IResource.FILE;
                        // Only track files if desired
                        isTracked |= trackFiles && resource.getType() == IResource.FILE;
                    }

                    //  If tracking a change, then add it to the change set for inclusion in the variable's output
                    if (isTracked)
                    {
                        String osPath = resource.getLocation().toOSString();
                        if (changedResources.add(osPath))
                        {
                            if (fileList.length() > 0)
                            {
                                fileList.append(FILE_LIST_SEPARATOR);
                            }

                            // Since space is our separator, we need to add quotes
                            // around each file to handle filenames with embedded
                            // spaces. We also need to escape out embedded quotes in
                            // the filename so they don't conflict with these
                            // special quotes.
                            //
                            osPath = osPath.replaceAll("\"", "\\\\\""); //$NON-NLS-1$ //$NON-NLS-2$
                            fileList.append("\"" + osPath + "\""); //$NON-NLS-1$ //$NON-NLS-2$
                        }
                    }
                    return true;
                }
            }, deltas);
            result = fileList.toString();
        }

        return result;
    }
}

Back to the top