Skip to content

Commit d23e3d2

Browse files
committed
Initial setup of Set-ResourceReadMe
1 parent b9c45c8 commit d23e3d2

File tree

2 files changed

+205
-0
lines changed

2 files changed

+205
-0
lines changed

resources/Microsoft.VSCode.Dsc/Microsoft.VSCode.Dsc.psm1

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,39 @@ class VSCodeExtension {
313313
[void] Uninstall() {
314314
$this.Uninstall($true)
315315
}
316+
317+
static [hashtable] GetResourceInfo() {
318+
return @{
319+
Description = 'The `VSCodeExtension` DSC Resource allows you to install, update, and remove Visual Studio Code extensions. This resource ensures that the specified Visual Studio Code extension is in the desired state.'
320+
# TODO: Add other fields like Synopsis, Notes, etc.
321+
}
322+
}
323+
324+
static [hashtable] GetResourcePropertyInfo() {
325+
# TODO: We can add JSON schemas for the properties and additionally allowed values if needed
326+
return @{
327+
Name = @{
328+
Description = 'The name of the Visual Studio Code extension to manage.'
329+
Type = 'string'
330+
Required = $true
331+
}
332+
Version = @{
333+
Description = 'The version of the Visual Studio Code extension to install. If not specified, the latest version will be installed.'
334+
Type = 'string'
335+
Required = $false
336+
}
337+
Exist = @{
338+
Description = 'Indicates whether the extension should exist. The default value is `$true`.'
339+
Type = 'bool'
340+
Required = $false
341+
}
342+
Insiders = @{
343+
Description = 'Indicates whether to manage the extension for the Insiders version of Visual Studio Code. The default value is `$false`.'
344+
Type = 'bool'
345+
Required = $false
346+
}
347+
}
348+
}
316349
#endregion VSCodeExtension helper functions
317350
}
318351
#endregion DSCResources
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
function Initialize-ResourceFile {
2+
3+
[CmdletBinding()]
4+
param (
5+
[Parameter(Mandatory = $true)]
6+
[string] $ModuleName,
7+
8+
[Parameter(Mandatory = $true)]
9+
[string] $ResourceName,
10+
11+
[Parameter(Mandatory = $true)]
12+
[string] $Synopsis,
13+
14+
[Parameter(Mandatory = $true)]
15+
[string] $Description
16+
)
17+
18+
$initialContent = @(
19+
'---',
20+
"external help file: $ModuleName-Help.xml",
21+
"Module Name: $ModuleName",
22+
"ms.date: $(Get-Date -Format 'MM/dd/yyyy')",
23+
"online version: $null",
24+
'schema: 2.0.0',
25+
"title: $ResourceName",
26+
'---',
27+
'',
28+
"# $ResourceName",
29+
'',
30+
'## SYNOPSIS',
31+
'',
32+
$Synopsis,
33+
'',
34+
'## DESCRIPTION',
35+
'',
36+
$Description,
37+
''
38+
) | Where-Object { $null -ne $_ } # Filter null values
39+
$readMeFileContent = $initialContent
40+
41+
return $readMeFileContent
42+
}
43+
44+
function Get-TypeInstanceFromModule {
45+
param(
46+
[Parameter(Mandatory = $true)]
47+
[string] $ModuleName,
48+
[Parameter(Mandatory = $true)]
49+
[string] $ClassName
50+
)
51+
$instance = & (Import-Module $ModuleName -PassThru) ([scriptblock]::Create("'$ClassName' -as 'type'"))
52+
return $instance
53+
}
54+
55+
function Set-ResourcePropertyParameters {
56+
[CmdletBinding()]
57+
param (
58+
[Parameter(Mandatory = $true)]
59+
[hashtable] $ParameterFileContent,
60+
61+
[Parameter(Mandatory = $true)]
62+
[object[]] $FileContent
63+
)
64+
65+
$parameterSection = @(
66+
'## PARAMETERS',
67+
'',
68+
'| **Parameter** | **Attribute** | **DataType** | **Description** | **Allowed values** |',
69+
'| :------------ | :------------ | :----------- | :-------------- | :----------------- |'
70+
)
71+
72+
$ParameterFileContent.GetEnumerator() | ForEach-Object {
73+
$Attribute = $_.Value.Required ? 'Mandatory' : 'Optional'
74+
$DataType = $_.Value.Type
75+
$Description = $_.Value.Description
76+
$AllowedValues = $null # TOCO: Figure a way to get allowed values
77+
$parameterSection += '| {0} | {1} | {2} | {3} | {4} |' -f $_.Key, $Attribute, $DataType, $Description, $AllowedValues
78+
}
79+
80+
$FileContent += $parameterSection
81+
return $FileContent
82+
}
83+
84+
function Set-ResourceReadme {
85+
[CmdletBinding(SupportsShouldProcess = $true)]
86+
param (
87+
[Parameter(Mandatory = $true)]
88+
[string] $ResourceFilePath,
89+
90+
[Parameter(Mandatory = $false)]
91+
[string] $ReadMeFilePath = (Join-Path (Split-Path $ResourceFilePath -Parent) 'Help'),
92+
93+
[Parameter(Mandatory = $false)]
94+
[ValidateSet(
95+
'Parameters'
96+
)]
97+
[string[]] $SectionsToRefresh = @(
98+
'Parameters'
99+
)
100+
)
101+
102+
# Check resource file Path
103+
$ResourceFilePath = Resolve-Path -Path $ResourceFilePath -ErrorAction Stop
104+
105+
if (-not (Test-Path $ResourceFilePath -PathType 'Leaf')) {
106+
throw "[$ResourceFilePath] is no valid file path."
107+
}
108+
109+
# Check the resources that are exported
110+
$moduleManifest = Import-PowerShellDataFile -Path ($ResourceFilePath.Replace('.psm1', '.psd1'))
111+
$moduleName = $moduleManifest.RootModule.Replace('.psm1', '')
112+
$resources = $moduleManifest.DscResourcesToExport
113+
$resources | ForEach-Object { Write-Verbose -Message ('Resource: {0}' -f $_) }
114+
115+
# TODO: Grab manual notes if needed
116+
117+
foreach ($resource in $resources) {
118+
$docFile = Join-Path -Path $ReadMeFilePath -ChildPath ('{0}.md' -f $resource)
119+
120+
$resourceInstance = Get-TypeInstanceFromModule -ModuleName $ResourceFilePath -ClassName $resource
121+
$dscResourceInstance = $resourceInstance::new()
122+
$t = $dscResourceInstance.GetType()
123+
124+
$resourceInfoMethod = $t.GetMethod('GetResourceInfo')
125+
if ($null -eq $resourceInfoMethod) {
126+
Write-Warning -Message ('Resource [{0}] does not have a GetResourceInfo method.' -f $resource)
127+
continue
128+
}
129+
130+
$resourceInfo = $resourceInfoMethod.Invoke($null, $null)
131+
132+
$resourcePropertyInfoMethod = $t.GetMethod('GetResourcePropertyInfo')
133+
if ($null -eq $resourcePropertyInfoMethod) {
134+
Write-Warning -Message ('Resource [{0}] does not have a GetResourcePropertyInfo method.' -f $resource)
135+
continue
136+
}
137+
138+
$resourcePropertyInfo = $resourcePropertyInfoMethod.Invoke($null, $null)
139+
140+
# Initialize the resource file
141+
$inputObject = @{
142+
ModuleName = $moduleName
143+
ResourceName = $resource
144+
Synopsis = $resourceInfo.Description
145+
Description = $resourceInfo.Description
146+
}
147+
$fileContent = Initialize-ResourceFile @inputObject
148+
149+
# =============== #
150+
# Set content #
151+
# =============== #
152+
153+
if ($SectionsToRefresh -contains 'Parameters') {
154+
$fileContent = Set-ResourcePropertyParameters -ParameterFileContent $resourcePropertyInfo -FileContent $fileContent
155+
}
156+
157+
return $fileContent
158+
159+
# $commandHelper = [Microsoft.PowerShell.PlatyPS.Model.CommandHelp]::new()
160+
# $commandHelper.Title = $resource
161+
# $commandHelper.Synopsis = $resourceInfo.Description
162+
# $commandHelper.Description = ($resourceInfo.Description + "`n`n## PARAMETERS`n`n" + ($parameterSection -join "`n"))
163+
# $commandHelper.Metadata = [ordered]@{
164+
# 'external help file' = ('{0}-Help.xml' -f $moduleManifest.RootModule)
165+
# 'Module Name' = 'test'
166+
# 'ms.date' = (Get-Date).ToString('MM/dd/yyyy')
167+
# 'online version' = $null
168+
# schema = '2.0.0'
169+
# title = $resource
170+
# }
171+
}
172+
}

0 commit comments

Comments
 (0)