Skip to content

Commit 4e9d1ae

Browse files
committed
refactor(mcl/utils/json): Refactor and format
1 parent 6648e77 commit 4e9d1ae

File tree

11 files changed

+633
-596
lines changed

11 files changed

+633
-596
lines changed

packages/mcl/src/src/mcl/commands/ci.d

Lines changed: 72 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ module mcl.commands.ci;
33
import std.file : readText;
44
import std.json : parseJSON, JSONValue;
55
import std.stdio : writeln, write;
6-
import std.algorithm : map;
6+
import std.algorithm : map, each;
77
import std.array : array, join;
88
import std.conv : to;
99
import std.process : ProcessPipes;
1010

1111
import mcl.utils.env : optional, parseEnv;
12-
import mcl.commands.ci_matrix: nixEvalJobs, SupportedSystem, Params, flakeAttr;
13-
import mcl.commands.shard_matrix: generateShardMatrix;
12+
import mcl.commands.ci_matrix : nixEvalJobs, SupportedSystem, Params, flakeAttr, Package;
13+
import mcl.commands.shard_matrix : generateShardMatrix, Shard;
1414
import mcl.utils.path : rootDir, createResultDirs;
1515
import mcl.utils.process : execute;
1616
import mcl.utils.nix : nix;
@@ -23,66 +23,81 @@ export void ci()
2323
params = parseEnv!Params;
2424

2525
auto shardMatrix = generateShardMatrix();
26-
foreach (shard; shardMatrix.include)
26+
shardMatrix.include.each!(handleShard);
27+
}
28+
29+
static immutable(SupportedSystem) platform()
30+
{
31+
version (AArch64)
32+
static immutable string arch = "aarch64";
33+
else version (X86_64)
34+
static immutable string arch = "x86_64";
35+
36+
version (linux)
37+
static immutable string os = "linux";
38+
else version (OSX)
39+
static immutable string os = "darwin";
40+
41+
return (arch ~ "_" ~ os).to!(SupportedSystem);
42+
}
43+
44+
void handleShard(Shard shard)
45+
{
46+
writeln("Shard ", shard.prefix ~ " ", shard.postfix ~ " ", shard.digit);
47+
params.flakePre = shard.prefix;
48+
params.flakePost = shard.postfix;
49+
50+
if (params.flakePre == "")
2751
{
28-
writeln("Shard ", shard.prefix ~ " ", shard.postfix ~ " ", shard.digit);
29-
params.flakePre = shard.prefix;
30-
params.flakePost = shard.postfix;
52+
params.flakePre = "checks";
53+
}
54+
if (params.flakePost != "")
55+
{
56+
params.flakePost = "." ~ params.flakePost;
57+
}
58+
string cachixUrl = "https://" ~ params.cachixCache ~ ".cachix.org";
59+
version (AArch64)
60+
{
61+
string arch = "aarch64";
62+
}
63+
version (X86_64)
64+
{
65+
string arch = "x86_64";
66+
}
3167

32-
if (params.flakePre == "")
33-
{
34-
params.flakePre = "checks";
35-
}
36-
if (params.flakePost != "")
37-
{
38-
params.flakePost = "." ~ params.flakePost;
39-
}
40-
string cachixUrl = "https://" ~ params.cachixCache ~ ".cachix.org";
41-
version (AArch64)
42-
{
43-
string arch = "aarch64";
44-
}
45-
version (X86_64)
46-
{
47-
string arch = "x86_64";
48-
}
68+
version (linux)
69+
{
70+
string os = "linux";
71+
}
72+
version (OSX)
73+
{
74+
string os = "darwin";
75+
}
4976

50-
version (linux)
51-
{
52-
string os = "linux";
53-
}
54-
version (OSX)
55-
{
56-
string os = "darwin";
57-
}
77+
auto matrix = flakeAttr(params.flakePre, arch, os, params.flakePost)
78+
.nixEvalJobs(cachixUrl, false);
79+
matrix.each!(handlePackage);
80+
}
5881

59-
auto matrix = flakeAttr(params.flakePre, arch, os, params.flakePost)
60-
.nixEvalJobs(cachixUrl, false);
82+
void handlePackage(Package pkg)
83+
{
84+
if (pkg.isCached)
85+
writeln("Package ", pkg.name, " is cached");
86+
else
87+
{
88+
writeln("Package ", pkg.name, " is not cached; building...");
89+
ProcessPipes res = execute!ProcessPipes([
90+
"nix", "build", "--json", ".#" ~ pkg.attrPath
91+
]);
6192

62-
foreach (pkg; matrix)
93+
foreach (line; res.stderr.byLine)
6394
{
64-
if (pkg.isCached)
65-
{
66-
writeln("Package ", pkg.name, " is cached");
67-
}
68-
else
69-
{
70-
writeln("Package ", pkg.name, " is not cached; building...");
71-
ProcessPipes res = execute!ProcessPipes([
72-
"nix", "build", "--json", ".#" ~ pkg.attrPath
73-
]);
74-
75-
foreach (line; res.stderr.byLine)
76-
{
77-
"\r".write;
78-
line.write;
79-
}
80-
"".writeln;
81-
auto json = parseJSON(res.stdout.byLine.join("\n").to!string);
82-
auto path = json.array[0]["outputs"]["out"].str;
83-
execute(["cachix", "push", params.cachixCache, path], false, true).writeln;
84-
}
95+
"\r".write;
96+
line.write;
8597
}
86-
98+
"".writeln;
99+
auto json = parseJSON(res.stdout.byLine.join("\n").to!string);
100+
auto path = json.array[0]["outputs"]["out"].str;
101+
execute(["cachix", "push", params.cachixCache, path], false, true).writeln;
87102
}
88103
}

0 commit comments

Comments
 (0)