-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
186 lines (152 loc) · 8.55 KB
/
Program.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
using CommandLine;
using NuGet.Common;
using NuGet.Configuration;
using NuGet.Packaging.Core;
using NuGet.Protocol;
using NuGet.Protocol.Core.Types;
using NugetPackagesMigrator;
await Parser.Default.ParseArguments<CommandLineOptions>(args: Environment.GetCommandLineArgs())
.WithParsedAsync(action: async options =>
{
var configuration = Settings.LoadDefaultSettings(root: Directory.GetCurrentDirectory());
var packageSourceProvider = new PackageSourceProvider(configuration);
var nugetSourceToCopyFrom = packageSourceProvider.GetPackageSourceByName(options.NugetSourceNameToCopyFrom);
if (nugetSourceToCopyFrom == null) throw new Exception(message: $"Can't find nuget source with name '{options.NugetSourceNameToCopyFrom}'");
var nugetSourceToCopyTo = packageSourceProvider.GetPackageSourceByName(options.NugetSourceNameToCopyTo);
if (nugetSourceToCopyTo == null) throw new Exception(message: $"Can't find nuget source with name '{options.NugetSourceNameToCopyTo}'");
var repositoryToCopyFrom = nugetSourceToCopyFrom.ProtocolVersion switch
{
3 => Repository.Factory.GetCoreV3(nugetSourceToCopyFrom),
2 => Repository.Factory.GetCoreV2(nugetSourceToCopyFrom),
_ => throw new Exception(message: $"Package source '{nugetSourceToCopyFrom.Name}' has Unsupported Nuget protocol version")
};
var repositoryToCopyTo = nugetSourceToCopyTo.ProtocolVersion switch
{
3 => Repository.Factory.GetCoreV3(nugetSourceToCopyTo),
2 => Repository.Factory.GetCoreV2(nugetSourceToCopyTo),
_ => throw new Exception(message: $"Package source '{nugetSourceToCopyTo.Name}' has Unsupported Nuget protocol version")
};
var targetRepositoryPackageLocator = repositoryToCopyTo.GetResource<FindPackageByIdResource>();
var packageDownloader = repositoryToCopyFrom.GetResource<DownloadResource>();
var packageUploader = repositoryToCopyTo.GetResource<PackageUpdateResource>();
var cacheContext = new SourceCacheContext();
var packageDownloadContext = new PackageDownloadContext(cacheContext);
var totalPackagesDiscoveredCount = 0;
var totalVersionsDiscoveredCount = 0;
var totalPackagesToTransferCount = 0;
var totalVersionsToTransferCount = 0;
var transferredPackagesCount = 0;
var transferredVersionsCount = 0;
var skip = 0;
const int take = 50;
while (true)
{
var packagesList = (await repositoryToCopyFrom.GetResource<PackageSearchResource>()
.SearchAsync(string.Empty, filters: new SearchFilter(includePrerelease: true), skip, take, log: new NullLogger(), CancellationToken.None))
.ToArray();
if (packagesList.Length == 0) break;
skip += take;
foreach (var packageSearchMetadata in packagesList)
{
var packageId = packageSearchMetadata.Identity.Id;
Console.WriteLine("Checking Package {0}", packageId);
totalPackagesDiscoveredCount++;
var doesPackageExistInTargetRepo = await targetRepositoryPackageLocator.DoesPackageExistAsync(id: packageId,
version: packageSearchMetadata.Identity.Version,
cacheContext: cacheContext,
logger: new NullLogger(),
cancellationToken: CancellationToken.None);
HashSet<Version> versionsInTargetRepoHashset = null!;
if (doesPackageExistInTargetRepo)
{
var versionsInTargetRepo = await targetRepositoryPackageLocator.GetAllVersionsAsync(packageId,
cacheContext,
logger: new NullLogger(),
CancellationToken.None);
versionsInTargetRepoHashset = versionsInTargetRepo.Select(selector: x => x.Version).ToHashSet();
}
else
{
Console.WriteLine("Package {0} does not exists in target repository, transferring...", packageId);
totalPackagesToTransferCount++;
}
var sourceRepoVersions = await packageSearchMetadata.GetVersionsAsync();
var currentPackageVersionTransferCount = 0;
foreach (var packageVersion in sourceRepoVersions)
{
totalVersionsDiscoveredCount++;
var versionId = packageVersion.PackageSearchMetadata.Identity;
if (doesPackageExistInTargetRepo && versionsInTargetRepoHashset.Contains(packageVersion.Version.Version))
{
continue;
}
Console.WriteLine("Transferring package version {0}", versionId);
totalVersionsToTransferCount++;
var downloadedPackage = await packageDownloader.GetDownloadResourceResultAsync(
identity: new PackageIdentity(packageId, packageVersion.Version),
packageDownloadContext,
"/packages",
logger: new NullLogger(),
CancellationToken.None);
if (downloadedPackage.Status != DownloadResourceResultStatus.Available) continue;
Console.WriteLine("{0} successfully downloaded, uploading...", versionId);
var tempFilePath = Path.GetTempFileName();
try
{
await using (var fileStream = File.Create(tempFilePath))
{
downloadedPackage.PackageStream.CopyTo(fileStream);
}
await packageUploader.Push(packagePaths: new List<string> { tempFilePath },
symbolSource: null,
timeoutInSecond: 120,
disableBuffering: false,
getApiKey: _ => null,
getSymbolApiKey: _ => null,
noServiceEndpoint: false,
skipDuplicate: true,
symbolPackageUpdateResource: null,
log: new NullLogger());
transferredVersionsCount++;
currentPackageVersionTransferCount++;
Console.WriteLine("{0} successfully uploaded", versionId);
}
catch (Exception e)
{
Console.WriteLine("Error While transferring package file. Message: {0}", e.Message);
}
finally
{
File.Delete(tempFilePath);
}
}
if (currentPackageVersionTransferCount > 0)
{
transferredPackagesCount++;
}
}
}
var leftToTransferVersionsCount = totalVersionsToTransferCount - transferredVersionsCount;
var statusText = leftToTransferVersionsCount switch
{
> 0 => "There were errors during transferring some package versions, check logs above",
0 when totalVersionsToTransferCount > 0 => "All packages and versions were migrated successfully",
0 when totalVersionsToTransferCount is 0 => "Nothing to transfer. All packages and versions are already present in target nuget source",
_ => throw new InvalidOperationException()
};
Console.WriteLine(Environment.NewLine);
Console.WriteLine("Migration Completed: {0}.", statusText);
Console.WriteLine("Total Packages Discovered: {0}", totalPackagesDiscoveredCount);
Console.WriteLine("Total Versions Discovered: {0}", totalVersionsDiscoveredCount);
Console.WriteLine("Total Packages Need To Transfer: {0}", totalPackagesToTransferCount);
Console.WriteLine("Total Package Versions Need To Transfer: {0}", totalVersionsToTransferCount);
Console.WriteLine("Transferred Packages: {0}", transferredPackagesCount);
Console.WriteLine("Transferred Versions: {0}", transferredVersionsCount);
Console.WriteLine("Versions Left To Transfer: {0}", leftToTransferVersionsCount);
})
.ContinueWith(continuationAction: t =>
{
if (t.Exception == null) return;
Console.WriteLine("Error: {0}", t.Exception.InnerException?.Message);
Environment.Exit(exitCode: -1);
});