aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.BuildTasks/CreateProjectReferenceDefineConstants.cs
blob: 5eae0850709a0aa8c6400413b28ce9fb72b94272 (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// 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 System.Globalization;
    using System.IO;
    using Microsoft.Build.Framework;
    using Microsoft.Build.Utilities;

    /// <summary>
    /// An MSBuild task to create a list of preprocessor defines to be passed to candle from the
    /// list of referenced projects.
    /// </summary>
    public sealed class CreateProjectReferenceDefineConstants : Task
    {
        private ITaskItem[] defineConstants;
        private ITaskItem[] projectConfigurations;
        private ITaskItem[] projectReferencePaths;

        [Output]
        public ITaskItem[] DefineConstants
        {
            get { return this.defineConstants; }
        }

        [Required]
        public ITaskItem[] ProjectReferencePaths
        {
            get { return this.projectReferencePaths; }
            set { this.projectReferencePaths = value; }
        }

        public ITaskItem[] ProjectConfigurations
        {
            get { return this.projectConfigurations; }
            set { this.projectConfigurations = value; }
        }

        public override bool Execute()
        {
            List<ITaskItem> outputItems = new List<ITaskItem>();
            Dictionary<string, string> defineConstants = new Dictionary<string, string>();

            for (int i = 0; i < this.ProjectReferencePaths.Length; i++)
            {
                ITaskItem item = this.ProjectReferencePaths[i];

                string configuration = item.GetMetadata("Configuration");
                string fullConfiguration = item.GetMetadata("FullConfiguration");
                string platform = item.GetMetadata("Platform");

                string projectPath = CreateProjectReferenceDefineConstants.GetProjectPath(this.ProjectReferencePaths, i);
                string projectDir = Path.GetDirectoryName(projectPath) + Path.DirectorySeparatorChar;
                string projectExt = Path.GetExtension(projectPath);
                string projectFileName = Path.GetFileName(projectPath);
                string projectName = Path.GetFileNameWithoutExtension(projectPath);

                string referenceName = CreateProjectReferenceDefineConstants.GetReferenceName(item, projectName);

                string targetPath = item.GetMetadata("FullPath");
                string targetDir = Path.GetDirectoryName(targetPath) + Path.DirectorySeparatorChar;
                string targetExt = Path.GetExtension(targetPath);
                string targetFileName = Path.GetFileName(targetPath);
                string targetName = Path.GetFileNameWithoutExtension(targetPath);

                // If there is no configuration metadata on the project reference task item,
                // check for any additional configuration data provided in the optional task property.
                if (String.IsNullOrEmpty(fullConfiguration))
                {
                    fullConfiguration = this.FindProjectConfiguration(projectName);
                    if (!String.IsNullOrEmpty(fullConfiguration))
                    {
                        string[] typeAndPlatform = fullConfiguration.Split('|');
                        configuration = typeAndPlatform[0];
                        platform = (typeAndPlatform.Length > 1 ? typeAndPlatform[1] : String.Empty);
                    }
                }

                // write out the platform/configuration defines
                defineConstants[String.Format(CultureInfo.InvariantCulture, "{0}.Configuration", referenceName)] = configuration;
                defineConstants[String.Format(CultureInfo.InvariantCulture, "{0}.FullConfiguration", referenceName)] = fullConfiguration;
                defineConstants[String.Format(CultureInfo.InvariantCulture, "{0}.Platform", referenceName)] = platform;

                // write out the ProjectX defines
                defineConstants[String.Format(CultureInfo.InvariantCulture, "{0}.ProjectDir", referenceName)] = projectDir;
                defineConstants[String.Format(CultureInfo.InvariantCulture, "{0}.ProjectExt", referenceName)] = projectExt;
                defineConstants[String.Format(CultureInfo.InvariantCulture, "{0}.ProjectFileName", referenceName)] = projectFileName;
                defineConstants[String.Format(CultureInfo.InvariantCulture, "{0}.ProjectName", referenceName)] = projectName;
                defineConstants[String.Format(CultureInfo.InvariantCulture, "{0}.ProjectPath", referenceName)] = projectPath;

                // write out the TargetX defines
                string targetDirDefine = String.Format(CultureInfo.InvariantCulture, "{0}.TargetDir", referenceName);
                if (defineConstants.ContainsKey(targetDirDefine))
                {
                    //if target dir was already defined, redefine it as the common root shared by multiple references from the same project
                    string commonDir = FindCommonRoot(targetDir, defineConstants[targetDirDefine]);
                    if (!String.IsNullOrEmpty(commonDir))
                    {
                        targetDir = commonDir;
                    }
                }
                defineConstants[targetDirDefine] = CreateProjectReferenceDefineConstants.EnsureEndsWithBackslash(targetDir);

                defineConstants[String.Format(CultureInfo.InvariantCulture, "{0}.TargetExt", referenceName)] = targetExt;
                defineConstants[String.Format(CultureInfo.InvariantCulture, "{0}.TargetFileName", referenceName)] = targetFileName;
                defineConstants[String.Format(CultureInfo.InvariantCulture, "{0}.TargetName", referenceName)] = targetName;

                //if target path was already defined, append to it creating a list of multiple references from the same project
                string targetPathDefine = String.Format(CultureInfo.InvariantCulture, "{0}.TargetPath", referenceName);
                if (defineConstants.ContainsKey(targetPathDefine))
                {
                    string oldTargetPath = defineConstants[targetPathDefine];
                    if (!targetPath.Equals(oldTargetPath, StringComparison.OrdinalIgnoreCase))
                    {
                        defineConstants[targetPathDefine] += ";" + targetPath;
                    }

                    //If there was only one targetpath we need to create its culture specific define
                    if (!oldTargetPath.Contains(";"))
                    {
                        string oldSubFolder = FindSubfolder(oldTargetPath, targetDir, targetFileName);
                        if (!String.IsNullOrEmpty(oldSubFolder))
                        {
                            defineConstants[String.Format(CultureInfo.InvariantCulture, "{0}.{1}.TargetPath", referenceName, oldSubFolder.Replace('\\', '_'))] = oldTargetPath;
                        }
                    }

                    // Create a culture specific define
                    string subFolder = FindSubfolder(targetPath, targetDir, targetFileName);
                    if (!String.IsNullOrEmpty(subFolder))
                    {
                        defineConstants[String.Format(CultureInfo.InvariantCulture, "{0}.{1}.TargetPath", referenceName, subFolder.Replace('\\', '_'))] = targetPath;
                    }

                }
                else
                {
                    defineConstants[targetPathDefine] = targetPath;
                }
            }

            foreach (KeyValuePair<string, string> define in defineConstants)
            {
                outputItems.Add(new TaskItem(String.Format(CultureInfo.InvariantCulture, "{0}={1}", define.Key, define.Value)));
            }
            this.defineConstants = outputItems.ToArray();

            return true;
        }

        public static string GetProjectPath(ITaskItem[] projectReferencePaths, int i)
        {
            return projectReferencePaths[i].GetMetadata("MSBuildSourceProjectFile");
        }

        public static string GetReferenceName(ITaskItem item, string projectName)
        {
            string referenceName = item.GetMetadata("Name");
            if (String.IsNullOrEmpty(referenceName))
            {
                referenceName = projectName;
            }

            // We cannot have an equals sign in the variable name because it
            // messes with the preprocessor definitions on the command line.
            referenceName = referenceName.Replace('=', '_');

            // We cannot have a double quote on the command line because it
            // there is no way to escape it on the command line.
            referenceName = referenceName.Replace('\"', '_');

            // We cannot have parens in the variable name because the WiX
            // preprocessor will not be able to parse it.
            referenceName = referenceName.Replace('(', '_');
            referenceName = referenceName.Replace(')', '_');

            return referenceName;
        }

        /// <summary>
        /// Look through the configuration data in the ProjectConfigurations property
        /// to find the configuration for a project, if available.
        /// </summary>
        /// <param name="projectName">Name of the project that is being searched for.</param>
        /// <returns>Full configuration spec, for example "Release|Win32".</returns>
        private string FindProjectConfiguration(string projectName)
        {
            string configuration = String.Empty;

            if (this.ProjectConfigurations != null)
            {
                foreach (ITaskItem configItem in this.ProjectConfigurations)
                {
                    string configProject = configItem.ItemSpec;
                    if (configProject.Length > projectName.Length &&
                        configProject.StartsWith(projectName) &&
                        configProject[projectName.Length] == '=')
                    {
                        configuration = configProject.Substring(projectName.Length + 1);
                        break;
                    }
                }
            }

            return configuration;
        }

        /// <summary>
        /// Finds the common root between two paths
        /// </summary>
        /// <param name="path1"></param>
        /// <param name="path2"></param>
        /// <returns>common root on success, empty string on failure</returns>
        private static string FindCommonRoot(string path1, string path2)
        {
            path1 = path1.TrimEnd(Path.DirectorySeparatorChar);
            path2 = path2.TrimEnd(Path.DirectorySeparatorChar);

            while (!String.IsNullOrEmpty(path1))
            {
                for (string searchPath = path2; !String.IsNullOrEmpty(searchPath); searchPath = Path.GetDirectoryName(searchPath))
                {
                    if (path1.Equals(searchPath, StringComparison.OrdinalIgnoreCase))
                    {
                        return searchPath;
                    }
                }

                path1 = Path.GetDirectoryName(path1);
            }

            return path1;
        }

        /// <summary>
        /// Finds the subfolder of a path, excluding a root and filename.
        /// </summary>
        /// <param name="path">Path to examine</param>
        /// <param name="rootPath">Root that must be present </param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        private static string FindSubfolder(string path, string rootPath, string fileName)
        {
            if (Path.GetFileName(path).Equals(fileName, StringComparison.OrdinalIgnoreCase))
            {
                path = Path.GetDirectoryName(path);
            }

            if (path.StartsWith(rootPath, StringComparison.OrdinalIgnoreCase))
            {
                // cut out the root and return the subpath
                return path.Substring(rootPath.Length).Trim(Path.DirectorySeparatorChar);
            }

            return String.Empty;
        }

        private static string EnsureEndsWithBackslash(string dir)
        {
            if (dir[dir.Length - 1] != Path.DirectorySeparatorChar)
            {
                dir += Path.DirectorySeparatorChar;
            }

            return dir;
        }
    }
}