Last active
November 7, 2020 15:48
-
-
Save steviecoaster/c7b06bc0d4de8f7762733c72b3cbc150 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Function New-ChocolateyExtension { | |
| <# | |
| .SYNOPSIS | |
| Generate chocolatey extension package from existing powershell module | |
| .DESCRIPTION | |
| Generate Chocolatey extension package from existing powershell module | |
| .PARAMETER PowerShellModule | |
| Use this to generate an extension from an existing, installed PowerShell module | |
| .PARAMETER Psd1File | |
| A local psd1 file you wish to generate a Chocolatey extension package from | |
| .PARAMETER Directory | |
| A directory of ps1 files to turn into a Chocolatey extension package | |
| .PARAMETER ExtensionSuffix | |
| The suffix of the extension package. 'test' in 'test.extension' for example. | |
| .PARAMETER OutputDirectory | |
| The directory to store the extension package files in | |
| .PARAMETER BuildPackage | |
| Run choco pack and build the package to the OutputDirectory | |
| .EXAMPLE | |
| New-ChocolateyExtension -Module PSFramework -ExtensionSuffix psframework -OutputDirectory C:\chocopackages | |
| .EXAMPLE | |
| New-ChocolateyExtension -Module PSFramework -ExtensionSuffix psframework -OutputDirectory C:\chocopackages -BuildPackage | |
| .EXAMPLE | |
| New-ChocolateyExtension -Psd1File C:\modules\development\FileSystemWatcher\FileSystemWatcher.psd1 -ExtensionSuffix filesystemwatcher -OutputDirectory C:\chocopackages | |
| .EXAMPLE | |
| New-ChocolateyExtension -Directory C:\development\scripts -ExtensionSuffix companyscripts -OutputDirectory C:\chocopackages | |
| #> | |
| [cmdletBinding(DefaultParameterSetName = "Module")] | |
| param( | |
| [Parameter(Mandatory, ParameterSetName = "Module")] | |
| [Alias('Module')] | |
| [ValidateScript( { | |
| $modules = (Get-Module -ListAvailable).name | |
| if ($_ -notin $modules) { | |
| throw "Module not installed. Use 'Install-Module $_' first, then retry the command again." | |
| } | |
| else { | |
| $true | |
| } | |
| })] | |
| [String] | |
| $PowerShellModule, | |
| [Parameter(Mandatory, ParameterSetName = "Psd")] | |
| [ValidateScript( { Test-Path $_ })] | |
| [String] | |
| $Psd1File, | |
| [Parameter(Mandatory, ParameterSetName = "Directory")] | |
| [String] | |
| $Directory, | |
| [Parameter(Mandatory, ParameterSetName = "Module")] | |
| [Parameter(Mandatory, ParameterSetName = "Directory")] | |
| [Parameter(Mandatory, ParameterSetName = "Psd")] | |
| [Alias('Suffix')] | |
| [String] | |
| $ExtensionSuffix, | |
| [Parameter(Mandatory, ParameterSetName = "Module")] | |
| [Parameter(Mandatory, ParameterSetName = "Directory")] | |
| [Parameter(Mandatory, ParameterSetName = "Psd")] | |
| [String] | |
| $OutputDirectory, | |
| [Parameter(ParameterSetName = "Module")] | |
| [Parameter(ParameterSetName = "Directory")] | |
| [Parameter(ParameterSetName = "Psd")] | |
| [Switch] | |
| $BuildPackage | |
| ) | |
| process { | |
| $chocoArgs = @('new', "$($ExtensionSuffix).extension", "--version='0.1.0'", "--maintainer='$((whoami).Split('\')[0])'", "--output-directory='$OutputDirectory'") | |
| & choco @chocoArgs | |
| Get-ChildItem $(Resolve-Path "$OutputDirectory\$ExtensionSuffix.extension") -Recurse -Exclude *.nuspec | Remove-Item -Recurse -Force | |
| $null = New-Item "$OutputDirectory\$ExtensionSuffix.extension\extensions" -ItemType Directory | |
| switch ($PSCmdlet.ParameterSetName) { | |
| 'Module' { | |
| Import-Module $PowerShellModule | |
| $commands = (Get-Command -Module $PowerShellModule).Name | |
| $ModulePath = (Get-Module $PowerShellModule).Path | |
| Copy-Item $ModulePath "$OutputDirectory\$ExtensionSuffix.extension\extensions\" | |
| $NewModuleManifestPath = (Get-ChildItem "$OutputDirectory\$ExtensionSuffix.extension" -Recurse -filter *.psm1).FullName | |
| "Export-ModuleMember $($commands -join ',')" | Add-Content $NewModuleManifestPath | |
| } | |
| 'Directory' { | |
| $null = New-Item "$OutputDirectory\$ExtensionSuffix.extension\extensions\$ExtensionSuffix.psm1" -ItemType File | |
| if (-not (Get-ChildItem $Directory -recurse -Filter *.ps1 -OutVariable Ps1Files)) { | |
| throw "No valid ps1 files found in this directory" | |
| } | |
| else { | |
| $FunctionNames = [System.Collections.Generic.List[string]]::new() | |
| $Ps1Files | Foreach-Object { | |
| $content = Get-Content $_.FullName | |
| $null = $content[0] -match '\s(?<function>[\w\-]+)' | |
| $function = $matches.function | |
| $FunctionNames.Add($function) | |
| Get-Content $_.FullName | Add-Content "$OutputDirectory\$ExtensionSuffix.extension\extensions\$ExtensionSuffix.psm1" | |
| } | |
| "Export-ModuleMember $($FunctionNames -join ',')" | Add-Content "$OutputDirectory\$ExtensionSuffix.extension\extensions\$ExtensionSuffix.psm1" | |
| } | |
| } | |
| 'Psd' { | |
| Import-Module $Psd1File | |
| $psdModule = (Test-ModuleManifest $Psd1File).Name | |
| $commands = (Get-Command -Module $PsdModule).Name | |
| $ModulePath = (Get-Module $PsdModule).Path | |
| Copy-Item $ModulePath "$OutputDirectory\$ExtensionSuffix.extension\extensions\" | |
| $NewModuleManifestPath = (Get-ChildItem "$OutputDirectory\$ExtensionSuffix.extension" -Recurse -filter *.psm1).FullName | |
| "Export-ModuleMember $($commands -join ',')" | Add-Content $NewModuleManifestPath | |
| } | |
| } | |
| $nuspec = (Get-ChildItem $OutputDirectory -Recurse -Filter *.nuspec).FullName | |
| $xml = New-Object System.Xml.XmlDocument | |
| $xml.Load("$nuspec") | |
| $xml.package.files.file.src = 'extensions\**' | |
| $xml.package.files.file.target = 'extensions' | |
| $xml.Save("$nuspec") | |
| if ($BuildPackage) { | |
| $buildArgs = @('pack', $nuspec,"--output-directory='$OutputDirectory'") | |
| & choco @buildArgs | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment