-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathAddWrapCommand.cs
More file actions
315 lines (267 loc) · 11.6 KB
/
AddWrapCommand.cs
File metadata and controls
315 lines (267 loc) · 11.6 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Security;
using OpenRasta.Client;
using OpenWrap.Collections;
using OpenWrap.PackageManagement;
using OpenWrap.PackageModel;
using OpenWrap.Repositories;
using SysPath = System.IO.Path;
namespace OpenWrap.Commands.Wrap
{
[Command(Verb = "add", Noun = "wrap")]
public class AddWrapCommand : WrapCommand
{
bool _packageNotFound;
bool? _project;
bool? _system;
FileBased<IPackageDescriptor> _targetDescriptor;
FolderRepository _userSpecifiedRepository;
[CommandInput]
public bool Anchored { get; set; }
[CommandInput]
public bool Content { get; set; }
[CommandInput]
public bool Edge { get; set; }
[CommandInput]
public string From { get; set; }
[CommandInput]
public bool IgnoreInvalidSSLCert { get; set; }
[CommandInput]
public string MaxVersion { get; set; }
[CommandInput]
public string MinVersion { get; set; }
[CommandInput(IsRequired = true, Position = 0)]
public string Name { get; set; }
[CommandInput]
public bool NoDescriptorUpdate { get; set; }
[CommandInput]
public bool NoHooks { get; set; }
[CommandInput]
public bool Project
{
get { return _project ?? (_system == null); }
set { _project = value; }
}
[CommandInput]
public string Scope { get; set; }
[CommandInput]
public bool System
{
get { return _system == true; }
set { _system = value; }
}
[CommandInput(Position = 1)]
public string Version { get; set; }
PackageAddOptions AddOptions
{
get
{
PackageAddOptions addOptions = 0;
if (Anchored)
addOptions |= PackageAddOptions.Anchor;
if (Content)
addOptions |= PackageAddOptions.Content;
if (!NoDescriptorUpdate)
addOptions |= PackageAddOptions.UpdateDescriptor;
if (!NoHooks)
addOptions |= PackageAddOptions.Hooks;
if (Edge)
addOptions |= PackageAddOptions.Edge;
return addOptions;
}
}
PackageRequest PackageRequest
{
get
{
if (Version != null) return PackageRequest.Exact(Name, Version.ToSemVer());
if (MinVersion != null || MaxVersion != null) return PackageRequest.Between(Name, MinVersion.ToSemVer(), MaxVersion.ToSemVer());
return PackageRequest.Any(Name);
}
}
bool ShouldUpdateDescriptor
{
get
{
return NoDescriptorUpdate == false &&
_targetDescriptor != null;
}
}
protected override IEnumerable<ICommandOutput> ExecuteCore()
{
_targetDescriptor = HostEnvironment.GetOrCreateScopedDescriptor(Scope ?? string.Empty);
yield return VerifyDescriptor(_targetDescriptor);
yield return VerifyProjectRepository();
yield return SetupEnvironmentForAdd();
if (IgnoreInvalidSSLCert)
{
ServicePointManager.ServerCertificateValidationCallback
+= new RemoteCertificateValidationCallback(SslCertificateValidators.ValidateAnyRemoteCertificate);
}
var sourceRepositories = GetSourceRepositories().ToList();
var descriptor = new PackageDescriptor(_targetDescriptor.Value);
if (Project && System)
{
var sysToAdd = new List<PackageIdentifier>();
using (SaveDescriptorOnSuccess(_targetDescriptor))
{
foreach (var m in AddProjectPackage(descriptor, sourceRepositories))
{
yield return ToOutput(m);
ParseSuccess(m, sysToAdd.Add);
}
foreach (var identifier in sysToAdd)
foreach (var m in PackageManager.AddSystemPackage(PackageRequest.Exact(identifier.Name, identifier.Version), sourceRepositories, HostEnvironment.SystemRepository))
yield return ToOutput(m);
}
}
else if (Project)
{
using (SaveDescriptorOnSuccess(_targetDescriptor))
{
foreach (var m in AddProjectPackage(descriptor, sourceRepositories))
{
yield return ToOutput(m);
}
}
}
else if (System)
{
foreach (var m in PackageManager.AddSystemPackage(PackageRequest, sourceRepositories, HostEnvironment.SystemRepository, AddOptions))
{
yield return ToOutput(m);
}
}
if (_packageNotFound)
{
var hit = false;
foreach (var m in PackageManager.ListPackages(sourceRepositories, Name))
{
if (!hit)
{
yield return new Info("Did you mean one of the following packages?", Name);
hit = true;
}
yield return ToOutput(m);
}
}
}
protected override ICommandOutput ToOutput(PackageOperationResult packageOperationResult)
{
If<PackageMissingResult>(packageOperationResult, _ => _packageNotFound = true);
if (!NoDescriptorUpdate)
If<PackageDependencyAddedResult>(packageOperationResult, _ => _targetDescriptor.Value.Dependencies.Add(_.Dependency));
return base.ToOutput(packageOperationResult);
}
protected override IEnumerable<Func<IEnumerable<ICommandOutput>>> Validators()
{
yield return ValidateInputs;
}
static void ParseSuccess(PackageOperationResult m, Action<PackageIdentifier> onSuccess, Action onFailure = null)
{
var id = m is PackageUpdatedResult
? ((PackageUpdatedResult)m).Package.Identifier
: (m is PackageAddedResult ? ((PackageAddedResult)m).Package.Identifier : null);
if (m.Success && id != null)
onSuccess(id);
else if (m.Success)
return;
else if (onFailure != null)
onFailure();
}
IEnumerable<PackageOperationResult> AddProjectPackage(IPackageDescriptor targetDescriptor, IEnumerable<IPackageRepository> sourceRepositories)
{
var packageDescriptor = targetDescriptor.Lock(HostEnvironment.ProjectRepository);
foreach (var m in PackageManager.AddProjectPackage(PackageRequest, sourceRepositories, packageDescriptor, HostEnvironment.ProjectRepository, AddOptions))
yield return m;
}
IEnumerable<IPackageRepository> GetSourceRepositories()
{
return new[] { _userSpecifiedRepository, HostEnvironment.CurrentDirectoryRepository, HostEnvironment.SystemRepository }
.Concat(Remotes.FetchRepositories())
.Concat(HostEnvironment.ProjectRepository)
.NotNull();
}
ICommandOutput SetupEnvironmentForAdd()
{
var directory = HostEnvironment.CurrentDirectory;
var fromDirectory = string.IsNullOrEmpty(From) ? null : FileSystem.GetDirectory(From);
if (fromDirectory != null && fromDirectory.Exists)
{
if (SysPath.GetExtension(Name).EqualsNoCase(".wrap") &&
SysPath.IsPathRooted(Name) &&
FileSystem.GetDirectory(SysPath.GetDirectoryName(Name)) != fromDirectory)
{
return new Error("You provided both -From and -Name, but -Name is a path. Try removing the -From parameter.");
}
directory = fromDirectory;
_userSpecifiedRepository = new FolderRepository(directory, FolderRepositoryOptions.Default);
}
if (SysPath.GetExtension(Name).EqualsNoCase(".wrap") && directory.GetFile(SysPath.GetFileName(Name)).Exists)
{
var originalName = Name;
Name = PackageNameUtility.GetName(SysPath.GetFileNameWithoutExtension(Name));
Version = PackageNameUtility.GetVersion(SysPath.GetFileNameWithoutExtension(originalName)).ToString();
return new Warning("The requested package contained '.wrap' in the name. Assuming you pointed to a file name and meant a package named '{0}' with version qualifier '{1}'.",
Name,
Version);
}
return null;
}
IEnumerable<ICommandOutput> ValidateInputs()
{
var gotVersion = Version != null;
var gotMinVersion = MinVersion != null;
var gotMaxVersion = MaxVersion != null;
var numberOfVersionInputTypes = (new[] { gotVersion, (gotMinVersion || gotMaxVersion) }).Count(v => v);
if (numberOfVersionInputTypes > 1)
{
yield return new Error("Arguments for 'version' and 'version boundaries' cannot be combined.");
yield break;
}
if (gotVersion && Version.ToSemVer() == null)
{
yield return new Error("Could not parse version: " + Version);
yield break;
}
if (gotMinVersion && MinVersion.ToSemVer() == null)
{
yield return new Error("Could not parse minversion: " + MinVersion);
yield break;
}
if (gotMaxVersion && MaxVersion.ToSemVer() == null)
{
yield return new Error("Could not parse maxversion: " + MaxVersion);
yield break;
}
if (Project && HostEnvironment.ProjectRepository == null)
{
yield return new Error("Project repository doesn't exist but -project has been specified.");
yield break;
}
if (!string.IsNullOrEmpty(Scope) && !Project)
{
yield return new Error("Cannot specify a scope when adding to a system package.");
yield break;
}
}
ICommandOutput VerifyDescriptor(FileBased<IPackageDescriptor> descriptor)
{
if (Project == false) return null;
if (NoDescriptorUpdate)
return new Info("Descriptor file will not be updated.");
return descriptor.File.Exists
? new Info(@"Using descriptor {0}.", descriptor.File.Name)
: new Info("Creating descriptor {0}.", descriptor.File.Name);
}
ICommandOutput VerifyProjectRepository()
{
return HostEnvironment.ProjectRepository != null
? new Info("Project repository present.")
: new Info("Project repository absent.");
}
}
}