forked from gildas/posh-ic
-
Notifications
You must be signed in to change notification settings - Fork 2
/
__Add-Types.ps1
121 lines (110 loc) · 3.69 KB
/
__Add-Types.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
$AssemblyPath = New-Item -Force -ItemType Directory -Path ([io.path]::combine($env:AppData, 'Interactive Intelligence', 'Posh-IC'))
$TypeName = 'ININ.ConnectionState' # {{{
if (-not ([System.Management.Automation.PSTypeName] $TypeName).Type)
{
$TypeAssembly = Join-Path $AssemblyPath "${TypeName}.dll"
Add-Type -OutputAssembly $TypeAssembly -OutputType Library -TypeDefinition @'
namespace ININ
{
public enum ConnectionState
{
None,
Up,
Down
};
}
'@
[System.Reflection.Assembly]::LoadFile($TypeAssembly) | Out-Null
Write-Verbose "Added New Type: $TypeName"
} # }}}
$TypeName = 'ININ.ICUser' # {{{
if (-not ([System.Management.Automation.PSTypeName] $TypeName).Type)
{
$TypeAssembly = Join-Path $AssemblyPath "${TypeName}.dll"
Add-Type -OutputAssembly $TypeAssembly -OutputType Library -TypeDefinition @'
using System;
using System.Management.Automation;
namespace ININ
{
public class ICUser
{
public string id { get; set; }
public string display { get; set; }
};
public class ICUserConverter : PSTypeConverter
{
public override bool CanConvertFrom(Object p_source, Type p_type)
{
return (p_source as string) != null;
}
public override object ConvertFrom(object p_source, Type p_type, IFormatProvider p_provider, bool p_ignore_case)
{
if (p_source == null) throw new InvalidCastException("no conversion possible");
if (CanConvertFrom(p_source, p_type))
{
try
{
string value = p_source as string;
ICUser icuser = new ICUser { id = value, display = value };
return icuser;
}
catch(Exception)
{
throw new InvalidCastException("no conversion possible");
}
}
throw new InvalidCastException("no conversion possible");
}
public override bool CanConvertTo(Object p_source, Type p_type)
{
return p_type == typeof(string);
}
public override object ConvertTo(object p_source, Type p_type, IFormatProvider p_provider, bool p_ignore_case)
{
if (CanConvertFrom(p_source, p_type))
{
try
{
ICUser icuser = p_source as ICUser;
return icuser.id;
}
catch(Exception)
{
throw new InvalidCastException("no conversion possible");
}
}
throw new InvalidCastException("no conversion possible");
}
}
}
'@
[System.Reflection.Assembly]::LoadFile($TypeAssembly) | Out-Null
Update-TypeData -TypeName $TypeName -TypeConverter "${TypeName}Converter"
Write-Verbose "Added New Type: $TypeName"
} # }}}
$TypeName='ININ.ICSession' # {{{
if (-not ([System.Management.Automation.PSTypeName] $TypeName).Type)
{
$TypeAssembly = Join-Path $AssemblyPath "${TypeName}.dll"
$refs = (Join-Path $AssemblyPath 'ININ.ICUser.dll'), 'Microsoft.Powershell.Commands.Utility'
Add-Type -OutputAssembly $TypeAssembly -OutputType Library -ReferencedAssemblies $refs -TypeDefinition @'
using ININ;
using Microsoft.PowerShell.Commands;
namespace ININ
{
public class ICSession
{
public string id { get; set; }
public string token { get; set; }
public string baseUrl { get; set; }
public string server { get; set; }
public string[] servers { get; set; }
public ICUser user { get; set; }
public string language { get; set; }
public Microsoft.PowerShell.Commands.WebRequestSession webSession { get; set; }
};
}
'@
[System.Reflection.Assembly]::LoadFile($TypeAssembly) | Out-Null
Write-Verbose "Added New Type: $TypeName"
} # }}}