-
Notifications
You must be signed in to change notification settings - Fork 379
/
Copy pathrust.ts
169 lines (150 loc) · 5.21 KB
/
rust.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
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import {GitHubFileContents} from '@google-automations/git-file-utils';
// Generic
import {Changelog} from '../updaters/changelog';
// Cargo.toml support
import {CargoToml} from '../updaters/rust/cargo-toml';
import {CargoLock} from '../updaters/rust/cargo-lock';
import {CargoManifest, parseCargoManifest} from '../updaters/rust/common';
import {BaseStrategy, BuildUpdatesOptions} from './base';
import {VersionsMap, Version} from '../version';
import {Update} from '../update';
export class Rust extends BaseStrategy {
private packageManifest?: CargoManifest | null;
private workspaceManifest?: CargoManifest | null;
protected async buildUpdates(
options: BuildUpdatesOptions
): Promise<Update[]> {
const updates: Update[] = [];
const version = options.newVersion;
updates.push({
path: this.addPath(this.changelogPath),
createIfMissing: true,
updater: new Changelog({
version,
changelogEntry: options.changelogEntry,
}),
});
const workspaceManifest = await this.getPackageManifest();
const versionsMap: VersionsMap = new Map();
if (workspaceManifest?.workspace?.members) {
const members = workspaceManifest.workspace.members;
if (workspaceManifest.package?.name) {
versionsMap.set(workspaceManifest.package.name, version);
} else {
this.logger.warn('No workspace manifest package name found');
}
this.logger.info(
`found workspace with ${members.length} members, upgrading all`
);
// Collect submodule names to update
const manifestsByPath: Map<string, GitHubFileContents> = new Map();
for (const member of members) {
const manifestPath = `${member}/Cargo.toml`;
const manifestContent = await this.getContent(manifestPath);
if (!manifestContent) {
this.logger.warn(
`member ${member} declared but did not find Cargo.toml`
);
continue;
}
const manifest = parseCargoManifest(manifestContent.parsedContent);
manifestsByPath.set(manifestPath, manifestContent);
if (!manifest.package?.name) {
this.logger.warn(`member ${member} has no package name`);
continue;
}
versionsMap.set(manifest.package.name, version);
}
this.logger.info(`updating ${manifestsByPath.size} submodules`);
this.logger.debug('versions map:', versionsMap);
for (const [manifestPath, manifestContent] of manifestsByPath) {
updates.push({
path: this.addPath(manifestPath),
createIfMissing: false,
cachedFileContents: manifestContent,
updater: new CargoToml({
version,
versionsMap,
}),
});
}
// Update root Cargo.toml
updates.push({
path: this.addPath('Cargo.toml'),
createIfMissing: false,
updater: new CargoToml({
version,
versionsMap,
}),
});
} else {
this.logger.info('single crate found, updating Cargo.toml');
const packageName = await this.getDefaultPackageName();
if (packageName) {
versionsMap.set(packageName, version);
} else {
this.logger.warn('No crate package name found');
}
updates.push({
path: this.addPath('Cargo.toml'),
createIfMissing: false,
updater: new CargoToml({
version,
versionsMap,
}),
});
}
updates.push({
path: this.addPath('Cargo.lock'),
createIfMissing: false,
updater: new CargoLock(versionsMap),
});
return updates;
}
protected initialReleaseVersion(): Version {
return Version.parse('0.1.0');
}
async getDefaultPackageName(): Promise<string | undefined> {
const packageManifest = await this.getPackageManifest();
if (packageManifest) {
return packageManifest.package?.name;
}
return undefined;
}
/**
* @returns the package's manifest, ie. `crates/foobar/Cargo.toml`
*/
protected async getPackageManifest(): Promise<CargoManifest | null> {
if (this.packageManifest === undefined) {
this.packageManifest = await this.getManifest('Cargo.toml');
}
return this.packageManifest;
}
private async getContent(path: string): Promise<GitHubFileContents | null> {
try {
return await this.github.getFileContentsOnBranch(
this.addPath(path),
this.targetBranch
);
} catch (e) {
return null;
}
}
protected async getManifest(path: string): Promise<CargoManifest | null> {
const content = await this.getContent(path);
return content ? parseCargoManifest(content.parsedContent) : null;
}
}