aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.BuildTasks/ResolveWixReferences.cs
blob: 9b8cfe6f8d435ba6b368fab76ab84282cd050a94 (plain)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.

namespace WixToolset.BuildTasks
{
    using System;
    using System.Collections.Generic;
    using Microsoft.Build.Utilities;
    using Microsoft.Build.Framework;
    using System.IO;

    /// <summary>
    /// This task searches for paths to references using the order specified in SearchPaths.
    /// </summary>
    public class ResolveWixReferences : Task
    {
        /// <summary>
        /// Token value used in SearchPaths to indicate that the item's HintPath metadata should
        /// be searched as a full file path to resolve the reference.  
        /// Must match wix.targets, case sensitive.
        /// </summary>
        private const string HintPathToken = "{HintPathFromItem}";

        /// <summary>
        /// Token value used in SearchPaths to indicate that the item's Identity should
        /// be searched as a full file path to resolve the reference.  
        /// Must match wix.targets, case sensitive.
        /// </summary>
        private const string RawFileNameToken = "{RawFileName}";

        /// <summary>
        /// The list of references to resolve.
        /// </summary>
        [Required]
        public ITaskItem[] WixReferences
        {
            get;
            set;
        }

        /// <summary>
        /// The directories or special locations that are searched to find the files 
        /// on disk that represent the references. The order in which the search paths are listed 
        /// is important. For each reference, the list of paths is searched from left to right. 
        /// When a file that represents the reference is found, that search stops and the search 
        /// for the next reference starts. 
        /// 
        /// This parameter accepts the following types of values: 
        ///     A directory path. 
        ///     {HintPathFromItem}: Specifies that the task will examine the HintPath metadata 
        ///                         of the base item. 
        ///     TODO : {CandidateAssemblyFiles}: Specifies that the task will examine the files 
        ///                                      passed in through the CandidateAssemblyFiles parameter. 
        ///     TODO : {Registry:_AssemblyFoldersBase_, _RuntimeVersion_, _AssemblyFoldersSuffix_}: 
        ///     TODO : {AssemblyFolders}: Specifies the task will use the Visual Studio.NET 2003 
        ///                               finding-assemblies-from-registry scheme. 
        ///     TODO : {GAC}: Specifies the task will search in the GAC. 
        ///     {RawFileName}: Specifies the task will consider the Include value of the item to be 
        ///                    an exact path and file name. 
        /// </summary>
        public string[] SearchPaths
        {
            get;
            set;
        }

        /// <summary>
        /// The filename extension(s) to be checked when searching.
        /// </summary>
        public string[] SearchFilenameExtensions
        {
            get;
            set;
        }

        /// <summary>
        /// Output items that contain the same metadata as input references and have been resolved to full paths.
        /// </summary>
        [Output]
        public ITaskItem[] ResolvedWixReferences
        {
            get;
            private set;
        }

        /// <summary>
        /// Resolves reference paths by searching for referenced items using the specified SearchPaths.
        /// </summary>
        /// <returns>True on success, or throws an exception on failure.</returns>
        public override bool Execute()
        {
            List<ITaskItem> resolvedReferences = new List<ITaskItem>();

            foreach (ITaskItem reference in this.WixReferences)
            {
                ITaskItem resolvedReference = ResolveWixReferences.ResolveReference(reference, this.SearchPaths, this.SearchFilenameExtensions, this.Log);

                this.Log.LogMessage(MessageImportance.Low, "Resolved path {0}", resolvedReference.ItemSpec);
                resolvedReferences.Add(resolvedReference);
            }

            this.ResolvedWixReferences = resolvedReferences.ToArray();
            return true;
        }

        /// <summary>
        /// Resolves a single reference item by searcheing for referenced items using the specified SearchPaths.
        /// This method is made public so the resolution logic can be reused by other tasks.
        /// </summary>
        /// <param name="reference">The referenced item.</param>
        /// <param name="searchPaths">The paths to search.</param>
        /// <param name="searchFilenameExtensions">Filename extensions to check.</param>
        /// <param name="log">Logging helper.</param>
        /// <returns>The resolved reference item, or the original reference if it could not be resolved.</returns>
        public static ITaskItem ResolveReference(ITaskItem reference, string[] searchPaths, string[] searchFilenameExtensions, TaskLoggingHelper log)
        {
            if (reference == null)
            {
                throw new ArgumentNullException("reference");
            }

            if (searchPaths == null)
            {
                // Nothing to search, so just return the original reference item.
                return reference;
            }

            if (searchFilenameExtensions == null)
            {
                searchFilenameExtensions = new string[] { };
            }

            // Copy all the metadata from the source
            TaskItem resolvedReference = new TaskItem(reference);
            log.LogMessage(MessageImportance.Low, "WixReference: {0}", reference.ItemSpec);

            // Now find the resolved path based on our order of precedence
            foreach (string searchPath in searchPaths)
            {
                log.LogMessage(MessageImportance.Low, "Trying {0}", searchPath);
                if (searchPath.Equals(HintPathToken, StringComparison.Ordinal))
                {
                    string path = reference.GetMetadata("HintPath");
                    log.LogMessage(MessageImportance.Low, "Trying path {0}", path);
                    if (File.Exists(path))
                    {
                        resolvedReference.ItemSpec = path;
                        break;
                    }
                }
                else if (searchPath.Equals(RawFileNameToken, StringComparison.Ordinal))
                {
                    log.LogMessage(MessageImportance.Low, "Trying path {0}", resolvedReference.ItemSpec);
                    if (File.Exists(resolvedReference.ItemSpec))
                    {
                        break;
                    }

                    if (ResolveWixReferences.ResolveFilenameExtensions(resolvedReference,
                        resolvedReference.ItemSpec, searchFilenameExtensions, log))
                    {
                        break;
                    }
                }
                else
                {
                    string path = Path.Combine(searchPath, Path.GetFileName(reference.ItemSpec));
                    log.LogMessage(MessageImportance.Low, "Trying path {0}", path);
                    if (File.Exists(path))
                    {
                        resolvedReference.ItemSpec = path;
                        break;
                    }

                    if (ResolveWixReferences.ResolveFilenameExtensions(resolvedReference,
                        path, searchFilenameExtensions, log))
                    {
                        break;
                    }
                }
            }

            // Normalize the item path
            resolvedReference.ItemSpec = resolvedReference.GetMetadata("FullPath");

            return resolvedReference;
        }

        /// <summary>
        /// Helper method for checking filename extensions when resolving references.
        /// </summary>
        /// <param name="reference">The reference being resolved.</param>
        /// <param name="basePath">Full filename path without extension.</param>
        /// <param name="filenameExtensions">Filename extensions to check.</param>
        /// <param name="log">Logging helper.</param>
        /// <returns>True if the item was resolved, else false.</returns>
        private static bool ResolveFilenameExtensions(ITaskItem reference, string basePath, string[] filenameExtensions, TaskLoggingHelper log)
        {
            foreach (string filenameExtension in filenameExtensions)
            {
                string path = basePath + filenameExtension;
                log.LogMessage(MessageImportance.Low, "Trying path {0}", path);
                if (File.Exists(path))
                {
                    reference.ItemSpec = path;
                    return true;
                }
            }

            return false;
        }
    }
}