-
Notifications
You must be signed in to change notification settings - Fork 0
/
BuildModule.ps1
81 lines (69 loc) · 2.86 KB
/
BuildModule.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
#Requires -Version 5.1 -Modules platyPS
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High')]
param
(
[Parameter(Mandatory)]
[Alias("ModuleVersion")]
[version]$Version,
[Parameter()]
[string]$Destination = "$PSScriptRoot\Releases",
[Parameter()]
[Validateset("Release","Debug")]
[string]$Configuration = "Release"
)
$ModuleName = "MouseSettings"
#region Create destination folder and make sure it is empty
$DestinationDirectory = [System.IO.Path]::Combine($Destination, $ModuleName, $Version)
$null = New-Item -Path $DestinationDirectory -ItemType Directory -Force
$ItemsToRemove = Get-ChildItem -LiteralPath $DestinationDirectory -Force
if ($ItemsToRemove)
{
if ($PSCmdlet.ShouldProcess($DestinationDirectory, "Deleting $($ItemsToRemove.Count) item(s)"))
{
Remove-Item -LiteralPath $ItemsToRemove.FullName -Recurse -Force
}
else
{
exit
}
}
#endregion
#region Compile and add all content to destination folder
MSBuild.exe "$PSScriptRoot\$ModuleName.sln" "-property:Configuration=$Configuration,Version=$Version"
if (!$?)
{
exit
}
$CompiledModule = Get-ChildItem -Path "$PSScriptRoot\src\$ModuleName\bin\$Configuration" -Recurse -Filter "$ModuleName.dll"
$ModuleInfo = Import-Module -Name $CompiledModule.FullName -PassThru -ErrorAction Stop
$CmdletsToExport = $ModuleInfo.ExportedCmdlets.Keys.ForEach({"'$_'"}) -join ','
Remove-Module -ModuleInfo $ModuleInfo
Copy-Item -LiteralPath $CompiledModule.FullName -Destination $DestinationDirectory -Force
Copy-Item -LiteralPath "$PSScriptRoot\ModuleManifest.psd1" -Destination "$DestinationDirectory\$ModuleName.psd1"
$HelpFile = New-ExternalHelp -Path "$PSScriptRoot\Docs" -OutputPath "$DestinationDirectory\en-US" -ErrorAction Stop
$HelpContent = [xml]::new()
$HelpContent.Load($HelpFile.FullName)
$HelpExamples = Select-Xml -Xml $HelpContent.helpItems -XPath //command:example -Namespace @{command = "http://schemas.microsoft.com/maml/dev/command/2004/10"}
foreach ($Example in $HelpExamples)
{
# Adds 2 linebreaks between each example
for ($i = 0; $i -lt 2; $i++)
{
$Element = $HelpContent.CreateElement('maml', 'para', 'http://schemas.microsoft.com/maml/2004/10')
$null = $Example.Node.remarks.AppendChild($Element)
}
}
$HelpContent.Save($HelpFile.FullName)
#endregion
#region update module manifest
$FileList = (Get-ChildItem -LiteralPath $DestinationDirectory -File -Recurse | ForEach-Object -Process {
"'$($_.FullName.Replace("$DestinationDirectory\", ''))'"
}) -join ','
$ReleaseNotes = Get-Content -LiteralPath "$PSScriptRoot\Release notes.txt" -Raw
((Get-Content -LiteralPath "$PSScriptRoot\ModuleManifest.psd1" -Raw) -replace '{(?=[^\d])','{{' -replace '(?<!\d)}','}}') -f @(
"'$Version'"
$CmdletsToExport
$FileList
$ReleaseNotes
) | Set-Content -LiteralPath "$DestinationDirectory\$ModuleName.psd1" -Force
#endregion