public bool Execute(ClassData classData, XamlBuildTypeGenerationExtensionContext buildContext) { string className = !string.IsNullOrEmpty(classData.Namespace) ? classData.Namespace + "." + classData.Name : classData.Name; buildContext.XamlBuildLogger.LogMessage(MessageImportance.Low, SR.InspectingClass(typeof(BeforeInitializeComponentExtension).Name, className)); this.schemaContext = classData.EmbeddedResourceXaml.Writer.SchemaContext; if (!IsAssignableTo(classData.BaseType, typeof(Activity))) { return(true); } if (ArePartialMethodsSupported(buildContext)) { buildContext.XamlBuildLogger.LogMessage(MessageImportance.Low, SR.GeneratingBeforeInitializeComponent(typeof(BeforeInitializeComponentExtension).Name, className)); CodeCompileUnit myCodeCompileUnit = GenerateCompileUnit(classData.Namespace ?? string.Empty, classData, buildContext.Language); WriteGeneratedCode(classData, buildContext, myCodeCompileUnit, buildContext.Language); } else { buildContext.XamlBuildLogger.LogWarning(SR.UnsupportedLanguage(typeof(BeforeInitializeComponentExtension).Name, className)); } return(true); }
/// <summary> /// Update debug symbol with check sum. /// </summary> /// <param name="classData">Class data to add the checksum.</param> /// <param name="buildContext">Build context</param> /// <returns>Whether the execution succeeds</returns> public bool Execute(ClassData classData, XamlBuildTypeGenerationExtensionContext buildContext) { string className = !string.IsNullOrEmpty(classData.Namespace) ? classData.Namespace + "." + classData.Name : classData.Name; buildContext.XamlBuildLogger.LogMessage(MessageImportance.Low, SR.InspectingClass(typeof(DebugBuildExtension).Name, className)); this.UpdateDebugSymbol(classData, buildContext); return(true); }
static string GenerateHelperResourceFilename(ClassData classData, XamlBuildTypeGenerationExtensionContext buildContext, string language) { // Generate a resource file that contains the name of the resource holding the XAML. // [<rootns][.][namespace][_]classname_FileNameSuffix.txt StringBuilder builder = new StringBuilder(); if (string.Equals(language, "VB", StringComparison.OrdinalIgnoreCase)) { if (!string.IsNullOrWhiteSpace(buildContext.RootNamespace)) { builder.Append(buildContext.RootNamespace); } if (!string.IsNullOrWhiteSpace(classData.Namespace)) { if (builder.Length > 0) { builder.Append("."); } builder.Append(classData.Namespace); } if (builder.Length > 0) { builder.Append("_"); } } else { if (!string.IsNullOrWhiteSpace(classData.Namespace)) { builder.Append(classData.Namespace); } if (builder.Length > 0) { builder.Append("_"); } } builder.Append(string.Format(CultureInfo.InvariantCulture, "{0}_{1}.{2}", classData.Name, FileNameSuffix, "txt")); return(builder.ToString()); }
static void WriteGeneratedCode(ClassData classData, XamlBuildTypeGenerationExtensionContext buildContext, CodeCompileUnit compileUnit, string language) { using (CodeDomProvider codeDomProvider = CodeDomProvider.CreateProvider(buildContext.Language)) { string codeFileName = string.Format(CultureInfo.InvariantCulture, "{0}_{1}_{2}.{3}", classData.Namespace, classData.Name, FileNameSuffix, codeDomProvider.FileExtension); string codeFilePath = Path.Combine(buildContext.OutputPath, codeFileName); using (StreamWriter fileStream = new StreamWriter(codeFilePath)) { using (IndentedTextWriter tw = new IndentedTextWriter(fileStream)) { codeDomProvider.GenerateCodeFromCompileUnit(compileUnit, tw, new CodeGeneratorOptions()); } } buildContext.AddGeneratedFile(codeFilePath); // Generate a resource file that contains the name of the resource holding the XAML. string resourceFilePath = Path.Combine(buildContext.OutputPath, GenerateHelperResourceFilename(classData, buildContext, language)); string xamlResourceName = classData.EmbeddedResourceFileName; using (StreamWriter fileStream = new StreamWriter(resourceFilePath)) { // The first line of the resource is the Xaml resource name. fileStream.WriteLine(xamlResourceName); // The second line of the resource is the full class name of the Xaml helper class. // In VB, that name is prepended with the root namespace. string helperClassName = null; if (string.Equals(language, "VB", StringComparison.OrdinalIgnoreCase)) { helperClassName = string.Format(CultureInfo.InvariantCulture, "{0}.{1}", buildContext.RootNamespace, classData.HelperClassFullName); } else { helperClassName = classData.HelperClassFullName; } fileStream.WriteLine(helperClassName); } buildContext.AddGeneratedResourceFile(resourceFilePath); } }
private void UpdateDebugSymbol(ClassData classData, XamlBuildTypeGenerationExtensionContext buildContext) { string path = Path.GetFullPath(classData.FileName); try { using (XamlReader reader = classData.EmbeddedResourceXaml.GetReader()) { XamlNodeList newList = new XamlNodeList(reader.SchemaContext); using (XamlWriter writer = newList.Writer) { bool nodesAvailable = reader.Read(); while (nodesAvailable) { if (reader.NodeType == XamlNodeType.StartMember) { writer.WriteNode(reader); if (reader.Member.DeclaringType != null && reader.Member.DeclaringType.UnderlyingType != null && string.CompareOrdinal(reader.Member.DeclaringType.UnderlyingType.FullName, debugSymbolTypeFullName) == 0) { reader.Read(); string symbolString = reader.Value as string; if (!string.IsNullOrEmpty(symbolString)) { WorkflowSymbol symbol = WorkflowSymbol.Decode(symbolString); symbol.FileName = path; symbol.CalculateChecksum(); writer.WriteValue(symbol.Encode()); } else { writer.WriteValue(reader.Value); } } nodesAvailable = reader.Read(); } else { writer.WriteNode(reader); nodesAvailable = reader.Read(); } } } classData.EmbeddedResourceXaml = newList; } } catch (Exception e) { if (Fx.IsFatal(e)) { throw; } buildContext.XamlBuildLogger.LogMessage( MessageImportance.High, SR.DebugBuildExtensionExceptionPrefix( typeof(DebugBuildExtension).Name, path, e.Message)); } }
bool ArePartialMethodsSupported(XamlBuildTypeGenerationExtensionContext buildContext) { return(string.Equals(buildContext.Language, "C#", StringComparison.OrdinalIgnoreCase) || string.Equals(buildContext.Language, "VB", StringComparison.OrdinalIgnoreCase)); }