Repository navigation
Expand file tree
/
Copy pathPublish-PSMDStagedModule.ps1
More file actions
129 lines (107 loc) · 4.39 KB
/
Copy pathPublish-PSMDStagedModule.ps1
File metadata and controls
129 lines (107 loc) · 4.39 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
function Publish-PSMDStagedModule
{
<#
.SYNOPSIS
Publish a module to your staging repository.
.DESCRIPTION
Publish a module to your staging repository.
Always publishes the latest version available when specifying a name.
.PARAMETER Name
The name of the module to publish.
.PARAMETER Path
The path to the module to publish.
.PARAMETER Repository
The repository from which to withdraw the module to then publish to the staging repository.
.PARAMETER EnableException
This parameters disables user-friendly warnings and enables the throwing of exceptions.
This is less user friendly, but allows catching exceptions in calling scripts.
.EXAMPLE
PS C:\> Publish-PSMDStagedModule -Name 'PSFramework'
Publishes the latest version of PSFramework found on the local machine.
.EXAMPLE
PS C:\> Publish-PSMDStagedModule -Name 'Microsoft.Graph' -Repository PSGallery
Publishes the entire kit of 'Microsoft.Graph' modules from the PSGallery to the staging repository.
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, ParameterSetName = 'Name')]
[string]
$Name,
[Parameter(Mandatory = $true, ParameterSetName = 'Path')]
[PsfValidateScript('PSModuleDevelopment.Validate.Path', ErrorString = 'PSModuleDevelopment.Validate.Path')]
[string]
$Path,
[Parameter(ParameterSetName = 'Name')]
[string]
$Repository,
[switch]
$EnableException
)
begin
{
$tempPath = Get-PSFPath -Name Temp
}
process
{
#region Explicit Path specified
if ($Path)
{
try { Publish-Module -Path $Path -Repository (Get-PSFConfigValue -FullName 'PSModuleDevelopment.Script.StagingRepository') -ErrorAction Stop }
catch
{
if ($_.FullyQualifiedErrorId -like '*ModuleVersionIsAlreadyAvailableInTheGallery*')
{
Write-PSFMessage -Level Warning -String 'Publish-PSMDStagedModule.Module.AlreadyPublished' -StringValues $moduleToPublish.Name, $moduleToPublish.Version -ErrorRecord $_
return
}
Stop-PSFFunction -String 'Publish-PSMDStagedModule.Module.PublishError' -StringValues $Name, $folder.Name -ErrorRecord $_ -EnableException $EnableException
return
}
return
}
#endregion Explicit Path specified
#region Deploy from source repository
if ($Repository)
{
$workingDirectory = Join-Path -Path $tempPath -ChildPath "psmd_$(Get-Random)"
$null = New-Item -Path $workingDirectory -ItemType Directory -Force
Save-Module -Name $Name -Repository $Repository -Path $workingDirectory
foreach ($folder in Get-ChildItem -Path $workingDirectory | Sort-Object -Property LastWriteTime)
{
$subFolder = Get-ChildItem -Path $folder.FullName | Sort-Object -Property Name -Descending | Select-Object -First 1
try { Publish-Module -Path $subFolder.FullName -Repository (Get-PSFConfigValue -FullName 'PSModuleDevelopment.Script.StagingRepository') -ErrorAction Stop }
catch
{
if ($_.FullyQualifiedErrorId -like '*ModuleVersionIsAlreadyAvailableInTheGallery*') { continue }
Remove-Item -Path $workingDirectory -Force -Recurse -ErrorAction Ignore
Stop-PSFFunction -String 'Publish-PSMDStagedModule.Module.PublishError' -StringValues $Name, $folder.Name -ErrorRecord $_ -EnableException $EnableException
return
}
}
Remove-Item -Path $workingDirectory -Force -Recurse -ErrorAction Ignore
}
#endregion Deploy from source repository
#region Deploy from local computer installation
else
{
$modules = Get-Module -Name $Name -ListAvailable
if (-not $modules)
{
Stop-PSFFunction -String 'Publish-PSMDStagedModule.Module.NotFound' -StringValues $Name -EnableException $EnableException
}
$moduleToPublish = $modules | Sort-Object -Property Version -Descending | Select-Object -First 1
try { Publish-Module -Path $moduleToPublish.ModuleBase -Repository (Get-PSFConfigValue -FullName 'PSModuleDevelopment.Script.StagingRepository') -ErrorAction Stop }
catch
{
if ($_.FullyQualifiedErrorId -like '*ModuleVersionIsAlreadyAvailableInTheGallery*')
{
Write-PSFMessage -Level Warning -String 'Publish-PSMDStagedModule.Module.AlreadyPublished' -StringValues $moduleToPublish.Name, $moduleToPublish.Version -ErrorRecord $_
return
}
Stop-PSFFunction -String 'Publish-PSMDStagedModule.Module.PublishError' -StringValues $Name, $folder.Name -ErrorRecord $_ -EnableException $EnableException
return
}
}
#endregion Deploy from local computer installation
}
}