-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathConvertTo-PSCustomObject.ps1
73 lines (52 loc) · 1.54 KB
/
ConvertTo-PSCustomObject.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
function ConvertTo-PSCustomObject {
function New-ParamStmt {
param($tokens)
$newList = $tokens| ForEach {"`t`$"+$_}
@"
param(
$($newList -join ",`r`n")
)
"@
}
function New-PSCustomObject {
param($tokens)
$newList = $tokens| ForEach { "`t{0} = `${0}" -f $_ }
@"
[PSCustomObject]@{
$($newList -join "`r`n")
}
"@
}
function Get-NewToken {
param($line)
$results = (
[System.Management.Automation.PSParser]::Tokenize($line, [ref]$null) |
where {
$_.Type -match 'variable|member|command' -and
$_.Content -ne "_"
}
).Content
$(foreach($result in $results) { ConvertTo-CamelCase $result }) -join ''
}
function Get-EditorTokens {
$txt = $psISE.CurrentFile.Editor.SelectedText
if([string]::IsNullOrEmpty($txt)) {
$psISE.CurrentFile.Editor.SelectCaretLine()
$txt = $psISE.CurrentFile.Editor.SelectedText
Get-NewToken $txt
} else {
$lines = $txt.split("`n")
foreach($line in $lines) {
$line=$line.trim()
if([string]::IsNullOrEmpty($line)) {continue}
Get-NewToken $line
}
}
}
$EditorTokens=Get-EditorTokens
$t = $(
New-ParamStmt $EditorTokens
New-PSCustomObject $EditorTokens
)
$psISE.CurrentFile.Editor.InsertText($t)
}