Skip to content

Commit

Permalink
publish.ps1
Browse files Browse the repository at this point in the history
  • Loading branch information
rulasg committed May 23, 2023
1 parent 3a3457a commit 96be33c
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 10 deletions.
12 changes: 6 additions & 6 deletions Docs.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RootModule = 'Docs.psm1'

# Version number of this module.
ModuleVersion = '0.1'
ModuleVersion = '2.0.0'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down Expand Up @@ -69,16 +69,16 @@ Description = 'Module to manage documents'
# NestedModules = @()

# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = '*'
# FunctionsToExport = '*'

# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = '*'
# CmdletsToExport = '*'

# Variables to export from this module
VariablesToExport = '*'
# VariablesToExport = '*'

# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
AliasesToExport = '*'
# AliasesToExport = '*'

# DSC resources to export from this module
# DscResourcesToExport = @()
Expand Down Expand Up @@ -110,7 +110,7 @@ PrivateData = @{
# ReleaseNotes = ''

# Prerelease string of this module
# Prerelease = ''
Prerelease = 'alpha'

# Flag to indicate whether the module requires explicit user acceptance for install/update/save
# RequireLicenseAcceptance = $false
Expand Down
21 changes: 19 additions & 2 deletions Docs.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ CREATED: 05/26/2021

Write-Host "Loading Docs ..." -ForegroundColor DarkCyan


# Script Variables
$script:StoresList = @()

Expand Down Expand Up @@ -1146,4 +1145,22 @@ function Format-DocsName {
$input | ForEach-Object { $_.Name() }
} # Export-ModuleMember -Function Format-DocsName -Alias "fname"

#endregion Formats
#endregion Formats

#region Utils
function Add-MyMember
{
[CmdletBinding()]
param(
[parameter(Mandatory,ValueFromPipeline)][Object] $object,
[parameter(Mandatory)][string]$NotePropertyName,
[parameter(Mandatory)][string]$NotePropertyValue
)

if ($Object.$NotePropertyName) {
$Object.$NotePropertyName = $NotePropertyValue
} else {
$object | Add-Member -NotePropertyName $NotePropertyName -NotePropertyValue $NotePropertyValue
}
}
#endregion Utils
4 changes: 2 additions & 2 deletions DocsTest/DocsTest.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -1830,8 +1830,6 @@ function DocsTest_GetDocsName_Simple{
Assert-NotImplemented
}

Export-ModuleMember -Function DocsTest_*

function SetupScenario1 () {

$Evidence = New-Object 'System.Collections.Generic.Dictionary[[string],[string]]'
Expand Down Expand Up @@ -2027,3 +2025,5 @@ function CheckDocName{

Write-AssertionDot -Color Yellow
}

Export-ModuleMember -Function DocsTest_*
79 changes: 79 additions & 0 deletions publish.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<#
.SYNOPSIS
Publish the Module to PSGallery
.DESCRIPTION
This script will publish the module to the PSGallery
.NOTES
You will need to create a NuGet API Key for the PSGallery at https://www.powershellgallery.com/account/apikeys
.LINK
https://raw.githubusercontent.com/rulasg/DemoPsModule/main/publish.ps1
.EXAMPLE
# Publish the module to the PSGallery without prompting
> Publish.ps1 -Force -NuGetApiKey "<API Key>""
.EXAMPLE
# Publish the module to the PSGallery using PAT on enviroment variable
> $env:NUGETAPIKEY = <API Key>
> ./publish.ps1
#>

[CmdletBinding(
SupportsShouldProcess,
ConfirmImpact='High'
)]
param(
# The NuGet API Key for the PSGallery
[Parameter(Mandatory=$false)] [string]$NuGetApiKey,
# Force the publish without prompting for confirmation
[Parameter(Mandatory=$false)] [switch]$Force,
# Force publishing package to the gallery. Equivalente to Import-Module -Force
[Parameter(Mandatory=$false)] [switch]$ForcePublish
)

# check that $NuggetApiKey is null or whitespace
# If it is use environment variable $env:NugetApiKey
if ( [string]::IsNullOrWhiteSpace($NuGetApiKey) ) {

if ( [string]::IsNullOrWhiteSpace($env:NUGETAPIKEY) ) {
Write-Error -Message '$Env:NUGETAPIKEY is not set. Try running `$Env:NUGETAPIKEY = (Find-DocsFile nugetapikey | rsk | Get-SecretData).Get()`'
return
}

$NuGetApiKey = $env:NUGETAPIKEY
}

# look for psd1 file on the same folder as this script
$psdPath = Get-ChildItem -Path $PSScriptRoot -Filter *.psd1

# check if $psd is set
if ( $null -eq $psdPath ) {
Write-Error -Message 'No psd1 file found'
return
}

# check if $psd is a single file
if ( $psdPath.Count -gt 1 ) {
Write-Error -Message 'More than one psd1 file found'
return
}

# Display Module Information
$psd1 = Import-PowerShellDataFile -Path $psdPath
$psd1
$psd1.PrivateData.PSData


# Confirm if not forced
if ($Force -and -not $Confirm){
$ConfirmPreference = 'None'
}

# Publish the module with ShouldProcess (-whatif, -confirm)
if ($PSCmdlet.ShouldProcess($psdPath, "Publish-Module")) {
$message ="Publishing {0} {1} {2} to PSGallery ..." -f $($psdPath.Name), $($psd1.ModuleVersion), $($psd1.PrivateData.pSData.Prerelease)
# show an empty line
Write-Information -InformationAction Continue -Message ""
Write-Information -InformationAction Continue -Message $message
Publish-Module -Name $psdPath -NuGetApiKey $NuGetApiKey -Force:$ForcePublish
}

0 comments on commit 96be33c

Please sign in to comment.