/
lib-nuget.cake
163 lines (131 loc) · 5.25 KB
/
lib-nuget.cake
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
public class NuGetServer
{
public string Url { get;set; }
public string ApiKey { get;set; }
public override string ToString()
{
var result = Url;
result += string.Format(" (ApiKey present: '{0}')", !string.IsNullOrWhiteSpace(ApiKey));
return result;
}
}
//-------------------------------------------------------------
public static List<NuGetServer> GetNuGetServers(string urls, string apiKeys)
{
var splittedUrls = urls.Split(new [] { ";" }, StringSplitOptions.None);
var splittedApiKeys = apiKeys.Split(new [] { ";" }, StringSplitOptions.None);
if (splittedUrls.Length != splittedApiKeys.Length)
{
throw new Exception("Number of api keys does not match number of urls. Even if an API key is not required, add an empty one");
}
var servers = new List<NuGetServer>();
for (int i = 0; i < splittedUrls.Length; i++)
{
var url = splittedUrls[i];
if (string.IsNullOrWhiteSpace(url))
{
throw new Exception("Url for NuGet server cannot be empty");
}
servers.Add(new NuGetServer
{
Url = url,
ApiKey = splittedApiKeys[i]
});
}
return servers;
}
//-------------------------------------------------------------
private static void RestoreNuGetPackages(BuildContext buildContext, Cake.Core.IO.FilePath solutionOrProjectFileName)
{
buildContext.CakeContext.LogSeparator("Restoring packages for '{0}'", solutionOrProjectFileName);
var sources = SplitSeparatedList(buildContext.General.NuGet.PackageSources, ';');
var runtimeIdentifiers = new List<string>(new []
{
"win-x64",
"browser-wasm"
});
RestoreNuGetPackagesUsingNuGet(buildContext, solutionOrProjectFileName, sources);
RestoreNuGetPackagesUsingDotnetRestore(buildContext, solutionOrProjectFileName, sources, runtimeIdentifiers);
}
//-------------------------------------------------------------
private static void RestoreNuGetPackagesUsingNuGet(BuildContext buildContext, Cake.Core.IO.FilePath solutionOrProjectFileName, List<string> sources)
{
buildContext.CakeContext.LogSeparator("Restoring packages for '{0}' using 'NuGet'", solutionOrProjectFileName);
try
{
var nuGetRestoreSettings = new NuGetRestoreSettings
{
DisableParallelProcessing = false,
NoCache = false,
};
if (sources.Count > 0)
{
nuGetRestoreSettings.Source = sources;
}
buildContext.CakeContext.NuGetRestore(solutionOrProjectFileName, nuGetRestoreSettings);
}
catch (Exception)
{
// Ignore
}
}
//-------------------------------------------------------------
private static void RestoreNuGetPackagesUsingDotnetRestore(BuildContext buildContext, Cake.Core.IO.FilePath solutionOrProjectFileName, List<string> sources, List<string> runtimeIdentifiers)
{
buildContext.CakeContext.LogSeparator("Restoring packages for '{0}' using 'dotnet restore'", solutionOrProjectFileName);
var projectFileContents = System.IO.File.ReadAllText(solutionOrProjectFileName.FullPath)?.ToLower();
var supportedRuntimeIdentifiers = new List<string>();
foreach (var runtimeIdentifier in runtimeIdentifiers)
{
if (!string.IsNullOrWhiteSpace(runtimeIdentifier))
{
if (!projectFileContents.Contains(runtimeIdentifier.ToLower()))
{
buildContext.CakeContext.Information("Project '{0}' does not support runtime identifier '{1}', skipping restore for this runtime identifier", solutionOrProjectFileName, runtimeIdentifier);
continue;
}
}
supportedRuntimeIdentifiers.Add(runtimeIdentifier);
}
if (supportedRuntimeIdentifiers.Count == 0)
{
// Default
supportedRuntimeIdentifiers.Add(string.Empty);
}
foreach (var runtimeIdentifier in supportedRuntimeIdentifiers)
{
try
{
buildContext.CakeContext.LogSeparator("Restoring packages for '{0}' using 'dotnet restore' using runtime identifier '{1}'", solutionOrProjectFileName, runtimeIdentifier);
var restoreSettings = new DotNetCoreRestoreSettings
{
DisableParallel = false,
Force = false,
ForceEvaluate = false,
IgnoreFailedSources = true,
NoCache = false,
NoDependencies = false, // use true to speed up things
Verbosity = DotNetCoreVerbosity.Normal
};
if (!string.IsNullOrWhiteSpace(runtimeIdentifier))
{
// This is a explicit supported runtime identifier, force re-evaluation
restoreSettings.Force = true;
restoreSettings.ForceEvaluate = true;
restoreSettings.Runtime = runtimeIdentifier;
}
if (sources.Count > 0)
{
restoreSettings.Sources = sources;
}
using (buildContext.CakeContext.UseDiagnosticVerbosity())
{
buildContext.CakeContext.DotNetCoreRestore(solutionOrProjectFileName.FullPath, restoreSettings);
}
}
catch (Exception)
{
// Ignore
}
}
}