/
extract.ts
240 lines (223 loc) · 6.42 KB
/
extract.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
import { parse } from '@iarna/toml';
import { logger } from '../../../logger';
import type { SkipReason } from '../../../types';
import { findLocalSiblingOrParent, readLocalFile } from '../../../util/fs';
import { CrateDatasource } from '../../datasource/crate';
import type {
ExtractConfig,
PackageDependency,
PackageFileContent,
} from '../types';
import type {
CargoConfig,
CargoManifest,
CargoRegistries,
CargoSection,
} from './types';
function getCargoIndexEnv(registryName: string): string | null {
const registry = registryName.toUpperCase().replaceAll('-', '_');
return process.env[`CARGO_REGISTRIES_${registry}_INDEX`] ?? null;
}
function extractFromSection(
parsedContent: CargoSection,
section: keyof CargoSection,
cargoRegistries: CargoRegistries,
target?: string,
depTypeOverride?: string
): PackageDependency[] {
const deps: PackageDependency[] = [];
const sectionContent = parsedContent[section];
if (!sectionContent) {
return [];
}
Object.keys(sectionContent).forEach((depName) => {
let skipReason: SkipReason | undefined;
let currentValue = sectionContent[depName];
let nestedVersion = false;
let registryUrls: string[] | undefined;
let packageName: string | undefined;
if (typeof currentValue !== 'string') {
const version = currentValue.version;
const path = currentValue.path;
const git = currentValue.git;
const registryName = currentValue.registry;
const workspace = currentValue.workspace;
packageName = currentValue.package;
if (version) {
currentValue = version;
nestedVersion = true;
if (registryName) {
const registryUrl =
cargoRegistries[registryName] ?? getCargoIndexEnv(registryName);
if (registryUrl) {
registryUrls = [registryUrl];
} else {
skipReason = 'unknown-registry';
}
}
if (path) {
skipReason = 'path-dependency';
}
if (git) {
skipReason = 'git-dependency';
}
} else if (path) {
currentValue = '';
skipReason = 'path-dependency';
} else if (git) {
currentValue = '';
skipReason = 'git-dependency';
} else if (workspace) {
currentValue = '';
skipReason = 'inherited-dependency';
} else {
currentValue = '';
skipReason = 'invalid-dependency-specification';
}
}
const dep: PackageDependency = {
depName,
depType: section,
currentValue: currentValue as any,
managerData: { nestedVersion },
datasource: CrateDatasource.id,
};
if (registryUrls) {
dep.registryUrls = registryUrls;
}
if (skipReason) {
dep.skipReason = skipReason;
}
if (target) {
dep.target = target;
}
if (packageName) {
dep.packageName = packageName;
}
if (depTypeOverride) {
dep.depType = depTypeOverride;
}
deps.push(dep);
});
return deps;
}
/** Reads `.cargo/config.toml`, or, if not found, `.cargo/config` */
async function readCargoConfig(): Promise<CargoConfig | null> {
for (const configName of ['config.toml', 'config']) {
const path = `.cargo/${configName}`;
const payload = await readLocalFile(path, 'utf8');
if (payload) {
try {
return parse(payload) as CargoConfig;
} catch (err) {
logger.debug({ err }, `Error parsing ${path}`);
}
break;
}
}
logger.debug('Neither .cargo/config nor .cargo/config.toml found');
return null;
}
/** Extracts a map of cargo registries from a CargoConfig */
function extractCargoRegistries(config: CargoConfig | null): CargoRegistries {
const result: CargoRegistries = {};
if (!config?.registries) {
return result;
}
const { registries } = config;
for (const registryName of Object.keys(registries)) {
const registry = registries[registryName];
if (registry.index) {
result[registryName] = registry.index;
} else {
logger.debug(`${registryName} cargo registry is missing index`);
}
}
return result;
}
export async function extractPackageFile(
content: string,
packageFile: string,
_config?: ExtractConfig
): Promise<PackageFileContent | null> {
logger.trace(`cargo.extractPackageFile(${packageFile})`);
const cargoConfig = await readCargoConfig();
const cargoRegistries = extractCargoRegistries(cargoConfig);
let cargoManifest: CargoManifest;
try {
cargoManifest = parse(content);
} catch (err) {
logger.debug({ err, packageFile }, 'Error parsing Cargo.toml file');
return null;
}
/*
There are the following sections in Cargo.toml:
[dependencies]
[dev-dependencies]
[build-dependencies]
[target.*.dependencies]
[workspace.dependencies]
*/
const targetSection = cargoManifest.target;
// An array of all dependencies in the target section
let targetDeps: PackageDependency[] = [];
if (targetSection) {
const targets = Object.keys(targetSection);
targets.forEach((target) => {
const targetContent = targetSection[target];
// Dependencies for `${target}`
const deps = [
...extractFromSection(
targetContent,
'dependencies',
cargoRegistries,
target
),
...extractFromSection(
targetContent,
'dev-dependencies',
cargoRegistries,
target
),
...extractFromSection(
targetContent,
'build-dependencies',
cargoRegistries,
target
),
];
targetDeps = targetDeps.concat(deps);
});
}
const workspaceSection = cargoManifest.workspace;
let workspaceDeps: PackageDependency[] = [];
if (workspaceSection) {
workspaceDeps = extractFromSection(
workspaceSection,
'dependencies',
cargoRegistries,
undefined,
'workspace.dependencies'
);
}
const deps = [
...extractFromSection(cargoManifest, 'dependencies', cargoRegistries),
...extractFromSection(cargoManifest, 'dev-dependencies', cargoRegistries),
...extractFromSection(cargoManifest, 'build-dependencies', cargoRegistries),
...targetDeps,
...workspaceDeps,
];
if (!deps.length) {
return null;
}
const lockFileName = await findLocalSiblingOrParent(
packageFile,
'Cargo.lock'
);
const res: PackageFileContent = { deps };
// istanbul ignore if
if (lockFileName) {
res.lockFiles = [lockFileName];
}
return res;
}