Skip to content

Commit 591ead4

Browse files
Flash0verclaude
andcommitted
refactor(dotnet): reduce cognitive complexity in pack script
Extract resolveTargets() and verifyBinaries() helpers from pack() to bring its complexity score below the linter's max of 15. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 8519366 commit 591ead4

File tree

1 file changed

+41
-29
lines changed

1 file changed

+41
-29
lines changed

script/pack.ts

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,12 @@ function getBinaryName(target: PackTarget): string {
6565

6666
/** Get .NET Runtime Identifier for a target */
6767
function getDotnetRid(target: PackTarget): string {
68-
if (target.os === "darwin") return `osx-${target.arch}`;
69-
if (target.os === "win32") return `win-${target.arch}`;
68+
if (target.os === "darwin") {
69+
return `osx-${target.arch}`;
70+
}
71+
if (target.os === "win32") {
72+
return `win-${target.arch}`;
73+
}
7074
return `${target.os}-${target.arch}`;
7175
}
7276

@@ -84,7 +88,10 @@ function parseTarget(targetStr: string): PackTarget | null {
8488
}
8589

8690
/** Pack a platform-specific NuGet package with embedded native binary */
87-
async function packTarget(target: PackTarget, version: string): Promise<boolean> {
91+
async function packTarget(
92+
target: PackTarget,
93+
version: string
94+
): Promise<boolean> {
8895
const rid = getDotnetRid(target);
8996
const packageName = getPackageName(target);
9097
const outfile = getNupkgPath(version, rid);
@@ -104,14 +111,14 @@ async function packTarget(target: PackTarget, version: string): Promise<boolean>
104111
/** Pack the "any" (framework-dependent, CoreCLR) package */
105112
async function packAny(version: string): Promise<boolean> {
106113
const outfile = getNupkgPath(version, "any");
107-
console.log(` Packing any (framework-dependent)...`);
114+
console.log(" Packing any (framework-dependent)...");
108115

109116
try {
110117
await $`dotnet pack ${PROJECT_DIR} -c Release -r any -p:PackageVersion=${version} -p:PublishAot=false`.quiet();
111118
console.log(` -> ${outfile}`);
112119
return true;
113120
} catch (error) {
114-
console.error(` Failed to pack any:`);
121+
console.error(" Failed to pack any:");
115122
console.error(error);
116123
return false;
117124
}
@@ -120,34 +127,25 @@ async function packAny(version: string): Promise<boolean> {
120127
/** Pack the root package (no RID — pointer/manifest package) */
121128
async function packRoot(version: string): Promise<boolean> {
122129
const outfile = getNupkgPath(version);
123-
console.log(` Packing root (no RID)...`);
130+
console.log(" Packing root (no RID)...");
124131

125132
try {
126133
await $`dotnet pack ${PROJECT_DIR} -c Release -p:PackageVersion=${version} -p:PublishAot=true`.quiet();
127134
console.log(` -> ${outfile}`);
128135
return true;
129136
} catch (error) {
130-
console.error(` Failed to pack root:`);
137+
console.error(" Failed to pack root:");
131138
console.error(error);
132139
return false;
133140
}
134141
}
135142

136-
/** Main pack function */
137-
async function pack(): Promise<void> {
138-
const args = process.argv.slice(2);
139-
const singlePack = args.includes("--single");
143+
/** Resolve pack targets from CLI args, printing a status line and exiting on error */
144+
function resolveTargets(args: string[]): PackTarget[] {
140145
const targetIndex = args.indexOf("--target");
141146
const targetArg = targetIndex !== -1 ? args[targetIndex + 1] : null;
142147

143-
console.log(`\nSentry CLI NuGet Pack v${pkg.version}`);
144-
console.log("=".repeat(40));
145-
146-
// Determine targets
147-
let targets: PackTarget[];
148-
149148
if (targetArg) {
150-
// Explicit target specified (for cross-compilation)
151149
const target = parseTarget(targetArg);
152150
if (!target) {
153151
console.error(`Invalid target: ${targetArg}`);
@@ -156,9 +154,11 @@ async function pack(): Promise<void> {
156154
);
157155
process.exit(1);
158156
}
159-
targets = [target];
160157
console.log(`\nPacking for target: ${getPackageName(target)}`);
161-
} else if (singlePack) {
158+
return [target];
159+
}
160+
161+
if (args.includes("--single")) {
162162
const currentTarget = ALL_TARGETS.find(
163163
(t) => t.os === process.platform && t.arch === process.arch
164164
);
@@ -168,23 +168,23 @@ async function pack(): Promise<void> {
168168
);
169169
process.exit(1);
170170
}
171-
targets = [currentTarget];
172171
console.log(
173172
`\nPacking for current platform: ${getPackageName(currentTarget)}`
174173
);
175-
} else {
176-
targets = ALL_TARGETS;
177-
console.log(`\nPacking for ${targets.length} targets`);
174+
return [currentTarget];
178175
}
179176

180-
// Verify native binaries for selected targets
177+
console.log(`\nPacking for ${ALL_TARGETS.length} targets`);
178+
return ALL_TARGETS;
179+
}
180+
181+
/** Verify that native binaries exist for all targets, exiting on missing files */
182+
async function verifyBinaries(targets: PackTarget[]): Promise<void> {
181183
console.log("\nVerifying native binaries...");
182184
let binaryMissing = false;
183185
for (const target of targets) {
184-
const binaryName = getBinaryName(target);
185-
const binaryPath = `${DIST_BIN_DIR}/${binaryName}`;
186-
const exists = await Bun.file(binaryPath).exists();
187-
if (exists) {
186+
const binaryPath = `${DIST_BIN_DIR}/${getBinaryName(target)}`;
187+
if (await Bun.file(binaryPath).exists()) {
188188
console.log(` ✓ ${binaryPath}`);
189189
} else {
190190
console.error(` ✗ ${binaryPath} not found`);
@@ -196,6 +196,18 @@ async function pack(): Promise<void> {
196196
console.error("Run 'bun run build:all' first to generate all binaries.");
197197
process.exit(1);
198198
}
199+
}
200+
201+
/** Main pack function */
202+
async function pack(): Promise<void> {
203+
const args = process.argv.slice(2);
204+
205+
console.log(`\nSentry CLI NuGet Pack v${pkg.version}`);
206+
console.log("=".repeat(40));
207+
208+
const targets = resolveTargets(args);
209+
210+
await verifyBinaries(targets);
199211

200212
// Clean output directory
201213
await $`rm -rf ${DIST_PKG_DIR}`.quiet();

0 commit comments

Comments
 (0)