Repository navigation
Expand file tree
/
Copy pathRepository.psm1
More file actions
257 lines (218 loc) · 11 KB
/
Copy pathRepository.psm1
File metadata and controls
257 lines (218 loc) · 11 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# FULL # BEGIN FULL: Don't include this in the installer script
$PoshCodeModuleRoot = Get-Variable PSScriptRoot -ErrorAction SilentlyContinue | ForEach-Object { $_.Value }
if(!$PoshCodeModuleRoot) {
$PoshCodeModuleRoot = Split-Path $MyInvocation.MyCommand.Path -Parent
}
. $PoshCodeModuleRoot\Constants.ps1
if(!(Get-Command Invoke-WebReques[t] -ErrorAction SilentlyContinue)){
Import-Module $PoshCodeModuleRoot\InvokeWeb
}
Import-Module $PoshCodeModuleRoot\Metadata.psm1
# FULL # END FULL
$CommonParameters = "ErrorAction", "WarningAction", "Verbose", "Debug", "ErrorVariable", "WarningVariable", "OutVariable", "OutBuffer", "PipelineVariable"
function Find-Module {
<#
.Synopsis
Find PoshCode packages online
.DESCRIPTION
This searches a list of online repositories (like github) for available modules packages.
.EXAMPLE
Find-Module
Lists all the module in all the available repositories
.EXAMPLE
Find-Module Viso
Find all modules which mention Visio in (their package manifests) in all repositories
.EXAMPLE
Find-Module -Author jrich523
Finds all modules by jrich523 in all repositories
.EXAMPLE
Find-Module -Author jrich523 -Name PSVA -Repository GitHub
Finds a specific module by a specific author in a specific repository
.OUTPUTS
PoshCode.Search.ModuleInfo
#>
[CmdletBinding()]
param
(
# Text to search for in name, description or tags (supports wildcards).
[string]$SearchTerm,
# Search for modules published by a particular author (supports wildcards, and searches for partial matches by default because multiple authors end up as one string in PowerShell modules).
[Parameter(ValueFromPipelineByPropertyName=$true)]
[string]$Author,
# Search for a specific module by name (supports wildcards).
[alias('ModuleName','MN')]
[Parameter(ValueFromPipelineByPropertyName=$true)]
[string]$Name,
# Search for a specific version. Not all repositories support versions.
[Parameter(ValueFromPipelineByPropertyName=$true)]
[Alias("ModuleVersion","MV")]
[string]$Version,
# The path of a configured repository (allows wildcards), or a hashtable of @{RepositoryType=@("RepositoryRootUri")}
[Parameter(ValueFromPipelineByPropertyName=$true)]
$Repository,
[int]
$Limit = 0
)
begin {
$Global:SearchErrors = New-Object System.Collections.ArrayList
$Count = 0
}
process {
if($Repository -is [hashtable]) {
$SelectedRepositories = $Repository
} else {
$SelectedRepositories = @{}
$ConfiguredRepositories = (Get-ConfigData).Repositories
if(!$Repository) {
Write-Verbose "Using SearchByDefault Repositories"
foreach($Repo in $ConfiguredRepositories.Keys | Where-Object { $ConfiguredRepositories.$_.SearchByDefault }) {
$SelectedRepositories."$Repo" = $ConfiguredRepositories."$Repo"
}
} else {
# Filter Repositories
# First try matching the name exactly:
if($ConfiguredRepositories."$Repository") {
Write-Verbose "Found exact name: $Repository"
$SelectedRepositories."$Repository" = $ConfiguredRepositories."$Repository"
# Then try wildcards:
} elseif($Keys = $ConfiguredRepositories.Keys | Where-Object { foreach($r in @($Repository)){ $_ -like "$r" } }) {
Write-Verbose "Found matching names: $Keys"
foreach($Repo in $Keys) {
$SelectedRepositories."$Repo" = $ConfiguredRepositories."$Repo"
}
} elseif($Keys = $ConfiguredRepositories.Keys | Where-Object { foreach($r in @($Repository)){ $_.Root -like "$r" } }) {
Write-Verbose "Found matching Roots: $Keys"
foreach($Repo in $Keys) {
$SelectedRepositories."$Repo" = $ConfiguredRepositories."$Repo"
}
}
}
}
if($SelectedRepositories.Count -eq 0) {
Write-Error "No repository specified for search. Please specify a repository name or set one or more of your configured repositories to SearchByDefault."
return
}
$null = $PSBoundParameters.Remove("Repository")
$null = $PSBoundParameters.Remove("Limit")
# Write-Verbose ($SelectedRepositories | %{ $_ | Format-Table -HideTableHeaders }| Out-String -Width 110)
foreach($Name in $SelectedRepositories.Keys) {
$Repo = $SelectedRepositories.$Name
$SearchParameters = @{} + $PSBoundParameters
$Command = Import-Module "${PoshCodeModuleRoot}\Repositories\$(${Repo}.Type)" -Passthru | % { $_.ExportedCommands['FindModule'] }
# We help out by mapping anything in the settings to their parameters
foreach($k in @($Repo.Keys) | Where-Object { ($Command.Parameters.Keys -contains $_) -and ("Type" -notcontains $_)}) {
$SearchParameters.$k = $Repo.$k
}
$Mandatory = $Command.Parameters.Values |
Where-Object { $_.Attributes.Mandatory -and ($SearchParameters.Keys -NotContains $_.Name)} |
ForEach-Object { $_.Name }
if($Mandatory) {
Write-Warning "Not searching $($Repo.Type) repository $Name, missing mandatory parameter(s) '$($Mandatory -join ''',''')'"
} else {
Write-Verbose "$(${Repo}.Type)\FindModule $(($SearchParameters.GetEnumerator() | %{ '-' + $_.Key + ' ''' + $_.Value + '''' }) -join ' ')"
# Write-Verbose ($SearchParameters | Format-Table | Out-String -Width 110)
try {
$(if($Limit -gt 0) {
&$Command @SearchParameters | ForEach-Object { if(($Count++) -lt $Limit){ $_ } else { break } }
} else {
&$Command @SearchParameters
}) | ConvertTo-PSModuleInfo -AddonInfo @{
ModuleType = "SearchResult" # As opposed to "Script" or "Binary" or "Manifest" (I think "CDXML" modules stay as "Manifest" modules after importing)
Repository = @{ $Name = $Repo }
} -PSTypeNames ("PoshCode.ModuleInfo", "PoshCode.Search.ModuleInfo", "PoshCode.Search.${Name}.ModuleInfo") -AsObject
}
catch
{
$SearchErrors.Add($_)
Write-Warning "Error Searching $($Repo.Type) $($Repo.Root) (See `$SearchErrors)"
}
if($Limit -gt 0 -and $Count -ge $Limit) { return }
}
}
}
}
function Publish-Module {
#.Synopsis
# Pushes a package to a NuGet-API compatible repository
param(
# The file you want to publish
[Parameter(Mandatory=$True, ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)]
$Package,
# The name or partial URL of a configured repository, or the full URL to a repository
$Repository,
# An API Key or login credentials for the repository
$ApiKey
)
begin {
$Global:PublishErrors = New-Object System.Collections.ArrayList
$Count = 0
}
process {
if($Repository -is [hashtable]) {
$SelectedRepositories = $Repository
} else {
$SelectedRepositories = @{}
$ConfiguredRepositories = (Get-ConfigData).Repositories
if(!$Repository) {
Write-Verbose "Using SearchByDefault Repositories"
foreach($Repo in $ConfiguredRepositories.Keys | Where-Object { $ConfiguredRepositories.$_.PublishByDefault }) {
$SelectedRepositories."$Repo" = $ConfiguredRepositories."$Repo"
}
} else {
# Filter Repositories
# First try matching the name exactly:
if($ConfiguredRepositories."$Repository") {
Write-Verbose "Found exact name: $Repository"
$SelectedRepositories."$Repository" = $ConfiguredRepositories."$Repository"
# Then try wildcards:
} elseif($Keys = $ConfiguredRepositories.Keys | Where-Object { foreach($r in @($Repository)){ $_ -like "$r" } }) {
Write-Verbose "Found matching names: $Keys"
foreach($Repo in $Keys) {
$SelectedRepositories."$Repo" = $ConfiguredRepositories."$Repo"
}
} elseif($Keys = $ConfiguredRepositories.Keys | Where-Object { foreach($r in @($Repository)){ $_.Root -like "$r" } }) {
Write-Verbose "Found matching Roots: $Keys"
foreach($Repo in $Keys) {
$SelectedRepositories."$Repo" = $ConfiguredRepositories."$Repo"
}
}
}
}
if($SelectedRepositories.Count -eq 0) {
Write-Error "No repository specified for search. Please specify a repository name or set one or more of your configured repositories to SearchByDefault."
return
}
$null = $PSBoundParameters.Remove("Repository")
foreach($Name in $SelectedRepositories.Keys) {
$Repo = $SelectedRepositories.$Name
$SearchParameters = @{} + $PSBoundParameters
Write-Verbose "$(${Repo}.Type)\PushModule -Root $($Repo.Root)"
$Command = Import-Module "${PoshCodeModuleRoot}\Repositories\$(${Repo}.Type)" -Passthru | % { $_.ExportedCommands['PushModule'] }
if(!$Command) {
Write-Error "Publishing not supported by $($Repo.Type) repositories!"
return
}
# We help out by mapping anything in the settings to their parameters
foreach($k in @($Repo.Keys) | Where-Object { ($Command.Parameters.Keys -contains $_) -and ("Type" -notcontains $_)}) {
$SearchParameters.$k = $Repo.$k
}
$Mandatory = $Command.Parameters.Values |
Where-Object { $_.Attributes.Mandatory -and ($SearchParameters.Keys -NotContains $_.Name)} |
ForEach-Object { $_.Name }
if($Mandatory) {
Write-Warning "Not publishing to $($Repo.Type) repository $($Name), missing mandatory parameter(s) '$($Mandatory -join ''',''')'"
} else {
# Write-Verbose ($SearchParameters | Format-Table | Out-String -Width 110)
try {
&$Command @SearchParameters
}
catch
{
$PublishErrors.Add($_)
Write-Warning "Error Publishing to $($Repo.Type) repository at $($Repo.Root) (See `$PublishErrors)"
}
}
}
}
}
Export-ModuleMember -Function 'Find-Module', 'Publish-Module'