Skip to content

Commit ec95224

Browse files
Friedrich WeinmannFriedrich Weinmann
authored andcommitted
Implemented administrastive logic
1 parent 176e51e commit ec95224

File tree

8 files changed

+352
-9
lines changed

8 files changed

+352
-9
lines changed

PSModuleDevelopment/PSModuleDevelopment.psd1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@
6969
'Get-PSMDConstructor',
7070
'Get-PSMDHelpEx',
7171
'Get-PSMDModuleDebug',
72+
'Get-PSMDTemplate',
7273
'Import-PSMDModuleDebug',
74+
'Invoke-PSMDTemplate',
7375
'Measure-PSMDCommandEx',
7476
'New-PSMDDotNetProject',
7577
'New-PSMDHeader',
@@ -78,6 +80,7 @@
7880
'New-PSMDTemplate',
7981
'New-PssModuleProject',
8082
'Remove-PSMDModuleDebug',
83+
'Remove-PSMDTemplate',
8184
'Rename-PSMDParameter',
8285
'Restart-PSMDShell',
8386
'Set-PSMDModuleDebug',

PSModuleDevelopment/en-us/about_psmoduledevelopment.help.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,12 @@ LONG DESCRIPTION
165165
# Changelog #
166166
#-------------------------------------------------------------------------#
167167

168+
2.2.0.10 (???)
169+
- new: Command New-PSMDTemplate
170+
- new: Command Get-PSMDTemplate
171+
- new: Command Invoke-PSMDTemplate
172+
- new: Command Remove-PSMDTemplate
173+
168174
2.1.1.3 (February 06th, 2018)
169175
- new: Command New-PSMDModuleNugetPackage
170176
A command that takes a module and writes it to a Nuget package.
Lines changed: 173 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,189 @@
11
function Get-PSMDTemplate
22
{
3-
[CmdletBinding()]
3+
<#
4+
.SYNOPSIS
5+
Search for templates to create from.
6+
7+
.DESCRIPTION
8+
Search for templates to create from.
9+
10+
.PARAMETER TemplateName
11+
The name of the template to search for.
12+
Templates are filtered by this using wildcard comparison.
13+
Defaults to "*" (everything).
14+
15+
.PARAMETER Store
16+
The template store to retrieve tempaltes from.
17+
By default, all stores are queried.
18+
19+
.PARAMETER Path
20+
Instead of a registered store, look in this path for templates.
21+
22+
.PARAMETER Tags
23+
Only return templates with the following tags.
24+
25+
.PARAMETER Author
26+
Only return templates by this author.
27+
28+
.PARAMETER MinimumVersion
29+
Only return templates with at least this version.
30+
31+
.PARAMETER RequiredVersion
32+
Only return templates with exactly this version.
33+
34+
.PARAMETER All
35+
Return all versions found.
36+
By default, only the latest matching version of a template will be returned.
37+
38+
.PARAMETER EnableException
39+
Replaces user friendly yellow warnings with bloody red exceptions of doom!
40+
Use this if you want the function to throw terminating errors you want to catch.
41+
42+
.EXAMPLE
43+
PS C:\> Get-PSMDTemplate
44+
45+
Returns all templates
46+
47+
.EXAMPLE
48+
PS C:\> Get-PSMDTemplate -TemplateName module
49+
50+
Returns the latest version of the template named module.
51+
#>
52+
[CmdletBinding(DefaultParameterSetName = 'Store')]
453
Param (
54+
[Parameter(Position = 0)]
55+
[string]
56+
$TemplateName = "*",
57+
58+
[Parameter(ParameterSetName = 'Store')]
559
[string]
6-
$TemplateName
60+
$Store = "*",
61+
62+
[Parameter(Mandatory = $true, ParameterSetName = 'Path')]
63+
[string]
64+
$Path,
65+
66+
[string[]]
67+
$Tags,
68+
69+
[string]
70+
$Author,
71+
72+
[version]
73+
$MinimumVersion,
74+
75+
[version]
76+
$RequiredVersion,
77+
78+
[switch]
79+
$All,
80+
81+
[switch]
82+
$EnableException
783
)
884

985
begin
1086
{
11-
Write-PSFMessage -Level InternalComment -Message "Bound parameters: $($PSBoundParameters.Keys -join ", ")" -Tag 'debug','start','param'
87+
Write-PSFMessage -Level InternalComment -Message "Bound parameters: $($PSBoundParameters.Keys -join ", ")" -Tag 'debug', 'start', 'param'
88+
89+
$prospects = @()
1290
}
1391
process
1492
{
15-
93+
#region Scan folders
94+
if (Test-PSFParameterBinding -ParameterName "Path")
95+
{
96+
$templateInfos = Get-ChildItem -Path $Path -Filter "$($TemplateName)-*.Info.xml" | Where-Object { ($_.Name -replace "-\d+(\.\d+){0,3}.Info.xml$") -like $TemplateName }
97+
98+
foreach ($info in $templateInfos)
99+
{
100+
$data = Import-Clixml $info.FullName
101+
$data.Path = $info.FullName -replace '\.Info\.xml$','.xml'
102+
$prospects += $data
103+
}
104+
}
105+
106+
else
107+
{
108+
$stores = Get-PsmdTemplateStore -Filter $Store
109+
110+
foreach ($item in $stores)
111+
{
112+
if ($item.Ensure())
113+
{
114+
$templateInfos = Get-ChildItem -Path $item.Path -Filter "$($TemplateName)-*.Info.xml" | Where-Object { ($_.Name -replace "-\d+(\.\d+){0,3}.Info.xml$") -like $TemplateName }
115+
116+
foreach ($info in $templateInfos)
117+
{
118+
$data = Import-Clixml $info.FullName
119+
$data.Path = $info.FullName -replace '\.Info\.xml$', '.xml'
120+
$data.Store = $item.Name
121+
$prospects += $data
122+
}
123+
}
124+
# If the user asked for a specific store, it should error out on him
125+
elseif ($item.Name -eq $Store)
126+
{
127+
Stop-PSFFunction -Message "Could not find store $Store" -EnableException $EnableException -Category OpenError -Tag 'fail','template','store','open'
128+
return
129+
}
130+
}
131+
}
132+
#endregion Scan folders
16133
}
17134
end
18135
{
19-
136+
$filteredProspects = @()
137+
138+
#region Apply filters
139+
foreach ($prospect in $prospects)
140+
{
141+
if ($Author)
142+
{
143+
if ($prospect.Author -notlike $Author) { continue }
144+
}
145+
if (Test-PSFParameterBinding -ParameterName MinimumVersion)
146+
{
147+
if ($prospect.Version -lt $MinimumVersion) { continue }
148+
}
149+
if (Test-PSFParameterBinding -ParameterName RequiredVersion)
150+
{
151+
if ($prospect.Version -ne $RequiredVersion) { continue }
152+
}
153+
if ($Tags)
154+
{
155+
$test = $false
156+
foreach ($tag in $Tags)
157+
{
158+
if ($prospect.Tags -contains $tag)
159+
{
160+
$test = $true
161+
break
162+
}
163+
}
164+
if (-not $test) { continue }
165+
}
166+
167+
$filteredProspects += $prospect
168+
}
169+
#endregion Apply filters
170+
171+
#region Return valid templates
172+
if ($All) { return $filteredProspects }
173+
174+
$prospectHash = @{ }
175+
foreach ($prospect in $filteredProspects)
176+
{
177+
if ($prospectHash.Keys -notcontains $prospect.Name)
178+
{
179+
$prospectHash[$prospect.Name] = $prospect
180+
}
181+
elseif ($prospectHash[$prospect.Name].Version -lt $prospect.Version)
182+
{
183+
$prospectHash[$prospect.Name] = $prospect
184+
}
185+
}
186+
$prospectHash.Values
187+
#endregion Return valid templates
20188
}
21189
}

