-
Notifications
You must be signed in to change notification settings - Fork 536
/
Copy pathbuildVersion.ts
179 lines (157 loc) · 6.12 KB
/
buildVersion.ts
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
/*!
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
* Licensed under the MIT License.
*/
import * as fs from "node:fs";
import { Flags } from "@oclif/core";
import { getIsLatest, getSimpleVersion } from "@fluid-tools/version-tools";
import { semverFlag } from "../../flags.js";
import { BaseCommand } from "../../library/index.js";
/**
* This command class is used to compute the version number of Fluid packages. The release version number is based on
* what's in the release group root package.json. The CI pipeline will supply the build number and branch to determine
* the prerelease suffix if it is not a tagged build.
*/
export default class GenerateBuildVersionCommand extends BaseCommand<
typeof GenerateBuildVersionCommand
> {
static readonly description =
`This command is used to compute the version number of Fluid packages. The release version number is based on what's in the release group root package.json. The CI pipeline will supply the build number and branch to determine the prerelease suffix if it is not a tagged build.`;
static readonly examples = ["<%= config.bin %> <%= command.id %>"];
static readonly flags = {
build: Flags.string({
description: "The CI build number.",
env: "VERSION_BUILDNUMBER",
required: true,
}),
testBuild: Flags.string({
description: "Indicates the build is a test build.",
env: "TEST_BUILD",
}),
release: Flags.string({
description: "Indicates the build is a release build.",
options: ["release", "prerelease", "none"],
env: "VERSION_RELEASE",
}),
patch: Flags.string({
description: `Indicates the build should use "simple patch versioning" where the value of the --build flag is used as the patch version.`,
env: "VERSION_PATCH",
}),
base: Flags.string({
description: "The base version. This will be read from package.json if not provided.",
}),
tag: Flags.string({
description: "The tag name to use.",
env: "VERSION_TAGNAME",
}),
includeInternalVersions: Flags.string({
char: "i",
description: "Include Fluid internal versions.",
env: "VERSION_INCLUDE_INTERNAL_VERSIONS",
}),
packageTypes: Flags.string({
description:
"If provided, the version generated will include extra strings based on the TypeScript types that are expected to be used. This flag should only be used in the Fluid Framework CI pipeline.",
options: ["none", "alpha", "beta", "public", "untrimmed"],
default: "none",
env: "PACKAGE_TYPES_FIELD",
}),
fileVersion: semverFlag({
description:
"Will be used as the version instead of reading from package.json. Used for testing.",
hidden: true,
}),
tags: Flags.string({
description:
"The git tags to consider when determining whether a version is latest. Used for testing.",
hidden: true,
multiple: true,
}),
...BaseCommand.flags,
} as const;
public async run(): Promise<void> {
const { flags } = this;
const isRelease = flags.release === "release";
const useSimplePatchVersion = flags.patch?.toLowerCase() === "true";
const useTestVersion = flags.testBuild?.toLowerCase() === "true";
const shouldIncludeInternalVersions =
flags.includeInternalVersions?.toLowerCase() === "true";
const isAlphaOrBetaTypes = ["alpha", "beta"].includes(flags.packageTypes);
// `alphabetaTypePrefix` will be either `alpha-types` or `beta-types`
const alphabetaTypePrefix = `${flags.packageTypes}-types`;
let fileVersion = "";
if (flags.base === undefined) {
fileVersion = flags.fileVersion?.version ?? this.getFileVersion();
if (!fileVersion) {
this.error("Missing version in package.json");
}
}
if (flags.testBuild?.toLowerCase() === "true" && isRelease) {
this.error("Test build shouldn't be released");
}
const context = await this.getContext();
const repo = await context.getGitRepository();
// eslint-disable-next-line unicorn/no-await-expression-member
const tags = flags.tags ?? (await repo.gitClient.tags()).all;
if (!useSimplePatchVersion && flags.tag !== undefined) {
const tagName = `${flags.tag}_v${fileVersion}`;
if (tags.includes(tagName)) {
if (isRelease) {
this.error(`Tag ${tagName} already exists.`);
}
this.warning(`Tag ${tagName} already exists.`);
}
}
// Generate and print the version to console
const simpleVersion = getSimpleVersion(
fileVersion,
flags.build,
isRelease,
useSimplePatchVersion,
);
let version = simpleVersion;
if (useTestVersion) {
// Determine the version string for test builds.
// If it's an alpha or beta type, append `alphabetaTypePrefix`.
version = isAlphaOrBetaTypes
? `0.0.0-${flags.build}-test-${alphabetaTypePrefix}`
: `0.0.0-${flags.build}-test`;
// Output the code version for test builds. This is used in the CI system.
// See common/build/build-common/gen_version.js
const codeVersion = isAlphaOrBetaTypes
? `${simpleVersion}-test-${alphabetaTypePrefix}`
: `${simpleVersion}-test`;
this.log(`codeVersion=${codeVersion}`);
this.log(`##vso[task.setvariable variable=codeVersion;isOutput=true]${codeVersion}`);
}
if (isAlphaOrBetaTypes) {
if (isRelease || flags.release === "none") {
this.errorLog(
"This release type is not supported. Alpha/beta ***prereleases*** are allowed.",
);
this.exit(1);
} else if (!useTestVersion) {
// For prereleases, update the version string with `alphabetaTypePrefix` prefix.
version = `${simpleVersion}-${alphabetaTypePrefix}`;
}
}
this.log(`version=${version}`);
this.log(`##vso[task.setvariable variable=version;isOutput=true]${version}`);
if (flags.tag !== undefined) {
const isLatest = getIsLatest(flags.tag, version, tags, shouldIncludeInternalVersions);
this.log(`isLatest=${isLatest}`);
if (isRelease && isLatest === true) {
this.log(`##vso[task.setvariable variable=isLatest;isOutput=true]${isLatest}`);
}
}
}
private getFileVersion(): string {
if (fs.existsSync("./package.json")) {
return (
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
JSON.parse(fs.readFileSync("./package.json", { encoding: "utf8" })).version as string
);
}
this.error(`package.json not found`);
}
}