microsoft/typespec

Public

mirrored from https://github.com/microsoft/typespecAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
a6b7827fcd2db3bf604d42e235216c8d5b244f13

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

packages/internal-build-utils/src/dotnet.ts

91lines · modecode

1import { execAsync, run, RunOptions } from "./common.js";
2import { MinimumDotnetVersion } from "./constants.js";
3
4export async function runDotnet(
5 args: string[],
6 options: Omit<RunOptions, "stdio" | "throwOnNonZeroExit"> = {}
7) {
8 await ensureDotnetVersion();
9 const result = await run("dotnet", args, {
10 ...options,
11 throwOnNonZeroExit: false,
12 stdio: [null, "pipe", "inherit"],
13 });
14
15 if (result.exitCode !== 0) {
16 // rush wants errors on stderr to show them to user without --verbose.
17 // eslint-disable-next-line no-console
18 console.error(result.stdout);
19 process.exit(result.exitCode);
20 }
21 // eslint-disable-next-line no-console
22 console.log(result.stdout);
23}
24
25export async function validateDotnetVersion(): Promise<{ error?: string }> {
26 try {
27 const result = await execAsync("dotnet", ["--version"], {
28 stdio: [null, "pipe", "inherit"],
29 });
30
31 if (result.exitCode !== 0 || !result.stdout) {
32 return { error: `dotnet not found.` };
33 }
34 const version = result.stdout.toString().trim();
35 const [major, minor, _patch] = version.split(".").map((x) => parseInt(x, 10));
36
37 if (
38 major < MinimumDotnetVersion.major ||
39 (major === MinimumDotnetVersion.major && minor < MinimumDotnetVersion.minor)
40 ) {
41 return {
42 error: `dotnet version ${version} does not meet minimum requirement ${MinimumDotnetVersion.major}.${MinimumDotnetVersion.minor}.x`,
43 };
44 }
45 return {};
46 } catch (e: any) {
47 if (e.code === "ENOENT") {
48 return { error: "dotnet not found." };
49 } else {
50 throw e;
51 }
52 }
53}
54
55let validatedDotnet = false;
56export async function ensureDotnetVersion(options: { exitWithSuccessInDevBuilds?: boolean } = {}) {
57 if (validatedDotnet) {
58 return;
59 }
60
61 const { error } = await validateDotnetVersion();
62 if (error) {
63 // If running in CI/AzureDevOps fail if dotnet is invalid.
64 if (process.env.CI || process.env.TF_BUILD || !options.exitWithSuccessInDevBuilds) {
65 // eslint-disable-next-line no-console
66 console.error(`error: ${error}`);
67 process.exit(1);
68 } else {
69 // eslint-disable-next-line no-console
70 console.log(`Skipping build step: ${error}`);
71 process.exit(0);
72 }
73 }
74
75 validatedDotnet = true;
76}
77
78/**
79 * Runs the dotnet formatter.
80 */
81export async function runDotnetFormat(...args: string[]) {
82 return runDotnet([
83 "format",
84 "whitespace",
85 ".",
86 `--exclude`,
87 "**/node_modules/**/*",
88 "--folder",
89 ...args,
90 ]);
91}
92