AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
UwpProjectPostProcess.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using System.Collections.Generic;
5 using System.IO;
6 using System.Linq;
7 using System.Text.RegularExpressions;
8 using System.Xml;
9 
10 namespace HoloToolkit.Unity
11 {
15  public static class UwpProjectPostProcess
16  {
17  private static readonly Regex PlatformRegex = new Regex(@"'\$\(Configuration\)\|\$\(Platform\)' == '(?<Configuration>.*)\|(?<Platform>.*)'");
18 
23  public static void Execute(string buildRootPath)
24  {
25  UpdateProjectFile(Path.Combine(buildRootPath, @"GeneratedProjects\UWP\Assembly-CSharp\Assembly-CSharp.csproj"));
26  UpdateProjectFile(Path.Combine(buildRootPath, @"GeneratedProjects\UWP\Assembly-CSharp-firstpass\Assembly-CSharp-firstpass.csproj"));
27  }
28 
34  private static void UpdateProjectFile(string filename)
35  {
36  if (!File.Exists(filename))
37  {
38  UnityEngine.Debug.LogWarningFormat("Unable to find file \"{0}\", double check that the build succeeded and that the C# Projects are set to be generated.", filename);
39  return;
40  }
41 
42  var projectDocument = new XmlDocument();
43  projectDocument.Load(filename);
44 
45  if (projectDocument.DocumentElement == null)
46  {
47  UnityEngine.Debug.LogWarningFormat("Unable to load file \"{0}\", double check that the build succeeded and that the C# Projects are set to be generated.", filename);
48  return;
49  }
50 
51  if (projectDocument.DocumentElement.Name != "Project")
52  {
53  UnityEngine.Debug.LogWarningFormat("The loaded project \"{0}\", does not appear to be a MSBuild Project file.", filename);
54  return;
55  }
56 
57  foreach (XmlNode node in projectDocument.DocumentElement.ChildNodes)
58  {
59  // Everything we are looking for is inside a PropertyGroup...
60  if (node.Name != "PropertyGroup" || node.Attributes == null)
61  {
62  continue;
63  }
64 
65  if (node.Attributes.Count == 0 && node["Configuration"] != null && node["Platform"] != null)
66  {
67  // Update the defaults to Release and x86 so that we can run NuGet restore.
68  node["Configuration"].InnerText = "Release";
69  node["Platform"].InnerText = "x86";
70  }
71  else if (node.Attributes["Condition"] != null)
72  {
73  // Update the DefineConstants to include the configuration allowing us to conditionally compile code based on the configuration.
74  Match match = PlatformRegex.Match(node.Attributes["Condition"].InnerText);
75 
76  if (match.Success)
77  {
78  UpdateDefineConstants(node["DefineConstants"], match.Groups["Configuration"].Value, match.Groups["Platform"].Value);
79  }
80  }
81  }
82 
83  WriteXmlDocumentToFile(projectDocument, filename);
84  }
85 
86  private static void UpdateDefineConstants(XmlNode defineConstants, string configuration, string platform)
87  {
88  if (defineConstants == null)
89  {
90  return;
91  }
92 
93  IEnumerable<string> symbols = defineConstants.InnerText.Split(';').Except(new[]
94  {
95  string.Empty,
99  }).Union(new[] { configuration.ToUpperInvariant() });
100 
101  defineConstants.InnerText = string.Join(";", symbols.ToArray());
102  //UnityEngine.Debug.LogFormat("Updating defines for Configuration|Platform: {0}|{1} => {2}", configuration, platform, defineConstants.InnerText);
103  }
104 
105  private static void WriteXmlDocumentToFile(XmlNode document, string fullPath)
106  {
107  FileStream fileStream = null;
108  try
109  {
110  fileStream = File.Open(fullPath, FileMode.Create);
111 
112  var settings = new XmlWriterSettings
113  {
114  Indent = true,
115  CloseOutput = true
116  };
117 
118  using (XmlWriter writer = XmlWriter.Create(fileStream, settings))
119  {
120  fileStream = null;
121  document.WriteTo(writer);
122  }
123  }
124  finally
125  {
126  if (fileStream != null)
127  {
128  fileStream.Dispose();
129  }
130  }
131  }
132  }
133 }
This class is designed to post process the UWP Assembly-CSharp projects to ensure that the defaults a...
static void Execute(string buildRootPath)
Executes the Post Processes on the C# Projects generated as part of the UWP build.
Class containing various utility methods to build a WSA solution from a Unity project.