Skip to content

Commit ec51e12

Browse files
Add Pode tools
Register auto completer for model Ids
1 parent 0b40782 commit ec51e12

6 files changed

+216
-29
lines changed

Diff for: CreateModuleManifest.ps1

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
1-
. .\DALLiteDBTools.ps1
2-
. .\ModelTools.ps1
1+
. "$PSScriptRoot\DALLiteDBTools.ps1"
2+
. "$PSScriptRoot\ModelTools.ps1"
3+
. "$PSScriptRoot\PodeTools.ps1"
34

45
$GUID = 'cc2b4f15-448b-4f48-9f1e-768fab346730'
56
$ModuleVersion = "0.0.1"
67

7-
$functions = Get-Command *-Model* -CommandType Function | Select-Object -ExpandProperty Name
8-
$functions += Get-Command *-DAL* -CommandType Function | Select-Object -ExpandProperty Name
8+
$functions = Get-Command *-Model* -CommandType Function -ListImported | Select-Object -ExpandProperty Name
9+
$functions += Get-Command *-DAL* -CommandType Function -ListImported | Select-Object -ExpandProperty Name
10+
$functions += Get-Command *-Pode* -CommandType Function -ListImported | Select-Object -ExpandProperty Name
911
$moduleParams = @{
1012
Path = ".\LOBTools.psd1"
1113
RootModule = ".\LOBTools.psm1"
1214
Author = "Code"
1315
CompanyName = "NumericOverflow"
1416
ModuleVersion = "0.0.1"
1517
Guid = $GUID
16-
Description = "Line of busines tools"
18+
Description = "Line Of Business tools"
1719
PowerShellVersion = "5.1"
1820
FunctionsToExport = $functions
1921
}
2022

2123
$PSData = "@{
22-
Tags = 'LOB','Line Of Business', 'LiteDB'
24+
Tags = 'LOB','Line Of Business', 'LiteDB', 'Pode'
2325
LicenseUri = 'https://raw.githubusercontent.com/code-numericoverflow/LOBTools/master/LOBTools/LICENSE.txt'
2426
ProjectUri = 'https://github.com/code-numericoverflow/LOBTools'
2527
IconUri = 'https://raw.githubusercontent.com/code-numericoverflow/LOBTools/master/icon.png'

Diff for: DALLiteDBTools.ps1

+26-12
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,20 @@ function Open-Connection {
5454
)
5555
$Script:DB = Open-LiteDBConnection -Database $Database -Mode Shared
5656
}
57+
58+
function Test-DatabaseConnection {
59+
if ($Script:DB -ne $null) {
60+
$true
61+
} else {
62+
$false
63+
}
64+
}
65+
66+
function Close-Connection {
67+
param (
68+
)
69+
Close-LiteDBConnection -Connection $Script:DB
70+
}
5771
'
5872
}
5973

