-
-
Notifications
You must be signed in to change notification settings - Fork 304
Expand file tree
/
Copy pathupdate-deps.cs
More file actions
executable file
·190 lines (156 loc) · 8.89 KB
/
update-deps.cs
File metadata and controls
executable file
·190 lines (156 loc) · 8.89 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
#! /usr/bin/env dotnet
#:property Nullable=enable
#:property PublishAOT=false
#:package ArcaneLibs@1.0.1-preview.2026*
using System.Diagnostics;
using ArcaneLibs;
using System.Text.Json;
#region Sync package versions for CDN worker
{
Console.WriteLine("==> Ensuring CDN worker dependencies are in sync...");
var origContent = await File.ReadAllTextAsync("Spacebar.Cdn.Worker/Spacebar.Cdn.Worker.Q16-HDRI.x86_64.csproj");
var depToReplace = "Magick.NET-Q16-HDRI-OpenMP-x64";
(string Project, string Dependency)[] replaceTargets = [
// ("Spacebar.Cdn.Worker/Spacebar.Cdn.Worker.Q16-HDRI.x86_64.csproj", "Magick.NET-Q16-HDRI-OpenMP-x64"), // source
("Spacebar.Cdn.Worker/Spacebar.Cdn.Worker.Q16.x86_64.csproj", "Magick.NET-Q16-OpenMP-x64"),
("Spacebar.Cdn.Worker/Spacebar.Cdn.Worker.Q8.x86_64.csproj", "Magick.NET-Q8-OpenMP-x64"),
("Spacebar.Cdn.Worker/Spacebar.Cdn.Worker.Q16-HDRI.aarch64.csproj", "Magick.NET-Q16-HDRI-OpenMP-arm64"),
("Spacebar.Cdn.Worker/Spacebar.Cdn.Worker.Q16.aarch64.csproj", "Magick.NET-Q16-OpenMP-arm64"),
("Spacebar.Cdn.Worker/Spacebar.Cdn.Worker.Q8.aarch64.csproj", "Magick.NET-Q8-OpenMP-arm64"),
("Spacebar.Cdn.Worker/Spacebar.Cdn.Worker.Q16-HDRI.AnyCPU.csproj", "Magick.NET-Q16-HDRI-AnyCPU"),
("Spacebar.Cdn.Worker/Spacebar.Cdn.Worker.Q16.AnyCPU.csproj", "Magick.NET-Q16-AnyCPU"),
("Spacebar.Cdn.Worker/Spacebar.Cdn.Worker.Q8.AnyCPU.csproj", "Magick.NET-Q8-AnyCPU"),
];
foreach (var target in replaceTargets) {
Console.WriteLine($" ==> {target.Project} -> {target.Dependency}");
await File.WriteAllTextAsync(target.Project, origContent.Replace(depToReplace, target.Dependency));
}
}
#endregion
Console.WriteLine("==> Getting outputs...");
var outNames = JsonSerializer
.Deserialize<string[]>(Util.GetCommandOutputSync("nix", $"eval --json .#packages.x86_64-linux --apply builtins.attrNames", silent: true, stderr: false))!
.Where(o => args.Length == 0 || args.Select(x => x.Replace('.', '-')).Any(o.Contains)).ToArray();
Console.WriteLine($"==> Updating dependencies for {outNames.Length} projects...");
#region Dependency resolution
var sortSw = Stopwatch.StartNew();
Console.WriteLine($"==> Sorting projects by dependencies...");
Console.WriteLine($" ==> Getting project references...");
(string Name, string[] References)[] byName = await Task.WhenAll(outNames
.Select(async x => {
await Task.Delay(Random.Shared.Next(outNames.Length));
var refJson = Util.GetCommandOutputSync("nix", $"eval --json .#packages.x86_64-linux.{x}.passthru.__sbDmProjectRefs.names", silent: true, stderr: false);
var references = string.IsNullOrEmpty(refJson) ? [] : JsonSerializer.Deserialize<string[]>(refJson)!.Select(x => x.Replace('.', '-')).ToArray();
if (references.Length > 0)
Console.WriteLine($" ==> Got {references.Length} project references for {x}");
// Console.WriteLine($" ==> Got {references.Length} project references for {x}: {string.Join(", ", references)}");
return (x, references);
}).ToList());
Console.WriteLine($" ==> Mapping to tree nodes...");
// create nodes first
var deps = byName.Select(x => new ProjectDependencyNode() {
Name = x.Name,
References = []
}).ToArray();
// actually fill in references
foreach (var d in deps) {
d.References = deps.Where(x => byName.First(bn => bn.Name == d.Name).References.Contains(x.Name)).ToArray();
Console.WriteLine($" ==> {d.Name} => {string.Join(", ", d.References.Select(x => x.GetShortName()))}");
}
Console.WriteLine($" ==> Sorting...");
deps = deps.OrderBy(x => x.GetDepth()).ThenBy(x => x.GetWeight()).ThenBy(x => x.Name).ToArray();
foreach (var d in deps) {
// just a nice thing when debugging
d.References = d.References.OrderBy(x => x.GetDepth()).ThenBy(x => x.GetWeight()).ThenBy(x => x.Name).ToArray();
Console.WriteLine($" ==> {d.GetNameWithWeights()} => {string.Join(", ", d.References.Select(x => $"{x.GetNameWithWeights(true)}"))}");
}
Console.WriteLine($" ==> Sorted dependency tree with {deps.Length} projects in {sortSw.Elapsed}");
#endregion
var maxNameLength = deps.Max(x => x.Name.Length);
foreach (var depthGroup in deps.GroupBy(x => x.GetDepth())) {
var tasks = depthGroup.Index().Select(indexedOutpEnt => Task.Run(async () => {
var (idx, outpEnt) = indexedOutpEnt;
var outp = outpEnt.Name;
var prefix = ConsoleUtils.ColoredString(
$"{outpEnt.GetDepth():00}.{idx:00}({outpEnt.GetWeight():000}) {outpEnt.Name.PadRight(maxNameLength)}>",
(byte)((outp.GetHashCode() >> 16) & 0xff),
(byte)((outp.GetHashCode() >> 8) & 0xff),
(byte)(outp.GetHashCode() & 0xff)
);
Console.WriteLine(prefix + ConsoleUtils.ColoredString($" ==> Updating {outp}...", 0x80, 0x80, 0xff));
var rootDir = outpEnt.GetRelativeRootDir();
var depsFile = outpEnt.GetDepsFile();
if (depsFile is null) {
Console.WriteLine(prefix + ConsoleUtils.ColoredString($" ==> No __nugetDeps attribute, skipping!", 0xff, 0x80, 0x80));
return;
}
Console.WriteLine(
prefix
+ ConsoleUtils.ColoredString($" ==> Got project root directory: ", 0x80, 0xff, 0xff)
+ ConsoleUtils.ColoredString($"{rootDir}", 0x80, 0xff, 0xff)
+ " - "
+ ConsoleUtils.ColoredString($"{depsFile.Value.StorePath.Replace(depsFile.Value.Name, "")}", 0x80, 0x80, 0xff)
+ ConsoleUtils.ColoredString($"{depsFile.Value.Name}", 0x80, 0xff, 0x80)
+ (File.Exists(depsFile.Value.LocalPath)
? ConsoleUtils.ColoredString($" (exists)", 0x80, 0xff, 0x80)
: ConsoleUtils.ColoredString($" (does not exist)", 0xf, 0x80, 0x80))
);
if (!File.Exists(depsFile.Value.LocalPath)) {
Console.WriteLine(prefix + ConsoleUtils.ColoredString($" ==> No NuGet deps file, skipping!", 0xff, 0x80, 0x80));
return;
}
Console.WriteLine(prefix + ConsoleUtils.ColoredString($" ==> Building fetch-deps script...", 0x80, 0xff, 0x80));
var fname = outpEnt.GetDepsUpdateScript();
Console.WriteLine(prefix + ConsoleUtils.ColoredString($" ==> Running fetch-deps script, writing into {depsFile.Value.LocalPath}...", 0x80, 0xff, 0x80));
RunCommandSync(fname, depsFile.Value.LocalPath);
var resolvedDeps = JsonSerializer.Deserialize<object[]>(await File.ReadAllTextAsync(depsFile.Value.LocalPath));
Console.WriteLine(prefix + ConsoleUtils.ColoredString($" ==> Locked {resolvedDeps.Length} dependencies!",
(byte)(resolvedDeps.Length == 0 ? 0xff : 0x80),
(byte)(resolvedDeps.Length == 0 ? 0x80 : 0xff),
0x80
));
RunCommandSync("nix", $"run nixpkgs#git -- add {depsFile.Value.LocalPath}");
})).ToList();
await Task.WhenAll(tasks);
}
static void RunCommandSync(string command, string args = "", bool silent = false) {
// Console.WriteLine($"Executing command (silent: {silent}): {command} {args}");
Util.RunCommandSync(command, args, silent);
}
public class ProjectDependencyNode : IComparable {
public required string Name;
public required ProjectDependencyNode[] References;
public int GetDepth() {
if (References.Length == 0) return 0;
return References.Max(x => x.GetDepth()) + 1;
}
public int GetWeight() {
if (References.Length == 0) return 1;
return References.Sum(x => x.GetWeight()) + 1;
}
public string GetNameWithWeights(bool compact = false) =>
compact
? $"{GetShortName()}:{GetDepth()}w{GetWeight()}"
: $"{Name} @ {GetDepth()} | {GetWeight()}";
public string GetShortName() => Name.Replace("Spacebar-", "Sb-")
.Replace("-Models-", "-Mdl-")
.Replace("-Interop-", "-Intp-")
.Replace("-Replication-", "-Rpl-")
.Replace("-Authentication-", "-Auth-");
public string GetRelativeRootDir() => JsonSerializer
.Deserialize<string>(Util.GetCommandOutputSync("nix", $"eval --json .#packages.x86_64-linux.{Name}.srcRoot", silent: true, stderr: false))!
.Split("/extra/admin-api/", 2)[1];
public string GetDepsUpdateScript() => Util.GetCommandOutputSync("nix", $"build .#{Name}.passthru.fetch-deps --no-link --print-out-paths", stderr: false);
public (string Name, string StorePath, string LocalPath)? GetDepsFile() {
var storePath = JsonSerializer.Deserialize<string>(Util.GetCommandOutputSync("nix", $"eval --json .#packages.x86_64-linux.{Name}.passthru.__nugetDeps", silent: true,
stderr: false));
return storePath == null
? null
: (
new FileInfo(storePath).Name,
storePath,
Path.Combine(GetRelativeRootDir(), new FileInfo(storePath).Name)
);
}
public int CompareTo(object? obj) => References.Any(x => x.Name == Name) ? 1 : 0;
}