-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathPublishPSResource.cs
212 lines (176 loc) · 7.28 KB
/
PublishPSResource.cs
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using Microsoft.PowerShell.PSResourceGet.UtilClasses;
using System;
using System.Diagnostics;
using System.Linq;
using System.Management.Automation;
using System.Net;
using System.Threading;
namespace Microsoft.PowerShell.PSResourceGet.Cmdlets
{
/// <summary>
/// Publishes a module, script, or nupkg to a designated repository.
/// </summary>
[Cmdlet(VerbsData.Publish,
"PSResource",
SupportsShouldProcess = true)]
[Alias("pbres")]
public sealed class PublishPSResource : PSCmdlet, IDynamicParameters
{
#region Parameters
private const string PathParameterSet = "PathParameterSet";
private const string NupkgPathParameterSet = "NupkgPathParameterSet";
private ContainerRegistryDynamicParameters _pkgPrefix;
/// <summary>
/// Specifies the API key that you want to use to publish a module to the online gallery.
/// </summary>
[Parameter]
[ValidateNotNullOrEmpty]
public string ApiKey { get; set; }
/// <summary>
/// Specifies the repository to publish to.
/// </summary>
[Parameter]
[ValidateNotNullOrEmpty]
[ArgumentCompleter(typeof(RepositoryNameCompleter))]
public string Repository { get; set; }
/// <summary>
/// Specifies the path to the resource that you want to publish. This parameter accepts the path to the folder that contains the resource.
/// Specifies a path to one or more locations. Wildcards are permitted. The default location is the current directory (.).
/// </summary>
[Parameter (Mandatory = true, Position = 0, ParameterSetName = PathParameterSet, HelpMessage = "Path to the resource to be published.")]
[ValidateNotNullOrEmpty]
public string Path { get; set; }
/// <summary>
/// Specifies the path to where the resource (as a nupkg) should be saved to. This parameter can be used in conjunction with the
/// -Repository parameter to publish to a repository and also save the exact same package to the local file system.
/// </summary>
[Parameter]
[ValidateNotNullOrEmpty]
public string DestinationPath { get; set; }
/// <summary>
/// Specifies a user account that has rights to a specific repository (used for finding dependencies).
/// </summary>
[Parameter]
[ValidateNotNullOrEmpty]
public PSCredential Credential { get; set; }
/// <summary>
/// Bypasses the default check that all dependencies are present.
/// </summary>
[Parameter]
public SwitchParameter SkipDependenciesCheck { get; set; }
/// <summary>
/// Bypasses validating a resource module manifest before publishing.
/// </summary>
[Parameter]
public SwitchParameter SkipModuleManifestValidate { get; set; }
/// <summary>
/// Specifies a proxy server for the request, rather than a direct connection to the internet resource.
/// </summary>
[Parameter]
[ValidateNotNullOrEmpty]
public Uri Proxy {
set
{
if (value != null)
{
WriteError(new ErrorRecord(
new ArgumentException("Not yet implemented."),
"ProxyNotImplemented",
ErrorCategory.InvalidData,
this));
}
}
}
/// <summary>
/// Specifies a user account that has permission to use the proxy server that is specified by the Proxy parameter.
/// </summary>
[Parameter]
[ValidateNotNullOrEmpty]
public PSCredential ProxyCredential {
set
{
if (value != null)
{
WriteError(new ErrorRecord(
new ArgumentException("Not yet implemented."),
"ProxyCredentialNotImplemented",
ErrorCategory.InvalidData,
this));
}
}
}
[Parameter(Mandatory = true, ParameterSetName = NupkgPathParameterSet, HelpMessage = "Path to the resource to be published.")]
[ValidateNotNullOrEmpty]
public string NupkgPath { get; set; }
[Parameter]
public SwitchParameter Latest { get; set; }
[Parameter]
public SwitchParameter LatestPreview { get; set; }
#endregion
#region DynamicParameters
public object GetDynamicParameters()
{
PSRepositoryInfo repository = RepositorySettings.Read(new[] { Repository }, out string[] _).FirstOrDefault();
if (repository is not null && repository.ApiVersion == PSRepositoryInfo.APIVersion.ContainerRegistry)
{
_pkgPrefix = new ContainerRegistryDynamicParameters();
return _pkgPrefix;
}
return null;
}
#endregion
#region Members
private CancellationToken _cancellationToken;
private NetworkCredential _networkCredential;
private bool _isNupkgPathSpecified = false;
private PublishHelper _publishHelper;
#endregion
#region Method overrides
protected override void BeginProcessing()
{
_cancellationToken = new CancellationToken();
_networkCredential = Credential != null ? new NetworkCredential(Credential.UserName, Credential.Password) : null;
if (!string.IsNullOrEmpty(NupkgPath))
{
_isNupkgPathSpecified = true;
Path = NupkgPath;
}
// Create a respository story (the PSResourceRepository.xml file) if it does not already exist
// This is to create a better experience for those who have just installed v3 and want to get up and running quickly
RepositorySettings.CheckRepositoryStore();
_publishHelper = new PublishHelper(
this,
Credential,
ApiKey,
Path,
DestinationPath,
SkipModuleManifestValidate,
_cancellationToken,
_isNupkgPathSpecified,
Latest,
LatestPreview);
_publishHelper.CheckAllParameterPaths();
}
protected override void EndProcessing()
{
if (!_isNupkgPathSpecified)
{
_publishHelper.PackResource();
}
if (_publishHelper.ScriptError || !_publishHelper.ShouldProcess)
{
return;
}
string modulePrefix = _pkgPrefix?.ModulePrefix;
_publishHelper.PushResource(Repository, modulePrefix, SkipDependenciesCheck, _networkCredential);
}
#endregion
}
public class ContainerRegistryDynamicParameters
{
[Parameter]
public string ModulePrefix { get; set; }
}
}