Skip to content

Instantly share code, notes, and snippets.

@mkht
Last active September 5, 2018 12:01
Show Gist options
  • Save mkht/a2e031eb020f88892a18aba20f8ae4e9 to your computer and use it in GitHub Desktop.
Save mkht/a2e031eb020f88892a18aba20f8ae4e9 to your computer and use it in GitHub Desktop.
Publish script for PowerShell Gallery
Param (
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$NugetApiKey,
[Parameter(Mandatory = $false)]
[ValidateNotNullOrEmpty()]
[string[]]$ExcludeDirs = @('.git', '.vscode'),
[Parameter(Mandatory = $false)]
[ValidateNotNullOrEmpty()]
[string[]]$ExcludeFiles = @('.gitignore'),
[switch]$WhatIf
)
$ModuleDir = $PSScriptRoot
$ModuleName = Split-Path $ModuleDir -Leaf
$MySelfName = Split-Path $PSCommandPath -Leaf
$Destination = Join-Path $env:TEMP $ModuleName
if (Test-Path $Destination) {
Remove-Item $Destination -Force -Recurse -ErrorAction Stop
}
if ($ExcludeDirs -notcontains '.git') {
$ExcludeDirs += '.git'
}
if ($ExcludeFiles -notcontains $MySelfName) {
$ExcludeFiles += $MySelfName
}
try {
robocopy $ModuleDir $Destination /MIR /XD $ExcludeDirs /XF $ExcludeFiles /NP > $null
Set-Location $Destination
Publish-Module -Path ./ -NuGetApiKey $NugetApiKey -Verbose -WhatIf:$WhatIf
}
finally {
Set-Location $ModuleDir
Remove-Item $Destination -Force -Recurse -ErrorAction Continue
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment