-
Notifications
You must be signed in to change notification settings - Fork 2
/
models.ts
326 lines (292 loc) · 7.54 KB
/
models.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import { stringify } from "@std/yaml";
import { green } from "@std/fmt/colors";
import { isGlob } from "@std/path/is-glob";
import { expandGlobSync } from "@std/fs/expand-glob";
import { relative } from "@std/path/relative";
export class AppError extends Error {}
class FilePattern {
constructor(
public path: string,
public patterns: string[],
) {}
async validate(v: string) {
let text: string;
try {
text = await Deno.readTextFile(this.path);
} catch (e) {
if (e.name === "NotFound") {
throw new AppError(`Error: The file ${this.path} not found`);
}
throw e;
}
for (const pattern of this.patterns) {
if (!pattern.includes("%.%.%")) {
throw new AppError(
`Error: The pattern '${pattern}' doesn't include '%.%.%'`,
);
}
const findPattern = pattern.replaceAll("%.%.%", v);
if (!text.includes(findPattern)) {
throw new AppError(
`Error: The file '${this.path}' doesn't include the pattern '${findPattern}'`,
);
}
}
}
async replace(from: string, to: string) {
let contents = await Deno.readTextFile(this.path);
for (const pattern of this.patterns) {
const beforeString = pattern.replaceAll("%.%.%", from);
const afterString = pattern.replaceAll("%.%.%", to);
contents = contents.replaceAll(beforeString, afterString);
}
await Deno.writeTextFile(this.path, contents);
}
}
export class Version {
static parse(v: string) {
// TODO(kt3k): Implement more strict parse rules.
const m = v.match(/(\d+).(\d+).(\d+)(-(.+))?/);
if (!m) {
throw new AppError(`Error: Invalid version number: ${v}`);
}
return new Version(
+m[1]!,
+m[2]!,
+m[3]!,
m[5],
);
}
constructor(
public major: number,
public minor: number,
public patch: number,
public preid: string | undefined,
) {
}
bumpMajor(): Version {
return new Version(
this.major + 1,
0,
0,
undefined,
);
}
bumpMinor(): Version {
return new Version(
this.major,
this.minor + 1,
0,
undefined,
);
}
bumpPatch(): Version {
return new Version(
this.major,
this.minor,
this.patch + 1,
undefined,
);
}
setPreid(preid: string): Version {
return new Version(
this.major,
this.minor,
this.patch,
preid,
);
}
release(): Version {
return new Version(
this.major,
this.minor,
this.patch,
undefined,
);
}
toString() {
const { major, minor, patch, preid } = this;
if (preid) {
return `${major}.${minor}.${patch}-${preid}`;
}
return `${major}.${minor}.${patch}`;
}
}
export type VersionInfoInput = {
version?: string;
commit?: string;
files?: Record<string, Array<string> | string>;
};
export class VersionInfo {
static createDefault() {
return VersionInfo.create({
version: "0.0.0",
commit: "chore: bump to v%.%.%",
files: {
"README.md": "v%.%.%",
},
});
}
static create(
{ version, commit, files }: VersionInfoInput,
path = ".bmp.yml",
) {
if (!version) {
throw new AppError(
"Error: version property is not given in the config file",
);
}
if (!files) {
throw new AppError(
"Error: files property is not given in the config file",
);
}
const v = Version.parse(version);
const filePatterns: FilePattern[] = [];
for (const [path, value] of Object.entries(files)) {
const paths = isGlob(path)
? expandGlobSync(path, {
includeDirs: false,
})
: [path];
const patterns: string[] = [];
if (typeof value === "string") {
patterns.push(value);
} else if (Array.isArray(value)) {
for (const val of value) {
if (typeof val === "string") {
patterns.push(val);
} else {
throw new Error(
`Error: Wrong type for file pattern: ${typeof val} (${val}) is given`,
);
}
}
} else {
throw new Error(
`Error: Wrong type for file pattern: ${typeof value} (${value}) is given`,
);
}
for (const entry of paths) {
const path = typeof entry === "string"
? entry
: relative(Deno.cwd(), entry.path);
filePatterns.push(new FilePattern(path, patterns));
}
}
return new VersionInfo(path, v, commit, filePatterns);
}
updateVersion: Version;
constructor(
public path: string,
public currentVersion: Version,
public commit: string | undefined,
public filePatterns: FilePattern[],
) {
this.updateVersion = currentVersion;
}
async validate() {
const v = this.currentVersion.toString();
for (const filePattern of this.filePatterns) {
await filePattern.validate(v);
}
}
major() {
this.updateVersion = this.currentVersion.bumpMajor();
}
minor() {
this.updateVersion = this.currentVersion.bumpMinor();
}
patch() {
this.updateVersion = this.currentVersion.bumpPatch();
}
preid(preid: string) {
this.updateVersion = this.currentVersion.setPreid(preid);
}
release() {
this.updateVersion = this.currentVersion.release();
}
toObject() {
const data = {
version: this.updateVersion.toString(),
// deno-lint-ignore no-explicit-any
} as any;
if (this.commit) {
data.commit = this.commit;
}
const files: Record<string, string | string[]> = {};
for (const { path, patterns } of this.filePatterns) {
if (patterns.length === 1) {
files[path] = patterns[0];
} else {
files[path] = patterns;
}
}
data.files = files;
return data;
}
toString() {
const buf: string[] = [];
buf.push(`Current version: ${green(this.currentVersion.toString())}`);
if (this.commit) {
buf.push(`Commit message: ${green(this.getCommitMessage())}`);
}
buf.push(`Version patterns:`);
for (const { path, patterns } of this.filePatterns) {
for (const pattern of patterns) {
buf.push(
` ${path}: ${
green(pattern.replaceAll("%.%.%", this.updateVersion.toString()))
}`,
);
}
}
return buf.join("\n");
}
bumpSummary() {
const buf: string[] = [];
const v0 = this.currentVersion.toString();
const v1 = this.updateVersion.toString();
buf.push("Updating version:");
buf.push(" " + green(`${v0} => ${v1}`));
buf.push("Version patterns:");
for (const { path, patterns } of this.filePatterns) {
for (const pattern of patterns) {
buf.push(
` ${path}: ${
green(
`${pattern.replaceAll("%.%.%", v0)} => ${
pattern.replaceAll("%.%.%", v1)
}`,
)
}`,
);
}
}
return buf.join("\n");
}
isUpdated() {
return this.currentVersion.toString() !== this.updateVersion.toString();
}
async performUpdate() {
for (const filePattern of this.filePatterns) {
await filePattern.replace(
this.currentVersion.toString(),
this.updateVersion.toString(),
);
}
await this.save();
}
async save() {
await Deno.writeTextFile(this.path, stringify(this.toObject()));
}
getTag() {
return `v${this.updateVersion.toString()}`;
}
getCommitMessage() {
if (this.commit) {
return this.commit.replaceAll("%.%.%", this.updateVersion.toString());
}
return `chore: bump to ${this.updateVersion.toString()}`;
}
}