Skip to content

Commit c8a9e63

Browse files
committed
Adding Workflow Designer files to .NET Framework 4.7.2 sources
1 parent e287463 commit c8a9e63

File tree

183 files changed

+27814
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

183 files changed

+27814
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
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+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// <copyright file="DebugBuildExtension.cs" company="Microsoft Corporation">
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
// </copyright>
4+
5+
namespace Microsoft.Activities.Build.Debugger
6+
{
7+
using System;
8+
using System.Activities.Debugger.Symbol;
9+
using System.CodeDom;
10+
using System.IO;
11+
using System.Runtime;
12+
using System.Xaml;
13+
using Microsoft.Build.Framework;
14+
using Microsoft.Build.Tasks.Xaml;
15+
16+
/// <summary>
17+
/// Build Extension for Workflow debugger support.
18+
/// </summary>
19+
public class DebugBuildExtension : IXamlBuildTypeGenerationExtension
20+
{
21+
private static string debugSymbolTypeFullName = typeof(DebugSymbol).FullName;
22+
23+
/// <summary>
24+
/// Update debug symbol with check sum.
25+
/// </summary>
26+
/// <param name="classData">Class data to add the checksum.</param>
27+
/// <param name="buildContext">Build context</param>
28+
/// <returns>Whether the execution succeeds</returns>
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(DebugBuildExtension).Name, className));
33+
this.UpdateDebugSymbol(classData, buildContext);
34+
return true;
35+
}
36+
37+
private void UpdateDebugSymbol(ClassData classData, XamlBuildTypeGenerationExtensionContext buildContext)
38+
{
39+
string path = Path.GetFullPath(classData.FileName);
40+
try
41+
{
42+
using (XamlReader reader = classData.EmbeddedResourceXaml.GetReader())
43+
{
44+
XamlNodeList newList = new XamlNodeList(reader.SchemaContext);
45+
using (XamlWriter writer = newList.Writer)
46+
{
47+
bool nodesAvailable = reader.Read();
48+
while (nodesAvailable)
49+
{
50+
if (reader.NodeType == XamlNodeType.StartMember)
51+
{
52+
writer.WriteNode(reader);
53+
if (reader.Member.DeclaringType != null &&
54+
reader.Member.DeclaringType.UnderlyingType != null &&
55+
string.CompareOrdinal(reader.Member.DeclaringType.UnderlyingType.FullName, debugSymbolTypeFullName) == 0)
56+
{
57+
reader.Read();
58+
string symbolString = reader.Value as string;
59+
if (!string.IsNullOrEmpty(symbolString))
60+
{
61+
WorkflowSymbol symbol = WorkflowSymbol.Decode(symbolString);
62+
symbol.FileName = path;
63+
symbol.CalculateChecksum();
64+
writer.WriteValue(symbol.Encode());
65+
}
66+
else
67+
{
68+
writer.WriteValue(reader.Value);
69+
}
70+
}
71+
72+
nodesAvailable = reader.Read();
73+
}
74+
else
75+
{
76+
writer.WriteNode(reader);
77+
nodesAvailable = reader.Read();
78+
}
79+
}
80+
}
81+
82+
classData.EmbeddedResourceXaml = newList;
83+
}
84+
}
85+
catch (Exception e)
86+
{
87+
if (Fx.IsFatal(e))
88+
{
89+
throw;
90+
}
91+
92+
buildContext.XamlBuildLogger.LogMessage(
93+
MessageImportance.High,
94+
SR.DebugBuildExtensionExceptionPrefix(
95+
typeof(DebugBuildExtension).Name,
96+
path,
97+
e.Message));
98+
}
99+
}
100+
}
101+
}

0 commit comments

Comments
 (0)