-
Notifications
You must be signed in to change notification settings - Fork 1
/
OurOwnNuSpec.cs
332 lines (293 loc) · 11 KB
/
OurOwnNuSpec.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
using NuGet;
using NuGet.Versioning;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Media;
namespace NuSpecHelper
{
internal class OurOwnNuSpec
{
internal FileInfo SpecFile;
private PackageIdentity _identity;
internal PackageIdentity Identity
{
get
{
if (_identity == null)
{
Init();
}
return _identity;
}
}
public OurOwnNuSpec(FileInfo fileinfo)
{
SpecFile = fileinfo;
}
private List<PackageIdentity> _dependencies;
public List<PackageIdentity> Dependencies
{
get
{
if (_dependencies == null)
Init();
return _dependencies;
}
}
public IEnumerable<PackageIdentity> AllDependencies
{
get
{
foreach (var dependency in Dependencies)
{
yield return dependency;
foreach (var v2 in Repository.GetAllDependecies(dependency.Id))
{
yield return v2;
}
}
}
}
IEnumerable<string> packageFoldersFor(PackageIdentity defPackage)
{
var re = new Regex("^" + defPackage.Id + @"\.([\d\. ]+)(-[\w\d]*)*$" );
if (SpecFile == null)
yield break;
if (SpecFile.DirectoryName == null)
yield break;
var d = new DirectoryInfo(Path.Combine(SpecFile.DirectoryName, @"Packages"));
var found = d.EnumerateDirectories(defPackage.Id + "*");
foreach (var directoryInfo in found)
{
if (re.IsMatch(directoryInfo.Name))
yield return directoryInfo.Name;
}
}
private List<ProjectPackages> _projects;
public List<ProjectPackages> Projects
{
get
{
if (_projects == null)
Init();
return _projects;
}
}
private void Init()
{
if (false)
{
//using (var r = SpecFile.OpenRead())
//{
// var man = Manifest.ReadFrom(r, false);
// var dep = man.Metadata.DependencySets.FirstOrDefault().Dependencies;
// foreach (var item in dep)
// {
// //VersionSpec
// //var v = new VersionSpec();
// //SemanticVersion.TryParse()
// }
//}
}
_dependencies = new List<PackageIdentity>();
using (var sr = SpecFile.OpenText())
{
var content = sr.ReadToEnd();
const string pattern = @"<dependency *id=""(?<Name>.*)"" *version=""(?<Version>.*)"" */>";
const RegexOptions regexOptions = RegexOptions.None;
var regex = new Regex(pattern, regexOptions);
foreach (Match mtch in regex.Matches(content))
{
var p = new PackageIdentity()
{
Id = mtch.Groups[@"Name"].Value,
Version = mtch.Groups[@"Version"].Value
};
_dependencies.Add(p);
}
_identity = new PackageIdentity()
{
Id = getXML(@"id", content),
Version = getXML("version", content)
};
}
_projects = ProjectPackages.GetFromDir(SpecFile.Directory).ToList();
}
private string getXML(string parameter, string searchXml)
{
var pattern = string.Format(@"<{0}>(.+)</{0}>", parameter);
const RegexOptions regexOptions = RegexOptions.None;
var regex = new Regex(pattern, regexOptions);
foreach (Match match in regex.Matches(searchXml))
{
return match.Groups[1].Value;
}
return @"";
}
internal void Report(IReporter reporter)
{
if (!Dependencies.Any())
return;
reporter.AppendLine(@"===" + SpecFile.FullName, Brushes.Blue);
foreach (var dep in Dependencies)
{
reporter.AppendLine(@" - " + dep.Id + " " + dep.Version);
var depRequirement = VersionRange.Parse(dep.Version);
foreach (var project in Projects)
{
var rp = project.RequiredPackages.FirstOrDefault(p => p.Id == dep.Id);
if (rp == null)
continue;
try
{
var sv = NuGet.Versioning.NuGetVersion.Parse(rp.Version);
var sat = depRequirement.Satisfies(sv);
if (!sat)
{
reporter.AppendLine(
$" - Mismatch: {rp.Version} referenced in {project.ConfigFile.Directory.Name}/{project.ConfigFile.Name}", Brushes.OrangeRed);
}
else if (string.Compare(rp.Version, depRequirement.MinVersion.ToString(), StringComparison.CurrentCulture) != 0)
{
reporter.AppendLine(
$" - Warning: MinVersion {depRequirement.MinVersion.ToString()} is lower than installed {rp.Version} in {project.ConfigFile.Directory.Name}/{project.ConfigFile.Name}", Brushes.Orange);
}
}
catch (Exception)
{
if (string.Compare(rp.Version, dep.Version, StringComparison.CurrentCulture) != 0)
{
reporter.AppendLine(
$" - Mismatch: {rp.Version} referenced in {project.ConfigFile.Directory.Name}/{project.ConfigFile.Name}", Brushes.OrangeRed);
}
}
}
}
}
internal void ReportArrear(List<OurOwnNuSpec> allNuSpecs, IReporter reporter)
{
var lst = allNuSpecs.Where(x => x.Identity == null).ToArray();
foreach (var l in lst)
{
Debug.Write("");
}
if (!Dependencies.Any())
return;
reporter.AppendLine(@"===" + SpecFile.FullName, Brushes.Blue);
reporter.AppendLine($"[{Identity.Id}, {Identity.Version}]");
foreach (var dep in Dependencies)
{
var depName = dep.Id;
var depReq = dep.Version;
var depRes = "<unknown>";
var depMatch = allNuSpecs.FirstOrDefault(x => x.Identity != null && x.Identity.Id == depName);
if (depMatch != null)
{
depRes = depMatch.Identity.Version;
}
Brush repColor = null;
if (depRes != "<unknown>")
{
var depRequirement = VersionRange.Parse(depReq);
var sv = NuGet.Versioning.NuGetVersion.Parse(depRes);
if (!depRequirement.Satisfies(sv))
{
repColor = Brushes.OrangeRed;
}
if (depRequirement.MinVersion < sv)
{
repColor = Brushes.Orange;
}
}
reporter.AppendLine($" - {depName}: req: {depReq} avail: {depRes}", repColor);
}
}
private DirectoryInfo PackagesFolder
{
get
{
var dir = SpecFile?.Directory?.GetDirectories("packages").FirstOrDefault();
return dir;
}
}
IEnumerable<PackageIdentity> GetAvailablePackages()
{
var re = new Regex(@"\.\d");
if (PackagesFolder == null)
yield break;
foreach (var dir in PackagesFolder.GetDirectories())
{
var m = re.Match(dir.Name);
if (!m.Success)
continue;
var left = dir.Name.Substring(0, m.Index);
var right = dir.Name.Substring(m.Index + 1);
yield return new PackageIdentity() {Id = left, Version = right};
}
}
internal IEnumerable<PackageIdentity> OldPackages()
{
if (_projects == null)
Init();
var ps = GetAvailablePackages().ToList();
foreach (var project in _projects)
{
foreach (var requiredPackage in project.RequiredPackages)
{
ps.RemoveAll(x => x.FullName == requiredPackage.FullName);
}
}
return ps;
}
internal void DeleteDownloadedPackage(PackageIdentity oldpackage)
{
if (PackagesFolder == null)
return;
var dir = PackagesFolder.GetDirectories(oldpackage.FullName).FirstOrDefault();
if (dir == null)
return;
Debug.WriteLine(dir.FullName);
DeleteDirectory(dir.FullName, true);
// dir.Delete(true);
}
public static void DeleteDirectory(string path)
{
DeleteDirectory(path, false);
}
public static void DeleteDirectory(string path, bool recursive)
{
// Delete all files and sub-folders?
if (recursive)
{
// Yep... Let's do this
var subfolders = Directory.GetDirectories(path);
foreach (var s in subfolders)
{
DeleteDirectory(s, recursive);
}
}
// Get all files of the folder
var files = Directory.GetFiles(path);
foreach (var f in files)
{
// Get the attributes of the file
var attr = File.GetAttributes(f);
// Is this file marked as 'read-only'?
if ((attr & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
{
// Yes... Remove the 'read-only' attribute, then
File.SetAttributes(f, attr ^ FileAttributes.ReadOnly);
}
// Delete the file
File.Delete(f);
}
// When we get here, all the files of the folder were
// already deleted, so we just delete the empty folder
Directory.Delete(path);
}
}
}