@@ -66,7 +80,7 @@ function Get-DALLiteDBCollectionScript {
6680
$collections = @()
6781
}
6882
process {
69-
$collections += $classAst.Name | Get-Plural
83+
$collections += $classAst.Name | Get-ModelPlural
7084
}
7185
end {
7286
$collections = $collections -join """, """
@@ -90,7 +104,7 @@ function Get-DALLiteDBIndexScript {
90104
$source = $Reference.Source
91105
$target = $Reference.Target
92106
$sourceId = $source + "Id"
93-
$collection = $target | Get-Plural
107+
$collection = $target | Get-ModelPlural
94108
$indexes += "`tNew-LiteDBIndex -Connection `$Script:DB -Collection $collection -Field $sourceId"
95109
}
96110
end {
@@ -142,7 +156,7 @@ function Get-DALLiteDBAddScript {
142156
)
143157
process {
144158
$className = $classAst.Name
145-
$CollectionName = $className | Get-Plural
159+
$CollectionName = $className | Get-ModelPlural
146160
$script = "
147161
function Add-$className {
148162
param (
@@ -168,9 +182,9 @@ function Get-DALLiteDBGetScript {
168182
)
169183
process {
170184
$className = $classAst.Name
171-
$CollectionName = $className | Get-Plural
185+
$CollectionName = $className | Get-ModelPlural
172186
$parameterName = $className + "Id"
173-
$parameterPlural = $parameterName | Get-Plural
187+
$parameterPlural = $parameterName | Get-ModelPlural
174188
$script = "
175189
function Get-$className {
176190
#[OutputType([$className])]
@@ -213,9 +227,9 @@ function Get-DALLiteDBTestScript {
213227
)
214228
process {
215229
$className = $classAst.Name
216-
$CollectionName = $className | Get-Plural
230+
$CollectionName = $className | Get-ModelPlural
217231
$parameterName = $className + "Id"
218-
$parameterPlural = $parameterName | Get-Plural
232+
$parameterPlural = $parameterName | Get-ModelPlural
219233
$script = "
220234
function Test-$className {
221235
#[OutputType([Boolean])]
@@ -245,7 +259,7 @@ function Get-DALLiteDBUpdateScript {
245259
)
246260
process {
247261
$className = $classAst.Name
248-
$CollectionName = $className | Get-Plural
262+
$CollectionName = $className | Get-ModelPlural
249263
$parameterName = $className
250264
$script = "
251265
function Update-$className {
@@ -268,7 +282,7 @@ function Get-DALLiteDBUSetScript {
268282
)
269283
process {
270284
$className = $classAst.Name
271-
$CollectionName = $className | Get-Plural
285+
$CollectionName = $className | Get-ModelPlural
272286
$parameterName = $className
273287
$script = "
274288
function Set-$className {
@@ -291,9 +305,9 @@ function Get-DALLiteDBRemoveScript {
291305
)
292306
process {
293307
$className = $classAst.Name
294-
$CollectionName = $className | Get-Plural
308+
$CollectionName = $className | Get-ModelPlural
295309
$parameterName = $className + "Id"
296-
$parameterPlural = $parameterName | Get-Plural
310+
$parameterPlural = $parameterName | Get-ModelPlural
297311
$script = "
298312
function Remove-$className {
299313
param (
@@ -315,7 +329,7 @@ function Get-DALLiteDBReferenceGetScript {
315329
$source = $Reference.Source
316330
$target = $Reference.Target
317331
$sourceId = $source + "Id"
318-
$collection = $target | Get-Plural
332+
$collection = $target | Get-ModelPlural
319333
$script = "
320334
function Get-$source$collection {
321335
#[OutputType([$target])]

Diff for: LOBTools.psd1

+14-10
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#
44
# Generado por Code
55
#
6-
# Generado el 16/11/2021
6+
# Generado el 01/04/2022
77
#
88

99
@{
@@ -27,10 +27,10 @@ Author = 'Code'
2727
CompanyName = 'NumericOverflow'
2828

2929
# Instrucción de copyright de este módulo.
30-
Copyright = '(c) 2021 Code. Todos los derechos reservados.'
30+
Copyright = '(c) 2022 Code. Todos los derechos reservados.'
3131

3232
# Descripción de la funcionalidad proporcionada por este módulo.
33-
Description = 'Line of busines tools'
33+
Description = 'Line Of Business tools'
3434

3535
# Versión mínima del motor de Windows PowerShell requerida por este módulo.
3636
PowerShellVersion = '5.1'
@@ -70,12 +70,16 @@ PowerShellVersion = '5.1'
7070

7171
# Funciones para exportar desde este módulo; para conseguir el mejor rendimiento, no uses caracteres comodines ni elimines la entrada; usa una matriz vacía si no hay funciones que exportar.
7272
FunctionsToExport = 'Get-ModelBaseCreationScript', 'Get-ModelNewScript',
73-
'Get-ModelReference', 'Get-DALLiteDBAddScript', 'Get-DALLiteDBBase',
74-
'Get-DALLiteDBCollectionScript', 'Get-DALLiteDBCreationScript',
75-
'Get-DALLiteDBFindScript', 'Get-DALLiteDBIndexScript',
76-
'Get-DALLiteDBNewScript', 'Get-DALLiteDBReferenceFindScript',
77-
'Get-DALLiteDBRemoveScript', 'Get-DALLiteDBTestScript',
78-
'Get-DALLiteDBUpdateScript', 'Get-DALLiteDBUpsertScript'
73+
'Get-ModelPlural', 'Get-ModelReference',
74+
'Register-ModelAutocompleter', 'Get-DALLiteDBAddScript',
75+
'Get-DALLiteDBBase', 'Get-DALLiteDBCollectionScript',
76+
'Get-DALLiteDBCreationScript', 'Get-DALLiteDBGetScript',
77+
'Get-DALLiteDBIndexScript', 'Get-DALLiteDBNewScript',
78+
'Get-DALLiteDBReferenceGetScript', 'Get-DALLiteDBRemoveScript',
79+
'Get-DALLiteDBTestScript', 'Get-DALLiteDBUpdateScript',
80+
'Get-DALLiteDBUSetScript', 'Convert-PodeFunctionVerbToHttpMethod',
81+
'Convert-PodeFunctionVerbToHttpRules', 'Get-PodeADSessionScript',
82+
'Get-PodeCommandProxy'
7983

8084
# Cmdlets para exportar desde este módulo; para conseguir el mejor rendimiento, no uses caracteres comodines ni elimines la entrada; usa una matriz vacía si no hay cmdlets que exportar.
8185
CmdletsToExport = '*'
@@ -99,7 +103,7 @@ AliasesToExport = '*'
99103
PrivateData = @{
100104

101105
PSData = @{
102-
Tags = 'LOB','Line Of Business', 'LiteDB'
106+
Tags = 'LOB','Line Of Business', 'LiteDB', 'Pode'
103107
LicenseUri = 'https://raw.githubusercontent.com/code-numericoverflow/LOBTools/master/LOBTools/LICENSE.txt'
104108
ProjectUri = 'https://github.com/code-numericoverflow/LOBTools'
105109
IconUri = 'https://raw.githubusercontent.com/code-numericoverflow/LOBTools/master/icon.png'

Diff for: LOBTools.psm1

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
. "$PSScriptRoot\ModelTools.ps1"
22
. "$PSScriptRoot\DALLiteDBTools.ps1"
3+
. "$PSScriptRoot\PodeTools.ps1"

Diff for: ModelTools.ps1

+33-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using namespace System.Management.Automation.Language
22

3-
function Get-Plural {
3+
function Get-ModelPlural {
44
param (
55
[Parameter(ValueFromPipeline=$true, Mandatory=$true)]
66
[String] $Noum
@@ -90,3 +90,35 @@ function Get-ModelNewScript {
9090
$script
9191
}
9292
}
93+
94+
function Register-ModelAutocompleter {
95+
param (
96+
[Parameter(ValueFromPipeline=$true)]
97+
[String] $Module = "*.DAL.*"
98+
)
99+
process {
100+
Get-Command -Module $Module -CommandType Function | ForEach-Object {
101+
$function = $_
102+
$functionName = $_.Name
103+
$parameters = $_.Parameters
104+
$parameters.Keys | ForEach-Object {
105+
$parameterName = $_
106+
$parameterValue = $parameters[$_]
107+
if ($parameterName.EndsWith("Id") -and $parameterName.Length -gt 2) {
108+
Register-ArgumentCompleter -CommandName $functionName -ParameterName $parameterName -ScriptBlock {
109+
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
110+
$modelName = $parameterName.Substring(0, $parameterName.Length - 2)
111+
$script = [ScriptBlock]::Create("Get-$modelName -Text ""$wordToComplete""")
112+
$options = Invoke-Command -ScriptBlock $script
113+
$options | ForEach-Object {
114+
$descriptionValue = $_.Name
115+
$codeValue = $_.Id
116+
$extendedValue = "'$codeValue' <# $descriptionValue #>"
117+
[System.Management.Automation.CompletionResult]::new($extendedValue, $descriptionValue, "ParameterValue", $codeValue)
118+
}
119+
}
120+
}
121+
}
122+
}
123+
}
124+
}

Diff for: PodeTools.ps1

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
function Get-PodeCommandProxy {
2+
param (
3+
[Parameter(ValueFromPipeline=$true, Mandatory=$true)]
4+
[String] $CommandName,
5+
[String] $Endpoint = "`$(`$env:PodeEndpoint)",
6+
[String] $Session = "`$env:PodeSession"
7+
)
8+
process {
9+
$cmd = Get-Command $CommandName
10+
$cmdMetadata = [System.Management.Automation.CommandMetadata]::new($cmd)
11+
12+
#$help = [System.Management.Automation.ProxyCommand]::GetHelpComments($cmdMetadata) # TODO
13+
$binding = [System.Management.Automation.ProxyCommand]::GetCmdletBindingAttribute($cmdMetadata)
14+
$param = [System.Management.Automation.ProxyCommand]::GetParamBlock($cmdMetadata)
15+
$dyna = [System.Management.Automation.ProxyCommand]::GetDynamicParam($cmdMetadata) # TODO
16+
$begin = [System.Management.Automation.ProxyCommand]::GetBegin($cmdMetadata)
17+
$process = [System.Management.Automation.ProxyCommand]::GetProcess($cmdMetadata)
18+
$end = [System.Management.Automation.ProxyCommand]::GetEnd($cmdMetadata)
19+
20+
$verb = ($CommandName -split '-')[0]
21+
$method = Convert-PodeFunctionVerbToHttpMethod -Verb $verb
22+
23+
$customBegin = "
24+
try {
25+
`$wrappedCmd = `$ExecutionContext.InvokeCommand.GetCommand('Invoke-RestMethod', [System.Management.Automation.CommandTypes]::Cmdlet)
26+
`$params = @{}
27+
`$params.Add('Method', '$method')
28+
`$params.Add('Uri' , ""$Endpoint/$CommandName"")
29+
`$params.Add('Body' , `$PSBoundParameters)
30+
`$scriptCmd = {& `$wrappedCmd @params }
31+
`$steppablePipeline = `$scriptCmd.GetSteppablePipeline()
32+
`$steppablePipeline.Begin(`$myInvocation.ExpectingInput, `$ExecutionContext)
33+
} catch {
34+
throw
35+
}
36+
"
37+
$customProcess = "
38+
try {
39+
`$body = `$PSBoundParameters
40+
`$result = Invoke-RestMethod -Method $method -Uri ""$Endpoint/$CommandName"" -Body @{ Body = (ConvertTo-Json `$body) } -WebSession $Session
41+
`$result
42+
} catch {
43+
throw
44+
}
45+
"
46+
47+
"function $CommandName {
48+
$binding
49+
param (
50+
$param
51+
)
52+
process {
53+
$customProcess
54+
}
55+
}"
56+
}
57+
}
58+
59+
function Get-PodeADSessionScript {
60+
'
61+
function New-PodeADSession {
62+
[CmdletBinding()]
63+
param (
64+
[String] $Url = "$($env:PodeEndpoint)",
65+
[Parameter(Mandatory=$true)]
66+
[String] $Username = $env:PodeUsername,
67+
[Parameter(Mandatory=$true)]
68+
[String] $Password = $env:PodePassword
69+
)
70+
Invoke-RestMethod -Method Get -Uri $Url -SessionVariable session | Out-Null
71+
Invoke-RestMethod -Method Post -Uri "$Url/login" -Body @{ username = $Username; password = $Password } -WebSession $session | Out-Null
72+
$session
73+
}
74+
'
75+
}
76+
77+
function Convert-PodeFunctionVerbToHttpMethod {
78+
param (
79+
[Parameter()]
80+
[string]
81+
$Verb
82+
)
83+
84+
# if empty, just return default
85+
switch ($Verb) {
86+
{ $_ -iin @('Find', 'Format', 'Get', 'Join', 'Search', 'Select', 'Split', 'Measure', 'Ping', 'Test', 'Trace') } { 'GET' }
87+
{ $_ -iin @('Set') } { 'PUT' }
88+
{ $_ -iin @('Rename', 'Edit', 'Update') } { 'PATCH' }
89+
{ $_ -iin @('Clear', 'Close', 'Exit', 'Hide', 'Remove', 'Undo', 'Dismount', 'Unpublish', 'Disable', 'Uninstall', 'Unregister') } { 'DELETE' }
90+
Default { 'POST' }
91+
}
92+
}
93+
94+
function Convert-PodeFunctionVerbToHttpRules {
95+
param (
96+
[Parameter()]
97+
[string]
98+
$CommandName
99+
)
100+
# Add => Post /Plural
101+
# Get => Get /Plural
102+
# => Get /Plural /:id
103+
# => Get /Plural /:id/Plural2
104+
# Set => Put /Plural /:id
105+
# Remove => Delete /Plural /:Id
106+
# Test => Get /Singular/:id
107+
108+
109+
# if empty, just return default
110+
switch ($Verb) {
111+
{ $_ -iin @('Find', 'Format', 'Get', 'Join', 'Search', 'Select', 'Split', 'Measure', 'Ping', 'Test', 'Trace') } { 'GET' }
112+
{ $_ -iin @('Set') } { 'PUT' }
113+
{ $_ -iin @('Rename', 'Edit', 'Update') } { 'PATCH' }
114+
{ $_ -iin @('Clear', 'Close', 'Exit', 'Hide', 'Remove', 'Undo', 'Dismount', 'Unpublish', 'Disable', 'Uninstall', 'Unregister') } { 'DELETE' }
115+
Default { 'POST' }
116+
}
117+
}
118+
119+
# BODY AS JSON
120+
121+
# $parameters["Body"] | Out-PodeHost
122+
# $body = ConvertFrom-Json $parameters["Body"]
123+
# $bodyParameters = ConvertTo-Hashtable $body
124+
# $newParameters = @{}
125+
# $bodyParameters.GetEnumerator() | ForEach-Object {
126+
# $isPresent = $_.Value.IsPresent
127+
# if ($isPresent -ne $null -and $_.Value.IsPresent) {
128+
# $newParameters += @{ "$($_.Name)" = $true }
129+
# } elseif ($isPresent -ne $null -and -not $_.Value.IsPresent) {
130+
# $newParameters += @{ "$($_.Name)" = $false }
131+
# } else {
132+
# $newParameters += @{ "$($_.Name)" = $_.Value }
133+
# }
134+
# }

0 commit comments

Comments
 (0)