-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenerateAPIShims.ps1
125 lines (91 loc) · 3.18 KB
/
GenerateAPIShims.ps1
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<#
.SYNOPSIS
Generates a C# file that redefines all the exported types in the given
packages.
The packages will be fetched in ~/.nuget/packages, and C# code will be emitted
to the standard output. The C# code does not have any implementation, but defines types
in a way that they can be used in a .csx script.
#>
param(
[string] $NugetPackagesDirectory = "$env:USERPROFILE\.nuget\packages",
[string[]] $Packages = @('System.Reactive', 'Mono.Cecil')
)
. $PSScriptRoot\Common.ps1
function Get-TypeName {
param([type] $Type)
$Name = $Type.Name
if (-not $Name.Contains('`')) {
return $Name
}
$Name = $Name.Substring(0, $Name.IndexOf('`'))
$Name += '<'
$Type.GetGenericArguments() | select -SkipLast 1 | % {
$Name += "$(Get-TypeName $_), "
}
$Type.GetGenericArguments() | select -Last 1 | % {
$Name += Get-TypeName $_
}
return $Name + '>'
}
function Write-Output {
Write-Host $Args -NoNewline
}
$Packages | % {
$PackageDirBase = "$NugetPackagesDirectory\$($_.ToLower())"
$PackageDir = Get-ChildItem $PackageDirBase -Directory `
| sort -Property DirectoryName -Descending `
| select -First 1
$PackageDll = Get-ChildItem $PackageDir.FullName -Recurse -File -Filter "$_.dll" `
| select -Last 1
Write-Output "[i] Loading $PackageDll..." -ForegroundColor Blue
$Assembly = [Reflection.Assembly]::LoadFrom($PackageDll.FullName)
Write-Output "[+] Loaded $Assembly." -ForegroundColor Green
$Assembly.ExportedTypes | ? { $_.IsPublic } | % {
if ($_.IsStatic) { Write-Output 'public static ' }
else { Write-Output 'public ' }
Write-Output "class $(Get-TypeName $_)`n{`n"
$_.DeclaredMethods | ? { $_.IsPublic -and $_.Name -notlike '*_*' } | % {
Write-Output ' '
if ($_.IsStatic) { Write-Output 'public static ' }
else { Write-Output 'public ' }
Write-Output "$(Get-TypeName $_.ReturnType) $($_.Name)"
$Generics = $_.GetGenericArguments()
if ($Generics) {
Write-Output '<'
$Generics | select -SkipLast 1 | % {
Write-Output "$($_.Name), "
}
$Generics | select -Last 1 | % {
Write-Output $_.Name
}
Write-Output '>'
}
Write-Output '('
if ($_.CustomAttributes | ? { $_.AttributeType.Name -eq 'ExtensionAttribute' }) {
Write-Output 'this '
}
$_.GetParameters() | select -SkipLast 1 | % {
Write-Output "$(Get-TypeName $_.ParameterType) $($_.Name), "
}
$_.GetParameters() | select -Last 1 | % {
Write-Output "$(Get-TypeName $_.ParameterType) $($_.Name)"
}
Write-Output ") => throw new NotImplementedException();`n"
}
$_.DeclaredProperties | % {
Write-Output ' '
if ($_.IsStatic) { Write-Output 'public static ' }
else { Write-Output 'public ' }
Write-Output "$(Get-TypeName $_.PropertyType) $($_.Name) `n {`n"
if ($_.GetMethod) {
Write-Output " get => throw new NotImplementedException();`n"
}
if ($_.SetMethod) {
Write-Output " set => throw new NotImplementedException();`n"
}
Write-Output " }`n"
}
Write-Output "}`n`n"
}
Write-Output
}