1
+ <#
2
+ This script publishes the module to the gallery.
3
+ It expects as input an ApiKey authorized to publish the module.
4
+
5
+ Insert any build steps you may need to take before publishing it here.
6
+ #>
7
+ param (
8
+ $ApiKey ,
9
+
10
+ $WorkingDirectory ,
11
+
12
+ $Repository = ' PSGallery' ,
13
+
14
+ [switch ]
15
+ $LocalRepo ,
16
+
17
+ [switch ]
18
+ $SkipPublish ,
19
+
20
+ [switch ]
21
+ $AutoVersion
22
+ )
23
+
24
+ # region ApiKey defaults
25
+ if (-not $ApiKey ) {
26
+ $ApiKey = $ ($env: psgallery_apiKey )
27
+ }
28
+
29
+ # region Handle Working Directory Defaults
30
+ if (-not $WorkingDirectory ) {
31
+ if ($env: RELEASE_PRIMARYARTIFACTSOURCEALIAS ) {
32
+ $WorkingDirectory = Join-Path - Path $env: SYSTEM_DEFAULTWORKINGDIRECTORY - ChildPath $env: RELEASE_PRIMARYARTIFACTSOURCEALIAS
33
+ }
34
+ else { $WorkingDirectory = $env: workingdirectory }
35
+ }
36
+ # endregion Handle Working Directory Defaults
37
+
38
+ # Prepare publish folder
39
+ try {
40
+ Write-PSFMessage - Level Important - Message " Creating and populating publishing directory"
41
+ $publishDir = New-Item - Path $WorkingDirectory - Name publish - ItemType Directory
42
+ Copy-Item - Path " $ ( $WorkingDirectory ) \dbarefresh" - Destination $publishDir.FullName - Recurse - Force
43
+ }
44
+ catch {
45
+ Stop-PSFFunction - Message " Something went wrong creating and populating publishing directory" - Target $publishDir - ErrorRecord $_
46
+ return
47
+ }
48
+
49
+ # region remove unneccesary directories
50
+ try {
51
+ Remove-Item - Path " $ ( $publishDir.FullName ) \dbarefresh\appveyor.yml" - Force - Recurse
52
+ Remove-Item - Path " $ ( $publishDir.FullName ) \dbarefresh\build" - Force - Recurse
53
+ Remove-Item - Path " $ ( $publishDir.FullName ) \dbarefresh\tests" - Force - Recurse
54
+ }
55
+ catch {
56
+ Stop-PSFFunction - Message " Could not remove directories" - Target $publishDir.FullName - ErrorRecord $_
57
+ return
58
+ }
59
+ # end region
60
+
61
+ # region Gather text data to compile
62
+ $text = @ ()
63
+ $processed = @ ()
64
+
65
+ # Gather Stuff to run before
66
+ foreach ($line in (Get-Content " $ ( $PSScriptRoot ) \filesBefore.txt" | Where-Object { $_ -notlike " #*" })) {
67
+ if ([string ]::IsNullOrWhiteSpace($line )) { continue }
68
+
69
+ $basePath = Join-Path - Path " $ ( $publishDir.FullName ) \dbarefresh" - ChildPath $line
70
+ foreach ($entry in (Resolve-PSFPath - Path $basePath )) {
71
+ $item = Get-Item $entry
72
+ if ($item.PSIsContainer ) { continue }
73
+ if ($item.FullName -in $processed ) { continue }
74
+ $text += [System.IO.File ]::ReadAllText($item.FullName )
75
+ $processed += $item.FullName
76
+ }
77
+ }
78
+
79
+ # Gather commands
80
+ Get-ChildItem - Path " $ ( $publishDir.FullName ) \dbarefresh\internal\functions\" - Recurse - File - Filter " *.ps1" | ForEach-Object {
81
+ $text += [System.IO.File ]::ReadAllText($_.FullName )
82
+ }
83
+ Get-ChildItem - Path " $ ( $publishDir.FullName ) \dbarefresh\functions\" - Recurse - File - Filter " *.ps1" | ForEach-Object {
84
+ $text += [System.IO.File ]::ReadAllText($_.FullName )
85
+ }
86
+
87
+ # Gather stuff to run afterwards
88
+ foreach ($line in (Get-Content " $ ( $PSScriptRoot ) \filesAfter.txt" | Where-Object { $_ -notlike " #*" })) {
89
+ if ([string ]::IsNullOrWhiteSpace($line )) { continue }
90
+
91
+ $basePath = Join-Path " $ ( $publishDir.FullName ) \dbarefresh" $line
92
+ foreach ($entry in (Resolve-PSFPath - Path $basePath )) {
93
+ $item = Get-Item $entry
94
+ if ($item.PSIsContainer ) { continue }
95
+ if ($item.FullName -in $processed ) { continue }
96
+ $text += [System.IO.File ]::ReadAllText($item.FullName )
97
+ $processed += $item.FullName
98
+ }
99
+ }
100
+ # endregion Gather text data to compile
101
+
102
+ # region Update the psm1 file
103
+ $fileData = Get-Content - Path " $ ( $publishDir.FullName ) \dbarefresh\dbarefresh.psm1" - Raw
104
+ $fileData = $fileData.Replace (' "<was not compiled>"' , ' "<was compiled>"' )
105
+ $fileData = $fileData.Replace (' "<compile code into here>"' , ($text -join " `n`n " ))
106
+ [System.IO.File ]::WriteAllText(" $ ( $publishDir.FullName ) \dbarefresh\dbarefresh.psm1" , $fileData , [System.Text.Encoding ]::UTF8)
107
+ # endregion Update the psm1 file
108
+
109
+ # region Updating the Module Version
110
+ Write-PSFMessage - Level Important - Message " Branch: $ ( $env: APPVEYOR_REPO_BRANCH ) "
111
+ # if ($env:APPVEYOR_REPO_BRANCH -eq 'master') {
112
+ if ($SkipPublish ) { return }
113
+ if ($AutoVersion ) {
114
+ Write-PSFMessage - Level Important - Message " Updating module version numbers."
115
+ try { [version ]$remoteVersion = (Find-Module ' dbarefresh' - Repository $Repository - ErrorAction Stop).Version }
116
+ catch {
117
+ Stop-PSFFunction - Message " Failed to access $ ( $Repository ) " - EnableException $true - ErrorRecord $_
118
+ }
119
+ if (-not $remoteVersion ) {
120
+ Stop-PSFFunction - Message " Couldn't find dbarefresh on repository $ ( $Repository ) " - EnableException $true
121
+ }
122
+ $newBuildNumber = $remoteVersion.Build + 1
123
+ [version ]$localVersion = (Import-PowerShellDataFile - Path " $ ( $publishDir.FullName ) \dbarefresh\dbarefresh.psd1" ).ModuleVersion
124
+ Update-ModuleManifest - Path " $ ( $publishDir.FullName ) \dbarefresh\dbarefresh.psd1" - ModuleVersion " $ ( $localVersion.Major ) .$ ( $localVersion.Minor ) .$ ( $newBuildNumber ) "
125
+ }
126
+ else {
127
+ [version ]$localVersion = (Import-PowerShellDataFile - Path " $ ( $publishDir.FullName ) \dbarefresh\dbarefresh.psd1" ).ModuleVersion
128
+ Update-ModuleManifest - Path " $ ( $publishDir.FullName ) \dbarefresh\dbarefresh.psd1" - ModuleVersion " $ ( $localVersion.Major ) .$ ( $localVersion.Minor ) .$ ( $env: APPVEYOR_BUILD_NUMBER ) "
129
+ }
130
+
131
+ # region Publish
132
+ if ($LocalRepo ) {
133
+ # Dependencies must go first
134
+ Write-PSFMessage - Level Important - Message " Creating Nuget Package for module: PSFramework"
135
+ New-PSMDModuleNugetPackage - ModulePath (Get-Module - Name PSFramework).ModuleBase - PackagePath .
136
+ Write-PSFMessage - Level Important - Message " Creating Nuget Package for module: dbarefresh"
137
+ New-PSMDModuleNugetPackage - ModulePath " $ ( $publishDir.FullName ) \dbarefresh" - PackagePath .
138
+ }
139
+ else {
140
+ # Publish to Gallery
141
+ Write-PSFMessage - Level Important - Message " Publishing the dbarefresh module to $ ( $Repository ) "
142
+ Publish-Module - Path " $ ( $publishDir.FullName ) \dbarefresh" - NuGetApiKey $ApiKey - Force - Repository $Repository
143
+ }
144
+ # endregion Updating the Module Version
145
+
146
+
147
+ # endregion Publish
0 commit comments