-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathHelper.cs
110 lines (97 loc) · 4.1 KB
/
Helper.cs
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
using Microsoft.CodeAnalysis;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Generator
{
public static class Helper
{
public static Guid EncodeGuid(byte[] data)
{
if (BitConverter.IsLittleEndian)
{
// swap bytes of int a
byte t = data[0];
data[0] = data[3];
data[3] = t;
t = data[1];
data[1] = data[2];
data[2] = t;
// swap bytes of short b
t = data[4];
data[4] = data[5];
data[5] = t;
// swap bytes of short c and encode rfc time/version field
t = data[6];
data[6] = data[7];
data[7] = (byte)((t & 0x0f) | (5 << 4));
// encode rfc clock/reserved field
data[8] = (byte)((data[8] & 0x3f) | 0x80);
}
return new Guid(data.Take(16).ToArray());
}
}
class AttributeDataComparer : IEqualityComparer<AttributeData>
{
public bool Equals(AttributeData x, AttributeData y)
{
return x.ToString() == y.ToString();
}
public int GetHashCode(AttributeData obj)
{
return obj.ToString().GetHashCode();
}
}
static class GeneratorExecutionContextHelper
{
public static string GetAssemblyName(this GeneratorExecutionContext context)
{
context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.AssemblyName", out var assemblyName);
return assemblyName;
}
public static string GetAssemblyVersion(this GeneratorExecutionContext context)
{
context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.AssemblyVersion", out var assemblyVersion);
return assemblyVersion;
}
public static string GetGeneratedFilesDir(this GeneratorExecutionContext context)
{
context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.CsWinRTGeneratedFilesDir", out var generatedFilesDir);
Directory.CreateDirectory(generatedFilesDir);
return generatedFilesDir;
}
public static bool IsCsWinRTComponent(this GeneratorExecutionContext context)
{
if (context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.CsWinRTComponent", out var isCsWinRTComponentStr))
{
return bool.TryParse(isCsWinRTComponentStr, out var isCsWinRTComponent) && isCsWinRTComponent;
}
return false;
}
public static string GetCsWinRTExe(this GeneratorExecutionContext context)
{
context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.CsWinRTExe", out var cswinrtExe);
return cswinrtExe;
}
public static bool GetKeepGeneratedSources(this GeneratorExecutionContext context)
{
context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.CsWinRTKeepGeneratedSources", out var keepGeneratedSourcesStr);
return keepGeneratedSourcesStr != null && bool.TryParse(keepGeneratedSourcesStr, out var keepGeneratedSources) && keepGeneratedSources;
}
public static string GetCsWinRTWindowsMetadata(this GeneratorExecutionContext context)
{
context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.CsWinRTWindowsMetadata", out var cswinrtWindowsMetadata);
return cswinrtWindowsMetadata;
}
public static string GetCsWinRTDependentMetadata(this GeneratorExecutionContext context)
{
context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.CsWinRTAuthoringInputs", out var winmds);
return winmds;
}
public static string GetWinmdOutputFile(this GeneratorExecutionContext context)
{
return Path.Combine(context.GetGeneratedFilesDir(), context.GetAssemblyName() + ".winmd");
}
}
}