PSModuleDevelopment/functions/templating/New-PSMDTemplate.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,10 @@
487487
}
488488
#endregion File
489489

490+
# Set identifier, so that Invoke-PSMDTemplate knows what to use when creating the item
491+
# Needed for sharing templates between users with different identifiers
492+
$object.Identifier = $Identifier
493+
490494
if ($Parent)
491495
{
492496
$null = $Parent.Children.Add($object)

PSModuleDevelopment/functions/templating/Remove-PSMDTemplate.ps1

Lines changed: 138 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,151 @@
11
function Remove-PSMDTemplate
22
{
3-
[CmdletBinding()]
4-
Param (
3+
<#
4+
.SYNOPSIS
5+
Removes templates
6+
7+
.DESCRIPTION
8+
This function removes templates used in the PSModuleDevelopment templating system.
9+
10+
.PARAMETER Template
11+
A template object returned by Get-PSMDTemplate.
12+
Will clear exactly the version specified, from exactly its location.
13+
14+
.PARAMETER TemplateName
15+
The name of the template to remove.
16+
Templates are filtered by this using wildcard comparison.
17+
18+
.PARAMETER Store
19+
The template store to retrieve tempaltes from.
20+
By default, all stores are queried.
21+
22+
.PARAMETER Path
23+
Instead of a registered store, look in this path for templates.
24+
25+
.PARAMETER Deprecated
26+
Will delete all versions of matching templates except for the latest one.
27+
Note:
28+
If the same template is found in multiple stores, it will keep a single copy across all stores.
29+
To process by store, be sure to specify the store parameter and loop over the stores desired.
530
31+
.PARAMETER EnableException
32+
Replaces user friendly yellow warnings with bloody red exceptions of doom!
33+
Use this if you want the function to throw terminating errors you want to catch.
34+
35+
.EXAMPLE
36+
PS C:\> Remove-PSMDTemplate -TemplateName '*' -Deprecated
37+
38+
Remove all templates that have been superseded by a newer version.
39+
40+
.EXAMPLE
41+
PS C:\> Get-PSMDTemplate -TemplateName 'module' -RequiredVersion '1.2.2.1' | Remove-PSMDTemplate
42+
43+
Removes all copies of the template 'module' with exactly the version '1.2.2.1'
44+
#>
45+
[CmdletBinding(DefaultParameterSetName = 'NameStore', SupportsShouldProcess = $true, ConfirmImpact = 'High')]
46+
Param (
47+
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = 'Template')]
48+
[PSModuleDevelopment.Template.TemplateInfo[]]
49+
$Template,
50+
51+
[Parameter(Mandatory = $true, Position = 0, ParameterSetName = 'NameStore')]
52+
[Parameter(Mandatory = $true, Position = 0, ParameterSetName = 'NamePath')]
53+
[string]
54+
$TemplateName,
55+
56+
[Parameter(ParameterSetName = 'NameStore')]
57+
[string]
58+
$Store = "*",
59+
60+
[Parameter(Mandatory = $true, ParameterSetName = 'NamePath')]
61+
[string]
62+
$Path,
63+
64+
[Parameter(ParameterSetName = 'NameStore')]
65+
[Parameter(ParameterSetName = 'NamePath')]
66+
[switch]
67+
$Deprecated,
68+
69+
[switch]
70+
$EnableException
671
)
772

873
begin
974
{
10-
Write-PSFMessage -Level InternalComment -Message "Bound parameters: $($PSBoundParameters.Keys -join ", ")" -Tag 'debug','start','param'
75+
Write-PSFMessage -Level InternalComment -Message "Bound parameters: $($PSBoundParameters.Keys -join ", ")" -Tag 'debug', 'start', 'param'
76+
77+
$templates = @()
78+
switch ($PSCmdlet.ParameterSetName)
79+
{
80+
'NameStore' { $templates = Get-PSMDTemplate -TemplateName $TemplateName -Store $Store -All }
81+
'NamePath' { $templates = Get-PSMDTemplate -TemplateName $TemplateName -Path $Path -All }
82+
}
83+
if ($Deprecated)
84+
{
85+
$toKill = @()
86+
$toKeep = @{ }
87+
foreach ($item in $templates)
88+
{
89+
if ($toKeep.Keys -notcontains $item.Name) { $toKeep[$item.Name] = $item }
90+
elseif ($toKeep[$item.Name].Version -lt $item.Version)
91+
{
92+
$toKill += $toKeep[$item.Name]
93+
$toKeep[$item.Name] = $item
94+
}
95+
else { $toKill += $item}
96+
}
97+
$templates = $toKill
98+
}
99+
100+
function Remove-Template
101+
{
102+
<#
103+
.SYNOPSIS
104+
Deletes the files associated with a given template.
105+
106+
.DESCRIPTION
107+
Deletes the files associated with a given template.
108+
Takes objects returned by Get-PSMDTemplate.
109+
110+
.PARAMETER Template
111+
The template to kill.
112+
113+
.EXAMPLE
114+
PS C:\> Remove-Template -Template $template
115+
116+
Removes the template stored in $template
117+
#>
118+
[CmdletBinding()]
119+
Param (
120+
[PSModuleDevelopment.Template.TemplateInfo]
121+
$Template
122+
)
123+
124+
$pathFile = $Template.Path
125+
$pathInfo = $Template.Path -replace '\.xml$', '.Info.xml'
126+
127+
Remove-Item $pathInfo -Force -ErrorAction Stop
128+
Remove-Item $pathFile -Force -ErrorAction Stop
129+
}
11130
}
12131
process
13132
{
14-
133+
foreach ($item in $Template)
134+
{
135+
if ($PSCmdlet.ShouldProcess($item, "Remove template"))
136+
{
137+
try { Remove-Template -Template $item }
138+
catch { Stop-PSFFunction -Message "Failed to remove template $($item)" -EnableException $EnableException -ErrorRecord $_ -Target $item -Tag 'fail', 'template', 'remove' -Continue }
139+
}
140+
}
141+
foreach ($item in $templates)
142+
{
143+
if ($PSCmdlet.ShouldProcess($item, "Remove template"))
144+
{
145+
try { Remove-Template -Template $item }
146+
catch { Stop-PSFFunction -Message "Failed to remove template $($item)" -EnableException $EnableException -ErrorRecord $_ -Target $item -Tag 'fail', 'template', 'remove' -Continue }
147+
}
148+
}
15149
}
16150
end
17151
{

0 commit comments

Comments
 (0)