|
| 1 | +//------------------------------------------------------------ |
| 2 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +//------------------------------------------------------------ |
| 4 | + |
| 5 | +namespace Microsoft.Activities.Build |
| 6 | +{ |
| 7 | + using System; |
| 8 | + using System.Activities; |
| 9 | + using System.CodeDom; |
| 10 | + using System.CodeDom.Compiler; |
| 11 | + using System.Diagnostics.CodeAnalysis; |
| 12 | + using System.Globalization; |
| 13 | + using System.IO; |
| 14 | + using System.Linq; |
| 15 | + using System.Reflection; |
| 16 | + using System.Xaml; |
| 17 | + using System.Xaml.Schema; |
| 18 | + using Microsoft.Build.Framework; |
| 19 | + using Microsoft.Build.Tasks.Xaml; |
| 20 | + using System.Runtime; |
| 21 | + using System.Text; |
| 22 | + |
| 23 | + class BeforeInitializeComponentExtension : IXamlBuildTypeGenerationExtension |
| 24 | + { |
| 25 | + const string FileNameSuffix = "BeforeInitializeComponentHelper"; |
| 26 | + |
| 27 | + XamlSchemaContext schemaContext; |
| 28 | + |
| 29 | + public bool Execute(ClassData classData, XamlBuildTypeGenerationExtensionContext buildContext) |
| 30 | + { |
| 31 | + string className = !string.IsNullOrEmpty(classData.Namespace) ? classData.Namespace + "." + classData.Name : classData.Name; |
| 32 | + buildContext.XamlBuildLogger.LogMessage(MessageImportance.Low, SR.InspectingClass(typeof(BeforeInitializeComponentExtension).Name, className)); |
| 33 | + |
| 34 | + this.schemaContext = classData.EmbeddedResourceXaml.Writer.SchemaContext; |
| 35 | + |
| 36 | + if (!IsAssignableTo(classData.BaseType, typeof(Activity))) |
| 37 | + { |
| 38 | + return true; |
| 39 | + } |
| 40 | + |
| 41 | + if (ArePartialMethodsSupported(buildContext)) |
| 42 | + { |
| 43 | + buildContext.XamlBuildLogger.LogMessage(MessageImportance.Low, SR.GeneratingBeforeInitializeComponent(typeof(BeforeInitializeComponentExtension).Name, className)); |
| 44 | + |
| 45 | + CodeCompileUnit myCodeCompileUnit = GenerateCompileUnit(classData.Namespace ?? string.Empty, classData, buildContext.Language); |
| 46 | + WriteGeneratedCode(classData, buildContext, myCodeCompileUnit, buildContext.Language); |
| 47 | + } |
| 48 | + else |
| 49 | + { |
| 50 | + buildContext.XamlBuildLogger.LogWarning(SR.UnsupportedLanguage(typeof(BeforeInitializeComponentExtension).Name, className)); |
| 51 | + } |
| 52 | + |
| 53 | + return true; |
| 54 | + } |
| 55 | + |
| 56 | + static void WriteGeneratedCode(ClassData classData, XamlBuildTypeGenerationExtensionContext buildContext, CodeCompileUnit compileUnit, string language) |
| 57 | + { |
| 58 | + using (CodeDomProvider codeDomProvider = CodeDomProvider.CreateProvider(buildContext.Language)) |
| 59 | + { |
| 60 | + string codeFileName = string.Format(CultureInfo.InvariantCulture, "{0}_{1}_{2}.{3}", classData.Namespace, classData.Name, FileNameSuffix, codeDomProvider.FileExtension); |
| 61 | + string codeFilePath = Path.Combine(buildContext.OutputPath, codeFileName); |
| 62 | + |
| 63 | + using (StreamWriter fileStream = new StreamWriter(codeFilePath)) |
| 64 | + { |
| 65 | + using (IndentedTextWriter tw = new IndentedTextWriter(fileStream)) |
| 66 | + { |
| 67 | + codeDomProvider.GenerateCodeFromCompileUnit(compileUnit, tw, new CodeGeneratorOptions()); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + buildContext.AddGeneratedFile(codeFilePath); |
| 72 | + |
| 73 | + // Generate a resource file that contains the name of the resource holding the XAML. |
| 74 | + string resourceFilePath = Path.Combine(buildContext.OutputPath, GenerateHelperResourceFilename(classData, buildContext, language)); |
| 75 | + |
| 76 | + string xamlResourceName = classData.EmbeddedResourceFileName; |
| 77 | + |
| 78 | + using (StreamWriter fileStream = new StreamWriter(resourceFilePath)) |
| 79 | + { |
| 80 | + // The first line of the resource is the Xaml resource name. |
| 81 | + fileStream.WriteLine(xamlResourceName); |
| 82 | + // The second line of the resource is the full class name of the Xaml helper class. |
| 83 | + // In VB, that name is prepended with the root namespace. |
| 84 | + string helperClassName = null; |
| 85 | + if (string.Equals(language, "VB", StringComparison.OrdinalIgnoreCase)) |
| 86 | + { |
| 87 | + helperClassName = string.Format(CultureInfo.InvariantCulture, "{0}.{1}", buildContext.RootNamespace, classData.HelperClassFullName); |
| 88 | + } |
| 89 | + else |
| 90 | + { |
| 91 | + helperClassName = classData.HelperClassFullName; |
| 92 | + } |
| 93 | + fileStream.WriteLine(helperClassName); |
| 94 | + } |
| 95 | + buildContext.AddGeneratedResourceFile(resourceFilePath); |
| 96 | + |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + static string GenerateHelperResourceFilename(ClassData classData, XamlBuildTypeGenerationExtensionContext buildContext, string language) |
| 101 | + { |
| 102 | + // Generate a resource file that contains the name of the resource holding the XAML. |
| 103 | + // [<rootns][.][namespace][_]classname_FileNameSuffix.txt |
| 104 | + StringBuilder builder = new StringBuilder(); |
| 105 | + if (string.Equals(language, "VB", StringComparison.OrdinalIgnoreCase)) |
| 106 | + { |
| 107 | + |
| 108 | + if (!string.IsNullOrWhiteSpace(buildContext.RootNamespace)) |
| 109 | + { |
| 110 | + builder.Append(buildContext.RootNamespace); |
| 111 | + } |
| 112 | + |
| 113 | + if (!string.IsNullOrWhiteSpace(classData.Namespace)) |
| 114 | + { |
| 115 | + if (builder.Length > 0) |
| 116 | + { |
| 117 | + builder.Append("."); |
| 118 | + } |
| 119 | + builder.Append(classData.Namespace); |
| 120 | + } |
| 121 | + |
| 122 | + if (builder.Length > 0) |
| 123 | + { |
| 124 | + builder.Append("_"); |
| 125 | + } |
| 126 | + } |
| 127 | + else |
| 128 | + { |
| 129 | + if (!string.IsNullOrWhiteSpace(classData.Namespace)) |
| 130 | + { |
| 131 | + builder.Append(classData.Namespace); |
| 132 | + } |
| 133 | + |
| 134 | + if (builder.Length > 0) |
| 135 | + { |
| 136 | + builder.Append("_"); |
| 137 | + } |
| 138 | + } |
| 139 | + builder.Append(string.Format(CultureInfo.InvariantCulture, "{0}_{1}.{2}", classData.Name, FileNameSuffix, "txt")); |
| 140 | + return builder.ToString(); |
| 141 | + } |
| 142 | + |
| 143 | + [SuppressMessage(FxCop.Category.Globalization, FxCop.Rule.DoNotPassLiteralsAsLocalizedParameters, |
| 144 | + Justification = "The string literals are code snippets, not localizable values.")] |
| 145 | + static CodeCompileUnit GenerateCompileUnit(string clrNamespace, ClassData classData, string language) |
| 146 | + { |
| 147 | + CodeTypeDeclaration typeDeclaration = new CodeTypeDeclaration |
| 148 | + { |
| 149 | + Name = classData.Name, |
| 150 | + IsPartial = true, |
| 151 | + TypeAttributes = classData.IsPublic ? TypeAttributes.Public : TypeAttributes.NotPublic, |
| 152 | + }; |
| 153 | + |
| 154 | + // Partial declarations are only supported in C# and VB |
| 155 | + string namespaceAndClassNameSnippet = null; |
| 156 | + if (string.Equals(language, "C#", StringComparison.OrdinalIgnoreCase)) |
| 157 | + { |
| 158 | + namespaceAndClassNameSnippet = CodeDomSnippets.BeforeInitializeComponentCS; |
| 159 | + } |
| 160 | + else if (string.Equals(language, "VB", StringComparison.OrdinalIgnoreCase)) |
| 161 | + { |
| 162 | + namespaceAndClassNameSnippet = CodeDomSnippets.BeforeInitializeComponentVB; |
| 163 | + } |
| 164 | + else |
| 165 | + { |
| 166 | + throw Fx.AssertAndThrow("This method should only have been called for VB or C#"); |
| 167 | + } |
| 168 | + |
| 169 | + namespaceAndClassNameSnippet = namespaceAndClassNameSnippet.Replace("ClassNameGoesHere", classData.Name); |
| 170 | + typeDeclaration.Members.Add(new CodeSnippetTypeMember(namespaceAndClassNameSnippet)); |
| 171 | + |
| 172 | + return new CodeCompileUnit |
| 173 | + { |
| 174 | + Namespaces = |
| 175 | + { |
| 176 | + new CodeNamespace |
| 177 | + { |
| 178 | + Name = clrNamespace, |
| 179 | + Types = |
| 180 | + { |
| 181 | + typeDeclaration |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + }; |
| 186 | + } |
| 187 | + |
| 188 | + bool IsAssignableTo(XamlType type, Type assignableTo) |
| 189 | + { |
| 190 | + XamlType liveXamlType = this.schemaContext.GetXamlType(assignableTo); |
| 191 | + XamlType rolXamlType = this.schemaContext.GetXamlType(new XamlTypeName(liveXamlType)); |
| 192 | + if (rolXamlType == null) |
| 193 | + { |
| 194 | + // assignableTo is not in the project references, so project must not be deriving from it |
| 195 | + return false; |
| 196 | + } |
| 197 | + return type.CanAssignTo(rolXamlType); |
| 198 | + } |
| 199 | + |
| 200 | + bool ArePartialMethodsSupported(XamlBuildTypeGenerationExtensionContext buildContext) |
| 201 | + { |
| 202 | + return string.Equals(buildContext.Language, "C#", StringComparison.OrdinalIgnoreCase) || |
| 203 | + string.Equals(buildContext.Language, "VB", StringComparison.OrdinalIgnoreCase); |
| 204 | + } |
| 205 | + |
| 206 | + static class CodeDomSnippets |
| 207 | + { |
| 208 | + public const string BeforeInitializeComponentCS = |
| 209 | +@" partial void BeforeInitializeComponent(ref bool isInitialized) |
| 210 | + { |
| 211 | + if (isInitialized == true) { |
| 212 | + return; |
| 213 | + } |
| 214 | +
|
| 215 | + System.Activities.XamlIntegration.ActivityXamlServices.InitializeComponent( |
| 216 | + typeof(ClassNameGoesHere), |
| 217 | + this |
| 218 | + ); |
| 219 | +
|
| 220 | + // Setting this will turn InitializeComponent into a no-op. |
| 221 | + isInitialized = true; |
| 222 | + } |
| 223 | +"; |
| 224 | + |
| 225 | + public const string BeforeInitializeComponentVB = |
| 226 | +@" Private Sub BeforeInitializeComponent(ByRef isInitialized as Boolean) |
| 227 | + If (isInitialized) |
| 228 | + return |
| 229 | + End If |
| 230 | +
|
| 231 | + System.Activities.XamlIntegration.ActivityXamlServices.InitializeComponent( |
| 232 | + GetType(ClassNameGoesHere), |
| 233 | + Me |
| 234 | + ) |
| 235 | +
|
| 236 | + isInitialized = true |
| 237 | + End Sub |
| 238 | +"; |
| 239 | + } |
| 240 | + } |
| 241 | +} |
| 242 | + |
0 commit comments